| 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 _List<E> implements List<E> { |
| 8 static final int _classId = (new _ObjectArray(0))._cid; | 8 static final int _classId = (new _List(0))._cid; |
| 9 | 9 |
| 10 factory _ObjectArray(length) native "ObjectArray_allocate"; | 10 factory _List(length) native "List_allocate"; |
| 11 | 11 |
| 12 E operator [](int index) native "ObjectArray_getIndexed"; | 12 E operator [](int index) native "List_getIndexed"; |
| 13 | 13 |
| 14 void operator []=(int index, E value) native "ObjectArray_setIndexed"; | 14 void operator []=(int index, E value) native "List_setIndexed"; |
| 15 | 15 |
| 16 String toString() { | 16 String toString() { |
| 17 return IterableMixinWorkaround.toStringIterable(this,'[' , ']'); | 17 return IterableMixinWorkaround.toStringIterable(this,'[' , ']'); |
| 18 } | 18 } |
| 19 | 19 |
| 20 int get length native "ObjectArray_getLength"; | 20 int get length native "List_getLength"; |
| 21 | 21 |
| 22 void _copyFromObjectArray(_ObjectArray src, | 22 void _copyFromObjectArray(_List src, |
| 23 int srcStart, | 23 int srcStart, |
| 24 int dstStart, | 24 int dstStart, |
| 25 int count) | 25 int count) |
| 26 native "ObjectArray_copyFromObjectArray"; | 26 native "List_copyFromObjectArray"; |
| 27 | 27 |
| 28 void insert(int index, E element) { | 28 void insert(int index, E element) { |
| 29 throw new UnsupportedError( | 29 throw new UnsupportedError( |
| 30 "Cannot add to a non-extendable array"); | 30 "Cannot add to a non-extendable array"); |
| 31 } | 31 } |
| 32 | 32 |
| 33 void insertAll(int index, Iterable<E> iterable) { | 33 void insertAll(int index, Iterable<E> iterable) { |
| 34 throw new UnsupportedError( | 34 throw new UnsupportedError( |
| 35 "Cannot add to a non-extendable array"); | 35 "Cannot add to a non-extendable array"); |
| 36 } | 36 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 67 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { | 67 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { |
| 68 if (start < 0 || start > this.length) { | 68 if (start < 0 || start > this.length) { |
| 69 throw new RangeError.range(start, 0, this.length); | 69 throw new RangeError.range(start, 0, this.length); |
| 70 } | 70 } |
| 71 if (end < start || end > this.length) { | 71 if (end < start || end > this.length) { |
| 72 throw new RangeError.range(end, start, this.length); | 72 throw new RangeError.range(end, start, this.length); |
| 73 } | 73 } |
| 74 int length = end - start; | 74 int length = end - start; |
| 75 if (length == 0) return; | 75 if (length == 0) return; |
| 76 | 76 |
| 77 if (iterable is _ObjectArray) { | 77 if (iterable is _List) { |
| 78 _copyFromObjectArray(iterable, skipCount, start, length); | 78 _copyFromObjectArray(iterable, skipCount, start, length); |
| 79 } else { | 79 } else { |
| 80 List otherList; | 80 List otherList; |
| 81 int otherStart; | 81 int otherStart; |
| 82 if (iterable is List) { | 82 if (iterable is List) { |
| 83 otherList = iterable; | 83 otherList = iterable; |
| 84 otherStart = skipCount; | 84 otherStart = skipCount; |
| 85 } else { | 85 } else { |
| 86 otherList = | 86 otherList = |
| 87 iterable.skip(skipCount).take(length).toList(growable: false); | 87 iterable.skip(skipCount).take(length).toList(growable: false); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 103 | 103 |
| 104 void fillRange(int start, int end, [E fillValue]) { | 104 void fillRange(int start, int end, [E fillValue]) { |
| 105 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); | 105 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); |
| 106 } | 106 } |
| 107 | 107 |
| 108 List<E> sublist(int start, [int end]) { | 108 List<E> sublist(int start, [int end]) { |
| 109 Arrays.indicesCheck(this, start, end); | 109 Arrays.indicesCheck(this, start, end); |
| 110 if (end == null) end = this.length; | 110 if (end == null) end = this.length; |
| 111 int length = end - start; | 111 int length = end - start; |
| 112 if (start == end) return []; | 112 if (start == end) return []; |
| 113 List list = new _GrowableObjectArray<E>.withCapacity(length); | 113 List list = new _GrowableList<E>.withCapacity(length); |
| 114 list.length = length; | 114 list.length = length; |
| 115 Arrays.copy(this, start, list, 0, length); | 115 Arrays.copy(this, start, list, 0, length); |
| 116 return list; | 116 return list; |
| 117 } | 117 } |
| 118 | 118 |
| 119 // Iterable interface. | 119 // Iterable interface. |
| 120 | 120 |
| 121 bool contains(Object element) { | 121 bool contains(Object element) { |
| 122 return IterableMixinWorkaround.contains(this, element); | 122 return IterableMixinWorkaround.contains(this, element); |
| 123 } | 123 } |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 Set<E> toSet() { | 263 Set<E> toSet() { |
| 264 return new Set<E>.from(this); | 264 return new Set<E>.from(this); |
| 265 } | 265 } |
| 266 | 266 |
| 267 Map<int, E> asMap() { | 267 Map<int, E> asMap() { |
| 268 return IterableMixinWorkaround.asMapList(this); | 268 return IterableMixinWorkaround.asMapList(this); |
| 269 } | 269 } |
| 270 } | 270 } |
| 271 | 271 |
| 272 | 272 |
| 273 // This is essentially the same class as _ObjectArray, but it does not | 273 // This is essentially the same class as _List, but it does not |
| 274 // permit any modification of array elements from Dart code. We use | 274 // permit any modification of array elements from Dart code. We use |
| 275 // this class for arrays constructed from Dart array literals. | 275 // this class for arrays constructed from Dart array literals. |
| 276 // TODO(hausner): We should consider the trade-offs between two | 276 // TODO(hausner): We should consider the trade-offs between two |
| 277 // classes (and inline cache misses) versus a field in the native | 277 // classes (and inline cache misses) versus a field in the native |
| 278 // implementation (checks when modifying). We should keep watching | 278 // implementation (checks when modifying). We should keep watching |
| 279 // the inline cache misses. | 279 // the inline cache misses. |
| 280 class _ImmutableArray<E> implements List<E> { | 280 class _ImmutableList<E> implements List<E> { |
| 281 static final int _classId = (const [])._cid; | 281 static final int _classId = (const [])._cid; |
| 282 | 282 |
| 283 factory _ImmutableArray._uninstantiable() { | 283 factory _ImmutableList._uninstantiable() { |
| 284 throw new UnsupportedError( | 284 throw new UnsupportedError( |
| 285 "ImmutableArray can only be allocated by the VM"); | 285 "ImmutableArray can only be allocated by the VM"); |
| 286 } | 286 } |
| 287 | 287 |
| 288 E operator [](int index) native "ObjectArray_getIndexed"; | 288 E operator [](int index) native "List_getIndexed"; |
| 289 | 289 |
| 290 void operator []=(int index, E value) { | 290 void operator []=(int index, E value) { |
| 291 throw new UnsupportedError( | 291 throw new UnsupportedError( |
| 292 "Cannot modify an immutable array"); | 292 "Cannot modify an immutable array"); |
| 293 } | 293 } |
| 294 | 294 |
| 295 int get length native "ObjectArray_getLength"; | 295 int get length native "List_getLength"; |
| 296 | 296 |
| 297 void insert(int index, E element) { | 297 void insert(int index, E element) { |
| 298 throw new UnsupportedError( | 298 throw new UnsupportedError( |
| 299 "Cannot add to an immutable array"); | 299 "Cannot add to an immutable array"); |
| 300 } | 300 } |
| 301 | 301 |
| 302 void insertAll(int index, Iterable<E> iterable) { | 302 void insertAll(int index, Iterable<E> iterable) { |
| 303 throw new UnsupportedError( | 303 throw new UnsupportedError( |
| 304 "Cannot add to an immutable array"); | 304 "Cannot add to an immutable array"); |
| 305 } | 305 } |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 | 530 |
| 531 // Iterator for arrays with fixed size. | 531 // Iterator for arrays with fixed size. |
| 532 class _FixedSizeArrayIterator<E> implements Iterator<E> { | 532 class _FixedSizeArrayIterator<E> implements Iterator<E> { |
| 533 final List<E> _array; | 533 final List<E> _array; |
| 534 final int _length; // Cache array length for faster access. | 534 final int _length; // Cache array length for faster access. |
| 535 int _position; | 535 int _position; |
| 536 E _current; | 536 E _current; |
| 537 | 537 |
| 538 _FixedSizeArrayIterator(List array) | 538 _FixedSizeArrayIterator(List array) |
| 539 : _array = array, _length = array.length, _position = -1 { | 539 : _array = array, _length = array.length, _position = -1 { |
| 540 assert(array is _ObjectArray || array is _ImmutableArray); | 540 assert(array is _List || array is _ImmutableList); |
| 541 } | 541 } |
| 542 | 542 |
| 543 bool moveNext() { | 543 bool moveNext() { |
| 544 int nextPosition = _position + 1; | 544 int nextPosition = _position + 1; |
| 545 if (nextPosition < _length) { | 545 if (nextPosition < _length) { |
| 546 _current = _array[nextPosition]; | 546 _current = _array[nextPosition]; |
| 547 _position = nextPosition; | 547 _position = nextPosition; |
| 548 return true; | 548 return true; |
| 549 } | 549 } |
| 550 _position = _length; | 550 _position = _length; |
| 551 _current = null; | 551 _current = null; |
| 552 return false; | 552 return false; |
| 553 } | 553 } |
| 554 | 554 |
| 555 E get current { | 555 E get current { |
| 556 return _current; | 556 return _current; |
| 557 } | 557 } |
| 558 } | 558 } |
| OLD | NEW |