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

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

Issue 26681002: Add EfficientLength marker interface to some iterabels. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Also document Map.length is efficient, while we are at it. Created 7 years, 2 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
« no previous file with comments | « sdk/lib/collection/list.dart ('k') | sdk/lib/collection/splay_tree.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 abstract class Queue<E> implements Iterable<E> { 12 abstract class Queue<E> implements Iterable<E>, EfficientLength {
13 13
14 /** 14 /**
15 * Creates a queue. 15 * Creates a queue.
16 */ 16 */
17 factory Queue() => new ListQueue<E>(); 17 factory Queue() = ListQueue<E>;
18 18
19 /** 19 /**
20 * Creates a queue with the elements of [other]. The order in 20 * Creates a queue with the elements of [other]. The order in
21 * the queue will be the order provided by the iterator of [other]. 21 * the queue will be the order provided by the iterator of [other].
22 */ 22 */
23 factory Queue.from(Iterable<E> other) => new ListQueue<E>.from(other); 23 factory Queue.from(Iterable<E> other) = ListQueue<E>.from;
24
25 /**
26 * Returns the number of elements in the queue.
27 *
28 * This operation is efficient and does not require iterating and counting
29 * the elements.
30 */
31 int get length;
24 32
25 /** 33 /**
26 * Removes and returns the first element of this queue. Throws an 34 * Removes and returns the first element of this queue. Throws an
27 * [StateError] exception if this queue is empty. 35 * [StateError] exception if this queue is empty.
28 */ 36 */
29 E removeFirst(); 37 E removeFirst();
30 38
31 /** 39 /**
32 * Removes and returns the last element of the queue. Throws an 40 * Removes and returns the last element of the queue. Throws an
33 * [StateError] exception if this queue is empty. 41 * [StateError] exception if this queue is empty.
(...skipping 16 matching lines...) Expand all
50 void add(E value); 58 void add(E value);
51 59
52 /** 60 /**
53 * Remove a single instance of [value] from the queue. 61 * Remove a single instance of [value] from the queue.
54 * 62 *
55 * Returns `true` if a value was removed, or `false` if the queue 63 * Returns `true` if a value was removed, or `false` if the queue
56 * contained no element equal to [value]. 64 * contained no element equal to [value].
57 */ 65 */
58 bool remove(Object object); 66 bool remove(Object object);
59 67
60
61 /** 68 /**
62 * Adds all elements of [iterable] at the end of the queue. The 69 * Adds all elements of [iterable] at the end of the queue. The
63 * length of the queue is extended by the length of [iterable]. 70 * length of the queue is extended by the length of [iterable].
64 */ 71 */
65 void addAll(Iterable<E> iterable); 72 void addAll(Iterable<E> iterable);
66 73
67 /** 74 /**
68 * Removes all elements in the queue. The size of the queue becomes zero. 75 * Removes all elements in the queue. The size of the queue becomes zero.
69 */ 76 */
70 void clear(); 77 void clear();
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 _DoubleLinkedQueueIterator<E> get iterator { 304 _DoubleLinkedQueueIterator<E> get iterator {
298 return new _DoubleLinkedQueueIterator<E>(_sentinel); 305 return new _DoubleLinkedQueueIterator<E>(_sentinel);
299 } 306 }
300 307
301 // TODO(zarah) Remove this, and let it be inherited by IterableBase 308 // TODO(zarah) Remove this, and let it be inherited by IterableBase
302 String toString() => IterableMixinWorkaround.toStringIterable(this, '{', '}'); 309 String toString() => IterableMixinWorkaround.toStringIterable(this, '{', '}');
303 } 310 }
304 311
305 class _DoubleLinkedQueueIterator<E> implements Iterator<E> { 312 class _DoubleLinkedQueueIterator<E> implements Iterator<E> {
306 _DoubleLinkedQueueEntrySentinel<E> _sentinel; 313 _DoubleLinkedQueueEntrySentinel<E> _sentinel;
307 DoubleLinkedQueueEntry<E> _currentEntry = null; 314 DoubleLinkedQueueEntry<E> _nextEntry = null;
308 E _current; 315 E _current;
309 316
310 _DoubleLinkedQueueIterator(_DoubleLinkedQueueEntrySentinel<E> sentinel) 317 _DoubleLinkedQueueIterator(_DoubleLinkedQueueEntrySentinel<E> sentinel)
311 : _sentinel = sentinel, _currentEntry = sentinel; 318 : _sentinel = sentinel, _nextEntry = sentinel._next;
312 319
313 bool moveNext() { 320 bool moveNext() {
314 // When [_currentEntry] it is set to [:null:] then it is at the end. 321 // When [_currentEntry] it is set to [:null:] then it is at the end.
315 if (_currentEntry == null) { 322 if (!identical(_nextEntry, _sentinel)) {
316 assert(_current == null); 323 _current = _nextEntry._element;
317 return false; 324 _nextEntry = _nextEntry._next;
325 return true;
318 } 326 }
319 _currentEntry = _currentEntry._next; 327 _current = null;
320 if (identical(_currentEntry, _sentinel)) { 328 _nextEntry = _sentinel = null; // Still identical.
321 _currentEntry = null; 329 return false;
322 _current = null;
323 _sentinel = null;
324 return false;
325 }
326 _current = _currentEntry.element;
327 return true;
328 } 330 }
329 331
330 E get current => _current; 332 E get current => _current;
331 } 333 }
332 334
333 /** 335 /**
334 * List based [Queue]. 336 * List based [Queue].
335 * 337 *
336 * Keeps a cyclic buffer of elements, and grows to a larger buffer when 338 * Keeps a cyclic buffer of elements, and grows to a larger buffer when
337 * it fills up. This guarantees constant time peek and remove operations, and 339 * it fills up. This guarantees constant time peek and remove operations, and
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
696 _queue._checkModification(_modificationCount); 698 _queue._checkModification(_modificationCount);
697 if (_position == _end) { 699 if (_position == _end) {
698 _current = null; 700 _current = null;
699 return false; 701 return false;
700 } 702 }
701 _current = _queue._table[_position]; 703 _current = _queue._table[_position];
702 _position = (_position + 1) & (_queue._table.length - 1); 704 _position = (_position + 1) & (_queue._table.length - 1);
703 return true; 705 return true;
704 } 706 }
705 } 707 }
OLDNEW
« no previous file with comments | « sdk/lib/collection/list.dart ('k') | sdk/lib/collection/splay_tree.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698