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 566 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 } | 577 } |
578 | 578 |
579 class SkipIterable<E> extends Iterable<E> { | 579 class SkipIterable<E> extends Iterable<E> { |
580 final Iterable<E> _iterable; | 580 final Iterable<E> _iterable; |
581 final int _skipCount; | 581 final int _skipCount; |
582 | 582 |
583 factory SkipIterable(Iterable<E> iterable, int count) { | 583 factory SkipIterable(Iterable<E> iterable, int count) { |
584 if (iterable is EfficientLengthIterable) { | 584 if (iterable is EfficientLengthIterable) { |
585 return new EfficientLengthSkipIterable<E>(iterable, count); | 585 return new EfficientLengthSkipIterable<E>(iterable, count); |
586 } | 586 } |
587 return new SkipIterable<E>._(iterable, count); | 587 return new SkipIterable<E>._(iterable, _checkCount(count)); |
588 } | 588 } |
589 | 589 |
590 SkipIterable._(this._iterable, this._skipCount) { | 590 SkipIterable._(this._iterable, this._skipCount); |
591 if (_skipCount is! int) { | |
592 throw new ArgumentError.value(_skipCount, "count is not an integer"); | |
593 } | |
594 RangeError.checkNotNegative(_skipCount, "count"); | |
595 } | |
596 | 591 |
597 Iterable<E> skip(int count) { | 592 Iterable<E> skip(int count) { |
598 if (_skipCount is! int) { | 593 return new SkipIterable<E>._(_iterable, _skipCount + _checkCount(count)); |
599 throw new ArgumentError.value(_skipCount, "count is not an integer"); | |
600 } | |
601 RangeError.checkNotNegative(_skipCount, "count"); | |
602 return new SkipIterable<E>._(_iterable, _skipCount + count); | |
603 } | 594 } |
604 | 595 |
605 Iterator<E> get iterator { | 596 Iterator<E> get iterator { |
606 return new SkipIterator<E>(_iterable.iterator, _skipCount); | 597 return new SkipIterator<E>(_iterable.iterator, _skipCount); |
607 } | 598 } |
608 } | 599 } |
609 | 600 |
610 class EfficientLengthSkipIterable<E> extends SkipIterable<E> | 601 class EfficientLengthSkipIterable<E> extends SkipIterable<E> |
611 implements EfficientLengthIterable<E> { | 602 implements EfficientLengthIterable<E> { |
612 EfficientLengthSkipIterable(Iterable<E> iterable, int skipCount) | 603 factory EfficientLengthSkipIterable(Iterable<E> iterable, int count) { |
613 : super._(iterable, skipCount); | 604 return new EfficientLengthSkipIterable<E>._(iterable, _checkCount(count)); |
| 605 } |
| 606 |
| 607 EfficientLengthSkipIterable._(Iterable<E> iterable, int count) |
| 608 : super._(iterable, count); |
614 | 609 |
615 int get length { | 610 int get length { |
616 int length = _iterable.length - _skipCount; | 611 int length = _iterable.length - _skipCount; |
617 if (length >= 0) return length; | 612 if (length >= 0) return length; |
618 return 0; | 613 return 0; |
619 } | 614 } |
| 615 |
| 616 Iterable<E> skip(int count) { |
| 617 return new EfficientLengthSkipIterable<E>._( |
| 618 _iterable, _skipCount + _checkCount(count)); |
| 619 } |
| 620 } |
| 621 |
| 622 int _checkCount(int count) { |
| 623 if (count is! int) { |
| 624 throw new ArgumentError.value(count, "count", "is not an integer"); |
| 625 } |
| 626 RangeError.checkNotNegative(count, "count"); |
| 627 return count; |
620 } | 628 } |
621 | 629 |
622 class SkipIterator<E> extends Iterator<E> { | 630 class SkipIterator<E> extends Iterator<E> { |
623 final Iterator<E> _iterator; | 631 final Iterator<E> _iterator; |
624 int _skipCount; | 632 int _skipCount; |
625 | 633 |
626 SkipIterator(this._iterator, this._skipCount) { | 634 SkipIterator(this._iterator, this._skipCount) { |
627 assert(_skipCount is int && _skipCount >= 0); | 635 assert(_skipCount is int && _skipCount >= 0); |
628 } | 636 } |
629 | 637 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
762 * Creates errors throw by [Iterable] when the element count is wrong. | 770 * Creates errors throw by [Iterable] when the element count is wrong. |
763 */ | 771 */ |
764 abstract class IterableElementError { | 772 abstract class IterableElementError { |
765 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ | 773 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ |
766 static StateError noElement() => new StateError("No element"); | 774 static StateError noElement() => new StateError("No element"); |
767 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ | 775 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ |
768 static StateError tooMany() => new StateError("Too many elements"); | 776 static StateError tooMany() => new StateError("Too many elements"); |
769 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ | 777 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ |
770 static StateError tooFew() => new StateError("Too few elements"); | 778 static StateError tooFew() => new StateError("Too few elements"); |
771 } | 779 } |
OLD | NEW |