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]. |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 | 286 |
287 /** | 287 /** |
288 * Creates a double-linked queue containing all [elements]. | 288 * Creates a double-linked queue containing all [elements]. |
289 * | 289 * |
290 * The element order in the queue is as if the elements were added using | 290 * The element order in the queue is as if the elements were added using |
291 * [addLast] in the order provided by [elements.iterator]. | 291 * [addLast] in the order provided by [elements.iterator]. |
292 */ | 292 */ |
293 factory DoubleLinkedQueue.from(Iterable elements) { | 293 factory DoubleLinkedQueue.from(Iterable elements) { |
294 Queue<E> list = new DoubleLinkedQueue<E>(); | 294 Queue<E> list = new DoubleLinkedQueue<E>(); |
295 for (final e in elements) { | 295 for (final e in elements) { |
296 list.addLast(e as E); | 296 E element = e as Object/*=E*/; |
| 297 list.addLast(element); |
297 } | 298 } |
298 return list; | 299 return list; |
299 } | 300 } |
300 | 301 |
301 int get length => _elementCount; | 302 int get length => _elementCount; |
302 | 303 |
303 void addLast(E value) { | 304 void addLast(E value) { |
304 _sentinel._prepend(value); | 305 _sentinel._prepend(value); |
305 _elementCount++; | 306 _elementCount++; |
306 } | 307 } |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 * `elements.iterator`. | 492 * `elements.iterator`. |
492 * | 493 * |
493 * All `elements` should be assignable to [E]. | 494 * All `elements` should be assignable to [E]. |
494 */ | 495 */ |
495 factory ListQueue.from(Iterable elements) { | 496 factory ListQueue.from(Iterable elements) { |
496 if (elements is List) { | 497 if (elements is List) { |
497 int length = elements.length; | 498 int length = elements.length; |
498 ListQueue<E> queue = new ListQueue(length + 1); | 499 ListQueue<E> queue = new ListQueue(length + 1); |
499 assert(queue._table.length > length); | 500 assert(queue._table.length > length); |
500 for (int i = 0; i < length; i++) { | 501 for (int i = 0; i < length; i++) { |
501 queue._table[i] = elements[i] as E; | 502 queue._table[i] = elements[i] as Object/*=E*/; |
502 } | 503 } |
503 queue._tail = length; | 504 queue._tail = length; |
504 return queue; | 505 return queue; |
505 } else { | 506 } else { |
506 int capacity = _INITIAL_CAPACITY; | 507 int capacity = _INITIAL_CAPACITY; |
507 if (elements is EfficientLength) { | 508 if (elements is EfficientLength) { |
508 capacity = elements.length; | 509 capacity = elements.length; |
509 } | 510 } |
510 ListQueue<E> result = new ListQueue<E>(capacity); | 511 ListQueue<E> result = new ListQueue<E>(capacity); |
511 for (final element in elements) { | 512 for (final element in elements) { |
512 result.addLast(element as E); | 513 result.addLast(element as Object/*=E*/); |
513 } | 514 } |
514 return result; | 515 return result; |
515 } | 516 } |
516 } | 517 } |
517 | 518 |
518 // Iterable interface. | 519 // Iterable interface. |
519 | 520 |
520 Iterator<E> get iterator => new _ListQueueIterator<E>(this); | 521 Iterator<E> get iterator => new _ListQueueIterator<E>(this); |
521 | 522 |
522 void forEach(void action (E element)) { | 523 void forEach(void action (E element)) { |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
832 _queue._checkModification(_modificationCount); | 833 _queue._checkModification(_modificationCount); |
833 if (_position == _end) { | 834 if (_position == _end) { |
834 _current = null; | 835 _current = null; |
835 return false; | 836 return false; |
836 } | 837 } |
837 _current = _queue._table[_position]; | 838 _current = _queue._table[_position]; |
838 _position = (_position + 1) & (_queue._table.length - 1); | 839 _position = (_position + 1) & (_queue._table.length - 1); |
839 return true; | 840 return true; |
840 } | 841 } |
841 } | 842 } |
OLD | NEW |