Chromium Code Reviews| Index: runtime/lib/typed_data.dart |
| =================================================================== |
| --- runtime/lib/typed_data.dart (revision 43243) |
| +++ runtime/lib/typed_data.dart (working copy) |
| @@ -241,58 +241,115 @@ |
| // Based class for _TypedList that provides common methods for implementing |
| // the collection and list interfaces. |
| -// TODO(13647): Make this extends ListBase<T> |
| +// This class does not extend ListBase<T> since that would add type arguments |
| +// to instances of _TypeListBase. Instead the subclasses use number-type specific |
|
Ivan Posva
2015/01/29 15:03:18
Long line.
Florian Schneider
2015/02/03 08:18:19
Done.
|
| +// mixins (like _IntListMixin, _DoubleListMixin) to implement ListBase<T>. |
| abstract class _TypedListBase { |
| // Method(s) implementing the Collection interface. |
| - bool contains(element) => IterableMixinWorkaround.contains(this, element); |
| + bool contains(element) { |
| + int len = this.length; |
| + for (int i = 0; i < len; ++i) { |
| + if (this[i] == element) return true; |
| + } |
| + return false; |
| + } |
| void forEach(void f(element)) { |
| - var len = this.length; |
| - for (var i = 0; i < len; i++) { |
| + int len = this.length; |
| + for (int i = 0; i < len; i++) { |
|
Ivan Posva
2015/01/29 15:03:18
Any reason for this change? Generally we tend to u
Florian Schneider
2015/02/03 08:18:19
Done.
|
| f(this[i]); |
| } |
| } |
| String join([String separator = ""]) { |
| - return IterableMixinWorkaround.join(this, separator); |
| + StringBuffer buffer = new StringBuffer(); |
| + buffer.writeAll(this, separator); |
| + return buffer.toString(); |
| } |
| dynamic reduce(dynamic combine(value, element)) { |
| - return IterableMixinWorkaround.reduce(this, combine); |
| + int len = this.length; |
| + if (len == 0) throw IterableElementError.noElement(); |
| + int i = 0; |
|
srdjan
2015/01/29 17:29:40
Remove int i = 0;
Florian Schneider
2015/02/03 08:18:19
Done.
|
| + var value = this[0]; |
| + for (var i = 1; i < len; ++i) { |
| + value = combine(value, this[i]); |
| + } |
| + return value; |
| } |
| dynamic fold(dynamic initialValue, |
| dynamic combine(dynamic initialValue, element)) { |
| - return IterableMixinWorkaround.fold(this, initialValue, combine); |
| + int len = this.length; |
| + for (int i = 0; i < len; ++i) { |
| + initialValue = combine(initialValue, this[i]); |
| + } |
| + return initialValue; |
| } |
| - Iterable map(f(element)) { |
| - return IterableMixinWorkaround.mapList(this, f); |
| - } |
| + Iterable map(f(element)) => new MappedIterable(this, f); |
| - Iterable expand(Iterable f(element)) { |
| - return IterableMixinWorkaround.expand(this, f); |
| - } |
| + Iterable expand(Iterable f(element)) => new ExpandIterable(this, f); |
| bool every(bool f(element)) { |
| - return IterableMixinWorkaround.every(this, f); |
| + int len = this.length; |
| + for (int i = 0; i < len; ++i) { |
| + if (!f(this[i])) return false; |
| + } |
| + return true; |
| } |
| bool any(bool f(element)) { |
| - return IterableMixinWorkaround.any(this, f); |
| + int len = this.length; |
| + for (int i = 0; i < len; ++i) { |
| + if (f(this[i])) return true; |
| + } |
| + return false; |
| } |
| dynamic firstWhere(bool test(element), {orElse()}) { |
| - return IterableMixinWorkaround.firstWhere(this, test, orElse); |
| + int len = this.length; |
| + for (int i = 0; i < len; ++i) { |
| + var element = this[i]; |
| + if (test(element)) return element; |
| + } |
| + if (orElse != null) return orElse(); |
| + throw IterableElementError.noElement(); |
| } |
| dynamic lastWhere(bool test(element), {orElse()}) { |
| - return IterableMixinWorkaround.lastWhereList(this, test, orElse); |
| + dynamic result = null; |
|
srdjan
2015/01/29 17:29:39
var result = null;
Florian Schneider
2015/02/03 08:18:19
Done.
|
| + bool foundMatching = false; |
| + int len = this.length; |
| + for (int i = 0; i < len; ++i) { |
|
srdjan
2015/01/29 17:29:39
Why not going backward len - 1 ... 0 and return fi
Florian Schneider
2015/02/03 08:18:19
Done.
|
| + dynamic element = this[i]; |
|
srdjan
2015/01/29 17:29:40
var element =
Florian Schneider
2015/02/03 08:18:19
Done.
|
| + if (test(element)) { |
| + result = element; |
| + foundMatching = true; |
| + } |
| + } |
| + if (foundMatching) return result; |
| + if (orElse != null) return orElse(); |
| + throw IterableElementError.noElement(); |
| } |
| dynamic singleWhere(bool test(element)) { |
| - return IterableMixinWorkaround.singleWhere(this, test); |
| + dynamic result = null; |
|
srdjan
2015/01/29 17:29:40
var result =
Florian Schneider
2015/02/03 08:18:19
Done.
|
| + bool foundMatching = false; |
| + int len = this.length; |
| + for (int i = 0; i < len; ++i) { |
| + dynamic element = this[i]; |
| + if (test(element)) { |
| + if (foundMatching) { |
| + throw IterableElementError.tooMany(); |
| + } |
| + result = element; |
| + foundMatching = true; |
| + } |
| + } |
| + if (foundMatching) return result; |
| + throw IterableElementError.noElement(); |
| } |
| dynamic elementAt(int index) { |
| @@ -333,19 +390,29 @@ |
| } |
| void sort([int compare(a, b)]) { |
| - IterableMixinWorkaround.sortList(this, compare); |
| + if (compare == null) compare = Comparable.compare; |
| + Sort.sort(this, compare); |
| } |
| void shuffle([Random random]) { |
| - IterableMixinWorkaround.shuffleList(this, random); |
| + if (random == null) random = new Random(); |
| + int i = this.length; |
| + while (i > 1) { |
| + int pos = random.nextInt(i); |
| + i -= 1; |
| + var tmp = this[i]; |
| + this[i] = this[pos]; |
| + this[pos] = tmp; |
| + } |
| } |
| int indexOf(element, [int start = 0]) { |
| - return IterableMixinWorkaround.indexOfList(this, element, start); |
| + return Lists.indexOf(this, element, start, this.length); |
| } |
| int lastIndexOf(element, [int start = null]) { |
| - return IterableMixinWorkaround.lastIndexOfList(this, element, start); |
| + if (start == null) start = this.length - 1; |
| + return Lists.lastIndexOf(this, element, start); |
| } |
| void clear() { |
| @@ -380,18 +447,18 @@ |
| dynamic get first { |
| if (length > 0) return this[0]; |
| - throw new StateError("No elements"); |
| + throw IterableElementError.noElement(); |
| } |
| dynamic get last { |
| if (length > 0) return this[length - 1]; |
| - throw new StateError("No elements"); |
| + throw IterableElementError.noElement(); |
| } |
| dynamic get single { |
| if (length == 1) return this[0]; |
| - if (length == 0) throw new StateError("No elements"); |
| - throw new StateError("More than one element"); |
| + if (length == 0) throw IterableElementError.noElement(); |
| + throw IterableElementError.tooMany(); |
| } |
| void removeRange(int start, int end) { |
| @@ -438,7 +505,7 @@ |
| final count = end - start; |
| if ((from.length - skipCount) < count) { |
| - throw new StateError("Not enough elements"); |
| + throw IterableElementError.tooFew(); |
| } |
| if (from is _TypedListBase) { |
| @@ -468,8 +535,21 @@ |
| return; |
| } |
| } |
| - IterableMixinWorkaround.setRangeList(this, start, |
| - end, from, skipCount); |
| + |
| + if (count == 0) return; |
| + List otherList; |
| + int otherStart; |
| + if (from is List) { |
| + otherList = from; |
| + otherStart = skipCount; |
| + } else { |
| + otherList = from.skip(skipCount).toList(growable: false); |
| + otherStart = 0; |
| + } |
| + if (otherStart + count > otherList.length) { |
| + throw IterableElementError.tooFew(); |
| + } |
| + Lists.copy(otherList, otherStart, this, start, count); |
| } |
| void setAll(int index, Iterable iterable) { |
| @@ -478,7 +558,10 @@ |
| } |
| void fillRange(int start, int end, [fillValue]) { |
| - IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); |
| + RangeError.checkValidRange(start, end, this.length); |
| + for (int i = start; i < end; ++i) { |
| + this[i] = fillValue; |
| + } |
| } |