OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 part of dart._internal; | 5 part of dart._internal; |
6 | 6 |
7 /** | 7 /** |
8 * Marker interface for [Iterable] subclasses that have an efficient | 8 * Marker interface for [Iterable] subclasses that have an efficient |
9 * [length] implementation. | 9 * [length] implementation. |
10 */ | 10 */ |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 Set<E> toSet() { | 219 Set<E> toSet() { |
220 Set<E> result = new Set<E>(); | 220 Set<E> result = new Set<E>(); |
221 for (int i = 0; i < length; i++) { | 221 for (int i = 0; i < length; i++) { |
222 result.add(elementAt(i)); | 222 result.add(elementAt(i)); |
223 } | 223 } |
224 return result; | 224 return result; |
225 } | 225 } |
226 } | 226 } |
227 | 227 |
228 class SubListIterable<E> extends ListIterable<E> { | 228 class SubListIterable<E> extends ListIterable<E> { |
229 final Iterable<E> _iterable; | 229 final Iterable<E> _iterable; // Has efficient length and elementAt. |
230 final int _start; | 230 final int _start; |
231 /** If null, represents the length of the iterable. */ | 231 /** If null, represents the length of the iterable. */ |
232 final int _endOrLength; | 232 final int _endOrLength; |
233 | 233 |
234 SubListIterable(this._iterable, this._start, this._endOrLength) { | 234 SubListIterable(this._iterable, this._start, this._endOrLength) { |
235 if (_start < 0) { | 235 if (_start < 0) { |
236 throw new RangeError.value(_start); | 236 throw new RangeError.value(_start); |
237 } | 237 } |
238 if (_endOrLength != null) { | 238 if (_endOrLength != null) { |
239 if (_endOrLength < 0) { | 239 if (_endOrLength < 0) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 Iterable<E> take(int count) { | 286 Iterable<E> take(int count) { |
287 if (count < 0) throw new RangeError.value(count); | 287 if (count < 0) throw new RangeError.value(count); |
288 if (_endOrLength == null) { | 288 if (_endOrLength == null) { |
289 return new SubListIterable<E>(_iterable, _start, _start + count); | 289 return new SubListIterable<E>(_iterable, _start, _start + count); |
290 } else { | 290 } else { |
291 int newEnd = _start + count; | 291 int newEnd = _start + count; |
292 if (_endOrLength < newEnd) return this; | 292 if (_endOrLength < newEnd) return this; |
293 return new SubListIterable<E>(_iterable, _start, newEnd); | 293 return new SubListIterable<E>(_iterable, _start, newEnd); |
294 } | 294 } |
295 } | 295 } |
| 296 |
| 297 List<E> toList({bool growable: false}) { |
| 298 int start = _start; |
| 299 int end = _iterable.length; |
| 300 if (_endOrLength != null && _endOrLength < end) end = _endOrLength; |
| 301 int length = end - start; |
| 302 if (length < 0) length = 0; |
| 303 List result = growable ? (new List<E>()..length = length) |
| 304 : new List<E>(length); |
| 305 for (int i = 0; i < length; i++) { |
| 306 result[i] = _iterable.elementAt(start + i); |
| 307 if (_iterable.length < end) throw new ConcurrentModificationError(this); |
| 308 } |
| 309 return result; |
| 310 } |
296 } | 311 } |
297 | 312 |
298 /** | 313 /** |
299 * An [Iterator] that iterates a list-like [Iterable]. | 314 * An [Iterator] that iterates a list-like [Iterable]. |
300 * | 315 * |
301 * All iterations is done in terms of [Iterable.length] and | 316 * All iterations is done in terms of [Iterable.length] and |
302 * [Iterable.elementAt]. These operations are fast for list-like | 317 * [Iterable.elementAt]. These operations are fast for list-like |
303 * iterables. | 318 * iterables. |
304 */ | 319 */ |
305 class ListIterator<E> implements Iterator<E> { | 320 class ListIterator<E> implements Iterator<E> { |
(...skipping 844 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1150 * Creates errors throw by [Iterable] when the element count is wrong. | 1165 * Creates errors throw by [Iterable] when the element count is wrong. |
1151 */ | 1166 */ |
1152 abstract class IterableElementError { | 1167 abstract class IterableElementError { |
1153 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ | 1168 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ |
1154 static StateError noElement() => new StateError("No element"); | 1169 static StateError noElement() => new StateError("No element"); |
1155 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ | 1170 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ |
1156 static StateError tooMany() => new StateError("Too many elements"); | 1171 static StateError tooMany() => new StateError("Too many elements"); |
1157 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ | 1172 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ |
1158 static StateError tooFew() => new StateError("Too few elements"); | 1173 static StateError tooFew() => new StateError("Too few elements"); |
1159 } | 1174 } |
OLD | NEW |