Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: sdk/lib/collection/linked_list.dart

Issue 13459002: Introduce new LinkedList to dart:collection. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix naming, concurrent modification and further tests. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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>>
Lasse Reichstein Nielsen 2013/06/13 11:36:59 Documentation of for class?
Anders Johnsen 2013/06/13 11:54:08 Done.
9 extends IterableBase<E>
10 implements _LinkedListLink {
11 int _modificationCount = 0;
12
13 int _length = 0;
14 _LinkedListLink _next;
15 _LinkedListLink _previous;
16
17 LinkedList() {
18 _next = _previous = this;
19 }
20
21 void addFirst(E entry) {
Lasse Reichstein Nielsen 2013/06/13 11:36:59 And documentation for all public methods?
Anders Johnsen 2013/06/13 11:54:08 Done.
22 _insertAfter(this, entry);
23 }
24
25 void add(E entry) {
26 _insertAfter(_previous, entry);
27 }
28
29 void addAll(Iterable<E> entries) {
30 entries.forEach(add);
Lasse Reichstein Nielsen 2013/06/13 11:36:59 Consider having an internal method, e.g., called "
Anders Johnsen 2013/06/13 11:54:08 Done.
31 }
32
33 bool remove(E entry) {
34 if (entry._list != this) return false;
35 _unlink(entry); // Unlink will decrement length.
36 return true;
37 }
38
39 void _insertAfter(_LinkedListLink entry, E newEntry) {
40 if (newEntry.list != null) {
41 throw new StateError(
42 'LinkedListEntry is already in a LinkedList');
43 }
44 _modificationCount++;
45 newEntry._list = this;
46 var predecessor = entry;
47 var successor = entry._next;
48 successor._previous = newEntry;
49 newEntry._previous = predecessor;
50 newEntry._next = successor;
51 predecessor._next = newEntry;
52 _length++;
53 }
54
55 void _unlink(E entry) {
56 _modificationCount++;
57 entry._next._previous = entry._previous;
58 entry._previous._next = entry._next;
59 _length--;
60 entry._list = entry._next = entry._previous = null;
61 }
62
63 Iterator<LinkedListEntry> get iterator {
64 return new _LinkedListIterator(this);
Lasse Reichstein Nielsen 2013/06/13 11:36:59 Iterator<E> get iterator => new _LinkedListIterato
Anders Johnsen 2013/06/13 11:54:08 Done.
65 }
66
67 String toString() => ToString.iterableToString(this);
68
69 int get length => _length;
70
71 void clear() {
72 _modificationCount++;
73 _LinkedListLink next = _next;
74 while (!identical(next, this)) {
75 _LinkedListLink entry = next;
76 next = entry._next;
77 entry._next = entry._previous = entry._list = null;
78 }
79 _next = _previous = this;
80 _length = 0;
81 }
82
83 E get first {
84 if (identical(_next, this)) {
85 throw new StateError('No such element');
86 }
87 return _next;
88 }
89
90 E get last {
91 if (identical(_previous, this)) {
92 throw new StateError('No such element');
93 }
94 return _previous;
95 }
96
97 E get single {
98 if (identical(_previous, this)) {
99 throw new StateError('No such element');
100 }
101 if (!identical(_previous, _next)) {
102 throw new StateError('Too many elements');
103 }
104 return _next;
105 }
106
107 void forEach(void action(E entry)) {
Lasse Reichstein Nielsen 2013/06/13 11:36:59 Mention that it is an error to remove or add eleme
Anders Johnsen 2013/06/13 11:54:08 Done.
108 int modificationCount = _modificationCount;
109 _LinkedListLink current = _next;
110 while (!identical(current, this)) {
111 action(current);
112 if (modificationCount != _modificationCount) {
113 throw new ConcurrentModificationError(this);
114 }
115 current = current._next;
116 }
117 }
118
119 bool get isEmpty => _length == 0;
120 }
121
122
123 class _LinkedListIterator<E extends LinkedListEntry>
124 implements Iterator<E> {
125 final LinkedList<E> _list;
126 final int _modificationCount;
127 E _current;
128 _LinkedListLink _next;
129
130 _LinkedListIterator(LinkedList<E> list)
131 : _list = list,
132 _modificationCount = list._modificationCount,
133 _next = list._next;
134
135 E get current => _current;
136
137 bool moveNext() {
138 if (identical(_next, _list)) {
139 _current = null;
140 return false;
141 }
142 if (_modificationCount != _list._modificationCount) {
143 throw new ConcurrentModificationError(this);
144 }
145 _current = _next;
146 _next = _next._next;
147 return true;
148 }
149 }
150
151
152 class _LinkedListLink {
153 _LinkedListLink _next;
154 _LinkedListLink _previous;
155 }
156
157
158 abstract class LinkedListEntry<E> extends _LinkedListLink {
Lasse Reichstein Nielsen 2013/06/13 11:36:59 Consider implementing _LinkedListLink instead of e
Anders Johnsen 2013/06/13 11:54:08 Done.
159 LinkedList<E> _list;
160
161 LinkedList<E> get list => _list;
162
163 void unlink() {
164 _list._unlink(this);
165 }
166
167 LinkedListEntry get next {
168 return _next;
169 }
170
171 LinkedListEntry get previous {
172 if (_previous == _list) return null;
173 return _previous;
174 }
175
176 void insertAfter(E entry) {
177 _list._insertAfter(this, entry);
178 }
179
180 void insertBefore(E entry) {
181 _list._insertAfter(_previous, entry);
182 }
183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698