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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.collection; 5 part of dart.collection;
6 6
7 /** 7 /**
8 * Abstract implementation of a list. 8 * Abstract implementation of a list.
9 * 9 *
10 * All operations are defined in terms of `length`, `operator[]`, 10 * All operations are defined in terms of `length`, `operator[]`,
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 this[start + i] = otherList[otherStart + i]; 382 this[start + i] = otherList[otherStart + i];
383 } 383 }
384 } else { 384 } else {
385 for (int i = 0; i < length; i++) { 385 for (int i = 0; i < length; i++) {
386 this[start + i] = otherList[otherStart + i]; 386 this[start + i] = otherList[otherStart + i];
387 } 387 }
388 } 388 }
389 } 389 }
390 390
391 void replaceRange(int start, int end, Iterable<E> newContents) { 391 void replaceRange(int start, int end, Iterable<E> newContents) {
392 // TODO(floitsch): Optimize this. 392 _rangeCheck(start, end);
393 removeRange(start, end); 393 if (newContents is! EfficientLength) {
394 insertAll(start, newContents); 394 newContents = newContents.toList();
395 }
396 int removeLength = end - start;
397 int insertLength = newContents.length;
398 if (removeLength >= insertLength) {
399 int delta = removeLength - insertLength;
400 int insertEnd = start + insertLength;
401 int newLength = this.length - delta;
402 this.setRange(start, insertEnd, newContents);
403 if (delta != 0) {
404 this.setRange(insertEnd, newLength, this, end);
405 this.length = newLength;
406 }
407 } else {
408 int delta = insertLength - removeLength;
409 int newLength = this.length + delta;
410 int insertEnd = start + insertLength; // aka. end + delta.
floitsch 2013/10/10 13:04:53 No need for comment (imho).
411 this.length = newLength;
412 this.setRange(insertEnd, newLength, this, end);
413 this.setRange(start, insertEnd, newContents);
414 }
395 } 415 }
396 416
397 int indexOf(Object element, [int startIndex = 0]) { 417 int indexOf(Object element, [int startIndex = 0]) {
398 if (startIndex >= this.length) { 418 if (startIndex >= this.length) {
399 return -1; 419 return -1;
400 } 420 }
401 if (startIndex < 0) { 421 if (startIndex < 0) {
402 startIndex = 0; 422 startIndex = 0;
403 } 423 }
404 for (int i = startIndex; i < this.length; i++) { 424 for (int i = startIndex; i < this.length; i++) {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 E result = this[index]; 474 E result = this[index];
455 setRange(index, this.length - 1, this, index + 1); 475 setRange(index, this.length - 1, this, index + 1);
456 length--; 476 length--;
457 return result; 477 return result;
458 } 478 }
459 479
460 void insertAll(int index, Iterable<E> iterable) { 480 void insertAll(int index, Iterable<E> iterable) {
461 if (index < 0 || index > length) { 481 if (index < 0 || index > length) {
462 throw new RangeError.range(index, 0, length); 482 throw new RangeError.range(index, 0, length);
463 } 483 }
464 // TODO(floitsch): we can probably detect more cases. 484 if (iterable is EfficientLength) {
465 if (iterable is! List && iterable is! Set && iterable is! SubListIterable) {
466 iterable = iterable.toList(); 485 iterable = iterable.toList();
467 } 486 }
468 int insertionLength = iterable.length; 487 int insertionLength = iterable.length;
469 // There might be errors after the length change, in which case the list 488 // There might be errors after the length change, in which case the list
470 // will end up being modified but the operation not complete. Unless we 489 // will end up being modified but the operation not complete. Unless we
471 // always go through a "toList" we can't really avoid that. 490 // always go through a "toList" we can't really avoid that.
472 this.length += insertionLength; 491 this.length += insertionLength;
473 setRange(index + insertionLength, this.length, this, index); 492 setRange(index + insertionLength, this.length, this, index);
474 setAll(index, iterable); 493 setAll(index, iterable);
475 } 494 }
(...skipping 22 matching lines...) Expand all
498 result.writeAll(this, ', '); 517 result.writeAll(this, ', ');
499 result.write(']'); 518 result.write(']');
500 } finally { 519 } finally {
501 assert(identical(_toStringList.last, this)); 520 assert(identical(_toStringList.last, this));
502 _toStringList.removeLast(); 521 _toStringList.removeLast();
503 } 522 }
504 523
505 return result.toString(); 524 return result.toString();
506 } 525 }
507 } 526 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698