| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 part of dart.collection; | |
| 6 | |
| 7 | |
| 8 /** | |
| 9 * A specialized double-linked list of elements that extends [LinkedListEntry]. | |
| 10 * | |
| 11 * This is not a generic data structure. It only accepts elements that extend | |
| 12 * the [LinkedListEntry] class. See the [Queue] implementations for | |
| 13 * generic collections that allow constant time adding and removing at the ends. | |
| 14 * | |
| 15 * This is not a [List] implementation. Despite its name, this class does not | |
| 16 * implement the [List] interface. It does not allow constant time lookup by | |
| 17 * index. | |
| 18 * | |
| 19 * Because the elements themselves contain the links of this linked list, | |
| 20 * each element can be in only one list at a time. To add an element to another | |
| 21 * list, it must first be removed from its current list (if any). | |
| 22 * | |
| 23 * In return, each element knows its own place in the linked list, as well as | |
| 24 * which list it is in. This allows constant time [LinkedListEntry.addAfter], | |
| 25 * [LinkedListEntry.addBefore] and [LinkedListEntry.unlink] operations | |
| 26 * when all you have is the element. | |
| 27 * | |
| 28 * A `LinkedList` also allows constant time adding and removing at either end, | |
| 29 * and a constant time length getter. | |
| 30 */ | |
| 31 class LinkedList<E extends LinkedListEntry<E>> | |
| 32 extends IterableBase<E> | |
| 33 implements _LinkedListLink { | |
| 34 | |
| 35 int _modificationCount = 0; | |
| 36 int _length = 0; | |
| 37 _LinkedListLink _next; | |
| 38 _LinkedListLink _previous; | |
| 39 | |
| 40 /** | |
| 41 * Construct a new empty linked list. | |
| 42 */ | |
| 43 LinkedList() { | |
| 44 _next = _previous = this; | |
| 45 } | |
| 46 | |
| 47 /** | |
| 48 * Add [entry] to the beginning of the linked list. | |
| 49 */ | |
| 50 void addFirst(E entry) { | |
| 51 _insertAfter(this, entry); | |
| 52 } | |
| 53 | |
| 54 /** | |
| 55 * Add [entry] to the end of the linked list. | |
| 56 */ | |
| 57 void add(E entry) { | |
| 58 _insertAfter(_previous, entry); | |
| 59 } | |
| 60 | |
| 61 /** | |
| 62 * Add [entries] to the end of the linked list. | |
| 63 */ | |
| 64 void addAll(Iterable<E> entries) { | |
| 65 entries.forEach((entry) => _insertAfter(_previous, entry)); | |
| 66 } | |
| 67 | |
| 68 /** | |
| 69 * Remove [entry] from the linked list. | |
| 70 * | |
| 71 * Returns false and does nothing if [entry] is not in this linked list. | |
| 72 * | |
| 73 * This is equivalent to calling `entry.unlink()` if the entry is in this | |
| 74 * list. | |
| 75 */ | |
| 76 bool remove(E entry) { | |
| 77 if (entry._list != this) return false; | |
| 78 _unlink(entry); // Unlink will decrement length. | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 Iterator<E> get iterator => new _LinkedListIterator<E>(this); | |
| 83 | |
| 84 int get length => _length; | |
| 85 | |
| 86 /** | |
| 87 * Remove all elements from this linked list. | |
| 88 */ | |
| 89 void clear() { | |
| 90 _modificationCount++; | |
| 91 _LinkedListLink next = _next; | |
| 92 while (!identical(next, this)) { | |
| 93 E entry = next; | |
| 94 next = entry._next; | |
| 95 entry._next = entry._previous = entry._list = null; | |
| 96 } | |
| 97 _next = _previous = this; | |
| 98 _length = 0; | |
| 99 } | |
| 100 | |
| 101 E get first { | |
| 102 if (identical(_next, this)) { | |
| 103 throw new StateError('No such element'); | |
| 104 } | |
| 105 return _next; | |
| 106 } | |
| 107 | |
| 108 E get last { | |
| 109 if (identical(_previous, this)) { | |
| 110 throw new StateError('No such element'); | |
| 111 } | |
| 112 return _previous; | |
| 113 } | |
| 114 | |
| 115 E get single { | |
| 116 if (identical(_previous, this)) { | |
| 117 throw new StateError('No such element'); | |
| 118 } | |
| 119 if (!identical(_previous, _next)) { | |
| 120 throw new StateError('Too many elements'); | |
| 121 } | |
| 122 return _next; | |
| 123 } | |
| 124 | |
| 125 /** | |
| 126 * Call [action] with each entry in this linked list. | |
| 127 * | |
| 128 * It's an error if [action] modify the linked list. | |
| 129 */ | |
| 130 void forEach(void action(E entry)) { | |
| 131 int modificationCount = _modificationCount; | |
| 132 _LinkedListLink current = _next; | |
| 133 while (!identical(current, this)) { | |
| 134 action(current); | |
| 135 if (modificationCount != _modificationCount) { | |
| 136 throw new ConcurrentModificationError(this); | |
| 137 } | |
| 138 current = current._next; | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 bool get isEmpty => _length == 0; | |
| 143 | |
| 144 void _insertAfter(_LinkedListLink entry, E newEntry) { | |
| 145 if (newEntry.list != null) { | |
| 146 throw new StateError( | |
| 147 'LinkedListEntry is already in a LinkedList'); | |
| 148 } | |
| 149 _modificationCount++; | |
| 150 newEntry._list = this; | |
| 151 var predecessor = entry; | |
| 152 var successor = entry._next; | |
| 153 successor._previous = newEntry; | |
| 154 newEntry._previous = predecessor; | |
| 155 newEntry._next = successor; | |
| 156 predecessor._next = newEntry; | |
| 157 _length++; | |
| 158 } | |
| 159 | |
| 160 void _unlink(LinkedListEntry<E> entry) { | |
| 161 _modificationCount++; | |
| 162 entry._next._previous = entry._previous; | |
| 163 entry._previous._next = entry._next; | |
| 164 _length--; | |
| 165 entry._list = entry._next = entry._previous = null; | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 | |
| 170 class _LinkedListIterator<E extends LinkedListEntry<E>> | |
| 171 implements Iterator<E> { | |
| 172 final LinkedList<E> _list; | |
| 173 final int _modificationCount; | |
| 174 E _current; | |
| 175 _LinkedListLink _next; | |
| 176 | |
| 177 _LinkedListIterator(LinkedList<E> list) | |
| 178 : _list = list, | |
| 179 _modificationCount = list._modificationCount, | |
| 180 _next = list._next; | |
| 181 | |
| 182 E get current => _current; | |
| 183 | |
| 184 bool moveNext() { | |
| 185 if (identical(_next, _list)) { | |
| 186 _current = null; | |
| 187 return false; | |
| 188 } | |
| 189 if (_modificationCount != _list._modificationCount) { | |
| 190 throw new ConcurrentModificationError(this); | |
| 191 } | |
| 192 _current = _next; | |
| 193 _next = _next._next; | |
| 194 return true; | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 | |
| 199 class _LinkedListLink { | |
| 200 _LinkedListLink _next; | |
| 201 _LinkedListLink _previous; | |
| 202 } | |
| 203 | |
| 204 | |
| 205 /** | |
| 206 * An object that can be an element in a [LinkedList]. | |
| 207 * | |
| 208 * All elements of a `LinkedList` must extend this class. | |
| 209 * The class provides the internal links that link elements together | |
| 210 * in the `LinkedList`, and a reference to the linked list itself | |
| 211 * that an element is currently part of. | |
| 212 * | |
| 213 * An entry can be in at most one linked list at a time. | |
| 214 * While an entry is in a linked list, the [list] property points to that | |
| 215 * linked list, and otherwise the `list` property is `null`. | |
| 216 * | |
| 217 * When created, an entry is not in any linked list. | |
| 218 */ | |
| 219 abstract class LinkedListEntry<E extends LinkedListEntry<E>> | |
| 220 implements _LinkedListLink { | |
| 221 LinkedList<E> _list; | |
| 222 _LinkedListLink _next; | |
| 223 _LinkedListLink _previous; | |
| 224 | |
| 225 /** | |
| 226 * Get the linked list containing this element. | |
| 227 * | |
| 228 * Returns `null` if this entry is not currently in any list. | |
| 229 */ | |
| 230 LinkedList<E> get list => _list; | |
| 231 | |
| 232 /** | |
| 233 * Unlink the element from its linked list. | |
| 234 * | |
| 235 * The entry must currently be in a linked list when this method is called. | |
| 236 */ | |
| 237 void unlink() { | |
| 238 _list._unlink(this); | |
| 239 } | |
| 240 | |
| 241 /** | |
| 242 * Return the succeessor of this element in its linked list. | |
| 243 * | |
| 244 * Returns `null` if there is no successor in the linked list, or if this | |
| 245 * entry is not currently in any list. | |
| 246 */ | |
| 247 E get next { | |
| 248 if (identical(_next, _list)) return null; | |
| 249 E result = _next; | |
| 250 return result; | |
| 251 } | |
| 252 | |
| 253 /** | |
| 254 * Return the predecessor of this element in its linked list. | |
| 255 * | |
| 256 * Returns `null` if there is no predecessor in the linked list, or if this | |
| 257 * entry is not currently in any list. | |
| 258 */ | |
| 259 E get previous { | |
| 260 if (identical(_previous, _list)) return null; | |
| 261 return _previous as E; | |
| 262 } | |
| 263 | |
| 264 /** | |
| 265 * Insert an element after this element in this element's linked list. | |
| 266 * | |
| 267 * This entry must be in a linked list when this method is called. | |
| 268 * The [entry] must not be in a linked list. | |
| 269 */ | |
| 270 void insertAfter(E entry) { | |
| 271 _list._insertAfter(this, entry); | |
| 272 } | |
| 273 | |
| 274 /** | |
| 275 * Insert an element before this element in this element's linked list. | |
| 276 * | |
| 277 * This entry must be in a linked list when this method is called. | |
| 278 * The [entry] must not be in a linked list. | |
| 279 */ | |
| 280 void insertBefore(E entry) { | |
| 281 _list._insertAfter(_previous, entry); | |
| 282 } | |
| 283 } | |
| OLD | NEW |