| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // TODO(ngeoffray): Temporary definition until we remove all uses of 'Array'. | |
| 6 interface Array<T> extends List<T> { | |
| 7 } | |
| 8 | |
| 9 class ListFactory<T> { | 5 class ListFactory<T> { |
| 10 | 6 |
| 11 factory List.from(Iterable<T> other) { | 7 factory List.from(Iterable<T> other) { |
| 12 GrowableObjectArray<T> list = new GrowableObjectArray<T>(); | 8 GrowableObjectArray<T> list = new GrowableObjectArray<T>(); |
| 13 for (final e in other) { | 9 for (final e in other) { |
| 14 list.add(e); | 10 list.add(e); |
| 15 } | 11 } |
| 16 return list; | 12 return list; |
| 17 } | 13 } |
| 18 | 14 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 31 factory List([int length = null]) { | 27 factory List([int length = null]) { |
| 32 if (length === null) { | 28 if (length === null) { |
| 33 return new GrowableObjectArray<T>(); | 29 return new GrowableObjectArray<T>(); |
| 34 } else { | 30 } else { |
| 35 return new ObjectArray<T>(length); | 31 return new ObjectArray<T>(length); |
| 36 } | 32 } |
| 37 } | 33 } |
| 38 } | 34 } |
| 39 | 35 |
| 40 // TODO(srdjan): Use shared array implementation. | 36 // TODO(srdjan): Use shared array implementation. |
| 41 class ObjectArray<T> implements Array<T> { | 37 class ObjectArray<T> implements List<T> { |
| 42 | 38 |
| 43 factory ObjectArray(int length) native "ObjectArray_allocate"; | 39 factory ObjectArray(int length) native "ObjectArray_allocate"; |
| 44 | 40 |
| 45 T operator [](int index) native "ObjectArray_getIndexed"; | 41 T operator [](int index) native "ObjectArray_getIndexed"; |
| 46 | 42 |
| 47 void operator []=(int index, T value) native "ObjectArray_setIndexed"; | 43 void operator []=(int index, T value) native "ObjectArray_setIndexed"; |
| 48 | 44 |
| 49 String toString() { | 45 String toString() { |
| 50 return Arrays.asString(this); | 46 return Arrays.asString(this); |
| 51 } | 47 } |
| 52 | 48 |
| 53 int get length() native "ObjectArray_getLength"; | 49 int get length() native "ObjectArray_getLength"; |
| 54 | 50 |
| 55 void copyFrom(Array src, int srcStart, int dstStart, int count) { | 51 void copyFrom(List src, int srcStart, int dstStart, int count) { |
| 56 if (src is ObjectArray) { | 52 if (src is ObjectArray) { |
| 57 _copyFromObjectArray(src, srcStart, dstStart, count); | 53 _copyFromObjectArray(src, srcStart, dstStart, count); |
| 58 } else { | 54 } else { |
| 59 Arrays.copy(src, srcStart, this, dstStart, count); | 55 Arrays.copy(src, srcStart, this, dstStart, count); |
| 60 } | 56 } |
| 61 } | 57 } |
| 62 | 58 |
| 63 void _copyFromObjectArray(ObjectArray src, | 59 void _copyFromObjectArray(ObjectArray src, |
| 64 int srcStart, | 60 int srcStart, |
| 65 int dstStart, | 61 int dstStart, |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 } | 160 } |
| 165 | 161 |
| 166 | 162 |
| 167 // This is essentially the same class as ObjectArray, but it does not | 163 // This is essentially the same class as ObjectArray, but it does not |
| 168 // permit any modification of array elements from Dart code. We use | 164 // permit any modification of array elements from Dart code. We use |
| 169 // this class for arrays constructed from Dart array literals. | 165 // this class for arrays constructed from Dart array literals. |
| 170 // TODO(hausner): We should consider the trade-offs between two | 166 // TODO(hausner): We should consider the trade-offs between two |
| 171 // classes (and inline cache misses) versus a field in the native | 167 // classes (and inline cache misses) versus a field in the native |
| 172 // implementation (checks when modifying). We should keep watching | 168 // implementation (checks when modifying). We should keep watching |
| 173 // the inline cache misses. | 169 // the inline cache misses. |
| 174 class ImmutableArray<T> implements Array<T> { | 170 class ImmutableArray<T> implements List<T> { |
| 175 | 171 |
| 176 T operator [](int index) native "ObjectArray_getIndexed"; | 172 T operator [](int index) native "ObjectArray_getIndexed"; |
| 177 | 173 |
| 178 void operator []=(int index, T value) { | 174 void operator []=(int index, T value) { |
| 179 throw const UnsupportedOperationException( | 175 throw const UnsupportedOperationException( |
| 180 "Cannot modify an immutable array"); | 176 "Cannot modify an immutable array"); |
| 181 } | 177 } |
| 182 | 178 |
| 183 int get length() native "ObjectArray_getLength"; | 179 int get length() native "ObjectArray_getLength"; |
| 184 | 180 |
| 185 void copyFrom(Array src, int srcStart, int dstStart, int count) { | 181 void copyFrom(List src, int srcStart, int dstStart, int count) { |
| 186 throw const UnsupportedOperationException( | 182 throw const UnsupportedOperationException( |
| 187 "Cannot modify an immutable array"); | 183 "Cannot modify an immutable array"); |
| 188 } | 184 } |
| 189 | 185 |
| 190 void setRange(int start, int length, List<T> from, [int startFrom = 0]) { | 186 void setRange(int start, int length, List<T> from, [int startFrom = 0]) { |
| 191 throw const UnsupportedOperationException( | 187 throw const UnsupportedOperationException( |
| 192 "Cannot modify an immutable array"); | 188 "Cannot modify an immutable array"); |
| 193 } | 189 } |
| 194 | 190 |
| 195 void removeRange(int start, int length) { | 191 void removeRange(int start, int length) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 } | 279 } |
| 284 | 280 |
| 285 T last() { | 281 T last() { |
| 286 return this[length - 1]; | 282 return this[length - 1]; |
| 287 } | 283 } |
| 288 } | 284 } |
| 289 | 285 |
| 290 | 286 |
| 291 // Iterator for arrays with fixed size. | 287 // Iterator for arrays with fixed size. |
| 292 class FixedSizeArrayIterator<T> implements Iterator<T> { | 288 class FixedSizeArrayIterator<T> implements Iterator<T> { |
| 293 FixedSizeArrayIterator(Array array) | 289 FixedSizeArrayIterator(List array) |
| 294 : _array = array, _length = array.length, _pos = 0 { | 290 : _array = array, _length = array.length, _pos = 0 { |
| 295 assert(array is ObjectArray || array is ImmutableArray); | 291 assert(array is ObjectArray || array is ImmutableArray); |
| 296 } | 292 } |
| 297 | 293 |
| 298 bool hasNext() { | 294 bool hasNext() { |
| 299 return _length > _pos; | 295 return _length > _pos; |
| 300 } | 296 } |
| 301 | 297 |
| 302 T next() { | 298 T next() { |
| 303 if (!hasNext()) { | 299 if (!hasNext()) { |
| 304 throw const NoMoreElementsException(); | 300 throw const NoMoreElementsException(); |
| 305 } | 301 } |
| 306 return _array[_pos++]; | 302 return _array[_pos++]; |
| 307 } | 303 } |
| 308 | 304 |
| 309 final Array<T> _array; | 305 final List<T> _array; |
| 310 final int _length; // Cache array length for faster access. | 306 final int _length; // Cache array length for faster access. |
| 311 int _pos; | 307 int _pos; |
| 312 } | 308 } |
| OLD | NEW |