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