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 _length; | 323 final int _originalLength; |
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, _length = iterable.length, _index = 0; | 328 : _iterable = iterable, _originalLength = 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 int length = _iterable.length; | 333 // Check for concurrent modifiction at each step in checked mode. |
334 if (_length != length) { | 334 assert((_originalLength == _iterable.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) { |
335 throw new ConcurrentModificationError(_iterable); | 343 throw new ConcurrentModificationError(_iterable); |
336 } | 344 } |
337 if (_index >= length) { | 345 _current = null; |
338 _current = null; | 346 return false; |
339 return false; | |
340 } | |
341 _current = _iterable.elementAt(_index); | |
342 _index++; | |
343 return true; | |
344 } | 347 } |
345 } | 348 } |
346 | 349 |
347 typedef T _Transformation<S, T>(S value); | 350 typedef T _Transformation<S, T>(S value); |
348 | 351 |
349 class MappedIterable<S, T> extends Iterable<T> { | 352 class MappedIterable<S, T> extends Iterable<T> { |
350 final Iterable<S> _iterable; | 353 final Iterable<S> _iterable; |
351 final _Transformation<S, T> _f; | 354 final _Transformation<S, T> _f; |
352 | 355 |
353 factory MappedIterable(Iterable iterable, T function(S value)) { | 356 factory MappedIterable(Iterable iterable, T function(S value)) { |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
762 * Creates errors throw by [Iterable] when the element count is wrong. | 765 * Creates errors throw by [Iterable] when the element count is wrong. |
763 */ | 766 */ |
764 abstract class IterableElementError { | 767 abstract class IterableElementError { |
765 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ | 768 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ |
766 static StateError noElement() => new StateError("No element"); | 769 static StateError noElement() => new StateError("No element"); |
767 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ | 770 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ |
768 static StateError tooMany() => new StateError("Too many elements"); | 771 static StateError tooMany() => new StateError("Too many elements"); |
769 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ | 772 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ |
770 static StateError tooFew() => new StateError("Too few elements"); | 773 static StateError tooFew() => new StateError("Too few elements"); |
771 } | 774 } |
OLD | NEW |