OLD | NEW |
| (Empty) |
1 dart_library.library('collection/src/queue_list', null, /* Imports */[ | |
2 'dart/_runtime', | |
3 'dart/core', | |
4 'dart/collection' | |
5 ], /* Lazy imports */[ | |
6 ], function(exports, dart, core, collection) { | |
7 'use strict'; | |
8 let dartx = dart.dartx; | |
9 const _head = Symbol('_head'); | |
10 const _tail = Symbol('_tail'); | |
11 const _table = Symbol('_table'); | |
12 const _add = Symbol('_add'); | |
13 const _preGrow = Symbol('_preGrow'); | |
14 const _grow = Symbol('_grow'); | |
15 const _writeToList = Symbol('_writeToList'); | |
16 const QueueList$ = dart.generic(function(E) { | |
17 class QueueList extends dart.mixin(core.Object, collection.ListMixin$(E)) { | |
18 QueueList(initialCapacity) { | |
19 if (initialCapacity === void 0) initialCapacity = null; | |
20 this[_head] = 0; | |
21 this[_tail] = 0; | |
22 this[_table] = null; | |
23 if (initialCapacity == null || dart.notNull(initialCapacity) < dart.notN
ull(QueueList$()._INITIAL_CAPACITY)) { | |
24 initialCapacity = QueueList$()._INITIAL_CAPACITY; | |
25 } else if (!dart.notNull(QueueList$()._isPowerOf2(initialCapacity))) { | |
26 initialCapacity = QueueList$()._nextPowerOf2(initialCapacity); | |
27 } | |
28 dart.assert(QueueList$()._isPowerOf2(initialCapacity)); | |
29 this[_table] = core.List$(E).new(initialCapacity); | |
30 } | |
31 static from(source) { | |
32 if (dart.is(source, core.List)) { | |
33 let length = source[dartx.length]; | |
34 let queue = new (QueueList$(E))(dart.notNull(length) + 1); | |
35 dart.assert(dart.notNull(queue[_table][dartx.length]) > dart.notNull(l
ength)); | |
36 let sourceList = dart.as(source, core.List); | |
37 queue[_table][dartx.setRange](0, length, dart.as(sourceList, core.Iter
able$(E)), 0); | |
38 queue[_tail] = length; | |
39 return queue; | |
40 } else { | |
41 let _ = new (QueueList$(E))(); | |
42 _.addAll(source); | |
43 return _; | |
44 } | |
45 } | |
46 add(element) { | |
47 dart.as(element, E); | |
48 this[_add](element); | |
49 } | |
50 addAll(elements) { | |
51 dart.as(elements, core.Iterable$(E)); | |
52 if (dart.is(elements, core.List)) { | |
53 let list = dart.as(elements, core.List); | |
54 let addCount = list[dartx.length]; | |
55 let length = this.length; | |
56 if (dart.notNull(length) + dart.notNull(addCount) >= dart.notNull(this
[_table][dartx.length])) { | |
57 this[_preGrow](dart.notNull(length) + dart.notNull(addCount)); | |
58 this[_table][dartx.setRange](length, dart.notNull(length) + dart.not
Null(addCount), dart.as(list, core.Iterable$(E)), 0); | |
59 this[_tail] = dart.notNull(this[_tail]) + dart.notNull(addCount); | |
60 } else { | |
61 let endSpace = dart.notNull(this[_table][dartx.length]) - dart.notNu
ll(this[_tail]); | |
62 if (dart.notNull(addCount) < dart.notNull(endSpace)) { | |
63 this[_table][dartx.setRange](this[_tail], dart.notNull(this[_tail]
) + dart.notNull(addCount), dart.as(list, core.Iterable$(E)), 0); | |
64 this[_tail] = dart.notNull(this[_tail]) + dart.notNull(addCount); | |
65 } else { | |
66 let preSpace = dart.notNull(addCount) - dart.notNull(endSpace); | |
67 this[_table][dartx.setRange](this[_tail], dart.notNull(this[_tail]
) + dart.notNull(endSpace), dart.as(list, core.Iterable$(E)), 0); | |
68 this[_table][dartx.setRange](0, preSpace, dart.as(list, core.Itera
ble$(E)), endSpace); | |
69 this[_tail] = preSpace; | |
70 } | |
71 } | |
72 } else { | |
73 for (let element of elements) | |
74 this[_add](element); | |
75 } | |
76 } | |
77 toString() { | |
78 return collection.IterableBase.iterableToFullString(this, "{", "}"); | |
79 } | |
80 addLast(element) { | |
81 dart.as(element, E); | |
82 this[_add](element); | |
83 } | |
84 addFirst(element) { | |
85 dart.as(element, E); | |
86 this[_head] = dart.notNull(this[_head]) - 1 & dart.notNull(this[_table][
dartx.length]) - 1; | |
87 this[_table][dartx.set](this[_head], element); | |
88 if (this[_head] == this[_tail]) this[_grow](); | |
89 } | |
90 removeFirst() { | |
91 if (this[_head] == this[_tail]) dart.throw(new core.StateError("No eleme
nt")); | |
92 let result = this[_table][dartx.get](this[_head]); | |
93 this[_table][dartx.set](this[_head], null); | |
94 this[_head] = dart.notNull(this[_head]) + 1 & dart.notNull(this[_table][
dartx.length]) - 1; | |
95 return result; | |
96 } | |
97 removeLast() { | |
98 if (this[_head] == this[_tail]) dart.throw(new core.StateError("No eleme
nt")); | |
99 this[_tail] = dart.notNull(this[_tail]) - 1 & dart.notNull(this[_table][
dartx.length]) - 1; | |
100 let result = this[_table][dartx.get](this[_tail]); | |
101 this[_table][dartx.set](this[_tail], null); | |
102 return result; | |
103 } | |
104 get length() { | |
105 return dart.notNull(this[_tail]) - dart.notNull(this[_head]) & dart.notN
ull(this[_table][dartx.length]) - 1; | |
106 } | |
107 set length(value) { | |
108 if (dart.notNull(value) < 0) dart.throw(new core.RangeError(`Length ${va
lue} may not be negative.`)); | |
109 let delta = dart.notNull(value) - dart.notNull(this.length); | |
110 if (dart.notNull(delta) >= 0) { | |
111 if (dart.notNull(this[_table][dartx.length]) <= dart.notNull(value)) { | |
112 this[_preGrow](value); | |
113 } | |
114 this[_tail] = dart.notNull(this[_tail]) + dart.notNull(delta) & dart.n
otNull(this[_table][dartx.length]) - 1; | |
115 return; | |
116 } | |
117 let newTail = dart.notNull(this[_tail]) + dart.notNull(delta); | |
118 if (dart.notNull(newTail) >= 0) { | |
119 this[_table][dartx.fillRange](newTail, this[_tail], null); | |
120 } else { | |
121 newTail = dart.notNull(newTail) + dart.notNull(this[_table][dartx.leng
th]); | |
122 this[_table][dartx.fillRange](0, this[_tail], null); | |
123 this[_table][dartx.fillRange](newTail, this[_table][dartx.length], nul
l); | |
124 } | |
125 this[_tail] = newTail; | |
126 } | |
127 get(index) { | |
128 if (dart.notNull(index) < 0 || dart.notNull(index) >= dart.notNull(this.
length)) { | |
129 dart.throw(new core.RangeError(`Index ${index} must be in the range [0
..${this.length}).`)); | |
130 } | |
131 return this[_table][dartx.get](dart.notNull(this[_head]) + dart.notNull(
index) & dart.notNull(this[_table][dartx.length]) - 1); | |
132 } | |
133 set(index, value) { | |
134 dart.as(value, E); | |
135 if (dart.notNull(index) < 0 || dart.notNull(index) >= dart.notNull(this.
length)) { | |
136 dart.throw(new core.RangeError(`Index ${index} must be in the range [0
..${this.length}).`)); | |
137 } | |
138 this[_table][dartx.set](dart.notNull(this[_head]) + dart.notNull(index)
& dart.notNull(this[_table][dartx.length]) - 1, value); | |
139 return value; | |
140 } | |
141 static _isPowerOf2(number) { | |
142 return (dart.notNull(number) & dart.notNull(number) - 1) == 0; | |
143 } | |
144 static _nextPowerOf2(number) { | |
145 dart.assert(dart.notNull(number) > 0); | |
146 number = (dart.notNull(number) << 1) - 1; | |
147 for (;;) { | |
148 let nextNumber = dart.notNull(number) & dart.notNull(number) - 1; | |
149 if (nextNumber == 0) return number; | |
150 number = nextNumber; | |
151 } | |
152 } | |
153 [_add](element) { | |
154 dart.as(element, E); | |
155 this[_table][dartx.set](this[_tail], element); | |
156 this[_tail] = dart.notNull(this[_tail]) + 1 & dart.notNull(this[_table][
dartx.length]) - 1; | |
157 if (this[_head] == this[_tail]) this[_grow](); | |
158 } | |
159 [_grow]() { | |
160 let newTable = core.List$(E).new(dart.notNull(this[_table][dartx.length]
) * 2); | |
161 let split = dart.notNull(this[_table][dartx.length]) - dart.notNull(this
[_head]); | |
162 newTable[dartx.setRange](0, split, this[_table], this[_head]); | |
163 newTable[dartx.setRange](split, dart.notNull(split) + dart.notNull(this[
_head]), this[_table], 0); | |
164 this[_head] = 0; | |
165 this[_tail] = this[_table][dartx.length]; | |
166 this[_table] = newTable; | |
167 } | |
168 [_writeToList](target) { | |
169 dart.as(target, core.List$(E)); | |
170 dart.assert(dart.notNull(target[dartx.length]) >= dart.notNull(this.leng
th)); | |
171 if (dart.notNull(this[_head]) <= dart.notNull(this[_tail])) { | |
172 let length = dart.notNull(this[_tail]) - dart.notNull(this[_head]); | |
173 target[dartx.setRange](0, length, this[_table], this[_head]); | |
174 return length; | |
175 } else { | |
176 let firstPartSize = dart.notNull(this[_table][dartx.length]) - dart.no
tNull(this[_head]); | |
177 target[dartx.setRange](0, firstPartSize, this[_table], this[_head]); | |
178 target[dartx.setRange](firstPartSize, dart.notNull(firstPartSize) + da
rt.notNull(this[_tail]), this[_table], 0); | |
179 return dart.notNull(this[_tail]) + dart.notNull(firstPartSize); | |
180 } | |
181 } | |
182 [_preGrow](newElementCount) { | |
183 dart.assert(dart.notNull(newElementCount) >= dart.notNull(this.length)); | |
184 newElementCount = dart.notNull(newElementCount) + (dart.notNull(newEleme
ntCount) >> 1); | |
185 let newCapacity = QueueList$()._nextPowerOf2(newElementCount); | |
186 let newTable = core.List$(E).new(newCapacity); | |
187 this[_tail] = this[_writeToList](newTable); | |
188 this[_table] = newTable; | |
189 this[_head] = 0; | |
190 } | |
191 } | |
192 QueueList[dart.implements] = () => [collection.Queue$(E)]; | |
193 dart.setSignature(QueueList, { | |
194 constructors: () => ({ | |
195 QueueList: [QueueList$(E), [], [core.int]], | |
196 from: [QueueList$(E), [core.Iterable$(E)]] | |
197 }), | |
198 methods: () => ({ | |
199 add: [dart.void, [E]], | |
200 addAll: [dart.void, [core.Iterable$(E)]], | |
201 addLast: [dart.void, [E]], | |
202 addFirst: [dart.void, [E]], | |
203 removeFirst: [E, []], | |
204 removeLast: [E, []], | |
205 get: [E, [core.int]], | |
206 set: [dart.void, [core.int, E]], | |
207 [_add]: [dart.void, [E]], | |
208 [_grow]: [dart.void, []], | |
209 [_writeToList]: [core.int, [core.List$(E)]], | |
210 [_preGrow]: [dart.void, [core.int]] | |
211 }), | |
212 statics: () => ({ | |
213 _isPowerOf2: [core.bool, [core.int]], | |
214 _nextPowerOf2: [core.int, [core.int]] | |
215 }), | |
216 names: ['_isPowerOf2', '_nextPowerOf2'] | |
217 }); | |
218 dart.defineExtensionMembers(QueueList, [ | |
219 'add', | |
220 'addAll', | |
221 'toString', | |
222 'removeLast', | |
223 'get', | |
224 'set', | |
225 'length', | |
226 'length' | |
227 ]); | |
228 return QueueList; | |
229 }); | |
230 let QueueList = QueueList$(); | |
231 QueueList._INITIAL_CAPACITY = 8; | |
232 // Exports: | |
233 exports.QueueList$ = QueueList$; | |
234 exports.QueueList = QueueList; | |
235 }); | |
OLD | NEW |