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

Side by Side Diff: packages/collection/lib/src/queue_list.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 4 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
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 import 'dart:collection'; 5 import 'dart:collection';
6 6
7 /** 7 /// A class that efficiently implements both [Queue] and [List].
8 * A class that efficiently implements both [Queue] and [List].
9 */
10 // TODO(nweiz): Currently this code is copied almost verbatim from 8 // TODO(nweiz): Currently this code is copied almost verbatim from
11 // dart:collection. The only changes are to implement List and to remove methods 9 // dart:collection. The only changes are to implement List and to remove methods
12 // that are redundant with ListMixin. Remove or simplify it when issue 21330 is 10 // that are redundant with ListMixin. Remove or simplify it when issue 21330 is
13 // fixed. 11 // fixed.
14 class QueueList<E> extends Object with ListMixin<E> implements Queue<E> { 12 class QueueList<E> extends Object with ListMixin<E> implements Queue<E> {
15 static const int _INITIAL_CAPACITY = 8; 13 static const int _INITIAL_CAPACITY = 8;
16 List<E> _table; 14 List<E> _table;
17 int _head; 15 int _head;
18 int _tail; 16 int _tail;
19 17
20 /** 18 /// Create an empty queue.
21 * Create an empty queue. 19 ///
22 * 20 /// If [initialCapacity] is given, prepare the queue for at least that many
23 * If [initialCapacity] is given, prepare the queue for at least that many 21 /// elements.
24 * elements. 22 QueueList([int initialCapacity])
25 */ 23 : _head = 0,
26 QueueList([int initialCapacity]) : _head = 0, _tail = 0 { 24 _tail = 0 {
27 if (initialCapacity == null || initialCapacity < _INITIAL_CAPACITY) { 25 if (initialCapacity == null || initialCapacity < _INITIAL_CAPACITY) {
28 initialCapacity = _INITIAL_CAPACITY; 26 initialCapacity = _INITIAL_CAPACITY;
29 } else if (!_isPowerOf2(initialCapacity)) { 27 } else if (!_isPowerOf2(initialCapacity)) {
30 initialCapacity = _nextPowerOf2(initialCapacity); 28 initialCapacity = _nextPowerOf2(initialCapacity);
31 } 29 }
32 assert(_isPowerOf2(initialCapacity)); 30 assert(_isPowerOf2(initialCapacity));
33 _table = new List<E>(initialCapacity); 31 _table = new List<E>(initialCapacity);
34 } 32 }
35 33
36 /** 34 /// Create a queue initially containing the elements of [source].
37 * Create a queue initially containing the elements of [source].
38 */
39 factory QueueList.from(Iterable<E> source) { 35 factory QueueList.from(Iterable<E> source) {
40 if (source is List) { 36 if (source is List) {
41 int length = source.length; 37 int length = source.length;
42 QueueList<E> queue = new QueueList(length + 1); 38 QueueList<E> queue = new QueueList(length + 1);
43 assert(queue._table.length > length); 39 assert(queue._table.length > length);
44 List sourceList = source; 40 var sourceList = source;
45 queue._table.setRange(0, length, sourceList, 0); 41 queue._table.setRange(0, length, sourceList, 0);
46 queue._tail = length; 42 queue._tail = length;
47 return queue; 43 return queue;
48 } else { 44 } else {
49 return new QueueList<E>()..addAll(source); 45 return new QueueList<E>()..addAll(source);
50 } 46 }
51 } 47 }
52 48
53 // Collection interface. 49 // Collection interface.
54 50
55 void add(E element) { 51 void add(E element) {
56 _add(element); 52 _add(element);
57 } 53 }
58 54
59 void addAll(Iterable<E> elements) { 55 void addAll(Iterable<E> elements) {
60 if (elements is List) { 56 if (elements is List) {
61 List list = elements; 57 var list = elements;
62 int addCount = list.length; 58 int addCount = list.length;
63 int length = this.length; 59 int length = this.length;
64 if (length + addCount >= _table.length) { 60 if (length + addCount >= _table.length) {
65 _preGrow(length + addCount); 61 _preGrow(length + addCount);
66 // After preGrow, all elements are at the start of the list. 62 // After preGrow, all elements are at the start of the list.
67 _table.setRange(length, length + addCount, list, 0); 63 _table.setRange(length, length + addCount, list, 0);
68 _tail += addCount; 64 _tail += addCount;
69 } else { 65 } else {
70 // Adding addCount elements won't reach _head. 66 // Adding addCount elements won't reach _head.
71 int endSpace = _table.length - _tail; 67 int endSpace = _table.length - _tail;
72 if (addCount < endSpace) { 68 if (addCount < endSpace) {
73 _table.setRange(_tail, _tail + addCount, list, 0); 69 _table.setRange(_tail, _tail + addCount, list, 0);
74 _tail += addCount; 70 _tail += addCount;
75 } else { 71 } else {
76 int preSpace = addCount - endSpace; 72 int preSpace = addCount - endSpace;
77 _table.setRange(_tail, _tail + endSpace, list, 0); 73 _table.setRange(_tail, _tail + endSpace, list, 0);
78 _table.setRange(0, preSpace, list, endSpace); 74 _table.setRange(0, preSpace, list, endSpace);
79 _tail = preSpace; 75 _tail = preSpace;
80 } 76 }
81 } 77 }
82 } else { 78 } else {
83 for (E element in elements) _add(element); 79 for (E element in elements) _add(element);
84 } 80 }
85 } 81 }
86 82
87 String toString() => IterableBase.iterableToFullString(this, "{", "}"); 83 String toString() => IterableBase.iterableToFullString(this, "{", "}");
88 84
89 // Queue interface. 85 // Queue interface.
90 86
91 void addLast(E element) { _add(element); } 87 void addLast(E element) {
88 _add(element);
89 }
92 90
93 void addFirst(E element) { 91 void addFirst(E element) {
94 _head = (_head - 1) & (_table.length - 1); 92 _head = (_head - 1) & (_table.length - 1);
95 _table[_head] = element; 93 _table[_head] = element;
96 if (_head == _tail) _grow(); 94 if (_head == _tail) _grow();
97 } 95 }
98 96
99 E removeFirst() { 97 E removeFirst() {
100 if (_head == _tail) throw new StateError("No element"); 98 if (_head == _tail) throw new StateError("No element");
101 E result = _table[_head]; 99 E result = _table[_head];
(...skipping 22 matching lines...) Expand all
124 if (_table.length <= value) { 122 if (_table.length <= value) {
125 _preGrow(value); 123 _preGrow(value);
126 } 124 }
127 _tail = (_tail + delta) & (_table.length - 1); 125 _tail = (_tail + delta) & (_table.length - 1);
128 return; 126 return;
129 } 127 }
130 128
131 int newTail = _tail + delta; // [delta] is negative. 129 int newTail = _tail + delta; // [delta] is negative.
132 if (newTail >= 0) { 130 if (newTail >= 0) {
133 _table.fillRange(newTail, _tail, null); 131 _table.fillRange(newTail, _tail, null);
134 } else { 132 } else {
135 newTail += _table.length; 133 newTail += _table.length;
136 _table.fillRange(0, _tail, null); 134 _table.fillRange(0, _tail, null);
137 _table.fillRange(newTail, _table.length, null); 135 _table.fillRange(newTail, _table.length, null);
138 } 136 }
139 _tail = newTail; 137 _tail = newTail;
140 } 138 }
141 139
142 E operator [](int index) { 140 E operator [](int index) {
143 if (index < 0 || index >= length) { 141 if (index < 0 || index >= length) {
144 throw new RangeError("Index $index must be in the range [0..$length)."); 142 throw new RangeError("Index $index must be in the range [0..$length).");
145 } 143 }
146 144
147 return _table[(_head + index) & (_table.length - 1)]; 145 return _table[(_head + index) & (_table.length - 1)];
148 } 146 }
149 147
150 void operator[]=(int index, E value) { 148 void operator []=(int index, E value) {
151 if (index < 0 || index >= length) { 149 if (index < 0 || index >= length) {
152 throw new RangeError("Index $index must be in the range [0..$length)."); 150 throw new RangeError("Index $index must be in the range [0..$length).");
153 } 151 }
154 152
155 _table[(_head + index) & (_table.length - 1)] = value; 153 _table[(_head + index) & (_table.length - 1)] = value;
156 } 154 }
157 155
158 // Internal helper functions. 156 // Internal helper functions.
159 157
160 /** 158 /// Whether [number] is a power of two.
161 * Whether [number] is a power of two. 159 ///
162 * 160 /// Only works for positive numbers.
163 * Only works for positive numbers.
164 */
165 static bool _isPowerOf2(int number) => (number & (number - 1)) == 0; 161 static bool _isPowerOf2(int number) => (number & (number - 1)) == 0;
166 162
167 /** 163 /// Rounds [number] up to the nearest power of 2.
168 * Rounds [number] up to the nearest power of 2. 164 ///
169 * 165 /// If [number] is a power of 2 already, it is returned.
170 * If [number] is a power of 2 already, it is returned. 166 ///
171 * 167 /// Only works for positive numbers.
172 * Only works for positive numbers.
173 */
174 static int _nextPowerOf2(int number) { 168 static int _nextPowerOf2(int number) {
175 assert(number > 0); 169 assert(number > 0);
176 number = (number << 1) - 1; 170 number = (number << 1) - 1;
177 for(;;) { 171 for (;;) {
178 int nextNumber = number & (number - 1); 172 int nextNumber = number & (number - 1);
179 if (nextNumber == 0) return number; 173 if (nextNumber == 0) return number;
180 number = nextNumber; 174 number = nextNumber;
181 } 175 }
182 } 176 }
183 177
184 /** Adds element at end of queue. Used by both [add] and [addAll]. */ 178 /// Adds element at end of queue. Used by both [add] and [addAll].
185 void _add(E element) { 179 void _add(E element) {
186 _table[_tail] = element; 180 _table[_tail] = element;
187 _tail = (_tail + 1) & (_table.length - 1); 181 _tail = (_tail + 1) & (_table.length - 1);
188 if (_head == _tail) _grow(); 182 if (_head == _tail) _grow();
189 } 183 }
190 184
191 /** 185 /// Grow the table when full.
192 * Grow the table when full.
193 */
194 void _grow() { 186 void _grow() {
195 List<E> newTable = new List<E>(_table.length * 2); 187 List<E> newTable = new List<E>(_table.length * 2);
196 int split = _table.length - _head; 188 int split = _table.length - _head;
197 newTable.setRange(0, split, _table, _head); 189 newTable.setRange(0, split, _table, _head);
198 newTable.setRange(split, split + _head, _table, 0); 190 newTable.setRange(split, split + _head, _table, 0);
199 _head = 0; 191 _head = 0;
200 _tail = _table.length; 192 _tail = _table.length;
201 _table = newTable; 193 _table = newTable;
202 } 194 }
203 195
204 int _writeToList(List<E> target) { 196 int _writeToList(List<E> target) {
205 assert(target.length >= length); 197 assert(target.length >= length);
206 if (_head <= _tail) { 198 if (_head <= _tail) {
207 int length = _tail - _head; 199 int length = _tail - _head;
208 target.setRange(0, length, _table, _head); 200 target.setRange(0, length, _table, _head);
209 return length; 201 return length;
210 } else { 202 } else {
211 int firstPartSize = _table.length - _head; 203 int firstPartSize = _table.length - _head;
212 target.setRange(0, firstPartSize, _table, _head); 204 target.setRange(0, firstPartSize, _table, _head);
213 target.setRange(firstPartSize, firstPartSize + _tail, _table, 0); 205 target.setRange(firstPartSize, firstPartSize + _tail, _table, 0);
214 return _tail + firstPartSize; 206 return _tail + firstPartSize;
215 } 207 }
216 } 208 }
217 209
218 /** Grows the table even if it is not full. */ 210 /// Grows the table even if it is not full.
219 void _preGrow(int newElementCount) { 211 void _preGrow(int newElementCount) {
220 assert(newElementCount >= length); 212 assert(newElementCount >= length);
221 213
222 // Add 1.5x extra room to ensure that there's room for more elements after 214 // Add 1.5x extra room to ensure that there's room for more elements after
223 // expansion. 215 // expansion.
224 newElementCount += newElementCount >> 1; 216 newElementCount += newElementCount >> 1;
225 int newCapacity = _nextPowerOf2(newElementCount); 217 int newCapacity = _nextPowerOf2(newElementCount);
226 List<E> newTable = new List<E>(newCapacity); 218 List<E> newTable = new List<E>(newCapacity);
227 _tail = _writeToList(newTable); 219 _tail = _writeToList(newTable);
228 _table = newTable; 220 _table = newTable;
229 _head = 0; 221 _head = 0;
230 } 222 }
231 } 223 }
OLDNEW
« no previous file with comments | « packages/collection/lib/src/priority_queue.dart ('k') | packages/collection/lib/src/typed_wrappers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698