| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 | 5 |
| 6 // TODO(srdjan): Use shared array implementation. | 6 // TODO(srdjan): Use shared array implementation. |
| 7 class ObjectArray<E> implements List<E> { | 7 class _ObjectArray<E> implements List<E> { |
| 8 | 8 |
| 9 factory ObjectArray(int length) native "ObjectArray_allocate"; | 9 factory _ObjectArray(int length) native "ObjectArray_allocate"; |
| 10 | 10 |
| 11 E operator [](int index) native "ObjectArray_getIndexed"; | 11 E operator [](int index) native "ObjectArray_getIndexed"; |
| 12 | 12 |
| 13 void operator []=(int index, E value) native "ObjectArray_setIndexed"; | 13 void operator []=(int index, E value) native "ObjectArray_setIndexed"; |
| 14 | 14 |
| 15 String toString() { | 15 String toString() { |
| 16 return Collections.collectionToString(this); | 16 return Collections.collectionToString(this); |
| 17 } | 17 } |
| 18 | 18 |
| 19 int get length native "ObjectArray_getLength"; | 19 int get length native "ObjectArray_getLength"; |
| 20 | 20 |
| 21 void _copyFromObjectArray(ObjectArray src, | 21 void _copyFromObjectArray(_ObjectArray src, |
| 22 int srcStart, | 22 int srcStart, |
| 23 int dstStart, | 23 int dstStart, |
| 24 int count) | 24 int count) |
| 25 native "ObjectArray_copyFromObjectArray"; | 25 native "ObjectArray_copyFromObjectArray"; |
| 26 | 26 |
| 27 E removeAt(int index) { | 27 E removeAt(int index) { |
| 28 throw const UnsupportedOperationException( | 28 throw const UnsupportedOperationException( |
| 29 "Cannot remove element of a non-extendable array"); | 29 "Cannot remove element of a non-extendable array"); |
| 30 } | 30 } |
| 31 | 31 |
| 32 void setRange(int start, int length, List<E> from, [int startFrom = 0]) { | 32 void setRange(int start, int length, List<E> from, [int startFrom = 0]) { |
| 33 if (length < 0) { | 33 if (length < 0) { |
| 34 throw new ArgumentError("negative length $length"); | 34 throw new ArgumentError("negative length $length"); |
| 35 } | 35 } |
| 36 if (from is ObjectArray) { | 36 if (from is _ObjectArray) { |
| 37 _copyFromObjectArray(from, startFrom, start, length); | 37 _copyFromObjectArray(from, startFrom, start, length); |
| 38 } else { | 38 } else { |
| 39 Arrays.copy(from, startFrom, this, start, length); | 39 Arrays.copy(from, startFrom, this, start, length); |
| 40 } | 40 } |
| 41 } | 41 } |
| 42 | 42 |
| 43 void removeRange(int start, int length) { | 43 void removeRange(int start, int length) { |
| 44 throw const UnsupportedOperationException( | 44 throw const UnsupportedOperationException( |
| 45 "Cannot remove range of a non-extendable array"); | 45 "Cannot remove range of a non-extendable array"); |
| 46 } | 46 } |
| 47 | 47 |
| 48 void insertRange(int start, int length, [E initialValue = null]) { | 48 void insertRange(int start, int length, [E initialValue = null]) { |
| 49 throw const UnsupportedOperationException( | 49 throw const UnsupportedOperationException( |
| 50 "Cannot insert range in a non-extendable array"); | 50 "Cannot insert range in a non-extendable array"); |
| 51 } | 51 } |
| 52 | 52 |
| 53 List<E> getRange(int start, int length) { | 53 List<E> getRange(int start, int length) { |
| 54 if (length == 0) return []; | 54 if (length == 0) return []; |
| 55 Arrays.rangeCheck(this, start, length); | 55 Arrays.rangeCheck(this, start, length); |
| 56 List list = new GrowableObjectArray<E>.withCapacity(length); | 56 List list = new _GrowableObjectArray<E>.withCapacity(length); |
| 57 list.length = length; | 57 list.length = length; |
| 58 Arrays.copy(this, start, list, 0, length); | 58 Arrays.copy(this, start, list, 0, length); |
| 59 return list; | 59 return list; |
| 60 } | 60 } |
| 61 | 61 |
| 62 /** | 62 /** |
| 63 * Collection interface. | 63 * Collection interface. |
| 64 */ | 64 */ |
| 65 | 65 |
| 66 void forEach(f(E element)) { | 66 void forEach(f(E element)) { |
| 67 Collections.forEach(this, f); | 67 Collections.forEach(this, f); |
| 68 } | 68 } |
| 69 | 69 |
| 70 Collection map(f(E element)) { | 70 Collection map(f(E element)) { |
| 71 return Collections.map(this, new GrowableObjectArray.withCapacity(length), f
); | 71 return Collections.map( |
| 72 this, new _GrowableObjectArray.withCapacity(length), f); |
| 72 } | 73 } |
| 73 | 74 |
| 74 reduce(initialValue, combine(previousValue, E element)) { | 75 reduce(initialValue, combine(previousValue, E element)) { |
| 75 return Collections.reduce(this, initialValue, combine); | 76 return Collections.reduce(this, initialValue, combine); |
| 76 } | 77 } |
| 77 | 78 |
| 78 Collection<E> filter(bool f(E element)) { | 79 Collection<E> filter(bool f(E element)) { |
| 79 return Collections.filter(this, new GrowableObjectArray<E>(), f); | 80 return Collections.filter(this, new _GrowableObjectArray<E>(), f); |
| 80 } | 81 } |
| 81 | 82 |
| 82 bool every(bool f(E element)) { | 83 bool every(bool f(E element)) { |
| 83 return Collections.every(this, f); | 84 return Collections.every(this, f); |
| 84 } | 85 } |
| 85 | 86 |
| 86 bool some(bool f(E element)) { | 87 bool some(bool f(E element)) { |
| 87 return Collections.some(this, f); | 88 return Collections.some(this, f); |
| 88 } | 89 } |
| 89 | 90 |
| 90 bool isEmpty() { | 91 bool isEmpty() { |
| 91 return this.length === 0; | 92 return this.length === 0; |
| 92 } | 93 } |
| 93 | 94 |
| 94 void sort(int compare(E a, E b)) { | 95 void sort(int compare(E a, E b)) { |
| 95 DualPivotQuicksort.sort(this, compare); | 96 DualPivotQuicksort.sort(this, compare); |
| 96 } | 97 } |
| 97 | 98 |
| 98 int indexOf(E element, [int start = 0]) { | 99 int indexOf(E element, [int start = 0]) { |
| 99 return Arrays.indexOf(this, element, start, this.length); | 100 return Arrays.indexOf(this, element, start, this.length); |
| 100 } | 101 } |
| 101 | 102 |
| 102 int lastIndexOf(E element, [int start = null]) { | 103 int lastIndexOf(E element, [int start = null]) { |
| 103 if (start === null) start = length - 1; | 104 if (start === null) start = length - 1; |
| 104 return Arrays.lastIndexOf(this, element, start); | 105 return Arrays.lastIndexOf(this, element, start); |
| 105 } | 106 } |
| 106 | 107 |
| 107 Iterator<E> iterator() { | 108 Iterator<E> iterator() { |
| 108 return new FixedSizeArrayIterator<E>(this); | 109 return new _FixedSizeArrayIterator<E>(this); |
| 109 } | 110 } |
| 110 | 111 |
| 111 void add(E element) { | 112 void add(E element) { |
| 112 throw const UnsupportedOperationException( | 113 throw const UnsupportedOperationException( |
| 113 "Cannot add to a non-extendable array"); | 114 "Cannot add to a non-extendable array"); |
| 114 } | 115 } |
| 115 | 116 |
| 116 void addLast(E element) { | 117 void addLast(E element) { |
| 117 add(element); | 118 add(element); |
| 118 } | 119 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 136 throw const UnsupportedOperationException( | 137 throw const UnsupportedOperationException( |
| 137 "Cannot remove in a non-extendable array"); | 138 "Cannot remove in a non-extendable array"); |
| 138 } | 139 } |
| 139 | 140 |
| 140 E last() { | 141 E last() { |
| 141 return this[length - 1]; | 142 return this[length - 1]; |
| 142 } | 143 } |
| 143 } | 144 } |
| 144 | 145 |
| 145 | 146 |
| 146 // This is essentially the same class as ObjectArray, but it does not | 147 // This is essentially the same class as _ObjectArray, but it does not |
| 147 // permit any modification of array elements from Dart code. We use | 148 // permit any modification of array elements from Dart code. We use |
| 148 // this class for arrays constructed from Dart array literals. | 149 // this class for arrays constructed from Dart array literals. |
| 149 // TODO(hausner): We should consider the trade-offs between two | 150 // TODO(hausner): We should consider the trade-offs between two |
| 150 // classes (and inline cache misses) versus a field in the native | 151 // classes (and inline cache misses) versus a field in the native |
| 151 // implementation (checks when modifying). We should keep watching | 152 // implementation (checks when modifying). We should keep watching |
| 152 // the inline cache misses. | 153 // the inline cache misses. |
| 153 class ImmutableArray<E> implements List<E> { | 154 class _ImmutableArray<E> implements List<E> { |
| 154 | 155 |
| 155 factory ImmutableArray._uninstantiable() { | 156 factory _ImmutableArray._uninstantiable() { |
| 156 throw const UnsupportedOperationException( | 157 throw const UnsupportedOperationException( |
| 157 "ImmutableArray can only be allocated by the VM"); | 158 "ImmutableArray can only be allocated by the VM"); |
| 158 } | 159 } |
| 159 | 160 |
| 160 E operator [](int index) native "ObjectArray_getIndexed"; | 161 E operator [](int index) native "ObjectArray_getIndexed"; |
| 161 | 162 |
| 162 void operator []=(int index, E value) { | 163 void operator []=(int index, E value) { |
| 163 throw const UnsupportedOperationException( | 164 throw const UnsupportedOperationException( |
| 164 "Cannot modify an immutable array"); | 165 "Cannot modify an immutable array"); |
| 165 } | 166 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 | 203 |
| 203 /** | 204 /** |
| 204 * Collection interface. | 205 * Collection interface. |
| 205 */ | 206 */ |
| 206 | 207 |
| 207 void forEach(f(E element)) { | 208 void forEach(f(E element)) { |
| 208 Collections.forEach(this, f); | 209 Collections.forEach(this, f); |
| 209 } | 210 } |
| 210 | 211 |
| 211 Collection map(f(E element)) { | 212 Collection map(f(E element)) { |
| 212 return Collections.map(this, new GrowableObjectArray.withCapacity(length), f
); | 213 return Collections.map( |
| 214 this, new _GrowableObjectArray.withCapacity(length), f); |
| 213 } | 215 } |
| 214 | 216 |
| 215 reduce(initialValue, combine(previousValue, E element)) { | 217 reduce(initialValue, combine(previousValue, E element)) { |
| 216 return Collections.reduce(this, initialValue, combine); | 218 return Collections.reduce(this, initialValue, combine); |
| 217 } | 219 } |
| 218 | 220 |
| 219 Collection<E> filter(bool f(E element)) { | 221 Collection<E> filter(bool f(E element)) { |
| 220 return Collections.filter(this, new GrowableObjectArray<E>(), f); | 222 return Collections.filter(this, new _GrowableObjectArray<E>(), f); |
| 221 } | 223 } |
| 222 | 224 |
| 223 bool every(bool f(E element)) { | 225 bool every(bool f(E element)) { |
| 224 return Collections.every(this, f); | 226 return Collections.every(this, f); |
| 225 } | 227 } |
| 226 | 228 |
| 227 bool some(bool f(E element)) { | 229 bool some(bool f(E element)) { |
| 228 return Collections.some(this, f); | 230 return Collections.some(this, f); |
| 229 } | 231 } |
| 230 | 232 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 244 int indexOf(E element, [int start = 0]) { | 246 int indexOf(E element, [int start = 0]) { |
| 245 return Arrays.indexOf(this, element, start, this.length); | 247 return Arrays.indexOf(this, element, start, this.length); |
| 246 } | 248 } |
| 247 | 249 |
| 248 int lastIndexOf(E element, [int start = null]) { | 250 int lastIndexOf(E element, [int start = null]) { |
| 249 if (start === null) start = length - 1; | 251 if (start === null) start = length - 1; |
| 250 return Arrays.lastIndexOf(this, element, start); | 252 return Arrays.lastIndexOf(this, element, start); |
| 251 } | 253 } |
| 252 | 254 |
| 253 Iterator<E> iterator() { | 255 Iterator<E> iterator() { |
| 254 return new FixedSizeArrayIterator<E>(this); | 256 return new _FixedSizeArrayIterator<E>(this); |
| 255 } | 257 } |
| 256 | 258 |
| 257 void add(E element) { | 259 void add(E element) { |
| 258 throw const UnsupportedOperationException( | 260 throw const UnsupportedOperationException( |
| 259 "Cannot add to an immutable array"); | 261 "Cannot add to an immutable array"); |
| 260 } | 262 } |
| 261 | 263 |
| 262 void addLast(E element) { | 264 void addLast(E element) { |
| 263 add(element); | 265 add(element); |
| 264 } | 266 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 283 "Cannot remove in a non-extendable array"); | 285 "Cannot remove in a non-extendable array"); |
| 284 } | 286 } |
| 285 | 287 |
| 286 E last() { | 288 E last() { |
| 287 return this[length - 1]; | 289 return this[length - 1]; |
| 288 } | 290 } |
| 289 } | 291 } |
| 290 | 292 |
| 291 | 293 |
| 292 // Iterator for arrays with fixed size. | 294 // Iterator for arrays with fixed size. |
| 293 class FixedSizeArrayIterator<E> implements Iterator<E> { | 295 class _FixedSizeArrayIterator<E> implements Iterator<E> { |
| 294 FixedSizeArrayIterator(List array) | 296 _FixedSizeArrayIterator(List array) |
| 295 : _array = array, _length = array.length, _pos = 0 { | 297 : _array = array, _length = array.length, _pos = 0 { |
| 296 assert(array is ObjectArray || array is ImmutableArray); | 298 assert(array is _ObjectArray || array is _ImmutableArray); |
| 297 } | 299 } |
| 298 | 300 |
| 299 bool hasNext() { | 301 bool hasNext() { |
| 300 return _length > _pos; | 302 return _length > _pos; |
| 301 } | 303 } |
| 302 | 304 |
| 303 E next() { | 305 E next() { |
| 304 if (!hasNext()) { | 306 if (!hasNext()) { |
| 305 throw const NoMoreElementsException(); | 307 throw const NoMoreElementsException(); |
| 306 } | 308 } |
| 307 return _array[_pos++]; | 309 return _array[_pos++]; |
| 308 } | 310 } |
| 309 | 311 |
| 310 final List<E> _array; | 312 final List<E> _array; |
| 311 final int _length; // Cache array length for faster access. | 313 final int _length; // Cache array length for faster access. |
| 312 int _pos; | 314 int _pos; |
| 313 } | 315 } |
| OLD | NEW |