Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 return _sentinel.nextEntry(); | 152 return _sentinel.nextEntry(); |
| 153 } | 153 } |
| 154 | 154 |
| 155 int get length { | 155 int get length { |
| 156 int counter = 0; | 156 int counter = 0; |
| 157 forEach(void _(E element) { counter++; }); | 157 forEach(void _(E element) { counter++; }); |
| 158 return counter; | 158 return counter; |
| 159 } | 159 } |
| 160 | 160 |
| 161 bool isEmpty() { | 161 bool isEmpty() { |
| 162 return (_sentinel._next === _sentinel); | 162 return (identical(_sentinel._next, _sentinel)); |
|
ahe
2012/10/22 09:05:21
Put _sentinel on the left to avoid using identical
floitsch
2012/10/22 12:07:37
I would prefer keeping it.
| |
| 163 } | 163 } |
| 164 | 164 |
| 165 void clear() { | 165 void clear() { |
| 166 _sentinel._next = _sentinel; | 166 _sentinel._next = _sentinel; |
| 167 _sentinel._previous = _sentinel; | 167 _sentinel._previous = _sentinel; |
| 168 } | 168 } |
| 169 | 169 |
| 170 void forEach(void f(E element)) { | 170 void forEach(void f(E element)) { |
| 171 DoubleLinkedQueueEntry<E> entry = _sentinel._next; | 171 DoubleLinkedQueueEntry<E> entry = _sentinel._next; |
| 172 while (entry !== _sentinel) { | 172 while (!identical(entry, _sentinel)) { |
| 173 DoubleLinkedQueueEntry<E> nextEntry = entry._next; | 173 DoubleLinkedQueueEntry<E> nextEntry = entry._next; |
| 174 f(entry._element); | 174 f(entry._element); |
| 175 entry = nextEntry; | 175 entry = nextEntry; |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 void forEachEntry(void f(DoubleLinkedQueueEntry<E> element)) { | 179 void forEachEntry(void f(DoubleLinkedQueueEntry<E> element)) { |
| 180 DoubleLinkedQueueEntry<E> entry = _sentinel._next; | 180 DoubleLinkedQueueEntry<E> entry = _sentinel._next; |
| 181 while (entry !== _sentinel) { | 181 while (!identical(entry, _sentinel)) { |
| 182 DoubleLinkedQueueEntry<E> nextEntry = entry._next; | 182 DoubleLinkedQueueEntry<E> nextEntry = entry._next; |
| 183 f(entry); | 183 f(entry); |
| 184 entry = nextEntry; | 184 entry = nextEntry; |
| 185 } | 185 } |
| 186 } | 186 } |
| 187 | 187 |
| 188 bool every(bool f(E element)) { | 188 bool every(bool f(E element)) { |
| 189 DoubleLinkedQueueEntry<E> entry = _sentinel._next; | 189 DoubleLinkedQueueEntry<E> entry = _sentinel._next; |
| 190 while (entry !== _sentinel) { | 190 while (!identical(entry, _sentinel)) { |
| 191 DoubleLinkedQueueEntry<E> nextEntry = entry._next; | 191 DoubleLinkedQueueEntry<E> nextEntry = entry._next; |
| 192 if (!f(entry._element)) return false; | 192 if (!f(entry._element)) return false; |
| 193 entry = nextEntry; | 193 entry = nextEntry; |
| 194 } | 194 } |
| 195 return true; | 195 return true; |
| 196 } | 196 } |
| 197 | 197 |
| 198 bool some(bool f(E element)) { | 198 bool some(bool f(E element)) { |
| 199 DoubleLinkedQueueEntry<E> entry = _sentinel._next; | 199 DoubleLinkedQueueEntry<E> entry = _sentinel._next; |
| 200 while (entry !== _sentinel) { | 200 while (!identical(entry, _sentinel)) { |
| 201 DoubleLinkedQueueEntry<E> nextEntry = entry._next; | 201 DoubleLinkedQueueEntry<E> nextEntry = entry._next; |
| 202 if (f(entry._element)) return true; | 202 if (f(entry._element)) return true; |
| 203 entry = nextEntry; | 203 entry = nextEntry; |
| 204 } | 204 } |
| 205 return false; | 205 return false; |
| 206 } | 206 } |
| 207 | 207 |
| 208 Queue map(f(E element)) { | 208 Queue map(f(E element)) { |
| 209 Queue other = new Queue(); | 209 Queue other = new Queue(); |
| 210 DoubleLinkedQueueEntry<E> entry = _sentinel._next; | 210 DoubleLinkedQueueEntry<E> entry = _sentinel._next; |
| 211 while (entry !== _sentinel) { | 211 while (!identical(entry, _sentinel)) { |
| 212 DoubleLinkedQueueEntry<E> nextEntry = entry._next; | 212 DoubleLinkedQueueEntry<E> nextEntry = entry._next; |
| 213 other.addLast(f(entry._element)); | 213 other.addLast(f(entry._element)); |
| 214 entry = nextEntry; | 214 entry = nextEntry; |
| 215 } | 215 } |
| 216 return other; | 216 return other; |
| 217 } | 217 } |
| 218 | 218 |
| 219 Dynamic reduce(Dynamic initialValue, | 219 Dynamic reduce(Dynamic initialValue, |
| 220 Dynamic combine(Dynamic previousValue, E element)) { | 220 Dynamic combine(Dynamic previousValue, E element)) { |
| 221 return Collections.reduce(this, initialValue, combine); | 221 return Collections.reduce(this, initialValue, combine); |
| 222 } | 222 } |
| 223 | 223 |
| 224 Queue<E> filter(bool f(E element)) { | 224 Queue<E> filter(bool f(E element)) { |
| 225 Queue<E> other = new Queue<E>(); | 225 Queue<E> other = new Queue<E>(); |
| 226 DoubleLinkedQueueEntry<E> entry = _sentinel._next; | 226 DoubleLinkedQueueEntry<E> entry = _sentinel._next; |
| 227 while (entry !== _sentinel) { | 227 while (!identical(entry, _sentinel)) { |
| 228 DoubleLinkedQueueEntry<E> nextEntry = entry._next; | 228 DoubleLinkedQueueEntry<E> nextEntry = entry._next; |
| 229 if (f(entry._element)) other.addLast(entry._element); | 229 if (f(entry._element)) other.addLast(entry._element); |
| 230 entry = nextEntry; | 230 entry = nextEntry; |
| 231 } | 231 } |
| 232 return other; | 232 return other; |
| 233 } | 233 } |
| 234 | 234 |
| 235 _DoubleLinkedQueueIterator<E> iterator() { | 235 _DoubleLinkedQueueIterator<E> iterator() { |
| 236 return new _DoubleLinkedQueueIterator<E>(_sentinel); | 236 return new _DoubleLinkedQueueIterator<E>(_sentinel); |
| 237 } | 237 } |
| 238 | 238 |
| 239 String toString() { | 239 String toString() { |
| 240 return Collections.collectionToString(this); | 240 return Collections.collectionToString(this); |
| 241 } | 241 } |
| 242 } | 242 } |
| 243 | 243 |
| 244 class _DoubleLinkedQueueIterator<E> implements Iterator<E> { | 244 class _DoubleLinkedQueueIterator<E> implements Iterator<E> { |
| 245 final _DoubleLinkedQueueEntrySentinel<E> _sentinel; | 245 final _DoubleLinkedQueueEntrySentinel<E> _sentinel; |
| 246 DoubleLinkedQueueEntry<E> _currentEntry; | 246 DoubleLinkedQueueEntry<E> _currentEntry; |
| 247 | 247 |
| 248 _DoubleLinkedQueueIterator(_DoubleLinkedQueueEntrySentinel this._sentinel) { | 248 _DoubleLinkedQueueIterator(_DoubleLinkedQueueEntrySentinel this._sentinel) { |
| 249 _currentEntry = _sentinel; | 249 _currentEntry = _sentinel; |
| 250 } | 250 } |
| 251 | 251 |
| 252 bool hasNext() { | 252 bool hasNext() { |
| 253 return _currentEntry._next !== _sentinel; | 253 return !identical(_currentEntry._next, _sentinel); |
| 254 } | 254 } |
| 255 | 255 |
| 256 E next() { | 256 E next() { |
| 257 if (!hasNext()) { | 257 if (!hasNext()) { |
| 258 throw const NoMoreElementsException(); | 258 throw const NoMoreElementsException(); |
| 259 } | 259 } |
| 260 _currentEntry = _currentEntry._next; | 260 _currentEntry = _currentEntry._next; |
| 261 return _currentEntry.element; | 261 return _currentEntry.element; |
| 262 } | 262 } |
| 263 } | 263 } |
| OLD | NEW |