| 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 class ListFactory { | 5 class ListFactory<T> { |
| 6 | 6 |
| 7 factory List<T>.from(Iterable<T> other) { | 7 factory List.from(Iterable<T> other) { |
| 8 GrowableObjectArray<T> list = new GrowableObjectArray<T>(); | 8 GrowableObjectArray<T> list = new GrowableObjectArray<T>(); |
| 9 for (final e in other) { | 9 for (final e in other) { |
| 10 list.add(e); | 10 list.add(e); |
| 11 } | 11 } |
| 12 return list; | 12 return list; |
| 13 } | 13 } |
| 14 | 14 |
| 15 factory List<T>([int length = null]) { | 15 factory List([int length = null]) { |
| 16 if (length === null) { | 16 if (length === null) { |
| 17 return new GrowableObjectArray<T>(); | 17 return new GrowableObjectArray<T>(); |
| 18 } else { | 18 } else { |
| 19 return new ObjectArray<T>(length); | 19 return new ObjectArray<T>(length); |
| 20 } | 20 } |
| 21 } | 21 } |
| 22 } | 22 } |
| 23 | 23 |
| 24 // TODO(srdjan): Use shared array implementation. | 24 // TODO(srdjan): Use shared array implementation. |
| 25 class ObjectArray<T> implements List<T> { | 25 class ObjectArray<T> implements List<T> { |
| 26 | 26 |
| 27 factory ObjectArray<T>(int length) native "ObjectArray_allocate"; | 27 factory ObjectArray(int length) native "ObjectArray_allocate"; |
| 28 | 28 |
| 29 T operator [](int index) native "ObjectArray_getIndexed"; | 29 T operator [](int index) native "ObjectArray_getIndexed"; |
| 30 | 30 |
| 31 void operator []=(int index, T value) native "ObjectArray_setIndexed"; | 31 void operator []=(int index, T value) native "ObjectArray_setIndexed"; |
| 32 | 32 |
| 33 String toString() { | 33 String toString() { |
| 34 return Arrays.asString(this); | 34 return Arrays.asString(this); |
| 35 } | 35 } |
| 36 | 36 |
| 37 int get length() native "ObjectArray_getLength"; | 37 int get length() native "ObjectArray_getLength"; |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 | 154 |
| 155 // This is essentially the same class as ObjectArray, but it does not | 155 // This is essentially the same class as ObjectArray, but it does not |
| 156 // permit any modification of array elements from Dart code. We use | 156 // permit any modification of array elements from Dart code. We use |
| 157 // this class for arrays constructed from Dart array literals. | 157 // this class for arrays constructed from Dart array literals. |
| 158 // TODO(hausner): We should consider the trade-offs between two | 158 // TODO(hausner): We should consider the trade-offs between two |
| 159 // classes (and inline cache misses) versus a field in the native | 159 // classes (and inline cache misses) versus a field in the native |
| 160 // implementation (checks when modifying). We should keep watching | 160 // implementation (checks when modifying). We should keep watching |
| 161 // the inline cache misses. | 161 // the inline cache misses. |
| 162 class ImmutableArray<T> implements List<T> { | 162 class ImmutableArray<T> implements List<T> { |
| 163 | 163 |
| 164 factory ImmutableArray<T>._uninstantiable() { | 164 factory ImmutableArray._uninstantiable() { |
| 165 throw const UnsupportedOperationException( | 165 throw const UnsupportedOperationException( |
| 166 "ImmutableArray can only be allocated by the VM"); | 166 "ImmutableArray can only be allocated by the VM"); |
| 167 } | 167 } |
| 168 | 168 |
| 169 T operator [](int index) native "ObjectArray_getIndexed"; | 169 T operator [](int index) native "ObjectArray_getIndexed"; |
| 170 | 170 |
| 171 void operator []=(int index, T value) { | 171 void operator []=(int index, T value) { |
| 172 throw const UnsupportedOperationException( | 172 throw const UnsupportedOperationException( |
| 173 "Cannot modify an immutable array"); | 173 "Cannot modify an immutable array"); |
| 174 } | 174 } |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 if (!hasNext()) { | 300 if (!hasNext()) { |
| 301 throw const NoMoreElementsException(); | 301 throw const NoMoreElementsException(); |
| 302 } | 302 } |
| 303 return _array[_pos++]; | 303 return _array[_pos++]; |
| 304 } | 304 } |
| 305 | 305 |
| 306 final List<T> _array; | 306 final List<T> _array; |
| 307 final int _length; // Cache array length for faster access. | 307 final int _length; // Cache array length for faster access. |
| 308 int _pos; | 308 int _pos; |
| 309 } | 309 } |
| OLD | NEW |