Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Unified Diff: tool/input_sdk/lib/internal/iterable.dart

Issue 1943563002: Updates for js_array (Closed) Base URL: https://github.com/dart-lang/dev_compiler@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tool/input_sdk/lib/collection/list.dart ('k') | tool/input_sdk/lib/internal/list.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tool/input_sdk/lib/internal/iterable.dart
diff --git a/tool/input_sdk/lib/internal/iterable.dart b/tool/input_sdk/lib/internal/iterable.dart
index 0816dd01c2c44915fdb9fc2e71768748981d4311..4d404ebb0186b60405ab589dc1be93f458573130 100644
--- a/tool/input_sdk/lib/internal/iterable.dart
+++ b/tool/input_sdk/lib/internal/iterable.dart
@@ -754,408 +754,6 @@ class EmptyIterator<E> implements Iterator<E> {
}
/**
- * This class provides default implementations for Iterables (including Lists).
- *
- * The uses of this class will be replaced by mixins.
- */
-class IterableMixinWorkaround<T> {
- static bool contains/*<E>*/(Iterable/*<E>*/ iterable, var element) {
- for (final e in iterable) {
- if (e == element) return true;
- }
- return false;
- }
-
- static void forEach/*<E>*/(Iterable/*<E>*/ iterable, void f(/*=E*/ o)) {
- for (final e in iterable) {
- f(e);
- }
- }
-
- static bool any/*<E>*/(Iterable/*<E>*/ iterable, bool f(/*=E*/ o)) {
- for (final e in iterable) {
- if (f(e)) return true;
- }
- return false;
- }
-
- static bool every/*<E>*/(Iterable/*<E>*/ iterable, bool f(/*=E*/ o)) {
- for (final e in iterable) {
- if (!f(e)) return false;
- }
- return true;
- }
-
- static dynamic/*=E*/ reduce/*<E>*/(Iterable/*<E>*/ iterable,
- dynamic/*=E*/ combine(/*=E*/ previousValue, /*=E*/ element)) {
- Iterator/*<E>*/ iterator = iterable.iterator;
- if (!iterator.moveNext()) throw IterableElementError.noElement();
- var value = iterator.current;
- while (iterator.moveNext()) {
- value = combine(value, iterator.current);
- }
- return value;
- }
-
- static/*=V*/ fold/*<E, V>*/(
- Iterable/*<E>*/ iterable,
- dynamic/*=V*/ initialValue,
- dynamic/*=V*/ combine(dynamic/*=V*/ previousValue, /*=E*/ element)) {
- for (final element in iterable) {
- initialValue = combine(initialValue, element);
- }
- return initialValue;
- }
-
- /**
- * Removes elements matching [test] from [list].
- *
- * This is performed in two steps, to avoid exposing an inconsistent state
- * to the [test] function. First the elements to retain are found, and then
- * the original list is updated to contain those elements.
- */
- static void removeWhereList/*<E>*/(
- List/*<E>*/ list, bool test(var/*=E*/ element)) {
- List/*<E>*/ retained = [];
- int length = list.length;
- for (int i = 0; i < length; i++) {
- var element = list[i];
- if (!test(element)) {
- retained.add(element);
- }
- if (length != list.length) {
- throw new ConcurrentModificationError(list);
- }
- }
- if (retained.length == length) return;
- list.length = retained.length;
- for (int i = 0; i < retained.length; i++) {
- list[i] = retained[i];
- }
- }
-
- static bool isEmpty/*<E>*/(Iterable/*<E>*/ iterable) {
- return !iterable.iterator.moveNext();
- }
-
- static dynamic/*=E*/ first/*<E>*/(Iterable/*<E>*/ iterable) {
- Iterator/*<E>*/ it = iterable.iterator;
- if (!it.moveNext()) {
- throw IterableElementError.noElement();
- }
- return it.current;
- }
-
- static dynamic/*=E*/ last/*<E>*/(Iterable/*<E>*/ iterable) {
- Iterator/*<E>*/ it = iterable.iterator;
- if (!it.moveNext()) {
- throw IterableElementError.noElement();
- }
- var/*=E*/ result;
- do {
- result = it.current;
- } while (it.moveNext());
- return result;
- }
-
- static dynamic/*=E*/ single/*<E>*/(Iterable/*<E>*/ iterable) {
- Iterator/*<E>*/ it = iterable.iterator;
- if (!it.moveNext()) throw IterableElementError.noElement();
- var result = it.current;
- if (it.moveNext()) throw IterableElementError.tooMany();
- return result;
- }
-
- static dynamic/*=E*/ firstWhere/*<E>*/(Iterable/*<E>*/ iterable,
- bool test(dynamic/*=E*/ value), dynamic/*=E*/ orElse()) {
- for (var element in iterable) {
- if (test(element)) return element;
- }
- if (orElse != null) return orElse();
- throw IterableElementError.noElement();
- }
-
- static dynamic/*=E*/ lastWhere/*<E>*/(Iterable/*<E>*/ iterable,
- bool test(dynamic/*=E*/ value), dynamic/*=E*/ orElse()) {
- dynamic/*=E*/ result = null;
- bool foundMatching = false;
- for (var element in iterable) {
- if (test(element)) {
- result = element;
- foundMatching = true;
- }
- }
- if (foundMatching) return result;
- if (orElse != null) return orElse();
- throw IterableElementError.noElement();
- }
-
- static dynamic/*=E*/ lastWhereList/*<E>*/(List/*<E>*/ list,
- bool test(dynamic/*=E*/ value), dynamic/*=E*/ orElse()) {
- // TODO(floitsch): check that arguments are of correct type?
- for (int i = list.length - 1; i >= 0; i--) {
- var element = list[i];
- if (test(element)) return element;
- }
- if (orElse != null) return orElse();
- throw IterableElementError.noElement();
- }
-
- static dynamic/*=E*/ singleWhere/*<E>*/(
- Iterable/*<E>*/ iterable, bool test(dynamic/*=E*/ value)) {
- dynamic/*=E*/ result = null;
- bool foundMatching = false;
- for (var element in iterable) {
- if (test(element)) {
- if (foundMatching) {
- throw IterableElementError.tooMany();
- }
- result = element;
- foundMatching = true;
- }
- }
- if (foundMatching) return result;
- throw IterableElementError.noElement();
- }
-
- static dynamic/*=E*/ elementAt/*<E>*/(Iterable/*<E>*/ iterable, int index) {
- if (index is! int) throw new ArgumentError.notNull("index");
- RangeError.checkNotNegative(index, "index");
- int elementIndex = 0;
- for (var element in iterable) {
- if (index == elementIndex) return element;
- elementIndex++;
- }
- throw new RangeError.index(index, iterable, "index", null, elementIndex);
- }
-
- static String join/*<E>*/(Iterable/*<E>*/ iterable, [String separator]) {
- StringBuffer buffer = new StringBuffer();
- buffer.writeAll(iterable, separator);
- return buffer.toString();
- }
-
- static String joinList/*<E>*/(List/*<E>*/ list, [String separator]) {
- if (list.isEmpty) return "";
- if (list.length == 1) return "${list[0]}";
- StringBuffer buffer = new StringBuffer();
- if (separator.isEmpty) {
- for (int i = 0; i < list.length; i++) {
- buffer.write(list[i]);
- }
- } else {
- buffer.write(list[0]);
- for (int i = 1; i < list.length; i++) {
- buffer.write(separator);
- buffer.write(list[i]);
- }
- }
- return buffer.toString();
- }
-
- Iterable<T> where(Iterable<T> iterable, bool f(T element)) {
- return new WhereIterable<T>(iterable, f);
- }
-
- static Iterable/*<V>*/ map/*<E, V>*/(
- Iterable/*<E>*/ iterable, /*=V*/ f(var/*=E*/ element)) {
- return new MappedIterable/*<E, V>*/(iterable, f);
- }
-
- static Iterable/*<V>*/ mapList/*<E, V>*/(
- List/*<E>*/ list, /*=V*/ f(var/*=E*/ element)) {
- return new MappedListIterable/*<E, V>*/(list, f);
- }
-
- static Iterable/*<V>*/ expand/*<E, V>*/(
- Iterable/*<E>*/ iterable, Iterable/*<V>*/ f(var/*=E*/ element)) {
- return new ExpandIterable/*<E, V>*/(iterable, f);
- }
-
- Iterable<T> takeList(List list, int n) {
- // The generic type is currently lost. It will be fixed with mixins.
- return new SubListIterable<T>(list, 0, n);
- }
-
- Iterable<T> takeWhile(Iterable iterable, bool test(var value)) {
- // The generic type is currently lost. It will be fixed with mixins.
- return new TakeWhileIterable<T>(iterable, test);
- }
-
- Iterable<T> skipList(List list, int n) {
- // The generic type is currently lost. It will be fixed with mixins.
- return new SubListIterable<T>(list, n, null);
- }
-
- Iterable<T> skipWhile(Iterable iterable, bool test(var value)) {
- // The generic type is currently lost. It will be fixed with mixins.
- return new SkipWhileIterable<T>(iterable, test);
- }
-
- Iterable<T> reversedList(List list) {
- return new ReversedListIterable<T>(list);
- }
-
- static void sortList(List list, int compare(a, b)) {
- if (compare == null) compare = Comparable.compare;
- Sort.sort(list, compare);
- }
-
- static void shuffleList(List list, Random random) {
- if (random == null) random = new Random();
- int length = list.length;
- while (length > 1) {
- int pos = random.nextInt(length);
- length -= 1;
- var tmp = list[length];
- list[length] = list[pos];
- list[pos] = tmp;
- }
- }
-
- static int indexOfList(List list, var element, int start) {
- return Lists.indexOf(list, element, start, list.length);
- }
-
- static int lastIndexOfList(List list, var element, int start) {
- if (start == null) start = list.length - 1;
- return Lists.lastIndexOf(list, element, start);
- }
-
- static void _rangeCheck(List list, int start, int end) {
- RangeError.checkValidRange(start, end, list.length);
- }
-
- Iterable<T> getRangeList(List list, int start, int end) {
- _rangeCheck(list, start, end);
- // The generic type is currently lost. It will be fixed with mixins.
- return new SubListIterable<T>(list, start, end);
- }
-
- static void setRangeList(
- List list, int start, int end, Iterable from, int skipCount) {
- _rangeCheck(list, start, end);
- int length = end - start;
- if (length == 0) return;
-
- if (skipCount < 0) throw new ArgumentError(skipCount);
-
- // TODO(floitsch): Make this accept more.
- List otherList;
- int otherStart;
- if (from is List) {
- otherList = from;
- otherStart = skipCount;
- } else {
- otherList = from.skip(skipCount).toList(growable: false);
- otherStart = 0;
- }
- if (otherStart + length > otherList.length) {
- throw IterableElementError.tooFew();
- }
- Lists.copy(otherList, otherStart, list, start, length);
- }
-
- static void replaceRangeList(
- List list, int start, int end, Iterable iterable) {
- _rangeCheck(list, start, end);
- if (iterable is! EfficientLength) {
- iterable = iterable.toList();
- }
- int removeLength = end - start;
- int insertLength = iterable.length;
- if (removeLength >= insertLength) {
- int delta = removeLength - insertLength;
- int insertEnd = start + insertLength;
- int newEnd = list.length - delta;
- list.setRange(start, insertEnd, iterable);
- if (delta != 0) {
- list.setRange(insertEnd, newEnd, list, end);
- list.length = newEnd;
- }
- } else {
- int delta = insertLength - removeLength;
- int newLength = list.length + delta;
- int insertEnd = start + insertLength; // aka. end + delta.
- list.length = newLength;
- list.setRange(insertEnd, newLength, list, end);
- list.setRange(start, insertEnd, iterable);
- }
- }
-
- static void fillRangeList(List list, int start, int end, fillValue) {
- _rangeCheck(list, start, end);
- for (int i = start; i < end; i++) {
- list[i] = fillValue;
- }
- }
-
- static void insertAllList(List list, int index, Iterable iterable) {
- RangeError.checkValueInInterval(index, 0, list.length, "index");
- if (iterable is! EfficientLength) {
- iterable = iterable.toList(growable: false);
- }
- int insertionLength = iterable.length;
- list.length += insertionLength;
- list.setRange(index + insertionLength, list.length, list, index);
- for (var element in iterable) {
- list[index++] = element;
- }
- }
-
- static void setAllList(List list, int index, Iterable iterable) {
- RangeError.checkValueInInterval(index, 0, list.length, "index");
- for (var element in iterable) {
- list[index++] = element;
- }
- }
-
- Map<int, T> asMapList(List l) {
- return new ListMapView<T>(l);
- }
-
- static bool setContainsAll(Set set, Iterable other) {
- for (var element in other) {
- if (!set.contains(element)) return false;
- }
- return true;
- }
-
- static Set setIntersection(Set set, Set other, Set result) {
- Set smaller;
- Set larger;
- if (set.length < other.length) {
- smaller = set;
- larger = other;
- } else {
- smaller = other;
- larger = set;
- }
- for (var element in smaller) {
- if (larger.contains(element)) {
- result.add(element);
- }
- }
- return result;
- }
-
- static Set setUnion(Set set, Set other, Set result) {
- result.addAll(set);
- result.addAll(other);
- return result;
- }
-
- static Set setDifference(Set set, Set other, Set result) {
- for (var element in set) {
- if (!other.contains(element)) {
- result.add(element);
- }
- }
- return result;
- }
-}
-
-/**
* Creates errors throw by [Iterable] when the element count is wrong.
*/
abstract class IterableElementError {
« no previous file with comments | « tool/input_sdk/lib/collection/list.dart ('k') | tool/input_sdk/lib/internal/list.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698