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

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

Issue 1999793002: Make Iterable.toList more efficient if the length is known. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 7 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
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 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 437
438 /** 438 /**
439 * List based [Queue]. 439 * List based [Queue].
440 * 440 *
441 * Keeps a cyclic buffer of elements, and grows to a larger buffer when 441 * Keeps a cyclic buffer of elements, and grows to a larger buffer when
442 * it fills up. This guarantees constant time peek and remove operations, and 442 * it fills up. This guarantees constant time peek and remove operations, and
443 * amortized constant time add operations. 443 * amortized constant time add operations.
444 * 444 *
445 * The structure is efficient for any queue or stack usage. 445 * The structure is efficient for any queue or stack usage.
446 */ 446 */
447 class ListQueue<E> extends Iterable<E> implements Queue<E> { 447 class ListQueue<E> extends ListIterable<E> implements Queue<E> {
448 static const int _INITIAL_CAPACITY = 8; 448 static const int _INITIAL_CAPACITY = 8;
449 List<E> _table; 449 List<E> _table;
450 int _head; 450 int _head;
451 int _tail; 451 int _tail;
452 int _modificationCount = 0; 452 int _modificationCount = 0;
453 453
454 /** 454 /**
455 * Create an empty queue. 455 * Create an empty queue.
456 * 456 *
457 * If [initialCapacity] is given, prepare the queue for at least that many 457 * If [initialCapacity] is given, prepare the queue for at least that many
(...skipping 13 matching lines...) Expand all
471 * Create a `ListQueue` containing all [elements]. 471 * Create a `ListQueue` containing all [elements].
472 * 472 *
473 * The elements are added to the queue, as by [addLast], in the order given by 473 * The elements are added to the queue, as by [addLast], in the order given by
474 * `elements.iterator`. 474 * `elements.iterator`.
475 * 475 *
476 * All `elements` should be assignable to [E]. 476 * All `elements` should be assignable to [E].
477 */ 477 */
478 factory ListQueue.from(Iterable elements) { 478 factory ListQueue.from(Iterable elements) {
479 if (elements is List) { 479 if (elements is List) {
480 int length = elements.length; 480 int length = elements.length;
481 ListQueue<E> queue = new ListQueue(length + 1); 481 ListQueue<E> queue = new ListQueue<E>(length + 1);
482 assert(queue._table.length > length); 482 assert(queue._table.length > length);
483 for (int i = 0; i < length; i++) { 483 for (int i = 0; i < length; i++) {
484 queue._table[i] = elements[i] as Object/*=E*/; 484 queue._table[i] = elements[i] as Object/*=E*/;
485 } 485 }
486 queue._tail = length; 486 queue._tail = length;
487 return queue; 487 return queue;
488 } else { 488 } else {
489 int capacity = _INITIAL_CAPACITY; 489 int capacity = _INITIAL_CAPACITY;
490 if (elements is EfficientLength) { 490 if (elements is EfficientLength) {
491 capacity = elements.length; 491 capacity = elements.length;
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
815 _queue._checkModification(_modificationCount); 815 _queue._checkModification(_modificationCount);
816 if (_position == _end) { 816 if (_position == _end) {
817 _current = null; 817 _current = null;
818 return false; 818 return false;
819 } 819 }
820 _current = _queue._table[_position]; 820 _current = _queue._table[_position];
821 _position = (_position + 1) & (_queue._table.length - 1); 821 _position = (_position + 1) & (_queue._table.length - 1);
822 return true; 822 return true;
823 } 823 }
824 } 824 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698