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

Unified Diff: sdk/lib/collection/list.dart

Issue 26681002: Add EfficientLength marker interface to some iterabels. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 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
Index: sdk/lib/collection/list.dart
diff --git a/sdk/lib/collection/list.dart b/sdk/lib/collection/list.dart
index eed6b32cefb5966d4803b52e881d8b85e1401697..4f3b9b208d56b0cbeaa002d744a087f8abf2bfd1 100644
--- a/sdk/lib/collection/list.dart
+++ b/sdk/lib/collection/list.dart
@@ -389,9 +389,29 @@ 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);
+ _rangeCheck(start, end);
+ if (newContents is! EfficientLength) {
+ newContents = newContents.toList();
+ }
+ 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;
+ }
+ } else {
+ int delta = insertLength - removeLength;
+ int newLength = this.length + delta;
+ int insertEnd = start + insertLength; // aka. end + delta.
floitsch 2013/10/10 13:04:53 No need for comment (imho).
+ this.length = newLength;
+ this.setRange(insertEnd, newLength, this, end);
+ this.setRange(start, insertEnd, newContents);
+ }
}
int indexOf(Object element, [int startIndex = 0]) {
@@ -461,8 +481,7 @@ abstract class ListMixin<E> implements List<E> {
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) {
+ if (iterable is EfficientLength) {
iterable = iterable.toList();
}
int insertionLength = iterable.length;

Powered by Google App Engine
This is Rietveld 408576698