Chromium Code Reviews| 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 | 5 |
| 6 /** | 6 /** |
| 7 * An entry in a doubly linked list. It contains a pointer to the next | 7 * An entry in a doubly linked list. It contains a pointer to the next |
| 8 * entry, the previous entry, and the boxed element. | 8 * entry, the previous entry, and the boxed element. |
| 9 */ | 9 */ |
| 10 class DoubleLinkedQueueEntry<E> { | 10 class DoubleLinkedQueueEntry<E> { |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 } | 164 } |
| 165 | 165 |
| 166 void clear() { | 166 void clear() { |
| 167 _sentinel._next = _sentinel; | 167 _sentinel._next = _sentinel; |
| 168 _sentinel._previous = _sentinel; | 168 _sentinel._previous = _sentinel; |
| 169 } | 169 } |
| 170 | 170 |
| 171 void forEach(void f(E element)) { | 171 void forEach(void f(E element)) { |
| 172 DoubleLinkedQueueEntry<E> entry = _sentinel._next; | 172 DoubleLinkedQueueEntry<E> entry = _sentinel._next; |
| 173 while (entry !== _sentinel) { | 173 while (entry !== _sentinel) { |
| 174 DoubleLinkedQueueEntry<E> nextEntry = entry._next; | |
| 174 f(entry._element); | 175 f(entry._element); |
| 175 entry = entry._next; | 176 entry = nextEntry; |
| 176 } | 177 } |
| 177 } | 178 } |
| 178 | 179 |
| 179 void forEachEntry(void f(DoubleLinkedQueueEntry<E> element)) { | 180 void forEachEntry(void f(DoubleLinkedQueueEntry<E> element)) { |
| 180 DoubleLinkedQueueEntry<E> entry = _sentinel._next; | 181 DoubleLinkedQueueEntry<E> entry = _sentinel._next; |
| 181 while (entry !== _sentinel) { | 182 while (entry !== _sentinel) { |
| 183 DoubleLinkedQueueEntry<E> nextEntry = entry._next; | |
| 182 f(entry); | 184 f(entry); |
| 183 entry = entry._next; | 185 entry = nextEntry; |
|
siva
2011/11/14 21:26:17
How will this protect against somebody writing an
hausner
2011/11/14 21:58:02
I agree with Siva's comment. Probably need to have
srdjan
2011/11/14 22:15:33
It is possible for programmers to write the closur
srdjan
2011/11/14 22:15:33
Filed bug 442 for clarification of modification be
| |
| 184 } | 186 } |
| 185 } | 187 } |
| 186 | 188 |
| 187 bool every(bool f(E element)) { | 189 bool every(bool f(E element)) { |
| 188 DoubleLinkedQueueEntry<E> entry = _sentinel._next; | 190 DoubleLinkedQueueEntry<E> entry = _sentinel._next; |
| 189 while (entry !== _sentinel) { | 191 while (entry !== _sentinel) { |
| 192 DoubleLinkedQueueEntry<E> nextEntry = entry._next; | |
| 190 if (!f(entry._element)) return false; | 193 if (!f(entry._element)) return false; |
| 191 entry = entry._next; | 194 entry = nextEntry; |
| 192 } | 195 } |
| 193 return true; | 196 return true; |
| 194 } | 197 } |
| 195 | 198 |
| 196 bool some(bool f(E element)) { | 199 bool some(bool f(E element)) { |
| 197 DoubleLinkedQueueEntry<E> entry = _sentinel._next; | 200 DoubleLinkedQueueEntry<E> entry = _sentinel._next; |
| 198 while (entry !== _sentinel) { | 201 while (entry !== _sentinel) { |
| 202 DoubleLinkedQueueEntry<E> nextEntry = entry._next; | |
| 199 if (f(entry._element)) return true; | 203 if (f(entry._element)) return true; |
| 200 entry = entry._next; | 204 entry = nextEntry; |
| 201 } | 205 } |
| 202 return false; | 206 return false; |
| 203 } | 207 } |
| 204 | 208 |
| 205 Queue<E> filter(bool f(E element)) { | 209 Queue<E> filter(bool f(E element)) { |
| 206 Queue<E> other = new Queue<E>(); | 210 Queue<E> other = new Queue<E>(); |
| 207 DoubleLinkedQueueEntry<E> entry = _sentinel._next; | 211 DoubleLinkedQueueEntry<E> entry = _sentinel._next; |
| 208 while (entry !== _sentinel) { | 212 while (entry !== _sentinel) { |
| 213 DoubleLinkedQueueEntry<E> nextEntry = entry._next; | |
| 209 if (f(entry._element)) other.addLast(entry._element); | 214 if (f(entry._element)) other.addLast(entry._element); |
| 210 entry = entry._next; | 215 entry = nextEntry; |
| 211 } | 216 } |
| 212 return other; | 217 return other; |
| 213 } | 218 } |
| 214 | 219 |
| 215 _DoubleLinkedQueueIterator<E> iterator() { | 220 _DoubleLinkedQueueIterator<E> iterator() { |
| 216 return new _DoubleLinkedQueueIterator<E>(_sentinel); | 221 return new _DoubleLinkedQueueIterator<E>(_sentinel); |
| 217 } | 222 } |
| 218 } | 223 } |
| 219 | 224 |
| 220 class _DoubleLinkedQueueIterator<E> implements Iterator<E> { | 225 class _DoubleLinkedQueueIterator<E> implements Iterator<E> { |
| 221 final _DoubleLinkedQueueEntrySentinel<E> _sentinel; | 226 final _DoubleLinkedQueueEntrySentinel<E> _sentinel; |
| 222 DoubleLinkedQueueEntry<E> _currentEntry; | 227 DoubleLinkedQueueEntry<E> _currentEntry; |
| 223 | 228 |
| 224 _DoubleLinkedQueueIterator(_DoubleLinkedQueueEntrySentinel this._sentinel) { | 229 _DoubleLinkedQueueIterator(_DoubleLinkedQueueEntrySentinel this._sentinel) { |
| 225 _currentEntry = _sentinel; | 230 _currentEntry = _sentinel; |
| 226 } | 231 } |
| 227 | 232 |
| 228 bool hasNext() { | 233 bool hasNext() { |
| 229 return _currentEntry._next !== _sentinel; | 234 return _currentEntry._next !== _sentinel; |
| 230 } | 235 } |
| 231 | 236 |
| 232 E next() { | 237 E next() { |
| 233 if (!hasNext()) { | 238 if (!hasNext()) { |
| 234 throw const NoMoreElementsException(); | 239 throw const NoMoreElementsException(); |
| 235 } | 240 } |
| 236 _currentEntry = _currentEntry._next; | 241 _currentEntry = _currentEntry._next; |
| 237 return _currentEntry.element; | 242 return _currentEntry.element; |
| 238 } | 243 } |
| 239 } | 244 } |
| OLD | NEW |