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 class LinkedList<E extends LinkedListEntry<E>> | |
9 extends IterableBase<E> | |
10 implements _LinkedListEntry { | |
11 int _length = 0; | |
12 _LinkedListEntry _next; | |
13 _LinkedListEntry _prev; | |
Lasse Reichstein Nielsen
2013/05/24 08:42:45
You need a modification count to catch concurrent
Anders Johnsen
2013/06/13 11:14:10
Done.
| |
14 | |
15 LinkedList() { | |
16 _next = _prev = this; | |
17 } | |
18 | |
19 void addFirst(E entry) { | |
20 _insertAfter(this, entry); | |
21 } | |
22 | |
23 void add(E entry) { | |
24 _insertAfter(_prev, entry); | |
25 } | |
26 | |
27 void addAll(Iterable<E> entries) { | |
28 entries.forEach(add); | |
29 } | |
30 | |
31 void remove(E entry) { | |
32 if (entry._list != this) return; | |
floitsch
2013/05/23 19:01:26
Shouldn't we throw here?
Lasse Reichstein Nielsen
2013/05/24 08:42:45
I think returning is fine. We don't throw in other
Anders Johnsen
2013/06/13 11:14:10
Done.
| |
33 entry.unlink(); // Unlink will decrement length. | |
34 } | |
35 | |
36 void _insertAfter(_LinkedListEntry entry, E newEntry) { | |
37 if (newEntry.list != null) { | |
38 throw new StateError( | |
39 'LinkedListEntry is already present in a LinkedList'); | |
floitsch
2013/05/23 19:01:26
is already in a LinkedList
Anders Johnsen
2013/06/13 11:14:10
Done.
| |
40 } | |
41 newEntry._list = this; | |
floitsch
2013/05/23 19:01:26
I think this would read easier as follows (or use
Anders Johnsen
2013/06/13 11:14:10
Done.
| |
42 entry._next._prev = newEntry; | |
43 newEntry._prev = entry; | |
44 newEntry._next = entry._next; | |
45 entry._next = newEntry; | |
46 _length++; | |
47 } | |
48 | |
49 Iterator<LinkedListEntry> get iterator { | |
50 return new _LinkedListIterator(this); | |
51 } | |
52 | |
53 String toString() => ToString.iterableToString(this); | |
54 | |
55 int get length => _length; | |
56 | |
57 void clear() { | |
58 for (var entry in this) { | |
floitsch
2013/05/23 19:01:26
This looks dangerous.
I would prefer not to use an
Lasse Reichstein Nielsen
2013/05/24 08:42:45
Agree.
_LinkedListEntry next = _this._next;
while
Anders Johnsen
2013/06/13 11:14:10
Done.
| |
59 entry._next = entry._prev = entry._list = null; | |
60 } | |
61 _next = _prev = this; | |
62 _length = 0; | |
63 } | |
64 | |
65 E get first { | |
66 if (identical(_next, this)) { | |
67 throw new StateError('No such element'); | |
68 } | |
69 return _next; | |
70 } | |
71 | |
72 E get last { | |
73 if (identical(_prev, this)) { | |
74 throw new StateError('No such element'); | |
75 } | |
76 return _prev; | |
77 } | |
Lasse Reichstein Nielsen
2013/05/24 08:42:45
E get single {
if (identical(_prev, this)) {
Anders Johnsen
2013/06/13 11:14:10
Done.
| |
78 | |
Lasse Reichstein Nielsen
2013/05/24 08:42:45
Implement forEach without an iterator too.
Remembe
Anders Johnsen
2013/06/13 11:14:10
Done.
| |
79 bool get isEmpty => _length == 0; | |
80 } | |
81 | |
82 | |
83 class _LinkedListIterator<E extends LinkedListEntry> | |
84 implements Iterator<E> { | |
85 LinkedList<E> _list; | |
86 E _current; | |
87 _LinkedListEntry _next; | |
88 | |
89 _LinkedListIterator(LinkedList<E> list) : _list = list, _next = list._next; | |
90 | |
91 E get current => _current; | |
92 | |
93 bool moveNext() { | |
94 if (identical(_next, _list)) { | |
floitsch
2013/05/23 19:01:26
Is it on purpose that you don't check the length (
Anders Johnsen
2013/06/13 11:14:10
Done.
| |
95 _current = null; | |
96 return false; | |
97 } | |
98 _current = _next; | |
99 _next = _next._next; | |
100 return true; | |
101 } | |
102 } | |
103 | |
104 | |
105 class _LinkedListEntry { | |
floitsch
2013/05/23 19:01:26
Call it something else.
_LinkedListLink ?
Lasse Reichstein Nielsen
2013/05/24 08:42:45
_ListLink?
Anders Johnsen
2013/06/13 11:14:10
Done.
| |
106 _LinkedListEntry _next; | |
107 _LinkedListEntry _prev; | |
108 } | |
109 | |
110 | |
111 abstract class LinkedListEntry<E> extends _LinkedListEntry { | |
112 LinkedList<E> _list; | |
113 | |
114 LinkedList<E> get list => _list; | |
115 | |
116 void unlink() { | |
117 _next._prev = _prev; | |
118 _prev._next = _next; | |
119 _list._length--; | |
120 _list = _next = _prev = null; | |
121 } | |
122 | |
123 LinkedListEntry get next { | |
124 return _next; | |
125 } | |
126 | |
127 LinkedListEntry get prev { | |
floitsch
2013/05/23 19:01:26
usually we don't abbreviate. Should "prev" really
Lasse Reichstein Nielsen
2013/05/24 08:42:45
I don't think so. I generally write it out (both "
Anders Johnsen
2013/06/13 11:14:10
Done.
| |
128 if (_prev == _list) return null; | |
129 return _prev; | |
130 } | |
131 | |
132 void insertAfter(E entry) { | |
133 _list._insertAfter(this, entry); | |
134 } | |
135 | |
136 void insertBefore(E entry) { | |
137 _list._insertAfter(_prev, entry); | |
138 } | |
139 } | |
OLD | NEW |