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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 while (!identical(entry, _sentinel)) { | 217 while (!identical(entry, _sentinel)) { |
218 if (entry.element == o) { | 218 if (entry.element == o) { |
219 entry.remove(); | 219 entry.remove(); |
220 _elementCount--; | 220 _elementCount--; |
221 return; | 221 return; |
222 } | 222 } |
223 entry = entry._next; | 223 entry = entry._next; |
224 } | 224 } |
225 } | 225 } |
226 | 226 |
227 void removeAll(Iterable elements) { | 227 void retainAll(Iterable elements) { |
228 // Use this method when remove is slow and removeWhere more efficient. | 228 _filterIterable(elements, true); |
229 IterableMixinWorkaround.removeAllList(this, elements); | |
230 } | 229 } |
231 | 230 |
232 void removeWhere(bool test(E element)) { | 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) { |
233 DoubleLinkedQueueEntry<E> entry = firstEntry(); | 246 DoubleLinkedQueueEntry<E> entry = firstEntry(); |
234 while (!identical(entry, _sentinel)) { | 247 while (!identical(entry, _sentinel)) { |
235 DoubleLinkedQueueEntry<E> next = entry._next; | 248 DoubleLinkedQueueEntry<E> next = entry._next; |
236 if (test(entry.element)) { | 249 if (test(entry.element) != retainMatching) { |
237 entry.remove(); | 250 entry.remove(); |
238 _elementCount--; | 251 _elementCount--; |
239 } | 252 } |
240 entry = next; | |
241 } | |
242 } | |
243 | |
244 void retainWhere(bool test(E element)) { | |
245 DoubleLinkedQueueEntry<E> entry = firstEntry(); | |
246 while (!identical(entry, _sentinel)) { | |
247 DoubleLinkedQueueEntry<E> next = entry._next; | |
248 if (!test(entry.element)) { | |
249 entry.remove(); | |
250 _elementCount--; | |
251 } | |
252 entry = next; | 253 entry = next; |
253 } | 254 } |
254 } | 255 } |
255 | 256 |
| 257 void removeWhere(bool test(E element)) { |
| 258 _filter(test, false); |
| 259 } |
| 260 |
| 261 void retainWhere(bool test(E element)) { |
| 262 _filter(test, true); |
| 263 } |
| 264 |
256 E get first { | 265 E get first { |
257 return _sentinel._next.element; | 266 return _sentinel._next.element; |
258 } | 267 } |
259 | 268 |
260 E get last { | 269 E get last { |
261 return _sentinel._previous.element; | 270 return _sentinel._previous.element; |
262 } | 271 } |
263 | 272 |
264 E get single { | 273 E get single { |
265 // Note that this also covers the case where the queue is empty. | 274 // Note that this also covers the case where the queue is empty. |
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 _queue._checkModification(_modificationCount); | 716 _queue._checkModification(_modificationCount); |
708 if (_position == _end) { | 717 if (_position == _end) { |
709 _current = null; | 718 _current = null; |
710 return false; | 719 return false; |
711 } | 720 } |
712 _current = _queue._table[_position]; | 721 _current = _queue._table[_position]; |
713 _position = (_position + 1) & (_queue._table.length - 1); | 722 _position = (_position + 1) & (_queue._table.length - 1); |
714 return true; | 723 return true; |
715 } | 724 } |
716 } | 725 } |
OLD | NEW |