| 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 _List<E> implements List<E> { | 7 class _List<E> implements List<E> { |
| 8 | 8 |
| 9 factory _List(length) native "List_allocate"; | 9 factory _List(length) native "List_allocate"; |
| 10 | 10 |
| 11 E operator [](int index) native "List_getIndexed"; | 11 E operator [](int index) native "List_getIndexed"; |
| 12 | 12 |
| 13 void operator []=(int index, E value) native "List_setIndexed"; | 13 void operator []=(int index, E value) native "List_setIndexed"; |
| 14 | 14 |
| 15 String toString() { | 15 String toString() { |
| 16 return ListBase.listToString(this); | 16 return ListBase.listToString(this); |
| 17 } | 17 } |
| 18 | 18 |
| 19 int get length native "List_getLength"; | 19 int get length native "List_getLength"; |
| 20 | 20 |
| 21 void _copyFromObjectArray(_List src, | 21 List _slice(int start, int count, bool needsTypeArgument) { |
| 22 int srcStart, | 22 if (count <= 64) { |
| 23 int dstStart, | 23 final result = needsTypeArgument ? new _List<E>(count) |
| 24 int count) { | 24 : new _List(count); |
| 25 if (count < 128) { | 25 for (int i = 0; i < result.length; i++) { |
| 26 for (int i = 0; i < count; i++) { | 26 result[i] = this[start + i]; |
| 27 this[dstStart + i] = src[srcStart + i]; | |
| 28 } | 27 } |
| 28 return result; |
| 29 } else { | 29 } else { |
| 30 _copyFromObjectArrayInternal(src, srcStart, dstStart, count); | 30 return _sliceInternal(start, count, needsTypeArgument); |
| 31 } | 31 } |
| 32 } | 32 } |
| 33 | 33 |
| 34 void _copyFromObjectArrayInternal(_List src, | 34 List _sliceInternal(int start, int count, bool needsTypeArgument) |
| 35 int srcStart, | 35 native "List_slice"; |
| 36 int dstStart, | |
| 37 int count) | |
| 38 native "List_copyFromObjectArray"; | |
| 39 | 36 |
| 40 void insert(int index, E element) { | 37 void insert(int index, E element) { |
| 41 throw NonGrowableListError.add(); | 38 throw NonGrowableListError.add(); |
| 42 } | 39 } |
| 43 | 40 |
| 44 void insertAll(int index, Iterable<E> iterable) { | 41 void insertAll(int index, Iterable<E> iterable) { |
| 45 throw NonGrowableListError.add(); | 42 throw NonGrowableListError.add(); |
| 46 } | 43 } |
| 47 | 44 |
| 48 void setAll(int index, Iterable<E> iterable) { | 45 void setAll(int index, Iterable<E> iterable) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 75 throw new RangeError.range(start, 0, this.length); | 72 throw new RangeError.range(start, 0, this.length); |
| 76 } | 73 } |
| 77 if (end < start || end > this.length) { | 74 if (end < start || end > this.length) { |
| 78 throw new RangeError.range(end, start, this.length); | 75 throw new RangeError.range(end, start, this.length); |
| 79 } | 76 } |
| 80 int length = end - start; | 77 int length = end - start; |
| 81 if (length == 0) return; | 78 if (length == 0) return; |
| 82 if (identical(this, iterable)) { | 79 if (identical(this, iterable)) { |
| 83 Lists.copy(iterable, skipCount, this, start, length); | 80 Lists.copy(iterable, skipCount, this, start, length); |
| 84 } else if (ClassID.getID(iterable) == ClassID.cidArray) { | 81 } else if (ClassID.getID(iterable) == ClassID.cidArray) { |
| 85 _copyFromObjectArray(iterable, skipCount, start, length); | 82 Lists.copy(iterable, skipCount, this, start, length); |
| 86 } else if (iterable is List) { | 83 } else if (iterable is List) { |
| 87 Lists.copy(iterable, skipCount, this, start, length); | 84 Lists.copy(iterable, skipCount, this, start, length); |
| 88 } else { | 85 } else { |
| 89 Iterator it = iterable.iterator; | 86 Iterator it = iterable.iterator; |
| 90 while (skipCount > 0) { | 87 while (skipCount > 0) { |
| 91 if (!it.moveNext()) return; | 88 if (!it.moveNext()) return; |
| 92 skipCount--; | 89 skipCount--; |
| 93 } | 90 } |
| 94 for (int i = start; i < end; i++) { | 91 for (int i = start; i < end; i++) { |
| 95 if (!it.moveNext()) return; | 92 if (!it.moveNext()) return; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 108 | 105 |
| 109 void fillRange(int start, int end, [E fillValue]) { | 106 void fillRange(int start, int end, [E fillValue]) { |
| 110 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); | 107 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); |
| 111 } | 108 } |
| 112 | 109 |
| 113 List<E> sublist(int start, [int end]) { | 110 List<E> sublist(int start, [int end]) { |
| 114 Lists.indicesCheck(this, start, end); | 111 Lists.indicesCheck(this, start, end); |
| 115 if (end == null) end = this.length; | 112 if (end == null) end = this.length; |
| 116 int length = end - start; | 113 int length = end - start; |
| 117 if (start == end) return <E>[]; | 114 if (start == end) return <E>[]; |
| 118 List list = new _List(length); | 115 var result = new _GrowableList<E>.withData(_slice(start, length, false)); |
| 119 list._copyFromObjectArray(this, start, 0, length); | |
| 120 var result = new _GrowableList<E>.withData(list); | |
| 121 result._setLength(length); | 116 result._setLength(length); |
| 122 return result; | 117 return result; |
| 123 } | 118 } |
| 124 | 119 |
| 125 // Iterable interface. | 120 // Iterable interface. |
| 126 | 121 |
| 127 bool contains(Object element) { | 122 bool contains(Object element) { |
| 128 return IterableMixinWorkaround.contains(this, element); | 123 return IterableMixinWorkaround.contains(this, element); |
| 129 } | 124 } |
| 130 | 125 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 | 256 |
| 262 E get single { | 257 E get single { |
| 263 if (length == 1) return this[0]; | 258 if (length == 1) return this[0]; |
| 264 if (length == 0) throw IterableElementError.noElement(); | 259 if (length == 0) throw IterableElementError.noElement(); |
| 265 throw IterableElementError.tooMany(); | 260 throw IterableElementError.tooMany(); |
| 266 } | 261 } |
| 267 | 262 |
| 268 List<E> toList({ bool growable: true }) { | 263 List<E> toList({ bool growable: true }) { |
| 269 var length = this.length; | 264 var length = this.length; |
| 270 if (length > 0) { | 265 if (length > 0) { |
| 271 var result = growable ? new _List(length) : new _List<E>(length); | 266 var result = _slice(0, length, !growable); |
| 272 result._copyFromObjectArray(this, 0, 0, length); | |
| 273 if (growable) { | 267 if (growable) { |
| 274 result = new _GrowableList<E>.withData(result); | 268 result = new _GrowableList<E>.withData(result); |
| 275 result._setLength(length); | 269 result._setLength(length); |
| 276 } | 270 } |
| 277 return result; | 271 return result; |
| 278 } | 272 } |
| 279 // _GrowableList.withData must not be called with empty list. | 273 // _GrowableList.withData must not be called with empty list. |
| 280 return growable ? <E>[] : new List<E>(0); | 274 return growable ? <E>[] : new List<E>(0); |
| 281 } | 275 } |
| 282 | 276 |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 bool moveNext() { | 564 bool moveNext() { |
| 571 if (_index >= _length) { | 565 if (_index >= _length) { |
| 572 _current = null; | 566 _current = null; |
| 573 return false; | 567 return false; |
| 574 } | 568 } |
| 575 _current = _array[_index]; | 569 _current = _array[_index]; |
| 576 _index++; | 570 _index++; |
| 577 return true; | 571 return true; |
| 578 } | 572 } |
| 579 } | 573 } |
| OLD | NEW |