- Timestamp:
- Jul 9, 2019 3:08:30 PM (5 months ago)
- Location:
- branches/dev-mj-1626
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/dev-mj-1626
- Property svn:externals
-
old new 1 1 https://svn.jmodelica.org/assimulo/tags/Assimulo-3.0@875 external/Assimulo 2 https://svn.jmodelica.org/PyFMI/tags/PyFMI-2.5 /src/common@12158Python/src/common2 https://svn.jmodelica.org/PyFMI/tags/PyFMI-2.5.3/src/common@13067 Python/src/common 3 3 https://github.com/chria/casadi/branches/tested_mod@11366 ThirdParty/CasADi/CasADi 4 4 https://svn.jmodelica.org/FMILibrary/trunk@10948 external/FMILibrary/FMILibrary 5 https://svn.jmodelica.org/PyFMI/tags/PyFMI-2.5. 1@12984external/PyFMI5 https://svn.jmodelica.org/PyFMI/tags/PyFMI-2.5.3@13069 external/PyFMI
-
- Property svn:mergeinfo changed
- Property svn:externals
-
branches/dev-mj-1626/Compiler/ModelicaFlatTree/src/jastadd/Arrays.jrag
r12940 r13095 23 23 import java.util.Iterator; 24 24 import java.util.NoSuchElementException; 25 26 import org.jmodelica.util.collections.ListUtil; 27 import org.jmodelica.util.collections.IntIterator; 25 28 26 29 aspect Arrays { … … 383 386 } 384 387 eq InstGlobalAccess.ndims() = getInstAccess().ndims(); 385 386 /** 387 * Get the expanded FArraySubscripts of each part of this access. 388 */ 389 eq InstAccess.allFArraySubscripts() = Arrays.<FArraySubscripts>asList(new FArrayLitSubscripts()); 390 eq InstArrayAccess.allFArraySubscripts() = Arrays.asList(getFArraySubscripts()); 391 eq InstScalarAccess.allFArraySubscripts() = Arrays.asList(getExpandedSubscripts()); 392 eq InstDot.allFArraySubscripts() { 393 int n = getNumInstAccess(); 394 java.util.List<FArraySubscripts>[] lists = new java.util.List[n]; 395 for (int i = 0; i < n; i++) 396 lists[i] = getInstAccess(i).allFArraySubscripts(); 397 return concatenation(lists); 398 } 399 eq InstGlobalAccess.allFArraySubscripts() = getInstAccess().allFArraySubscripts(); 400 401 /** 402 * Get the expanded FArraySubscripts of each part of the fully qualified version of this access. 403 */ 404 syn java.util.List<FArraySubscripts> InstAccess.qualifiedAllFArraySubscripts() = 405 concatenation(surroundingFArraySubscripts(), allFArraySubscripts()); 406 eq InstGlobalAccess.qualifiedAllFArraySubscripts() = allFArraySubscripts(); 407 408 /** 409 * Get the expanded FArraySubscripts for any surrounding components. 410 */ 411 inh ArrayList<FArraySubscripts> InstAccess.surroundingFArraySubscripts(); 412 inh lazy ArrayList<FArraySubscripts> InstArrayComponentDecl.surroundingFArraySubscripts(); 413 eq Root.getChild().surroundingFArraySubscripts() = new ArrayList<FArraySubscripts>(0); 414 eq InstClassDecl.getChild().surroundingFArraySubscripts() = new ArrayList<FArraySubscripts>(0); 415 eq InstArrayComponentDecl.getChild().surroundingFArraySubscripts() { 416 FArraySubscripts fas; 417 ArrayList<FArraySubscripts> res; 418 if (isTopDimension()) { 419 fas = new FArrayLitSubscripts(); 420 res = concatenation(surroundingFArraySubscripts(), Collections.singletonList(fas)); 421 } else { 422 res = (ArrayList<FArraySubscripts>) surroundingFArraySubscripts().clone(); 423 int i = res.size() - 1; 424 fas = res.get(i).fullCopy(); 425 res.set(i, fas); 426 } 427 fas.addFSubscript(getIndex()); 428 return res; 429 } 430 431 public class ASTNode { 432 public static <T> ArrayList<T> concatenation(java.util.List<T>... lists) { 433 int size = 0; 434 for (java.util.List<T> list : lists) 435 size += list.size(); 436 ArrayList<T> res = new ArrayList<T>(size); 437 for (java.util.List<T> list : lists) 438 res.addAll(list); 439 return res; 440 } 441 } 442 388 389 /** 390 * Get array dimensions up to this part for a part of a dotted access. For 391 * an access that is not part of a dotted access, this is equivalent to ndims(). 392 * 393 * A value > 0 indicates an array, 0 indicates a scalar and -1 indicates 394 * an error in computation of the array dimensions. 395 */ 396 syn int InstAccess.combinedNdims() = collectCombinedNdims() + ndims(); 397 398 inh int InstAccess.collectCombinedNdims(); 399 eq BaseNode.getChild() .collectCombinedNdims() = 0; 400 eq InstDot .getInstAccess(int i).collectCombinedNdims() { 401 int n = 0; 402 for (int j = 0; j < i; j++) { 403 n += getInstAccess(j).ndims(); 404 } 405 return n; 406 } 407 408 /** 409 * Get the expanded FArraySubscripts of each part of this access. 410 */ 411 eq InstAccess .allFArraySubscripts() = Arrays.<FArraySubscripts>asList(new FArrayLitSubscripts()); 412 eq InstArrayAccess .allFArraySubscripts() = Arrays.asList(getFArraySubscripts()); 413 eq InstScalarAccess.allFArraySubscripts() = Arrays.asList(getExpandedSubscripts()); 414 eq InstDot .allFArraySubscripts() = allFArraySubscriptsTo(getNumInstAccess()); 415 eq InstGlobalAccess.allFArraySubscripts() = getInstAccess().allFArraySubscripts(); 416 417 /** 418 * Get the expanded FArraySubscripts of each part up to this for a part of a dotted access. 419 * For an access that is not part of a dotted access, this is equivalent to 420 * {@link InstAccess#qualifiedAllFArraySubscripts()}. 421 */ 422 syn java.util.List<FArraySubscripts> InstAccess.combinedFArraySubscripts() = 423 ListUtil.concatenate(collectCombinedFArraySubscripts(), 424 allFArraySubscripts()); 425 426 inh java.util.List<FArraySubscripts> InstAccess.collectCombinedFArraySubscripts(); 427 eq BaseNode.getChild() .collectCombinedFArraySubscripts() = Collections.emptyList(); 428 eq InstDot .getInstAccess(int i).collectCombinedFArraySubscripts() = allFArraySubscriptsTo(i); 429 430 private java.util.List<FArraySubscripts> InstDot.allFArraySubscriptsTo(int n) { 431 java.util.List<FArraySubscripts>[] lists = new java.util.List[n]; 432 for (int i = 0; i < n; i++) { 433 lists[i] = getInstAccess(i).allFArraySubscripts(); 434 } 435 return ListUtil.concatenate(lists); 436 } 437 443 438 /** 444 439 * Get the expanded FArraySubscripts of each part of this access. … … 1120 1115 * Get the array sizes. 1121 1116 */ 1122 eq InstAccess.size() = isArray() ? arraySize() : Size.SCALAR; 1123 1124 syn Size InstAccess.arraySize() = Size.SCALAR; 1125 eq InstGlobalAccess.arraySize() = getInstAccess().arraySize(); 1126 eq InstScalarAccess.arraySize() = getExpandedSubscripts().accessSize(); 1127 eq InstArrayAccess.arraySize() = getFArraySubscripts().accessSize(); 1128 eq InstDot.arraySize() { 1129 MutableSize s = new MutableSize(ndims()); 1130 for (InstAccess ia : getInstAccesss()) { 1131 s.append(ia.size()); 1132 } 1133 return s; 1134 } 1135 1117 eq InstAccess.size() = Size.SCALAR; 1118 eq InstGlobalAccess.size() = getInstAccess().size(); 1119 eq InstScalarAccess.size() = getExpandedSubscripts().accessSize(); 1120 eq InstArrayAccess.size() = getFArraySubscripts().accessSize(); 1121 eq InstDot.size() { 1122 if (isArray()) { 1123 MutableSize s = new MutableSize(ndims()); 1124 for (InstAccess ia : getInstAccesss()) { 1125 s.append(ia.size()); 1126 } 1127 return s; 1128 } else { 1129 return Size.SCALAR; 1130 } 1131 } 1132 1133 /** 1134 * Get the size of all parts of an InstDot up to this part. 1135 */ 1136 inh Size InstAccess.partSize(); 1137 eq BaseNode.getChild().partSize() = null; 1138 eq InstDot.getInstAccess(int i).partSize() { 1139 int ndims = 0; 1140 for (int j = 0; j <= i; j++) { 1141 ndims += getInstAccess(j).ndims(); 1142 } 1143 if (ndims == 0) { 1144 return Size.SCALAR; 1145 } else { 1146 MutableSize s = new MutableSize(ndims); 1147 for (int j = 0; j <= i; j++) { 1148 s.append(getInstAccess(j).size()); 1149 } 1150 return s; 1151 } 1152 } 1153 1136 1154 /** 1137 1155 * Get the array sizes. … … 2153 2171 } 2154 2172 } 2155 2173 2174 /** 2175 * Get a specific cell of this array component decl. 2176 * 2177 * Only valid if this is an array and <code>i</code> refers to an element of it. 2178 */ 2179 syn InstComponentDecl InstComponentDecl.specify(Index i) = specifyHelper(i, 0); 2180 2181 syn InstComponentDecl InstComponentDecl.specifyHelper(Index i, int d) = 2182 (d >= i.ndims() || !isArray()) 2183 ? this 2184 : getInstComponentDecl(i.get(d) - 1).specifyHelper(i, d + 1); 2185 eq InstPrimitive.specifyHelper(Index i, int d) = this; 2186 2156 2187 syn InstArrayAccess InstScalarAccess.getArrayCopy(FArraySubscripts fas); 2157 2188 eq InstParseAccess.getArrayCopy(FArraySubscripts fas) = new InstParseArrayAccess(getID(), fas); … … 2281 2312 * @return An Indices object containing all indices in each array dimension. 2282 2313 */ 2283 syn Indices InstAccess.indices() = Indices.createFromFas(qualifiedAllFArraySubscripts()); 2314 syn Indices InstAccess.indices() = Indices.createFromFas(allFArraySubscripts()); 2315 2316 /** 2317 * Get the set of array indices up to this part for a part of a dotted access. For 2318 * an access that is not part of a dotted access, this is equivalent to indices(). 2319 */ 2320 syn Indices InstAccess.combinedIndices() = Indices.createFromFas(combinedFArraySubscripts()); 2284 2321 2285 2322 /** … … 3899 3936 * multi-dimensional) array. 3900 3937 * 3901 * Index objects are reused to a high degree. Always use clone() when retaining3938 * Index objects are reused to a high degree. Always use fix() when retaining 3902 3939 * an Index object. 3903 3940 */ 3904 public class Index implements Cloneable{3941 public class Index { 3905 3942 3906 3943 public static final Index NULL = new Index(0); … … 3942 3979 return index.length > 0 ? index[index.length - 1] : -1; 3943 3980 } 3944 3981 3982 /** 3983 * Returns an iterator over the indices for each dimension. 3984 */ 3985 public IntIterator iterator() { 3986 return new IntIterator() { 3987 int d = 0; 3988 3989 public boolean hasNext() { return d < index.length; } 3990 3991 public int next() { return index[d++]; } 3992 }; 3993 } 3994 3945 3995 /** 3946 3996 * Returns the index for the <code>i</code>th dimension (zero-based). … … 3998 4048 return this; 3999 4049 if (i == j) 4000 return NULL;4050 return Index.NULL; 4001 4051 Index copy = new Index(j - i); 4002 4052 System.arraycopy(index, i, copy.index, 0, copy.index.length); … … 4035 4085 return new FArrayLitSubscripts(index); 4036 4086 } 4037 4038 4039 * Copies this Index. Always use clone() when saving an Index. 4040 */ 4041 public Index clone() { 4042 try { 4043 Index i = (Index) super.clone(); 4044 i.index = index.clone(); 4045 return i;4046 } catch (CloneNotSupportedException e){4047 return null;4048 4049 4050 4087 4088 /** 4089 * Returns an Index that has the same value as this one, but will not be changed by 4090 * an iterator that might have returned this Index. Usually creates a copy. 4091 * Always use fix() when saving an Index. 4092 */ 4093 public Index fix() { 4094 if (index.length == 0) { 4095 return Index.NULL; 4096 } else { 4097 return new Index(index.clone()); 4098 } 4099 } 4100 4051 4101 /** 4052 4102 * Returns an Index that is the result of appending <code>i</code> to this Index. … … 4236 4286 return size; 4237 4287 } 4238 4239 /** 4240 * Translates an Index to the corresponding Index in the underlying 4241 * expression. 4242 * 4243 * Default implementation always returns <code>i</code>. 4244 */ 4245 public Index translate(Index i) { 4246 return i; 4247 } 4248 4288 4289 /** 4290 * Translates an Index to the corresponding Index in the underlying expression. 4291 * If i has fewer dimensions than this, than it should correspond to the outermost ones. 4292 * 4293 * Note that for some implementations, the returned index may be changed by subsequent 4294 * calls to this method. 4295 * 4296 * Default implementation always returns <code>i</code>. 4297 */ 4298 public Index translate(Index i) { 4299 return i; 4300 } 4301 4302 public Indices translated() { 4303 return this; 4304 } 4305 4249 4306 /** 4250 4307 * Add values to an index map for a given index from this set and a given set of for indices. … … 4295 4352 } 4296 4353 4297 /** 4298 * Iterates over all indices spanned by this Indices. 4299 */ 4300 protected class IIterator implements Iterator<Index> { 4301 4302 protected IIndex index; 4303 protected int max; 4304 4305 public IIterator() { 4306 index = new IIndex(); 4307 max = numElements() - 1; 4308 } 4309 4310 public boolean hasNext() { 4311 return index.internal < max; 4312 } 4313 4314 public Index next() { 4315 if (index.internal >= max) 4316 throw new NoSuchElementException(); 4317 index.internal++; 4318 if (ndims == 0) 4319 return index; 4320 int[] ind = index.index(); 4321 int i; 4322 for (i = ndims - 1; i > 0 && ind[i] >= size.get(i); i--) 4323 ind[i] = 1; 4324 ind[i]++; 4325 return index; 4326 } 4327 4328 public void remove() { 4329 throw new UnsupportedOperationException(); 4330 } 4331 4332 } 4354 /** 4355 * Iterates over all indices spanned by this Indices. 4356 */ 4357 protected class IIterator implements Iterator<Index> { 4358 4359 protected IIndex index; 4360 protected int max; 4361 4362 public IIterator() { 4363 index = new IIndex(); 4364 max = numElements() - 1; 4365 } 4366 4367 public boolean hasNext() { 4368 return index.internal < max; 4369 } 4370 4371 public Index next() { 4372 if (index.internal >= max) 4373 throw new NoSuchElementException(); 4374 index.internal++; 4375 if (ndims == 0) { 4376 return Index.NULL; 4377 } 4378 int[] ind = index.index(); 4379 int i; 4380 for (i = ndims - 1; i > 0 && ind[i] >= size.get(i); i--) 4381 ind[i] = 1; 4382 ind[i]++; 4383 return index; 4384 } 4385 4386 public void remove() { 4387 throw new UnsupportedOperationException(); 4388 } 4389 4390 } 4333 4391 4334 4392 /** … … 4456 4514 } 4457 4515 4458 /** 4459 * Translates an Index to the corresponding Index in the underlying 4460 * expression. 4461 */ 4462 public Index translate(Index i) { 4463 int[] ii = i.index(); 4464 int[] ti = trans.index(); 4465 for (int ji = 0, jt = 0; ji < ndims && jt < ti.length; ji++, jt++) { 4466 while (trim[jt]) 4467 jt++; 4468 ti[jt] = indices.get(jt)[ii[ji] - 1]; 4469 } 4470 return trans; 4471 } 4472 4473 4474 /** 4475 * Internal constructor to create a NonConsecutiveIndices object based on a 4476 * list of int arrays containing the used indexes on the underlying array. 4477 * 4478 * @param trim the dimensions to remove 4479 * @param extra number of extra dimensions of length 1 to add 4480 */ 4481 protected NonConsecutiveIndices(ArrayList<int[]> ind, boolean[] trim, int extra) { 4482 super(getSize(ind, trim, extra)); 4483 this.trim = trim; 4484 indices = ind; 4485 trans = new Index(ind.size()); 4486 for (int i = 0; i < ind.size(); i++) 4487 if (indices.get(i).length > 0) 4488 trans.index()[i] = indices.get(i)[0]; 4489 } 4490 4516 /** 4517 * Internal constructor to create a NonConsecutiveIndices object based on a 4518 * list of int arrays containing the used indexes on the underlying array. 4519 * 4520 * @param trim the dimensions to remove 4521 * @param extra number of extra dimensions of length 1 to add 4522 */ 4523 protected NonConsecutiveIndices(ArrayList<int[]> ind, boolean[] trim, int extra) { 4524 super(getSize(ind, trim, extra)); 4525 this.trim = trim; 4526 indices = ind; 4527 trans = intializeTranslated(ind.size()); 4528 } 4529 4530 /** 4531 * Translates an Index to the corresponding Index in the underlying expression. 4532 * If i has fewer dimensions than this, than it should correspond to the outermost ones. 4533 * 4534 * Note that the returned Index may be changed by subsequent calls to this method, 4535 * use {@link Index.fix()} if you need to save it. 4536 */ 4537 @Override 4538 public Index translate(Index i) { 4539 int[] ii = i.index(); 4540 int transNDims = ii.length + trim.length - ndims; 4541 if (trans.ndims() != transNDims) { 4542 trans = intializeTranslated(transNDims); 4543 } 4544 int[] ti = trans.index(); 4545 for (int ji = 0, jt = 0; ji < ndims && jt < ti.length; ji++, jt++) { 4546 while (trim[jt]) { 4547 jt++; 4548 } 4549 ti[jt] = indices.get(jt)[ii[ji] - 1]; 4550 } 4551 return trans; 4552 } 4553 4554 /** 4555 * Prepare a new Index for returning from the translate method. 4556 */ 4557 protected Index intializeTranslated(int n) { 4558 Index trans = new Index(n); 4559 for (int i = 0; i < n; i++) { 4560 if (indices.get(i).length > 0) { 4561 trans.index()[i] = indices.get(i)[0]; 4562 } 4563 } 4564 return trans; 4565 } 4566 4567 @Override 4568 public Indices translated() { 4569 return new TranslatedIndices(this); 4570 } 4571 4572 private static class TranslatedIndices extends Indices { 4573 4574 private NonConsecutiveIndices indices; 4575 4576 private TranslatedIndices(NonConsecutiveIndices indices) { 4577 super(indices.size); 4578 this.indices = indices; 4579 } 4580 4581 @Override 4582 public Iterator<Index> iterator() { 4583 return new Iter(); 4584 } 4585 4586 private class Iter implements Iterator<Index> { 4587 4588 private Iterator<Index> iterator; 4589 4590 private Iter() { 4591 iterator = indices.iterator(); 4592 } 4593 4594 public boolean hasNext() { 4595 return iterator.hasNext(); 4596 } 4597 public Index next() { 4598 return indices.translate(iterator.next()); 4599 } 4600 public void remove() { 4601 iterator.remove(); 4602 } 4603 } 4604 } 4605 4491 4606 /** 4492 4607 * Calculate size for this indices from a list of int arrays containing the … … 4498 4613 if (!tr) 4499 4614 n++; 4500 if (n == 0) {4615 if (n + extra == 0) { 4501 4616 return Size.SCALAR; 4502 4617 }
Note: See TracChangeset
for help on using the changeset viewer.