| 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._collection.dev; | 5 part of dart._collection.dev; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * An [Iterable] for classes that have efficient [length] and [elementAt]. | 8 * An [Iterable] for classes that have efficient [length] and [elementAt]. |
| 9 * | 9 * |
| 10 * All other methods are implemented in terms of [length] and [elementAt], | 10 * All other methods are implemented in terms of [length] and [elementAt], |
| 11 * including [iterator]. | 11 * including [iterator]. |
| 12 */ | 12 */ |
| 13 abstract class ListIterable<E> extends Iterable<E> { | 13 abstract class ListIterable<E> extends IterableBase<E> { |
| 14 int get length; | 14 int get length; |
| 15 E elementAt(int i); | 15 E elementAt(int i); |
| 16 | 16 |
| 17 const ListIterable(); | 17 const ListIterable(); |
| 18 | 18 |
| 19 Iterator<E> get iterator => new ListIterator<E>(this); | 19 Iterator<E> get iterator => new ListIterator<E>(this); |
| 20 | 20 |
| 21 void forEach(void action(E element)) { | 21 void forEach(void action(E element)) { |
| 22 int length = this.length; | 22 int length = this.length; |
| 23 for (int i = 0; i < length; i++) { | 23 for (int i = 0; i < length; i++) { |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 return false; | 291 return false; |
| 292 } | 292 } |
| 293 _current = _iterable.elementAt(_index); | 293 _current = _iterable.elementAt(_index); |
| 294 _index++; | 294 _index++; |
| 295 return true; | 295 return true; |
| 296 } | 296 } |
| 297 } | 297 } |
| 298 | 298 |
| 299 typedef T _Transformation<S, T>(S value); | 299 typedef T _Transformation<S, T>(S value); |
| 300 | 300 |
| 301 class MappedIterable<S, T> extends Iterable<T> { | 301 class MappedIterable<S, T> extends IterableBase<T> { |
| 302 final Iterable<S> _iterable; | 302 final Iterable<S> _iterable; |
| 303 // TODO(ahe): Restore type when feature is implemented in dart2js | 303 // TODO(ahe): Restore type when feature is implemented in dart2js |
| 304 // checked mode. http://dartbug.com/7733 | 304 // checked mode. http://dartbug.com/7733 |
| 305 final /* _Transformation<S, T> */ _f; | 305 final /* _Transformation<S, T> */ _f; |
| 306 | 306 |
| 307 MappedIterable(this._iterable, T this._f(S element)); | 307 MappedIterable(this._iterable, T this._f(S element)); |
| 308 | 308 |
| 309 Iterator<T> get iterator => new MappedIterator<S, T>(_iterable.iterator, _f); | 309 Iterator<T> get iterator => new MappedIterator<S, T>(_iterable.iterator, _f); |
| 310 | 310 |
| 311 // Length related functions are independent of the mapping. | 311 // Length related functions are independent of the mapping. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 | 349 |
| 350 MappedListIterable(this._source, T this._f(S value)); | 350 MappedListIterable(this._source, T this._f(S value)); |
| 351 | 351 |
| 352 int get length => _source.length; | 352 int get length => _source.length; |
| 353 T elementAt(int index) => _f(_source.elementAt(index)); | 353 T elementAt(int index) => _f(_source.elementAt(index)); |
| 354 } | 354 } |
| 355 | 355 |
| 356 | 356 |
| 357 typedef bool _ElementPredicate<E>(E element); | 357 typedef bool _ElementPredicate<E>(E element); |
| 358 | 358 |
| 359 class WhereIterable<E> extends Iterable<E> { | 359 class WhereIterable<E> extends IterableBase<E> { |
| 360 final Iterable<E> _iterable; | 360 final Iterable<E> _iterable; |
| 361 // TODO(ahe): Restore type when feature is implemented in dart2js | 361 // TODO(ahe): Restore type when feature is implemented in dart2js |
| 362 // checked mode. http://dartbug.com/7733 | 362 // checked mode. http://dartbug.com/7733 |
| 363 final /* _ElementPredicate */ _f; | 363 final /* _ElementPredicate */ _f; |
| 364 | 364 |
| 365 WhereIterable(this._iterable, bool this._f(E element)); | 365 WhereIterable(this._iterable, bool this._f(E element)); |
| 366 | 366 |
| 367 Iterator<E> get iterator => new WhereIterator<E>(_iterable.iterator, _f); | 367 Iterator<E> get iterator => new WhereIterator<E>(_iterable.iterator, _f); |
| 368 } | 368 } |
| 369 | 369 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 382 } | 382 } |
| 383 } | 383 } |
| 384 return false; | 384 return false; |
| 385 } | 385 } |
| 386 | 386 |
| 387 E get current => _iterator.current; | 387 E get current => _iterator.current; |
| 388 } | 388 } |
| 389 | 389 |
| 390 typedef Iterable<T> _ExpandFunction<S, T>(S sourceElement); | 390 typedef Iterable<T> _ExpandFunction<S, T>(S sourceElement); |
| 391 | 391 |
| 392 class ExpandIterable<S, T> extends Iterable<T> { | 392 class ExpandIterable<S, T> extends IterableBase<T> { |
| 393 final Iterable<S> _iterable; | 393 final Iterable<S> _iterable; |
| 394 // TODO(ahe): Restore type when feature is implemented in dart2js | 394 // TODO(ahe): Restore type when feature is implemented in dart2js |
| 395 // checked mode. http://dartbug.com/7733 | 395 // checked mode. http://dartbug.com/7733 |
| 396 final /* _ExpandFunction */ _f; | 396 final /* _ExpandFunction */ _f; |
| 397 | 397 |
| 398 ExpandIterable(this._iterable, Iterable<T> this._f(S element)); | 398 ExpandIterable(this._iterable, Iterable<T> this._f(S element)); |
| 399 | 399 |
| 400 Iterator<T> get iterator => new ExpandIterator<S, T>(_iterable.iterator, _f); | 400 Iterator<T> get iterator => new ExpandIterator<S, T>(_iterable.iterator, _f); |
| 401 } | 401 } |
| 402 | 402 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 429 _currentExpansion = _f(_iterator.current).iterator; | 429 _currentExpansion = _f(_iterator.current).iterator; |
| 430 } else { | 430 } else { |
| 431 return false; | 431 return false; |
| 432 } | 432 } |
| 433 } | 433 } |
| 434 _current = _currentExpansion.current; | 434 _current = _currentExpansion.current; |
| 435 return true; | 435 return true; |
| 436 } | 436 } |
| 437 } | 437 } |
| 438 | 438 |
| 439 class TakeIterable<E> extends Iterable<E> { | 439 class TakeIterable<E> extends IterableBase<E> { |
| 440 final Iterable<E> _iterable; | 440 final Iterable<E> _iterable; |
| 441 final int _takeCount; | 441 final int _takeCount; |
| 442 | 442 |
| 443 TakeIterable(this._iterable, this._takeCount) { | 443 TakeIterable(this._iterable, this._takeCount) { |
| 444 if (_takeCount is! int || _takeCount < 0) { | 444 if (_takeCount is! int || _takeCount < 0) { |
| 445 throw new ArgumentError(_takeCount); | 445 throw new ArgumentError(_takeCount); |
| 446 } | 446 } |
| 447 } | 447 } |
| 448 | 448 |
| 449 Iterator<E> get iterator { | 449 Iterator<E> get iterator { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 467 _remaining = -1; | 467 _remaining = -1; |
| 468 return false; | 468 return false; |
| 469 } | 469 } |
| 470 | 470 |
| 471 E get current { | 471 E get current { |
| 472 if (_remaining < 0) return null; | 472 if (_remaining < 0) return null; |
| 473 return _iterator.current; | 473 return _iterator.current; |
| 474 } | 474 } |
| 475 } | 475 } |
| 476 | 476 |
| 477 class TakeWhileIterable<E> extends Iterable<E> { | 477 class TakeWhileIterable<E> extends IterableBase<E> { |
| 478 final Iterable<E> _iterable; | 478 final Iterable<E> _iterable; |
| 479 // TODO(ahe): Restore type when feature is implemented in dart2js | 479 // TODO(ahe): Restore type when feature is implemented in dart2js |
| 480 // checked mode. http://dartbug.com/7733 | 480 // checked mode. http://dartbug.com/7733 |
| 481 final /* _ElementPredicate */ _f; | 481 final /* _ElementPredicate */ _f; |
| 482 | 482 |
| 483 TakeWhileIterable(this._iterable, bool this._f(E element)); | 483 TakeWhileIterable(this._iterable, bool this._f(E element)); |
| 484 | 484 |
| 485 Iterator<E> get iterator { | 485 Iterator<E> get iterator { |
| 486 return new TakeWhileIterator<E>(_iterable.iterator, _f); | 486 return new TakeWhileIterator<E>(_iterable.iterator, _f); |
| 487 } | 487 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 504 } | 504 } |
| 505 return true; | 505 return true; |
| 506 } | 506 } |
| 507 | 507 |
| 508 E get current { | 508 E get current { |
| 509 if (_isFinished) return null; | 509 if (_isFinished) return null; |
| 510 return _iterator.current; | 510 return _iterator.current; |
| 511 } | 511 } |
| 512 } | 512 } |
| 513 | 513 |
| 514 class SkipIterable<E> extends Iterable<E> { | 514 class SkipIterable<E> extends IterableBase<E> { |
| 515 final Iterable<E> _iterable; | 515 final Iterable<E> _iterable; |
| 516 final int _skipCount; | 516 final int _skipCount; |
| 517 | 517 |
| 518 SkipIterable(this._iterable, this._skipCount) { | 518 SkipIterable(this._iterable, this._skipCount) { |
| 519 if (_skipCount is! int || _skipCount < 0) { | 519 if (_skipCount is! int || _skipCount < 0) { |
| 520 throw new ArgumentError(_skipCount); | 520 throw new ArgumentError(_skipCount); |
| 521 } | 521 } |
| 522 } | 522 } |
| 523 | 523 |
| 524 Iterable<E> skip(int n) { | 524 Iterable<E> skip(int n) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 543 | 543 |
| 544 bool moveNext() { | 544 bool moveNext() { |
| 545 for (int i = 0; i < _skipCount; i++) _iterator.moveNext(); | 545 for (int i = 0; i < _skipCount; i++) _iterator.moveNext(); |
| 546 _skipCount = 0; | 546 _skipCount = 0; |
| 547 return _iterator.moveNext(); | 547 return _iterator.moveNext(); |
| 548 } | 548 } |
| 549 | 549 |
| 550 E get current => _iterator.current; | 550 E get current => _iterator.current; |
| 551 } | 551 } |
| 552 | 552 |
| 553 class SkipWhileIterable<E> extends Iterable<E> { | 553 class SkipWhileIterable<E> extends IterableBase<E> { |
| 554 final Iterable<E> _iterable; | 554 final Iterable<E> _iterable; |
| 555 // TODO(ahe): Restore type when feature is implemented in dart2js | 555 // TODO(ahe): Restore type when feature is implemented in dart2js |
| 556 // checked mode. http://dartbug.com/7733 | 556 // checked mode. http://dartbug.com/7733 |
| 557 final /* _ElementPredicate */ _f; | 557 final /* _ElementPredicate */ _f; |
| 558 | 558 |
| 559 SkipWhileIterable(this._iterable, bool this._f(E element)); | 559 SkipWhileIterable(this._iterable, bool this._f(E element)); |
| 560 | 560 |
| 561 Iterator<E> get iterator { | 561 Iterator<E> get iterator { |
| 562 return new SkipWhileIterator<E>(_iterable.iterator, _f); | 562 return new SkipWhileIterator<E>(_iterable.iterator, _f); |
| 563 } | 563 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 581 } | 581 } |
| 582 return _iterator.moveNext(); | 582 return _iterator.moveNext(); |
| 583 } | 583 } |
| 584 | 584 |
| 585 E get current => _iterator.current; | 585 E get current => _iterator.current; |
| 586 } | 586 } |
| 587 | 587 |
| 588 /** | 588 /** |
| 589 * The always empty [Iterable]. | 589 * The always empty [Iterable]. |
| 590 */ | 590 */ |
| 591 class EmptyIterable<E> extends Iterable<E> { | 591 class EmptyIterable<E> extends IterableBase<E> { |
| 592 const EmptyIterable(); | 592 const EmptyIterable(); |
| 593 | 593 |
| 594 Iterator<E> get iterator => const EmptyIterator(); | 594 Iterator<E> get iterator => const EmptyIterator(); |
| 595 | 595 |
| 596 void forEach(void action(E element)) {} | 596 void forEach(void action(E element)) {} |
| 597 | 597 |
| 598 bool get isEmpty => true; | 598 bool get isEmpty => true; |
| 599 | 599 |
| 600 int get length => 0; | 600 int get length => 0; |
| 601 | 601 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 659 class EmptyIterator<E> implements Iterator<E> { | 659 class EmptyIterator<E> implements Iterator<E> { |
| 660 const EmptyIterator(); | 660 const EmptyIterator(); |
| 661 bool moveNext() => false; | 661 bool moveNext() => false; |
| 662 E get current => null; | 662 E get current => null; |
| 663 } | 663 } |
| 664 | 664 |
| 665 /** An [Iterator] that can move in both directions. */ | 665 /** An [Iterator] that can move in both directions. */ |
| 666 abstract class BidirectionalIterator<T> implements Iterator<T> { | 666 abstract class BidirectionalIterator<T> implements Iterator<T> { |
| 667 bool movePrevious(); | 667 bool movePrevious(); |
| 668 } | 668 } |
| OLD | NEW |