| 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 EfficientLengthIterable<E> { | 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 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 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 int length = elements.length; | 480 int length = elements.length; |
| 481 ListQueue<E> queue = new ListQueue<E>(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 EfficientLengthIterable) { | 490 if (elements is EfficientLength) { |
| 491 capacity = elements.length; | 491 capacity = elements.length; |
| 492 } | 492 } |
| 493 ListQueue<E> result = new ListQueue<E>(capacity); | 493 ListQueue<E> result = new ListQueue<E>(capacity); |
| 494 for (final element in elements) { | 494 for (final element in elements) { |
| 495 result.addLast(element as Object/*=E*/); | 495 result.addLast(element as Object/*=E*/); |
| 496 } | 496 } |
| 497 return result; | 497 return result; |
| 498 } | 498 } |
| 499 } | 499 } |
| 500 | 500 |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 } |
| OLD | NEW |