Chromium Code Reviews| Index: sdk/lib/collection/list.dart |
| diff --git a/sdk/lib/collection/list.dart b/sdk/lib/collection/list.dart |
| index eb6a0be525a5b2e76d80b63da18636a0b942f579..f9f6617c23d08c3860491feba765100799aa98c3 100644 |
| --- a/sdk/lib/collection/list.dart |
| +++ b/sdk/lib/collection/list.dart |
| @@ -174,6 +174,9 @@ abstract class ListMixin<E> implements List<E> { |
| Iterable map(f(E element)) => new MappedListIterable(this, f); |
| + Iterable expand(Iterable f(E element)) => |
|
floitsch
2013/04/15 16:22:48
Extracted into a separate CL.
I need to commit bef
|
| + new ExpandIterable<E, dynamic>(this, f); |
| + |
| E reduce(E combine(E previousValue, E element)) { |
| if (length == 0) throw new StateError("No elements"); |
| E value = this[0]; |
| @@ -298,14 +301,18 @@ abstract class ListMixin<E> implements List<E> { |
| return new ListMapView(this); |
| } |
| - List<E> sublist(int start, [int end]) { |
| - if (end == null) end = length; |
| + void _rangeCheck(int start, int end) { |
| if (start < 0 || start > this.length) { |
| throw new RangeError.range(start, 0, this.length); |
| } |
| if (end < start || end > this.length) { |
| throw new RangeError.range(end, start, this.length); |
| } |
| + } |
| + |
| + List<E> sublist(int start, [int end]) { |
| + if (end == null) end = length; |
| + _rangeCheck(start, end); |
| int length = end - start; |
| List<E> result = new List<E>()..length = length; |
| for (int i = 0; i < length; i++) { |
| @@ -315,40 +322,26 @@ abstract class ListMixin<E> implements List<E> { |
| } |
| Iterable<E> getRange(int start, int end) { |
| - if (start < 0 || start > this.length) { |
| - throw new RangeError.range(start, 0, this.length); |
| - } |
| - if (end < start || end > this.length) { |
| - throw new RangeError.range(end, start, this.length); |
| - } |
| + _rangeCheck(start, end); |
| return new SubListIterable(this, start, end); |
| } |
| void removeRange(int start, int end) { |
| - if (start < 0 || start > this.length) { |
| - throw new RangeError.range(start, 0, this.length); |
| - } |
| - if (end < start || end > this.length) { |
| - throw new RangeError.range(end, start, this.length); |
| - } |
| + _rangeCheck(start, end); |
| int length = end - start; |
| setRange(start, this.length - length, this, end); |
| this.length -= length; |
| } |
| - void clearRange(int start, int length, [E fill]) { |
| - for (int i = 0; i < length; i++) { |
| - this[start + i] = fill; |
| + void fillRange(int start, int end, [E fill]) { |
| + _rangeCheck(start, end); |
| + for (int i = start; i < end; i++) { |
| + this[i] = fill; |
| } |
| } |
| void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { |
| - if (start < 0 || start > this.length) { |
| - throw new RangeError.range(start, 0, this.length); |
| - } |
| - if (end < 0 || end > this.length) { |
| - throw new RangeError.range(end, start, this.length); |
| - } |
| + _rangeCheck(start, end); |
| int length = end - start; |
| if (length == 0) return; |
| @@ -379,6 +372,12 @@ abstract class ListMixin<E> implements List<E> { |
| } |
| } |
| + void replaceRange(int start, int end, Iterable<E> newContents) { |
| + // TODO(floitsch): Optimize this. |
| + removeRange(start, end); |
| + insertAll(start, newContents); |
| + } |
| + |
| int indexOf(E element, [int startIndex = 0]) { |
| if (startIndex >= this.length) { |
| return -1; |
| @@ -418,5 +417,56 @@ abstract class ListMixin<E> implements List<E> { |
| return -1; |
| } |
| + void insert(int index, E element) { |
|
floitsch
2013/04/15 16:22:48
Extracted into a separate CL.
I need to commit bef
|
| + if (index < 0 || index > length) { |
| + throw new RangeError.range(index, 0, length); |
| + } |
| + if (index == this.length) { |
| + add(element); |
| + return; |
| + } |
| + // We are modifying the length just below the is-check. Without the check |
| + // Array.copy could throw an exception, leaving the list in a bad state |
| + // (with a length that has been increased, but without a new element). |
| + if (index is! int) throw new ArgumentError(index); |
| + this.length++; |
| + setRange(index + 1, this.length, this, index); |
| + this[index] = element; |
| + } |
| + |
| + E removeAt(int index) { |
|
floitsch
2013/04/15 16:22:48
Extracted into a separate CL.
I need to commit bef
|
| + E result = this[index]; |
| + setRange(index, this.length - 1, this, index + 1); |
| + length--; |
| + return result; |
| + } |
| + |
| + void insertAll(int index, Iterable<E> iterable) { |
| + if (index < 0 || index > length) { |
| + throw new RangeError.range(index, 0, length); |
| + } |
| + // TODO(floitsch): we can probably detect more cases. |
| + if (iterable is! List && iterable is! Set && iterable is! SubListIterable) { |
| + iterable = iterable.toList(); |
| + } |
| + int insertionLength = iterable.length; |
| + // There might be errors after the length change, in which case the list |
| + // will end up being modified but the operation not complete. Unless we |
| + // always go through a "toList" we can't really avoid that. |
| + this.length += insertionLength; |
| + setRange(index + insertionLength, this.length, this, index); |
| + setAll(index, iterable); |
| + } |
| + |
| + void setAll(int index, Iterable<E> iterable) { |
| + if (iterable is List) { |
| + setRange(index, index + iterable.length, iterable); |
| + } else { |
| + for (E element in iterable) { |
| + this[index++] = element; |
| + } |
| + } |
| + } |
| + |
| Iterable<E> get reversed => new ReversedListIterable(this); |
| } |