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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 if (length != this.length) { | 165 if (length != this.length) { |
166 throw new ConcurrentModificationError(this); | 166 throw new ConcurrentModificationError(this); |
167 } | 167 } |
168 } | 168 } |
169 return buffer.toString(); | 169 return buffer.toString(); |
170 } | 170 } |
171 } | 171 } |
172 | 172 |
173 Iterable<E> where(bool test(E element)) => super.where(test); | 173 Iterable<E> where(bool test(E element)) => super.where(test); |
174 | 174 |
175 Iterable map(f(E element)) => new MappedListIterable(this, f); | 175 Iterable/*<T>*/ map/*<T>*/(/*=T*/ f(E element)) => |
| 176 new MappedListIterable<E, dynamic/*=T*/ >(this, f); |
176 | 177 |
177 E reduce(E combine(var value, E element)) { | 178 E reduce(E combine(var value, E element)) { |
178 int length = this.length; | 179 int length = this.length; |
179 if (length == 0) throw IterableElementError.noElement(); | 180 if (length == 0) throw IterableElementError.noElement(); |
180 E value = elementAt(0); | 181 E value = elementAt(0); |
181 for (int i = 1; i < length; i++) { | 182 for (int i = 1; i < length; i++) { |
182 value = combine(value, elementAt(i)); | 183 value = combine(value, elementAt(i)); |
183 if (length != this.length) { | 184 if (length != this.length) { |
184 throw new ConcurrentModificationError(this); | 185 throw new ConcurrentModificationError(this); |
185 } | 186 } |
186 | 187 |
187 } | 188 } |
188 return value; | 189 return value; |
189 } | 190 } |
190 | 191 |
191 fold(var initialValue, combine(var previousValue, E element)) { | 192 /*=T*/ fold/*<T>*/( |
| 193 var/*=T*/ initialValue, /*=T*/ combine( |
| 194 var/*=T*/ previousValue, E element)) { |
192 var value = initialValue; | 195 var value = initialValue; |
193 int length = this.length; | 196 int length = this.length; |
194 for (int i = 0; i < length; i++) { | 197 for (int i = 0; i < length; i++) { |
195 value = combine(value, elementAt(i)); | 198 value = combine(value, elementAt(i)); |
196 if (length != this.length) { | 199 if (length != this.length) { |
197 throw new ConcurrentModificationError(this); | 200 throw new ConcurrentModificationError(this); |
198 } | 201 } |
199 } | 202 } |
200 return value; | 203 return value; |
201 } | 204 } |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 | 551 |
549 TakeWhileIterable(this._iterable, bool this._f(E element)); | 552 TakeWhileIterable(this._iterable, bool this._f(E element)); |
550 | 553 |
551 Iterator<E> get iterator { | 554 Iterator<E> get iterator { |
552 return new TakeWhileIterator<E>(_iterable.iterator, _f); | 555 return new TakeWhileIterator<E>(_iterable.iterator, _f); |
553 } | 556 } |
554 } | 557 } |
555 | 558 |
556 class TakeWhileIterator<E> extends Iterator<E> { | 559 class TakeWhileIterator<E> extends Iterator<E> { |
557 final Iterator<E> _iterator; | 560 final Iterator<E> _iterator; |
558 final _ElementPredicate _f; | 561 final _ElementPredicate<E> _f; |
559 bool _isFinished = false; | 562 bool _isFinished = false; |
560 | 563 |
561 TakeWhileIterator(this._iterator, bool this._f(E element)); | 564 TakeWhileIterator(this._iterator, bool this._f(E element)); |
562 | 565 |
563 bool moveNext() { | 566 bool moveNext() { |
564 if (_isFinished) return false; | 567 if (_isFinished) return false; |
565 if (!_iterator.moveNext() || !_f(_iterator.current)) { | 568 if (!_iterator.moveNext() || !_f(_iterator.current)) { |
566 _isFinished = true; | 569 _isFinished = true; |
567 return false; | 570 return false; |
568 } | 571 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 for (int i = 0; i < _skipCount; i++) _iterator.moveNext(); | 633 for (int i = 0; i < _skipCount; i++) _iterator.moveNext(); |
631 _skipCount = 0; | 634 _skipCount = 0; |
632 return _iterator.moveNext(); | 635 return _iterator.moveNext(); |
633 } | 636 } |
634 | 637 |
635 E get current => _iterator.current; | 638 E get current => _iterator.current; |
636 } | 639 } |
637 | 640 |
638 class SkipWhileIterable<E> extends Iterable<E> { | 641 class SkipWhileIterable<E> extends Iterable<E> { |
639 final Iterable<E> _iterable; | 642 final Iterable<E> _iterable; |
640 final _ElementPredicate _f; | 643 final _ElementPredicate<E> _f; |
641 | 644 |
642 SkipWhileIterable(this._iterable, bool this._f(E element)); | 645 SkipWhileIterable(this._iterable, bool this._f(E element)); |
643 | 646 |
644 Iterator<E> get iterator { | 647 Iterator<E> get iterator { |
645 return new SkipWhileIterator<E>(_iterable.iterator, _f); | 648 return new SkipWhileIterator<E>(_iterable.iterator, _f); |
646 } | 649 } |
647 } | 650 } |
648 | 651 |
649 class SkipWhileIterator<E> extends Iterator<E> { | 652 class SkipWhileIterator<E> extends Iterator<E> { |
650 final Iterator<E> _iterator; | 653 final Iterator<E> _iterator; |
651 final _ElementPredicate _f; | 654 final _ElementPredicate<E> _f; |
652 bool _hasSkipped = false; | 655 bool _hasSkipped = false; |
653 | 656 |
654 SkipWhileIterator(this._iterator, bool this._f(E element)); | 657 SkipWhileIterator(this._iterator, bool this._f(E element)); |
655 | 658 |
656 bool moveNext() { | 659 bool moveNext() { |
657 if (!_hasSkipped) { | 660 if (!_hasSkipped) { |
658 _hasSkipped = true; | 661 _hasSkipped = true; |
659 while (_iterator.moveNext()) { | 662 while (_iterator.moveNext()) { |
660 if (!_f(_iterator.current)) return true; | 663 if (!_f(_iterator.current)) return true; |
661 } | 664 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 | 709 |
707 E singleWhere(bool test(E element), { E orElse() }) { | 710 E singleWhere(bool test(E element), { E orElse() }) { |
708 if (orElse != null) return orElse(); | 711 if (orElse != null) return orElse(); |
709 throw IterableElementError.noElement(); | 712 throw IterableElementError.noElement(); |
710 } | 713 } |
711 | 714 |
712 String join([String separator = ""]) => ""; | 715 String join([String separator = ""]) => ""; |
713 | 716 |
714 Iterable<E> where(bool test(E element)) => this; | 717 Iterable<E> where(bool test(E element)) => this; |
715 | 718 |
716 Iterable map(f(E element)) => const EmptyIterable(); | 719 Iterable/*<T>*/ map/*<T>*/(/*=T*/ f(E element)) => const EmptyIterable(); |
717 | 720 |
718 E reduce(E combine(E value, E element)) { | 721 E reduce(E combine(E value, E element)) { |
719 throw IterableElementError.noElement(); | 722 throw IterableElementError.noElement(); |
720 } | 723 } |
721 | 724 |
722 fold(var initialValue, combine(var previousValue, E element)) { | 725 /*=T*/ fold/*<T>*/( |
| 726 var/*=T*/ initialValue, /*=T*/ combine( |
| 727 var/*=T*/ previousValue, E element)) { |
723 return initialValue; | 728 return initialValue; |
724 } | 729 } |
725 | 730 |
726 Iterable<E> skip(int count) { | 731 Iterable<E> skip(int count) { |
727 RangeError.checkNotNegative(count, "count"); | 732 RangeError.checkNotNegative(count, "count"); |
728 return this; | 733 return this; |
729 } | 734 } |
730 | 735 |
731 Iterable<E> skipWhile(bool test(E element)) => this; | 736 Iterable<E> skipWhile(bool test(E element)) => this; |
732 | 737 |
733 Iterable<E> take(int count) { | 738 Iterable<E> take(int count) { |
734 RangeError.checkNotNegative(count, "count"); | 739 RangeError.checkNotNegative(count, "count"); |
735 return this; | 740 return this; |
736 } | 741 } |
737 | 742 |
738 Iterable<E> takeWhile(bool test(E element)) => this; | 743 Iterable<E> takeWhile(bool test(E element)) => this; |
739 | 744 |
740 List toList({ bool growable: true }) => growable ? <E>[] : new List<E>(0); | 745 List<E> toList({bool growable: true}) => growable ? <E>[] : new List<E>(0); |
741 | 746 |
742 Set toSet() => new Set<E>(); | 747 Set<E> toSet() => new Set<E>(); |
743 } | 748 } |
744 | 749 |
745 /** The always empty iterator. */ | 750 /** The always empty iterator. */ |
746 class EmptyIterator<E> implements Iterator<E> { | 751 class EmptyIterator<E> implements Iterator<E> { |
747 const EmptyIterator(); | 752 const EmptyIterator(); |
748 bool moveNext() => false; | 753 bool moveNext() => false; |
749 E get current => null; | 754 E get current => null; |
750 } | 755 } |
751 | 756 |
752 /** | 757 /** |
753 * Creates errors throw by [Iterable] when the element count is wrong. | 758 * Creates errors throw by [Iterable] when the element count is wrong. |
754 */ | 759 */ |
755 abstract class IterableElementError { | 760 abstract class IterableElementError { |
756 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ | 761 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ |
757 static StateError noElement() => new StateError("No element"); | 762 static StateError noElement() => new StateError("No element"); |
758 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ | 763 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ |
759 static StateError tooMany() => new StateError("Too many elements"); | 764 static StateError tooMany() => new StateError("Too many elements"); |
760 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ | 765 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ |
761 static StateError tooFew() => new StateError("Too few elements"); | 766 static StateError tooFew() => new StateError("Too few elements"); |
762 } | 767 } |
OLD | NEW |