| 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 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 int length = this.length; | 188 int length = this.length; |
| 189 for (int i = 0; i < length; i++) { | 189 for (int i = 0; i < length; i++) { |
| 190 value = combine(value, elementAt(i)); | 190 value = combine(value, elementAt(i)); |
| 191 if (length != this.length) { | 191 if (length != this.length) { |
| 192 throw new ConcurrentModificationError(this); | 192 throw new ConcurrentModificationError(this); |
| 193 } | 193 } |
| 194 } | 194 } |
| 195 return value; | 195 return value; |
| 196 } | 196 } |
| 197 | 197 |
| 198 Iterable<E> skip(int count) => new SubListIterable(this, count, null); | 198 Iterable<E> skip(int count) => new SubListIterable<E>(this, count, null); |
| 199 | 199 |
| 200 Iterable<E> skipWhile(bool test(E element)) => super.skipWhile(test); | 200 Iterable<E> skipWhile(bool test(E element)) => super.skipWhile(test); |
| 201 | 201 |
| 202 Iterable<E> take(int count) => new SubListIterable(this, 0, count); | 202 Iterable<E> take(int count) => new SubListIterable<E>(this, 0, count); |
| 203 | 203 |
| 204 Iterable<E> takeWhile(bool test(E element)) => super.takeWhile(test); | 204 Iterable<E> takeWhile(bool test(E element)) => super.takeWhile(test); |
| 205 | 205 |
| 206 List<E> toList({ bool growable: true }) { | 206 List<E> toList({ bool growable: true }) { |
| 207 List<E> result; | 207 List<E> result; |
| 208 if (growable) { | 208 if (growable) { |
| 209 result = new List<E>()..length = length; | 209 result = new List<E>()..length = length; |
| 210 } else { | 210 } else { |
| 211 result = new List<E>(length); | 211 result = new List<E>(length); |
| 212 } | 212 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 E elementAt(int index) { | 269 E elementAt(int index) { |
| 270 int realIndex = _startIndex + index; | 270 int realIndex = _startIndex + index; |
| 271 if (index < 0 || realIndex >= _endIndex) { | 271 if (index < 0 || realIndex >= _endIndex) { |
| 272 throw new RangeError.range(index, 0, length); | 272 throw new RangeError.range(index, 0, length); |
| 273 } | 273 } |
| 274 return _iterable.elementAt(realIndex); | 274 return _iterable.elementAt(realIndex); |
| 275 } | 275 } |
| 276 | 276 |
| 277 Iterable<E> skip(int count) { | 277 Iterable<E> skip(int count) { |
| 278 if (count < 0) throw new RangeError.value(count); | 278 if (count < 0) throw new RangeError.value(count); |
| 279 return new SubListIterable(_iterable, _start + count, _endOrLength); | 279 int newStart = _start + count; |
| 280 if (_endOrLength != null && newStart >= _endOrLength) { |
| 281 return new EmptyIterable<E>(); |
| 282 } |
| 283 return new SubListIterable<E>(_iterable, newStart, _endOrLength); |
| 280 } | 284 } |
| 281 | 285 |
| 282 Iterable<E> take(int count) { | 286 Iterable<E> take(int count) { |
| 283 if (count < 0) throw new RangeError.value(count); | 287 if (count < 0) throw new RangeError.value(count); |
| 284 if (_endOrLength == null) { | 288 if (_endOrLength == null) { |
| 285 return new SubListIterable(_iterable, _start, _start + count); | 289 return new SubListIterable<E>(_iterable, _start, _start + count); |
| 286 } else { | 290 } else { |
| 287 int newEnd = _start + count; | 291 int newEnd = _start + count; |
| 288 if (_endOrLength < newEnd) return this; | 292 if (_endOrLength < newEnd) return this; |
| 289 return new SubListIterable(_iterable, _start, newEnd); | 293 return new SubListIterable<E>(_iterable, _start, newEnd); |
| 290 } | 294 } |
| 291 } | 295 } |
| 292 } | 296 } |
| 293 | 297 |
| 294 /** | 298 /** |
| 295 * An [Iterator] that iterates a list-like [Iterable]. | 299 * An [Iterator] that iterates a list-like [Iterable]. |
| 296 * | 300 * |
| 297 * All iterations is done in terms of [Iterable.length] and | 301 * All iterations is done in terms of [Iterable.length] and |
| 298 * [Iterable.elementAt]. These operations are fast for list-like | 302 * [Iterable.elementAt]. These operations are fast for list-like |
| 299 * iterables. | 303 * iterables. |
| (...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 /** An [Iterator] that can move in both directions. */ | 737 /** An [Iterator] that can move in both directions. */ |
| 734 abstract class BidirectionalIterator<T> implements Iterator<T> { | 738 abstract class BidirectionalIterator<T> implements Iterator<T> { |
| 735 bool movePrevious(); | 739 bool movePrevious(); |
| 736 } | 740 } |
| 737 | 741 |
| 738 /** | 742 /** |
| 739 * This class provides default implementations for Iterables (including Lists). | 743 * This class provides default implementations for Iterables (including Lists). |
| 740 * | 744 * |
| 741 * The uses of this class will be replaced by mixins. | 745 * The uses of this class will be replaced by mixins. |
| 742 */ | 746 */ |
| 743 class IterableMixinWorkaround { | 747 class IterableMixinWorkaround<T> { |
| 744 static bool contains(Iterable iterable, var element) { | 748 static bool contains(Iterable iterable, var element) { |
| 745 for (final e in iterable) { | 749 for (final e in iterable) { |
| 746 if (e == element) return true; | 750 if (e == element) return true; |
| 747 } | 751 } |
| 748 return false; | 752 return false; |
| 749 } | 753 } |
| 750 | 754 |
| 751 static void forEach(Iterable iterable, void f(o)) { | 755 static void forEach(Iterable iterable, void f(o)) { |
| 752 for (final e in iterable) { | 756 for (final e in iterable) { |
| 753 f(e); | 757 f(e); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 927 } else { | 931 } else { |
| 928 buffer.write(list[0]); | 932 buffer.write(list[0]); |
| 929 for (int i = 1; i < list.length; i++) { | 933 for (int i = 1; i < list.length; i++) { |
| 930 buffer.write(separator); | 934 buffer.write(separator); |
| 931 buffer.write(list[i]); | 935 buffer.write(list[i]); |
| 932 } | 936 } |
| 933 } | 937 } |
| 934 return buffer.toString(); | 938 return buffer.toString(); |
| 935 } | 939 } |
| 936 | 940 |
| 937 static Iterable where(Iterable iterable, bool f(var element)) { | 941 Iterable<T> where(Iterable iterable, bool f(var element)) { |
| 938 return new WhereIterable(iterable, f); | 942 return new WhereIterable<T>(iterable, f); |
| 939 } | 943 } |
| 940 | 944 |
| 941 static Iterable map(Iterable iterable, f(var element)) { | 945 static Iterable map(Iterable iterable, f(var element)) { |
| 942 return new MappedIterable(iterable, f); | 946 return new MappedIterable(iterable, f); |
| 943 } | 947 } |
| 944 | 948 |
| 945 static Iterable mapList(List list, f(var element)) { | 949 static Iterable mapList(List list, f(var element)) { |
| 946 return new MappedListIterable(list, f); | 950 return new MappedListIterable(list, f); |
| 947 } | 951 } |
| 948 | 952 |
| 949 static Iterable expand(Iterable iterable, Iterable f(var element)) { | 953 static Iterable expand(Iterable iterable, Iterable f(var element)) { |
| 950 return new ExpandIterable(iterable, f); | 954 return new ExpandIterable(iterable, f); |
| 951 } | 955 } |
| 952 | 956 |
| 953 static Iterable takeList(List list, int n) { | 957 Iterable<T> takeList(List list, int n) { |
| 954 // The generic type is currently lost. It will be fixed with mixins. | 958 // The generic type is currently lost. It will be fixed with mixins. |
| 955 return new SubListIterable(list, 0, n); | 959 return new SubListIterable<T>(list, 0, n); |
| 956 } | 960 } |
| 957 | 961 |
| 958 static Iterable takeWhile(Iterable iterable, bool test(var value)) { | 962 Iterable<T> takeWhile(Iterable iterable, bool test(var value)) { |
| 959 // The generic type is currently lost. It will be fixed with mixins. | 963 // The generic type is currently lost. It will be fixed with mixins. |
| 960 return new TakeWhileIterable(iterable, test); | 964 return new TakeWhileIterable<T>(iterable, test); |
| 961 } | 965 } |
| 962 | 966 |
| 963 static Iterable skipList(List list, int n) { | 967 Iterable<T> skipList(List list, int n) { |
| 964 // The generic type is currently lost. It will be fixed with mixins. | 968 // The generic type is currently lost. It will be fixed with mixins. |
| 965 return new SubListIterable(list, n, null); | 969 return new SubListIterable<T>(list, n, null); |
| 966 } | 970 } |
| 967 | 971 |
| 968 static Iterable skipWhile(Iterable iterable, bool test(var value)) { | 972 Iterable<T> skipWhile(Iterable iterable, bool test(var value)) { |
| 969 // The generic type is currently lost. It will be fixed with mixins. | 973 // The generic type is currently lost. It will be fixed with mixins. |
| 970 return new SkipWhileIterable(iterable, test); | 974 return new SkipWhileIterable<T>(iterable, test); |
| 971 } | 975 } |
| 972 | 976 |
| 973 static Iterable reversedList(List list) { | 977 Iterable<T> reversedList(List list) { |
| 974 return new ReversedListIterable(list); | 978 return new ReversedListIterable<T>(list); |
| 975 } | 979 } |
| 976 | 980 |
| 977 static void sortList(List list, int compare(a, b)) { | 981 static void sortList(List list, int compare(a, b)) { |
| 978 if (compare == null) compare = Comparable.compare; | 982 if (compare == null) compare = Comparable.compare; |
| 979 Sort.sort(list, compare); | 983 Sort.sort(list, compare); |
| 980 } | 984 } |
| 981 | 985 |
| 982 static void shuffleList(List list, Random random) { | 986 static void shuffleList(List list, Random random) { |
| 983 if (random == null) random = new Random(); | 987 if (random == null) random = new Random(); |
| 984 int length = list.length; | 988 int length = list.length; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1002 | 1006 |
| 1003 static void _rangeCheck(List list, int start, int end) { | 1007 static void _rangeCheck(List list, int start, int end) { |
| 1004 if (start < 0 || start > list.length) { | 1008 if (start < 0 || start > list.length) { |
| 1005 throw new RangeError.range(start, 0, list.length); | 1009 throw new RangeError.range(start, 0, list.length); |
| 1006 } | 1010 } |
| 1007 if (end < start || end > list.length) { | 1011 if (end < start || end > list.length) { |
| 1008 throw new RangeError.range(end, start, list.length); | 1012 throw new RangeError.range(end, start, list.length); |
| 1009 } | 1013 } |
| 1010 } | 1014 } |
| 1011 | 1015 |
| 1012 static Iterable getRangeList(List list, int start, int end) { | 1016 Iterable<T> getRangeList(List list, int start, int end) { |
| 1013 _rangeCheck(list, start, end); | 1017 _rangeCheck(list, start, end); |
| 1014 // The generic type is currently lost. It will be fixed with mixins. | 1018 // The generic type is currently lost. It will be fixed with mixins. |
| 1015 return new SubListIterable(list, start, end); | 1019 return new SubListIterable<T>(list, start, end); |
| 1016 } | 1020 } |
| 1017 | 1021 |
| 1018 static void setRangeList(List list, int start, int end, | 1022 static void setRangeList(List list, int start, int end, |
| 1019 Iterable from, int skipCount) { | 1023 Iterable from, int skipCount) { |
| 1020 _rangeCheck(list, start, end); | 1024 _rangeCheck(list, start, end); |
| 1021 int length = end - start; | 1025 int length = end - start; |
| 1022 if (length == 0) return; | 1026 if (length == 0) return; |
| 1023 | 1027 |
| 1024 if (skipCount < 0) throw new ArgumentError(skipCount); | 1028 if (skipCount < 0) throw new ArgumentError(skipCount); |
| 1025 | 1029 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1090 | 1094 |
| 1091 static void setAllList(List list, int index, Iterable iterable) { | 1095 static void setAllList(List list, int index, Iterable iterable) { |
| 1092 if (index < 0 || index > list.length) { | 1096 if (index < 0 || index > list.length) { |
| 1093 throw new RangeError.range(index, 0, list.length); | 1097 throw new RangeError.range(index, 0, list.length); |
| 1094 } | 1098 } |
| 1095 for (var element in iterable) { | 1099 for (var element in iterable) { |
| 1096 list[index++] = element; | 1100 list[index++] = element; |
| 1097 } | 1101 } |
| 1098 } | 1102 } |
| 1099 | 1103 |
| 1100 static Map<int, dynamic> asMapList(List l) { | 1104 Map<int, T> asMapList(List l) { |
| 1101 return new ListMapView(l); | 1105 return new ListMapView<T>(l); |
| 1102 } | 1106 } |
| 1103 | 1107 |
| 1104 static bool setContainsAll(Set set, Iterable other) { | 1108 static bool setContainsAll(Set set, Iterable other) { |
| 1105 for (var element in other) { | 1109 for (var element in other) { |
| 1106 if (!set.contains(element)) return false; | 1110 if (!set.contains(element)) return false; |
| 1107 } | 1111 } |
| 1108 return true; | 1112 return true; |
| 1109 } | 1113 } |
| 1110 | 1114 |
| 1111 static Set setIntersection(Set set, Set other, Set result) { | 1115 static Set setIntersection(Set set, Set other, Set result) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1146 * Creates errors throw by [Iterable] when the element count is wrong. | 1150 * Creates errors throw by [Iterable] when the element count is wrong. |
| 1147 */ | 1151 */ |
| 1148 abstract class IterableElementError { | 1152 abstract class IterableElementError { |
| 1149 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ | 1153 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ |
| 1150 static StateError noElement() => new StateError("No element"); | 1154 static StateError noElement() => new StateError("No element"); |
| 1151 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ | 1155 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ |
| 1152 static StateError tooMany() => new StateError("Too many elements"); | 1156 static StateError tooMany() => new StateError("Too many elements"); |
| 1153 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ | 1157 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ |
| 1154 static StateError tooFew() => new StateError("Too few elements"); | 1158 static StateError tooFew() => new StateError("Too few elements"); |
| 1155 } | 1159 } |
| OLD | NEW |