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 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 | 313 |
314 /** | 314 /** |
315 * An [Iterator] that iterates a list-like [Iterable]. | 315 * An [Iterator] that iterates a list-like [Iterable]. |
316 * | 316 * |
317 * All iterations is done in terms of [Iterable.length] and | 317 * All iterations is done in terms of [Iterable.length] and |
318 * [Iterable.elementAt]. These operations are fast for list-like | 318 * [Iterable.elementAt]. These operations are fast for list-like |
319 * iterables. | 319 * iterables. |
320 */ | 320 */ |
321 class ListIterator<E> implements Iterator<E> { | 321 class ListIterator<E> implements Iterator<E> { |
322 final Iterable<E> _iterable; | 322 final Iterable<E> _iterable; |
323 final int _originalLength; | 323 final int _length; |
324 int _index; | 324 int _index; |
325 E _current; | 325 E _current; |
326 | 326 |
327 ListIterator(Iterable<E> iterable) | 327 ListIterator(Iterable<E> iterable) |
328 : _iterable = iterable, _originalLength = iterable.length, _index = 0; | 328 : _iterable = iterable, _length = iterable.length, _index = 0; |
329 | 329 |
330 E get current => _current; | 330 E get current => _current; |
331 | 331 |
332 bool moveNext() { | 332 bool moveNext() { |
333 // Check for concurrent modifiction at each step in checked mode. | 333 int length = _iterable.length; |
334 assert((_originalLength == _iterable.length) || | 334 if (_length != length) { |
335 (throw new ConcurrentModificationError(_iterable))); | |
336 if (_index < _iterable.length) { | |
337 _current = _iterable.elementAt(_index); | |
338 _index++; | |
339 return true; | |
340 } | |
341 // Check for concurrent modification only at the end in production mode. | |
342 if (_originalLength != _iterable.length) { | |
343 throw new ConcurrentModificationError(_iterable); | 335 throw new ConcurrentModificationError(_iterable); |
344 } | 336 } |
345 _current = null; | 337 if (_index >= length) { |
346 return false; | 338 _current = null; |
| 339 return false; |
| 340 } |
| 341 _current = _iterable.elementAt(_index); |
| 342 _index++; |
| 343 return true; |
347 } | 344 } |
348 } | 345 } |
349 | 346 |
350 typedef T _Transformation<S, T>(S value); | 347 typedef T _Transformation<S, T>(S value); |
351 | 348 |
352 class MappedIterable<S, T> extends Iterable<T> { | 349 class MappedIterable<S, T> extends Iterable<T> { |
353 final Iterable<S> _iterable; | 350 final Iterable<S> _iterable; |
354 final _Transformation<S, T> _f; | 351 final _Transformation<S, T> _f; |
355 | 352 |
356 factory MappedIterable(Iterable iterable, T function(S value)) { | 353 factory MappedIterable(Iterable iterable, T function(S value)) { |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
765 * Creates errors throw by [Iterable] when the element count is wrong. | 762 * Creates errors throw by [Iterable] when the element count is wrong. |
766 */ | 763 */ |
767 abstract class IterableElementError { | 764 abstract class IterableElementError { |
768 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ | 765 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ |
769 static StateError noElement() => new StateError("No element"); | 766 static StateError noElement() => new StateError("No element"); |
770 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ | 767 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ |
771 static StateError tooMany() => new StateError("Too many elements"); | 768 static StateError tooMany() => new StateError("Too many elements"); |
772 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ | 769 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ |
773 static StateError tooFew() => new StateError("Too few elements"); | 770 static StateError tooFew() => new StateError("Too few elements"); |
774 } | 771 } |
OLD | NEW |