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

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

Issue 838463002: Change List/Set/Map/Queue.from constructrs to accept any iterable. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 11 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) 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].
11 * 11 *
12 * It is generally not allowed to modify the queue (add or remove entries) while 12 * It is generally not allowed to modify the queue (add or remove entries) while
13 * an operation on the queue is being performed, for example during a call to 13 * an operation on the queue is being performed, for example during a call to
14 * [forEach]. 14 * [forEach].
15 * Modifying the queue while it is being iterated will most likely break the 15 * Modifying the queue while it is being iterated will most likely break the
16 * iteration. 16 * iteration.
17 * This goes both for using the [iterator] directly, or for iterating an 17 * This goes both for using the [iterator] directly, or for iterating an
18 * `Iterable` returned by a method like [map] or [where]. 18 * `Iterable` returned by a method like [map] or [where].
19 */ 19 */
20 abstract class Queue<E> implements Iterable<E>, EfficientLength { 20 abstract class Queue<E> implements Iterable<E>, EfficientLength {
21 21
22 /** 22 /**
23 * Creates a queue. 23 * Creates a queue.
24 */ 24 */
25 factory Queue() = ListQueue<E>; 25 factory Queue() = ListQueue<E>;
26 26
27 /** 27 /**
28 * Creates a queue with the elements of [other]. The order in 28 * Creates a queue containing all of [elements].
29 * the queue will be the order provided by the iterator of [other]. 29 *
30 * The element order in the queue is as if the elements were added using
31 * [addLast] in the order provided by [elements.iterator].
30 */ 32 */
31 factory Queue.from(Iterable<E> other) = ListQueue<E>.from; 33 factory Queue.from(Iterable elements) = ListQueue<E>.from;
32 34
33 /** 35 /**
34 * Removes and returns the first element of this queue. 36 * Removes and returns the first element of this queue.
35 * 37 *
36 * The queue must not be empty when this method is called. 38 * The queue must not be empty when this method is called.
37 */ 39 */
38 E removeFirst(); 40 E removeFirst();
39 41
40 /** 42 /**
41 * Removes and returns the last element of the queue. 43 * Removes and returns the last element of the queue.
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 * Allows constant time add, remove-at-ends and peek operations. 190 * Allows constant time add, remove-at-ends and peek operations.
189 */ 191 */
190 class DoubleLinkedQueue<E> extends IterableBase<E> implements Queue<E> { 192 class DoubleLinkedQueue<E> extends IterableBase<E> implements Queue<E> {
191 _DoubleLinkedQueueEntrySentinel<E> _sentinel; 193 _DoubleLinkedQueueEntrySentinel<E> _sentinel;
192 int _elementCount = 0; 194 int _elementCount = 0;
193 195
194 DoubleLinkedQueue() { 196 DoubleLinkedQueue() {
195 _sentinel = new _DoubleLinkedQueueEntrySentinel<E>(); 197 _sentinel = new _DoubleLinkedQueueEntrySentinel<E>();
196 } 198 }
197 199
198 factory DoubleLinkedQueue.from(Iterable<E> other) { 200 /**
201 * Creates a double-linked queue containing all of [elements].
202 *
203 * The element order in the queue is as if the elements were added using
204 * [addLast] in the order provided by [elements.iterator].
205 */
206 factory DoubleLinkedQueue.from(Iterable elements) {
199 Queue<E> list = new DoubleLinkedQueue(); 207 Queue<E> list = new DoubleLinkedQueue();
200 for (final e in other) { 208 for (final E e in elements) {
201 list.addLast(e); 209 list.addLast(e);
202 } 210 }
203 return list; 211 return list;
204 } 212 }
205 213
206 int get length => _elementCount; 214 int get length => _elementCount;
207 215
208 void addLast(E value) { 216 void addLast(E value) {
209 _sentinel.prepend(value); 217 _sentinel.prepend(value);
210 _elementCount++; 218 _elementCount++;
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 if (initialCapacity == null || initialCapacity < _INITIAL_CAPACITY) { 379 if (initialCapacity == null || initialCapacity < _INITIAL_CAPACITY) {
372 initialCapacity = _INITIAL_CAPACITY; 380 initialCapacity = _INITIAL_CAPACITY;
373 } else if (!_isPowerOf2(initialCapacity)) { 381 } else if (!_isPowerOf2(initialCapacity)) {
374 initialCapacity = _nextPowerOf2(initialCapacity); 382 initialCapacity = _nextPowerOf2(initialCapacity);
375 } 383 }
376 assert(_isPowerOf2(initialCapacity)); 384 assert(_isPowerOf2(initialCapacity));
377 _table = new List<E>(initialCapacity); 385 _table = new List<E>(initialCapacity);
378 } 386 }
379 387
380 /** 388 /**
381 * Create a queue initially containing the elements of [source]. 389 * Create a `ListQueue` containing all of [elements].
390 *
391 * The elements are added to the queue, as by [addLast], in the order given by
392 * `elements.iterator`.
393 *
394 * All `elements` should be assignable to [E].
382 */ 395 */
383 factory ListQueue.from(Iterable<E> source) { 396 factory ListQueue.from(Iterable elements) {
384 if (source is List) { 397 if (elements is List) {
385 int length = source.length; 398 int length = elements.length;
386 ListQueue<E> queue = new ListQueue(length + 1); 399 ListQueue<E> queue = new ListQueue(length + 1);
387 assert(queue._table.length > length); 400 assert(queue._table.length > length);
388 List sourceList = source; 401 List sourceList = elements;
389 queue._table.setRange(0, length, sourceList, 0); 402 queue._table.setRange(0, length, sourceList, 0);
390 queue._tail = length; 403 queue._tail = length;
391 return queue; 404 return queue;
392 } else { 405 } else {
393 return new ListQueue<E>()..addAll(source); 406 int capacity = _INITIAL_CAPACITY;
407 if (elements is EfficientLength) {
408 capacity = elements.length;
409 }
410 ListQueue<E> result = new ListQueue<E>(capacity);
411 for (final E element in elements) {
412 result.addLast(element);
413 }
414 return result;
394 } 415 }
395 } 416 }
396 417
397 // Iterable interface. 418 // Iterable interface.
398 419
399 Iterator<E> get iterator => new _ListQueueIterator<E>(this); 420 Iterator<E> get iterator => new _ListQueueIterator<E>(this);
400 421
401 void forEach(void action (E element)) { 422 void forEach(void action (E element)) {
402 int modificationCount = _modificationCount; 423 int modificationCount = _modificationCount;
403 for (int i = _head; i != _tail; i = (i + 1) & (_table.length - 1)) { 424 for (int i = _head; i != _tail; i = (i + 1) & (_table.length - 1)) {
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 _queue._checkModification(_modificationCount); 733 _queue._checkModification(_modificationCount);
713 if (_position == _end) { 734 if (_position == _end) {
714 _current = null; 735 _current = null;
715 return false; 736 return false;
716 } 737 }
717 _current = _queue._table[_position]; 738 _current = _queue._table[_position];
718 _position = (_position + 1) & (_queue._table.length - 1); 739 _position = (_position + 1) & (_queue._table.length - 1);
719 return true; 740 return true;
720 } 741 }
721 } 742 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698