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

Side by Side Diff: sdk/lib/collection/queue.dart

Issue 13863012: Refactor List.setRange function. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed comments. Created 7 years, 8 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
« no previous file with comments | « sdk/lib/collection/list.dart ('k') | sdk/lib/core/list.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 * A [Queue] is a collection that can be manipulated at both ends. One 8 * A [Queue] is a collection that can be manipulated at both ends. One
9 * can iterate over the elements of a queue through [forEach] or with 9 * can iterate over the elements of a queue through [forEach] or with
10 * an [Iterator]. 10 * an [Iterator].
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 } 451 }
452 452
453 void addAll(Iterable<E> elements) { 453 void addAll(Iterable<E> elements) {
454 if (elements is List) { 454 if (elements is List) {
455 List list = elements; 455 List list = elements;
456 int addCount = list.length; 456 int addCount = list.length;
457 int length = this.length; 457 int length = this.length;
458 if (length + addCount >= _table.length) { 458 if (length + addCount >= _table.length) {
459 _preGrow(length + addCount); 459 _preGrow(length + addCount);
460 // After preGrow, all elements are at the start of the list. 460 // After preGrow, all elements are at the start of the list.
461 _table.setRange(length, addCount, list, 0); 461 _table.setRange(length, length + addCount, list, 0);
462 _tail += addCount; 462 _tail += addCount;
463 } else { 463 } else {
464 // Adding addCount elements won't reach _head. 464 // Adding addCount elements won't reach _head.
465 int endSpace = _table.length - _tail; 465 int endSpace = _table.length - _tail;
466 if (addCount < endSpace) { 466 if (addCount < endSpace) {
467 _table.setRange(_tail, addCount, list, 0); 467 _table.setRange(_tail, _tail + addCount, list, 0);
468 _tail += addCount; 468 _tail += addCount;
469 } else { 469 } else {
470 int preSpace = addCount - endSpace; 470 int preSpace = addCount - endSpace;
471 _table.setRange(_tail, endSpace, list, 0); 471 _table.setRange(_tail, _tail + endSpace, list, 0);
472 _table.setRange(0, preSpace, list, endSpace); 472 _table.setRange(0, preSpace, list, endSpace);
473 _tail = preSpace; 473 _tail = preSpace;
474 } 474 }
475 } 475 }
476 _modificationCount++; 476 _modificationCount++;
477 } else { 477 } else {
478 for (E element in elements) _add(element); 478 for (E element in elements) _add(element);
479 } 479 }
480 } 480 }
481 481
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 } 646 }
647 } 647 }
648 648
649 /** 649 /**
650 * Grow the table when full. 650 * Grow the table when full.
651 */ 651 */
652 void _grow() { 652 void _grow() {
653 List<E> newTable = new List<E>(_table.length * 2); 653 List<E> newTable = new List<E>(_table.length * 2);
654 int split = _table.length - _head; 654 int split = _table.length - _head;
655 newTable.setRange(0, split, _table, _head); 655 newTable.setRange(0, split, _table, _head);
656 newTable.setRange(split, _head, _table, 0); 656 newTable.setRange(split, split + _head, _table, 0);
657 _head = 0; 657 _head = 0;
658 _tail = _table.length; 658 _tail = _table.length;
659 _table = newTable; 659 _table = newTable;
660 } 660 }
661 661
662 int _writeToList(List<E> target) { 662 int _writeToList(List<E> target) {
663 assert(target.length >= length); 663 assert(target.length >= length);
664 if (_head <= _tail) { 664 if (_head <= _tail) {
665 int length = _tail - _head; 665 int length = _tail - _head;
666 target.setRange(0, length, _table, _head); 666 target.setRange(0, length, _table, _head);
667 return length; 667 return length;
668 } else { 668 } else {
669 int firstPartSize = _table.length - _head; 669 int firstPartSize = _table.length - _head;
670 target.setRange(0, firstPartSize, _table, _head); 670 target.setRange(0, firstPartSize, _table, _head);
671 target.setRange(firstPartSize, _tail, _table, 0); 671 target.setRange(firstPartSize, firstPartSize + _tail, _table, 0);
672 return _tail + firstPartSize; 672 return _tail + firstPartSize;
673 } 673 }
674 } 674 }
675 675
676 /** Grows the table even if it is not full. */ 676 /** Grows the table even if it is not full. */
677 void _preGrow(int newElementCount) { 677 void _preGrow(int newElementCount) {
678 assert(newElementCount >= length); 678 assert(newElementCount >= length);
679 int newCapacity = _nextPowerOf2(newElementCount); 679 int newCapacity = _nextPowerOf2(newElementCount);
680 List<E> newTable = new List<E>(newCapacity); 680 List<E> newTable = new List<E>(newCapacity);
681 _tail = _writeToList(newTable); 681 _tail = _writeToList(newTable);
(...skipping 26 matching lines...) Expand all
708 _queue._checkModification(_modificationCount); 708 _queue._checkModification(_modificationCount);
709 if (_position == _end) { 709 if (_position == _end) {
710 _current = null; 710 _current = null;
711 return false; 711 return false;
712 } 712 }
713 _current = _queue._table[_position]; 713 _current = _queue._table[_position];
714 _position = (_position + 1) & (_queue._table.length - 1); 714 _position = (_position + 1) & (_queue._table.length - 1);
715 return true; 715 return true;
716 } 716 }
717 } 717 }
OLDNEW
« no previous file with comments | « sdk/lib/collection/list.dart ('k') | sdk/lib/core/list.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698