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 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 assert(_isPowerOf2(initialCapacity)); | 352 assert(_isPowerOf2(initialCapacity)); |
353 _table = new List<E>.fixedLength(initialCapacity); | 353 _table = new List<E>.fixedLength(initialCapacity); |
354 } | 354 } |
355 | 355 |
356 /** | 356 /** |
357 * Create a queue initially containing the elements of [source]. | 357 * Create a queue initially containing the elements of [source]. |
358 */ | 358 */ |
359 factory ListQueue.from(Iterable<E> source) { | 359 factory ListQueue.from(Iterable<E> source) { |
360 if (source is List) { | 360 if (source is List) { |
361 int length = source.length; | 361 int length = source.length; |
362 ListQueue<E> queue = new ListQueue(length); | 362 ListQueue<E> queue = new ListQueue(length + 1); |
| 363 assert(queue._table.length > length); |
363 List sourceList = source; | 364 List sourceList = source; |
364 queue._table.setRange(0, length, sourceList, 0); | 365 queue._table.setRange(0, length, sourceList, 0); |
365 queue._tail = length; | 366 queue._tail = length; |
366 return queue; | 367 return queue; |
367 } else { | 368 } else { |
368 return new ListQueue<E>()..addAll(source); | 369 return new ListQueue<E>()..addAll(source); |
369 } | 370 } |
370 } | 371 } |
371 | 372 |
372 // Iterable interface. | 373 // Iterable interface. |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
686 _queue._checkModification(_modificationCount); | 687 _queue._checkModification(_modificationCount); |
687 if (_position == _end) { | 688 if (_position == _end) { |
688 _current = null; | 689 _current = null; |
689 return false; | 690 return false; |
690 } | 691 } |
691 _current = _queue._table[_position]; | 692 _current = _queue._table[_position]; |
692 _position = (_position + 1) & (_queue._table.length - 1); | 693 _position = (_position + 1) & (_queue._table.length - 1); |
693 return true; | 694 return true; |
694 } | 695 } |
695 } | 696 } |
OLD | NEW |