| 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.core; | 5 part of dart.core; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * The [Iterable] interface allows to get an [Iterator] out of an | 8 * The [Iterable] interface allows to get an [Iterator] out of an |
| 9 * [Iterable] object. | 9 * [Iterable] object. |
| 10 * | 10 * |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 if (index is! int || index < 0) throw new RangeError.value(index); | 365 if (index is! int || index < 0) throw new RangeError.value(index); |
| 366 int remaining = index; | 366 int remaining = index; |
| 367 for (E element in this) { | 367 for (E element in this) { |
| 368 if (remaining == 0) return element; | 368 if (remaining == 0) return element; |
| 369 remaining--; | 369 remaining--; |
| 370 } | 370 } |
| 371 throw new RangeError.value(index); | 371 throw new RangeError.value(index); |
| 372 } | 372 } |
| 373 } | 373 } |
| 374 | 374 |
| 375 typedef T _Transformation<S, T>(S value); | |
| 376 | |
| 377 class MappedIterable<S, T> extends Iterable<T> { | |
| 378 final Iterable<S> _iterable; | |
| 379 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 380 // checked mode. http://dartbug.com/7733 | |
| 381 final /* _Transformation<S, T> */ _f; | |
| 382 | |
| 383 MappedIterable(this._iterable, T this._f(S element)); | |
| 384 | |
| 385 Iterator<T> get iterator => new MappedIterator<S, T>(_iterable.iterator, _f); | |
| 386 | |
| 387 // Length related functions are independent of the mapping. | |
| 388 int get length => _iterable.length; | |
| 389 bool get isEmpty => _iterable.isEmpty; | |
| 390 } | |
| 391 | |
| 392 class MappedIterator<S, T> extends Iterator<T> { | |
| 393 T _current; | |
| 394 final Iterator<S> _iterator; | |
| 395 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 396 // checked mode. http://dartbug.com/7733 | |
| 397 final /* _Transformation<S, T> */ _f; | |
| 398 | |
| 399 MappedIterator(this._iterator, T this._f(S element)); | |
| 400 | |
| 401 bool moveNext() { | |
| 402 if (_iterator.moveNext()) { | |
| 403 _current = _f(_iterator.current); | |
| 404 return true; | |
| 405 } else { | |
| 406 _current = null; | |
| 407 return false; | |
| 408 } | |
| 409 } | |
| 410 | |
| 411 T get current => _current; | |
| 412 } | |
| 413 | |
| 414 typedef bool _ElementPredicate<E>(E element); | |
| 415 | |
| 416 class WhereIterable<E> extends Iterable<E> { | |
| 417 final Iterable<E> _iterable; | |
| 418 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 419 // checked mode. http://dartbug.com/7733 | |
| 420 final /* _ElementPredicate */ _f; | |
| 421 | |
| 422 WhereIterable(this._iterable, bool this._f(E element)); | |
| 423 | |
| 424 Iterator<E> get iterator => new WhereIterator<E>(_iterable.iterator, _f); | |
| 425 } | |
| 426 | |
| 427 class WhereIterator<E> extends Iterator<E> { | |
| 428 final Iterator<E> _iterator; | |
| 429 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 430 // checked mode. http://dartbug.com/7733 | |
| 431 final /* _ElementPredicate */ _f; | |
| 432 | |
| 433 WhereIterator(this._iterator, bool this._f(E element)); | |
| 434 | |
| 435 bool moveNext() { | |
| 436 while (_iterator.moveNext()) { | |
| 437 if (_f(_iterator.current)) { | |
| 438 return true; | |
| 439 } | |
| 440 } | |
| 441 return false; | |
| 442 } | |
| 443 | |
| 444 E get current => _iterator.current; | |
| 445 } | |
| 446 | |
| 447 class TakeIterable<E> extends Iterable<E> { | |
| 448 final Iterable<E> _iterable; | |
| 449 final int _takeCount; | |
| 450 | |
| 451 TakeIterable(this._iterable, this._takeCount) { | |
| 452 if (_takeCount is! int || _takeCount < 0) { | |
| 453 throw new ArgumentError(_takeCount); | |
| 454 } | |
| 455 } | |
| 456 | |
| 457 Iterator<E> get iterator { | |
| 458 return new TakeIterator<E>(_iterable.iterator, _takeCount); | |
| 459 } | |
| 460 } | |
| 461 | |
| 462 class TakeIterator<E> extends Iterator<E> { | |
| 463 final Iterator<E> _iterator; | |
| 464 int _remaining; | |
| 465 | |
| 466 TakeIterator(this._iterator, this._remaining) { | |
| 467 assert(_remaining is int && _remaining >= 0); | |
| 468 } | |
| 469 | |
| 470 bool moveNext() { | |
| 471 _remaining--; | |
| 472 if (_remaining >= 0) { | |
| 473 return _iterator.moveNext(); | |
| 474 } | |
| 475 _remaining = -1; | |
| 476 return false; | |
| 477 } | |
| 478 | |
| 479 E get current { | |
| 480 if (_remaining < 0) return null; | |
| 481 return _iterator.current; | |
| 482 } | |
| 483 } | |
| 484 | |
| 485 class TakeWhileIterable<E> extends Iterable<E> { | |
| 486 final Iterable<E> _iterable; | |
| 487 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 488 // checked mode. http://dartbug.com/7733 | |
| 489 final /* _ElementPredicate */ _f; | |
| 490 | |
| 491 TakeWhileIterable(this._iterable, bool this._f(E element)); | |
| 492 | |
| 493 Iterator<E> get iterator { | |
| 494 return new TakeWhileIterator<E>(_iterable.iterator, _f); | |
| 495 } | |
| 496 } | |
| 497 | |
| 498 class TakeWhileIterator<E> extends Iterator<E> { | |
| 499 final Iterator<E> _iterator; | |
| 500 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 501 // checked mode. http://dartbug.com/7733 | |
| 502 final /* _ElementPredicate */ _f; | |
| 503 bool _isFinished = false; | |
| 504 | |
| 505 TakeWhileIterator(this._iterator, bool this._f(E element)); | |
| 506 | |
| 507 bool moveNext() { | |
| 508 if (_isFinished) return false; | |
| 509 if (!_iterator.moveNext() || !_f(_iterator.current)) { | |
| 510 _isFinished = true; | |
| 511 return false; | |
| 512 } | |
| 513 return true; | |
| 514 } | |
| 515 | |
| 516 E get current { | |
| 517 if (_isFinished) return null; | |
| 518 return _iterator.current; | |
| 519 } | |
| 520 } | |
| 521 | |
| 522 class SkipIterable<E> extends Iterable<E> { | |
| 523 final Iterable<E> _iterable; | |
| 524 final int _skipCount; | |
| 525 | |
| 526 SkipIterable(this._iterable, this._skipCount) { | |
| 527 if (_skipCount is! int || _skipCount < 0) { | |
| 528 throw new ArgumentError(_skipCount); | |
| 529 } | |
| 530 } | |
| 531 | |
| 532 Iterable<E> skip(int n) { | |
| 533 if (n is! int || n < 0) { | |
| 534 throw new ArgumentError(n); | |
| 535 } | |
| 536 return new SkipIterable<E>(_iterable, _skipCount + n); | |
| 537 } | |
| 538 | |
| 539 Iterator<E> get iterator { | |
| 540 return new SkipIterator<E>(_iterable.iterator, _skipCount); | |
| 541 } | |
| 542 } | |
| 543 | |
| 544 class SkipIterator<E> extends Iterator<E> { | |
| 545 final Iterator<E> _iterator; | |
| 546 int _skipCount; | |
| 547 | |
| 548 SkipIterator(this._iterator, this._skipCount) { | |
| 549 assert(_skipCount is int && _skipCount >= 0); | |
| 550 } | |
| 551 | |
| 552 bool moveNext() { | |
| 553 for (int i = 0; i < _skipCount; i++) _iterator.moveNext(); | |
| 554 _skipCount = 0; | |
| 555 return _iterator.moveNext(); | |
| 556 } | |
| 557 | |
| 558 E get current => _iterator.current; | |
| 559 } | |
| 560 | |
| 561 class SkipWhileIterable<E> extends Iterable<E> { | |
| 562 final Iterable<E> _iterable; | |
| 563 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 564 // checked mode. http://dartbug.com/7733 | |
| 565 final /* _ElementPredicate */ _f; | |
| 566 | |
| 567 SkipWhileIterable(this._iterable, bool this._f(E element)); | |
| 568 | |
| 569 Iterator<E> get iterator { | |
| 570 return new SkipWhileIterator<E>(_iterable.iterator, _f); | |
| 571 } | |
| 572 } | |
| 573 | |
| 574 class SkipWhileIterator<E> extends Iterator<E> { | |
| 575 final Iterator<E> _iterator; | |
| 576 // TODO(ahe): Restore type when feature is implemented in dart2js | |
| 577 // checked mode. http://dartbug.com/7733 | |
| 578 final /* _ElementPredicate */ _f; | |
| 579 bool _hasSkipped = false; | |
| 580 | |
| 581 SkipWhileIterator(this._iterator, bool this._f(E element)); | |
| 582 | |
| 583 bool moveNext() { | |
| 584 if (!_hasSkipped) { | |
| 585 _hasSkipped = true; | |
| 586 while (_iterator.moveNext()) { | |
| 587 if (!_f(_iterator.current)) return true; | |
| 588 } | |
| 589 } | |
| 590 return _iterator.moveNext(); | |
| 591 } | |
| 592 | |
| 593 E get current => _iterator.current; | |
| 594 } | |
| 595 | |
| 596 | 375 |
| 597 typedef E _Generator<E>(int index); | 376 typedef E _Generator<E>(int index); |
| 598 | 377 |
| 599 class _GeneratorIterable<E> extends Iterable<E> { | 378 class _GeneratorIterable<E> extends Iterable<E> { |
| 600 final int _count; | 379 final int _count; |
| 601 final _Generator<E> _generator; | 380 final _Generator<E> _generator; |
| 602 _GeneratorIterable(this._count, this._generator); | 381 _GeneratorIterable(this._count, this._generator); |
| 603 Iterator<E> get iterator => new _GeneratorIterator(_count, _generator); | 382 Iterator<E> get iterator => new _GeneratorIterator(_count, _generator); |
| 604 } | 383 } |
| 605 | 384 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 617 _index++; | 396 _index++; |
| 618 return true; | 397 return true; |
| 619 } else { | 398 } else { |
| 620 _current = null; | 399 _current = null; |
| 621 return false; | 400 return false; |
| 622 } | 401 } |
| 623 } | 402 } |
| 624 | 403 |
| 625 E get current => _current; | 404 E get current => _current; |
| 626 } | 405 } |
| OLD | NEW |