| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 * Adds [value] at the end of the queue. | 43 * Adds [value] at the end of the queue. |
| 44 */ | 44 */ |
| 45 void addLast(E value); | 45 void addLast(E value); |
| 46 | 46 |
| 47 /** | 47 /** |
| 48 * Adds [value] at the end of the queue. | 48 * Adds [value] at the end of the queue. |
| 49 */ | 49 */ |
| 50 void add(E value); | 50 void add(E value); |
| 51 | 51 |
| 52 /** | 52 /** |
| 53 * Remove a single instance of [value] from the queue. |
| 54 * |
| 55 * Returns `true` if a value was removed, or `false` if the queue |
| 56 * contained no element equal to [value]. |
| 57 */ |
| 58 bool remove(Object object); |
| 59 |
| 60 |
| 61 /** |
| 53 * Adds all elements of [iterable] at the end of the queue. The | 62 * Adds all elements of [iterable] at the end of the queue. The |
| 54 * length of the queue is extended by the length of [iterable]. | 63 * length of the queue is extended by the length of [iterable]. |
| 55 */ | 64 */ |
| 56 void addAll(Iterable<E> iterable); | 65 void addAll(Iterable<E> iterable); |
| 57 | 66 |
| 58 /** | 67 /** |
| 59 * Removes all elements in the queue. The size of the queue becomes zero. | 68 * Removes all elements in the queue. The size of the queue becomes zero. |
| 60 */ | 69 */ |
| 61 void clear(); | 70 void clear(); |
| 62 } | 71 } |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 _elementCount--; | 214 _elementCount--; |
| 206 return result; | 215 return result; |
| 207 } | 216 } |
| 208 | 217 |
| 209 E removeFirst() { | 218 E removeFirst() { |
| 210 E result = _sentinel._next.remove(); | 219 E result = _sentinel._next.remove(); |
| 211 _elementCount--; | 220 _elementCount--; |
| 212 return result; | 221 return result; |
| 213 } | 222 } |
| 214 | 223 |
| 215 void remove(Object o) { | 224 bool remove(Object o) { |
| 216 DoubleLinkedQueueEntry<E> entry = firstEntry(); | 225 DoubleLinkedQueueEntry<E> entry = firstEntry(); |
| 217 while (!identical(entry, _sentinel)) { | 226 while (!identical(entry, _sentinel)) { |
| 218 if (entry.element == o) { | 227 if (entry.element == o) { |
| 219 entry.remove(); | 228 entry.remove(); |
| 220 _elementCount--; | 229 _elementCount--; |
| 221 return; | 230 return true; |
| 222 } | 231 } |
| 223 entry = entry._next; | 232 entry = entry._next; |
| 224 } | 233 } |
| 234 return false; |
| 225 } | 235 } |
| 226 | 236 |
| 227 void retainAll(Iterable elements) { | 237 void _filter(bool test(E element), bool removeMatching) { |
| 228 _filterIterable(elements, true); | |
| 229 } | |
| 230 | |
| 231 void removeAll(Iterable elements) { | |
| 232 _filterIterable(elements, false); | |
| 233 } | |
| 234 | |
| 235 void _filterIterable(Iterable elements, bool retainMatching) { | |
| 236 Set elementSet; | |
| 237 if (elements is Set) { | |
| 238 elementSet = elements; | |
| 239 } else { | |
| 240 elementSet = elements.toSet(); | |
| 241 } | |
| 242 _filter(elementSet.contains, retainMatching); | |
| 243 } | |
| 244 | |
| 245 void _filter(bool test(E element), bool retainMatching) { | |
| 246 DoubleLinkedQueueEntry<E> entry = firstEntry(); | 238 DoubleLinkedQueueEntry<E> entry = firstEntry(); |
| 247 while (!identical(entry, _sentinel)) { | 239 while (!identical(entry, _sentinel)) { |
| 248 DoubleLinkedQueueEntry<E> next = entry._next; | 240 DoubleLinkedQueueEntry<E> next = entry._next; |
| 249 if (test(entry.element) != retainMatching) { | 241 if (identical(removeMatching, test(entry.element))) { |
| 250 entry.remove(); | 242 entry.remove(); |
| 251 _elementCount--; | 243 _elementCount--; |
| 252 } | 244 } |
| 253 entry = next; | 245 entry = next; |
| 254 } | 246 } |
| 255 } | 247 } |
| 256 | 248 |
| 257 void removeWhere(bool test(E element)) { | 249 void removeWhere(bool test(E element)) { |
| 250 _filter(test, true); |
| 251 } |
| 252 |
| 253 void retainWhere(bool test(E element)) { |
| 258 _filter(test, false); | 254 _filter(test, false); |
| 259 } | 255 } |
| 260 | 256 |
| 261 void retainWhere(bool test(E element)) { | |
| 262 _filter(test, true); | |
| 263 } | |
| 264 | |
| 265 E get first { | 257 E get first { |
| 266 return _sentinel._next.element; | 258 return _sentinel._next.element; |
| 267 } | 259 } |
| 268 | 260 |
| 269 E get last { | 261 E get last { |
| 270 return _sentinel._previous.element; | 262 return _sentinel._previous.element; |
| 271 } | 263 } |
| 272 | 264 |
| 273 E get single { | 265 E get single { |
| 274 // Note that this also covers the case where the queue is empty. | 266 // Note that this also covers the case where the queue is empty. |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 _table.setRange(0, preSpace, list, endSpace); | 464 _table.setRange(0, preSpace, list, endSpace); |
| 473 _tail = preSpace; | 465 _tail = preSpace; |
| 474 } | 466 } |
| 475 } | 467 } |
| 476 _modificationCount++; | 468 _modificationCount++; |
| 477 } else { | 469 } else { |
| 478 for (E element in elements) _add(element); | 470 for (E element in elements) _add(element); |
| 479 } | 471 } |
| 480 } | 472 } |
| 481 | 473 |
| 482 void remove(Object object) { | 474 bool remove(Object object) { |
| 483 for (int i = _head; i != _tail; i = (i + 1) & (_table.length - 1)) { | 475 for (int i = _head; i != _tail; i = (i + 1) & (_table.length - 1)) { |
| 484 E element = _table[i]; | 476 E element = _table[i]; |
| 485 if (element == object) { | 477 if (element == object) { |
| 486 _remove(i); | 478 _remove(i); |
| 487 return; | 479 _modificationCount++; |
| 480 return true; |
| 488 } | 481 } |
| 489 } | 482 } |
| 490 _modificationCount++; | 483 return false; |
| 491 } | 484 } |
| 492 | 485 |
| 493 void _filterWhere(bool test(E element), bool removeMatching) { | 486 void _filterWhere(bool test(E element), bool removeMatching) { |
| 494 int index = _head; | 487 int index = _head; |
| 495 int modificationCount = _modificationCount; | 488 int modificationCount = _modificationCount; |
| 496 int i = _head; | 489 int i = _head; |
| 497 while (i != _tail) { | 490 while (i != _tail) { |
| 498 E element = _table[i]; | 491 E element = _table[i]; |
| 499 bool remove = (test(element) == removeMatching); | 492 bool remove = identical(removeMatching, test(element)); |
| 500 _checkModification(modificationCount); | 493 _checkModification(modificationCount); |
| 501 if (remove) { | 494 if (remove) { |
| 502 i = _remove(i); | 495 i = _remove(i); |
| 503 modificationCount = ++_modificationCount; | 496 modificationCount = ++_modificationCount; |
| 504 } else { | 497 } else { |
| 505 i = (i + 1) & (_table.length - 1); | 498 i = (i + 1) & (_table.length - 1); |
| 506 } | 499 } |
| 507 } | 500 } |
| 508 } | 501 } |
| 509 | 502 |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 _queue._checkModification(_modificationCount); | 701 _queue._checkModification(_modificationCount); |
| 709 if (_position == _end) { | 702 if (_position == _end) { |
| 710 _current = null; | 703 _current = null; |
| 711 return false; | 704 return false; |
| 712 } | 705 } |
| 713 _current = _queue._table[_position]; | 706 _current = _queue._table[_position]; |
| 714 _position = (_position + 1) & (_queue._table.length - 1); | 707 _position = (_position + 1) & (_queue._table.length - 1); |
| 715 return true; | 708 return true; |
| 716 } | 709 } |
| 717 } | 710 } |
| OLD | NEW |