Skip to content

Commit

Permalink
special-case ListMixin.setRange from same list
Browse files Browse the repository at this point in the history
BUG=
R=lrn@google.com

Review-Url: https://codereview.chromium.org/2875013005 .
  • Loading branch information
rakudrama committed Jun 26, 2017
1 parent 2f7c6d6 commit 001ce2a
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions sdk/lib/collection/list.dart
Expand Up @@ -256,14 +256,27 @@ abstract class ListMixin<E> implements List<E> {
bool remove(Object element) {
for (int i = 0; i < this.length; i++) {
if (this[i] == element) {
this.setRange(i, this.length - 1, this, i + 1);
this.length -= 1;
this._closeGap(i, i + 1);
return true;
}
}
return false;
}

/// Removes elements from the list starting at [start] up to but not including
/// [end]. Arguments are pre-validated.
void _closeGap(int start, int end) {
int length = this.length;
assert(0 <= start);
assert(start < end);
assert(end <= length);
int size = end - start;
for (int i = end; i < length; i++) {
this[i - size] = this[i];
}
this.length = length - size;
}

void removeWhere(bool test(E element)) {
_filter(test, false);
}
Expand Down Expand Up @@ -350,9 +363,9 @@ abstract class ListMixin<E> implements List<E> {

void removeRange(int start, int end) {
RangeError.checkValidRange(start, end, this.length);
int length = end - start;
setRange(start, this.length - length, this, end);
this.length -= length;
if (end > start) {
_closeGap(start, end);
}
}

void fillRange(int start, int end, [E fill]) {
Expand Down Expand Up @@ -401,13 +414,10 @@ abstract class ListMixin<E> implements List<E> {
int removeLength = end - start;
int insertLength = newContents.length;
if (removeLength >= insertLength) {
int delta = removeLength - insertLength;
int insertEnd = start + insertLength;
int newLength = this.length - delta;
this.setRange(start, insertEnd, newContents);
if (delta != 0) {
this.setRange(insertEnd, newLength, this, end);
this.length = newLength;
if (removeLength > insertLength) {
_closeGap(insertEnd, end);
}
} else {
int delta = insertLength - removeLength;
Expand Down Expand Up @@ -470,8 +480,7 @@ abstract class ListMixin<E> implements List<E> {

E removeAt(int index) {
E result = this[index];
setRange(index, this.length - 1, this, index + 1);
length--;
_closeGap(index, index + 1);
return result;
}

Expand Down

0 comments on commit 001ce2a

Please sign in to comment.