OLD | NEW |
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 EfficientLengthIterable<E> { |
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 containing all [elements]. | 28 * Creates a queue containing all [elements]. |
29 * | 29 * |
30 * The element order in the queue is as if the elements were added using | 30 * The element order in the queue is as if the elements were added using |
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 if (elements is List) { | 475 if (elements is List) { |
476 int length = elements.length; | 476 int length = elements.length; |
477 ListQueue<E> queue = new ListQueue(length + 1); | 477 ListQueue<E> queue = new ListQueue(length + 1); |
478 assert(queue._table.length > length); | 478 assert(queue._table.length > length); |
479 List sourceList = elements; | 479 List sourceList = elements; |
480 queue._table.setRange(0, length, sourceList, 0); | 480 queue._table.setRange(0, length, sourceList, 0); |
481 queue._tail = length; | 481 queue._tail = length; |
482 return queue; | 482 return queue; |
483 } else { | 483 } else { |
484 int capacity = _INITIAL_CAPACITY; | 484 int capacity = _INITIAL_CAPACITY; |
485 if (elements is EfficientLength) { | 485 if (elements is EfficientLengthIterable) { |
486 capacity = elements.length; | 486 capacity = elements.length; |
487 } | 487 } |
488 ListQueue<E> result = new ListQueue<E>(capacity); | 488 ListQueue<E> result = new ListQueue<E>(capacity); |
489 for (final E element in elements) { | 489 for (final E element in elements) { |
490 result.addLast(element); | 490 result.addLast(element); |
491 } | 491 } |
492 return result; | 492 return result; |
493 } | 493 } |
494 } | 494 } |
495 | 495 |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
810 _queue._checkModification(_modificationCount); | 810 _queue._checkModification(_modificationCount); |
811 if (_position == _end) { | 811 if (_position == _end) { |
812 _current = null; | 812 _current = null; |
813 return false; | 813 return false; |
814 } | 814 } |
815 _current = _queue._table[_position]; | 815 _current = _queue._table[_position]; |
816 _position = (_position + 1) & (_queue._table.length - 1); | 816 _position = (_position + 1) & (_queue._table.length - 1); |
817 return true; | 817 return true; |
818 } | 818 } |
819 } | 819 } |
OLD | NEW |