| 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 // TODO(srdjan): Use shared array implementation. | 6 // TODO(srdjan): Use shared array implementation. |
| 6 class _List<E> extends FixedLengthListBase<E> { | 7 class _List<E> extends FixedLengthListBase<E> { |
| 8 |
| 7 factory _List(length) native "List_allocate"; | 9 factory _List(length) native "List_allocate"; |
| 8 | 10 |
| 9 E operator [](int index) native "List_getIndexed"; | 11 E operator [](int index) native "List_getIndexed"; |
| 10 | 12 |
| 11 void operator []=(int index, E value) native "List_setIndexed"; | 13 void operator []=(int index, E value) native "List_setIndexed"; |
| 12 | 14 |
| 13 int get length native "List_getLength"; | 15 int get length native "List_getLength"; |
| 14 | 16 |
| 15 List _slice(int start, int count, bool needsTypeArgument) { | 17 List _slice(int start, int count, bool needsTypeArgument) { |
| 16 if (count <= 64) { | 18 if (count <= 64) { |
| 17 final result = needsTypeArgument ? new _List<E>(count) : new _List(count); | 19 final result = needsTypeArgument ? new _List<E>(count) |
| 20 : new _List(count); |
| 18 for (int i = 0; i < result.length; i++) { | 21 for (int i = 0; i < result.length; i++) { |
| 19 result[i] = this[start + i]; | 22 result[i] = this[start + i]; |
| 20 } | 23 } |
| 21 return result; | 24 return result; |
| 22 } else { | 25 } else { |
| 23 return _sliceInternal(start, count, needsTypeArgument); | 26 return _sliceInternal(start, count, needsTypeArgument); |
| 24 } | 27 } |
| 25 } | 28 } |
| 26 | 29 |
| 27 List _sliceInternal(int start, int count, bool needsTypeArgument) | 30 List _sliceInternal(int start, int count, bool needsTypeArgument) |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 if (length > 0) return this[length - 1]; | 90 if (length > 0) return this[length - 1]; |
| 88 throw IterableElementError.noElement(); | 91 throw IterableElementError.noElement(); |
| 89 } | 92 } |
| 90 | 93 |
| 91 E get single { | 94 E get single { |
| 92 if (length == 1) return this[0]; | 95 if (length == 1) return this[0]; |
| 93 if (length == 0) throw IterableElementError.noElement(); | 96 if (length == 0) throw IterableElementError.noElement(); |
| 94 throw IterableElementError.tooMany(); | 97 throw IterableElementError.tooMany(); |
| 95 } | 98 } |
| 96 | 99 |
| 97 List<E> toList({bool growable: true}) { | 100 List<E> toList({ bool growable: true }) { |
| 98 var length = this.length; | 101 var length = this.length; |
| 99 if (length > 0) { | 102 if (length > 0) { |
| 100 var result = _slice(0, length, !growable); | 103 var result = _slice(0, length, !growable); |
| 101 if (growable) { | 104 if (growable) { |
| 102 result = new _GrowableList<E>.withData(result); | 105 result = new _GrowableList<E>.withData(result); |
| 103 result._setLength(length); | 106 result._setLength(length); |
| 104 } | 107 } |
| 105 return result; | 108 return result; |
| 106 } | 109 } |
| 107 // _GrowableList.withData must not be called with empty list. | 110 // _GrowableList.withData must not be called with empty list. |
| 108 return growable ? <E>[] : new List<E>(0); | 111 return growable ? <E>[] : new List<E>(0); |
| 109 } | 112 } |
| 110 } | 113 } |
| 111 | 114 |
| 115 |
| 112 // This is essentially the same class as _List, but it does not | 116 // This is essentially the same class as _List, but it does not |
| 113 // permit any modification of array elements from Dart code. We use | 117 // permit any modification of array elements from Dart code. We use |
| 114 // this class for arrays constructed from Dart array literals. | 118 // this class for arrays constructed from Dart array literals. |
| 115 // TODO(hausner): We should consider the trade-offs between two | 119 // TODO(hausner): We should consider the trade-offs between two |
| 116 // classes (and inline cache misses) versus a field in the native | 120 // classes (and inline cache misses) versus a field in the native |
| 117 // implementation (checks when modifying). We should keep watching | 121 // implementation (checks when modifying). We should keep watching |
| 118 // the inline cache misses. | 122 // the inline cache misses. |
| 119 class _ImmutableList<E> extends UnmodifiableListBase<E> { | 123 class _ImmutableList<E> extends UnmodifiableListBase<E> { |
| 124 |
| 120 factory _ImmutableList._uninstantiable() { | 125 factory _ImmutableList._uninstantiable() { |
| 121 throw new UnsupportedError( | 126 throw new UnsupportedError( |
| 122 "ImmutableArray can only be allocated by the VM"); | 127 "ImmutableArray can only be allocated by the VM"); |
| 123 } | 128 } |
| 124 | 129 |
| 125 factory _ImmutableList._from(List from, int offset, int length) | 130 factory _ImmutableList._from(List from, int offset, int length) |
| 126 native "ImmutableList_from"; | 131 native "ImmutableList_from"; |
| 127 | 132 |
| 128 E operator [](int index) native "List_getIndexed"; | 133 E operator [](int index) native "List_getIndexed"; |
| 129 | 134 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 if (length > 0) return this[length - 1]; | 169 if (length > 0) return this[length - 1]; |
| 165 throw IterableElementError.noElement(); | 170 throw IterableElementError.noElement(); |
| 166 } | 171 } |
| 167 | 172 |
| 168 E get single { | 173 E get single { |
| 169 if (length == 1) return this[0]; | 174 if (length == 1) return this[0]; |
| 170 if (length == 0) throw IterableElementError.noElement(); | 175 if (length == 0) throw IterableElementError.noElement(); |
| 171 throw IterableElementError.tooMany(); | 176 throw IterableElementError.tooMany(); |
| 172 } | 177 } |
| 173 | 178 |
| 174 List<E> toList({bool growable: true}) { | 179 List<E> toList({ bool growable: true }) { |
| 175 var length = this.length; | 180 var length = this.length; |
| 176 if (length > 0) { | 181 if (length > 0) { |
| 177 List list = growable ? new _List(length) : new _List<E>(length); | 182 List list = growable ? new _List(length) : new _List<E>(length); |
| 178 for (int i = 0; i < length; i++) { | 183 for (int i = 0; i < length; i++) { |
| 179 list[i] = this[i]; | 184 list[i] = this[i]; |
| 180 } | 185 } |
| 181 if (!growable) return list; | 186 if (!growable) return list; |
| 182 var result = new _GrowableList<E>.withData(list); | 187 var result = new _GrowableList<E>.withData(list); |
| 183 result._setLength(length); | 188 result._setLength(length); |
| 184 return result; | 189 return result; |
| 185 } | 190 } |
| 186 return growable ? <E>[] : new _List<E>(0); | 191 return growable ? <E>[] : new _List<E>(0); |
| 187 } | 192 } |
| 188 } | 193 } |
| 189 | 194 |
| 195 |
| 190 // Iterator for arrays with fixed size. | 196 // Iterator for arrays with fixed size. |
| 191 class _FixedSizeArrayIterator<E> implements Iterator<E> { | 197 class _FixedSizeArrayIterator<E> implements Iterator<E> { |
| 192 final List<E> _array; | 198 final List<E> _array; |
| 193 final int _length; // Cache array length for faster access. | 199 final int _length; // Cache array length for faster access. |
| 194 int _index; | 200 int _index; |
| 195 E _current; | 201 E _current; |
| 196 | 202 |
| 197 _FixedSizeArrayIterator(List array) | 203 _FixedSizeArrayIterator(List array) |
| 198 : _array = array, | 204 : _array = array, _length = array.length, _index = 0 { |
| 199 _length = array.length, | |
| 200 _index = 0 { | |
| 201 assert(array is _List || array is _ImmutableList); | 205 assert(array is _List || array is _ImmutableList); |
| 202 } | 206 } |
| 203 | 207 |
| 204 E get current => _current; | 208 E get current => _current; |
| 205 | 209 |
| 206 bool moveNext() { | 210 bool moveNext() { |
| 207 if (_index >= _length) { | 211 if (_index >= _length) { |
| 208 _current = null; | 212 _current = null; |
| 209 return false; | 213 return false; |
| 210 } | 214 } |
| 211 _current = _array[_index]; | 215 _current = _array[_index]; |
| 212 _index++; | 216 _index++; |
| 213 return true; | 217 return true; |
| 214 } | 218 } |
| 215 } | 219 } |
| OLD | NEW |