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