OLD | NEW |
| (Empty) |
1 part of dart.collection; | |
2 abstract class ListBase<E> extends Object with ListMixin<E> {static String list
ToString(List list) => IterableBase.iterableToFullString(list, '[', ']'); | |
3 } | |
4 abstract class ListMixin<E> implements List<E> {Iterator<E> get iterator => new
ListIterator<E>(this); | |
5 E elementAt(int index) => this[index]; | |
6 void forEach(void action(E element)) { | |
7 int length = this.length; | |
8 for (int i = 0; i < length; i++) { | |
9 action(this[i]); | |
10 if (length != this.length) { | |
11 throw new ConcurrentModificationError(this); | |
12 } | |
13 } | |
14 } | |
15 bool get isEmpty => length == 0; | |
16 bool get isNotEmpty => !isEmpty; | |
17 E get first { | |
18 if (length == 0) throw IterableElementError.noElement(); | |
19 return this[0]; | |
20 } | |
21 E get last { | |
22 if (length == 0) throw IterableElementError.noElement(); | |
23 return this[length - 1]; | |
24 } | |
25 E get single { | |
26 if (length == 0) throw IterableElementError.noElement(); | |
27 if (length > 1) throw IterableElementError.tooMany(); | |
28 return this[0]; | |
29 } | |
30 bool contains(Object element) { | |
31 int length = this.length; | |
32 for (int i = 0; i < this.length; i++) { | |
33 if (this[i] == element) return true; | |
34 if (length != this.length) { | |
35 throw new ConcurrentModificationError(this); | |
36 } | |
37 } | |
38 return false; | |
39 } | |
40 bool every(bool test(E element)) { | |
41 int length = this.length; | |
42 for (int i = 0; i < length; i++) { | |
43 if (!test(this[i])) return false; | |
44 if (length != this.length) { | |
45 throw new ConcurrentModificationError(this); | |
46 } | |
47 } | |
48 return true; | |
49 } | |
50 bool any(bool test(E element)) { | |
51 int length = this.length; | |
52 for (int i = 0; i < length; i++) { | |
53 if (test(this[i])) return true; | |
54 if (length != this.length) { | |
55 throw new ConcurrentModificationError(this); | |
56 } | |
57 } | |
58 return false; | |
59 } | |
60 E firstWhere(bool test(E element), { | |
61 E orElse()} | |
62 ) { | |
63 int length = this.length; | |
64 for (int i = 0; i < length; i++) { | |
65 E element = this[i]; | |
66 if (test(element)) return element; | |
67 if (length != this.length) { | |
68 throw new ConcurrentModificationError(this); | |
69 } | |
70 } | |
71 if (orElse != null) return orElse(); | |
72 throw IterableElementError.noElement(); | |
73 } | |
74 E lastWhere(bool test(E element), { | |
75 E orElse()} | |
76 ) { | |
77 int length = this.length; | |
78 for (int i = length - 1; i >= 0; i--) { | |
79 E element = this[i]; | |
80 if (test(element)) return element; | |
81 if (length != this.length) { | |
82 throw new ConcurrentModificationError(this); | |
83 } | |
84 } | |
85 if (orElse != null) return orElse(); | |
86 throw IterableElementError.noElement(); | |
87 } | |
88 E singleWhere(bool test(E element)) { | |
89 int length = this.length; | |
90 E match = null; | |
91 bool matchFound = false; | |
92 for (int i = 0; i < length; i++) { | |
93 E element = this[i]; | |
94 if (test(element)) { | |
95 if (matchFound) { | |
96 throw IterableElementError.tooMany(); | |
97 } | |
98 matchFound = true; | |
99 match = element; | |
100 } | |
101 if (length != this.length) { | |
102 throw new ConcurrentModificationError(this); | |
103 } | |
104 } | |
105 if (matchFound) return match; | |
106 throw IterableElementError.noElement(); | |
107 } | |
108 String join([String separator = ""]) { | |
109 if (length == 0) return ""; | |
110 StringBuffer buffer = new StringBuffer()..writeAll(this, separator); | |
111 return buffer.toString(); | |
112 } | |
113 Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test); | |
114 Iterable map(f(E element)) => new MappedListIterable(this, f); | |
115 Iterable expand(Iterable f(E element)) => new ExpandIterable<E, dynamic>(this,
f); | |
116 E reduce(E combine(E previousValue, E element)) { | |
117 int length = this.length; | |
118 if (length == 0) throw IterableElementError.noElement(); | |
119 E value = this[0]; | |
120 for (int i = 1; i < length; i++) { | |
121 value = combine(value, this[i]); | |
122 if (length != this.length) { | |
123 throw new ConcurrentModificationError(this); | |
124 } | |
125 } | |
126 return value; | |
127 } | |
128 fold(var initialValue, combine(var previousValue, E element)) { | |
129 var value = initialValue; | |
130 int length = this.length; | |
131 for (int i = 0; i < length; i++) { | |
132 value = combine(value, this[i]); | |
133 if (length != this.length) { | |
134 throw new ConcurrentModificationError(this); | |
135 } | |
136 } | |
137 return value; | |
138 } | |
139 Iterable<E> skip(int count) => new SubListIterable<E>(this, count, null); | |
140 Iterable<E> skipWhile(bool test(E element)) { | |
141 return new SkipWhileIterable<E>(this, test); | |
142 } | |
143 Iterable<E> take(int count) => new SubListIterable<E>(this, 0, count); | |
144 Iterable<E> takeWhile(bool test(E element)) { | |
145 return new TakeWhileIterable<E>(this, test); | |
146 } | |
147 List<E> toList({ | |
148 bool growable : true} | |
149 ) { | |
150 List<E> result; | |
151 if (growable) { | |
152 result = new List<E>()..length = length; | |
153 } | |
154 else { | |
155 result = new List<E>(length); | |
156 } | |
157 for (int i = 0; i < length; i++) { | |
158 result[i] = this[i]; | |
159 } | |
160 return result; | |
161 } | |
162 Set<E> toSet() { | |
163 Set<E> result = new Set<E>(); | |
164 for (int i = 0; i < length; i++) { | |
165 result.add(this[i]); | |
166 } | |
167 return result; | |
168 } | |
169 void add(E element) { | |
170 this[this.length++] = element; | |
171 } | |
172 void addAll(Iterable<E> iterable) { | |
173 for (E element in iterable) { | |
174 this[this.length++] = element; | |
175 } | |
176 } | |
177 bool remove(Object element) { | |
178 for (int i = 0; i < this.length; i++) { | |
179 if (this[i] == element) { | |
180 this.setRange(i, this.length - 1, this, i + 1); | |
181 this.length -= 1; | |
182 return true; | |
183 } | |
184 } | |
185 return false; | |
186 } | |
187 void removeWhere(bool test(E element)) { | |
188 _filter(this, test, false); | |
189 } | |
190 void retainWhere(bool test(E element)) { | |
191 _filter(this, test, true); | |
192 } | |
193 static void _filter(List source, bool test(var element), bool retainMatching) { | |
194 List retained = []; | |
195 int length = source.length; | |
196 for (int i = 0; i < length; i++) { | |
197 var element = source[i]; | |
198 if (test(element) == retainMatching) { | |
199 retained.add(element); | |
200 } | |
201 if (length != source.length) { | |
202 throw new ConcurrentModificationError(source); | |
203 } | |
204 } | |
205 if (retained.length != source.length) { | |
206 source.setRange(0, retained.length, retained); | |
207 source.length = retained.length; | |
208 } | |
209 } | |
210 void clear() { | |
211 this.length = 0; | |
212 } | |
213 E removeLast() { | |
214 if (length == 0) { | |
215 throw IterableElementError.noElement(); | |
216 } | |
217 E result = this[length - 1]; | |
218 length--; | |
219 return result; | |
220 } | |
221 void sort([int compare(E a, E b)]) { | |
222 Sort.sort(this, compare == null ? Comparable.compare : compare); | |
223 } | |
224 void shuffle([Random random]) { | |
225 if (random == null) random = new Random(); | |
226 int length = this.length; | |
227 while (length > 1) { | |
228 int pos = random.nextInt(length); | |
229 length -= 1; | |
230 var tmp = this[length]; | |
231 this[length] = this[pos]; | |
232 this[pos] = tmp; | |
233 } | |
234 } | |
235 Map<int, E> asMap() { | |
236 return new ListMapView<E>(this); | |
237 } | |
238 List<E> sublist(int start, [int end]) { | |
239 int listLength = this.length; | |
240 if (end == null) end = listLength; | |
241 RangeError.checkValidRange(start, end, listLength); | |
242 int length = end - start; | |
243 List<E> result = new List<E>()..length = length; | |
244 for (int i = 0; i < length; i++) { | |
245 result[i] = this[start + i]; | |
246 } | |
247 return result; | |
248 } | |
249 Iterable<E> getRange(int start, int end) { | |
250 RangeError.checkValidRange(start, end, this.length); | |
251 return new SubListIterable<E>(this, start, end); | |
252 } | |
253 void removeRange(int start, int end) { | |
254 RangeError.checkValidRange(start, end, this.length); | |
255 int length = end - start; | |
256 setRange(start, this.length - length, this, end); | |
257 this.length -= length; | |
258 } | |
259 void fillRange(int start, int end, [E fill]) { | |
260 RangeError.checkValidRange(start, end, this.length); | |
261 for (int i = start; i < end; i++) { | |
262 this[i] = fill; | |
263 } | |
264 } | |
265 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { | |
266 RangeError.checkValidRange(start, end, this.length); | |
267 int length = end - start; | |
268 if (length == 0) return; RangeError.checkNotNegative(skipCount, "skipCount"); | |
269 List otherList; | |
270 int otherStart; | |
271 if (iterable is List) { | |
272 otherList = DEVC$RT.cast(iterable, DEVC$RT.type((Iterable<E> _) { | |
273 } | |
274 ), DEVC$RT.type((List<dynamic> _) { | |
275 } | |
276 ), "ImplicitCast", """line 365, column 19 of dart:collection/list.dart: """, i
terable is List<dynamic>, true); | |
277 otherStart = skipCount; | |
278 } | |
279 else { | |
280 otherList = iterable.skip(skipCount).toList(growable: false); | |
281 otherStart = 0; | |
282 } | |
283 if (otherStart + length > otherList.length) { | |
284 throw IterableElementError.tooFew(); | |
285 } | |
286 if (otherStart < start) { | |
287 for (int i = length - 1; i >= 0; i--) { | |
288 this[start + i] = ((__x5) => DEVC$RT.cast(__x5, dynamic, E, "CompositeCast",
"""line 377, column 27 of dart:collection/list.dart: """, __x5 is E, false))(ot
herList[otherStart + i]); | |
289 } | |
290 } | |
291 else { | |
292 for (int i = 0; i < length; i++) { | |
293 this[start + i] = ((__x6) => DEVC$RT.cast(__x6, dynamic, E, "CompositeCast",
"""line 381, column 27 of dart:collection/list.dart: """, __x6 is E, false))(ot
herList[otherStart + i]); | |
294 } | |
295 } | |
296 } | |
297 void replaceRange(int start, int end, Iterable<E> newContents) { | |
298 RangeError.checkValidRange(start, end, this.length); | |
299 if (newContents is! EfficientLength) { | |
300 newContents = newContents.toList(); | |
301 } | |
302 int removeLength = end - start; | |
303 int insertLength = newContents.length; | |
304 if (removeLength >= insertLength) { | |
305 int delta = removeLength - insertLength; | |
306 int insertEnd = start + insertLength; | |
307 int newLength = this.length - delta; | |
308 this.setRange(start, insertEnd, newContents); | |
309 if (delta != 0) { | |
310 this.setRange(insertEnd, newLength, this, end); | |
311 this.length = newLength; | |
312 } | |
313 } | |
314 else { | |
315 int delta = insertLength - removeLength; | |
316 int newLength = this.length + delta; | |
317 int insertEnd = start + insertLength; | |
318 this.length = newLength; | |
319 this.setRange(insertEnd, newLength, this, end); | |
320 this.setRange(start, insertEnd, newContents); | |
321 } | |
322 } | |
323 int indexOf(Object element, [int startIndex = 0]) { | |
324 if (startIndex >= this.length) { | |
325 return -1; | |
326 } | |
327 if (startIndex < 0) { | |
328 startIndex = 0; | |
329 } | |
330 for (int i = startIndex; i < this.length; i++) { | |
331 if (this[i] == element) { | |
332 return i; | |
333 } | |
334 } | |
335 return -1; | |
336 } | |
337 int lastIndexOf(Object element, [int startIndex]) { | |
338 if (startIndex == null) { | |
339 startIndex = this.length - 1; | |
340 } | |
341 else { | |
342 if (startIndex < 0) { | |
343 return -1; | |
344 } | |
345 if (startIndex >= this.length) { | |
346 startIndex = this.length - 1; | |
347 } | |
348 } | |
349 for (int i = startIndex; i >= 0; i--) { | |
350 if (this[i] == element) { | |
351 return i; | |
352 } | |
353 } | |
354 return -1; | |
355 } | |
356 void insert(int index, E element) { | |
357 RangeError.checkValueInInterval(index, 0, length, "index"); | |
358 if (index == this.length) { | |
359 add(element); | |
360 return;} | |
361 if (index is! int) throw new ArgumentError(index); | |
362 this.length++; | |
363 setRange(index + 1, this.length, this, index); | |
364 this[index] = element; | |
365 } | |
366 E removeAt(int index) { | |
367 E result = this[index]; | |
368 setRange(index, this.length - 1, this, index + 1); | |
369 length--; | |
370 return result; | |
371 } | |
372 void insertAll(int index, Iterable<E> iterable) { | |
373 RangeError.checkValueInInterval(index, 0, length, "index"); | |
374 if (iterable is EfficientLength) { | |
375 iterable = iterable.toList(); | |
376 } | |
377 int insertionLength = iterable.length; | |
378 this.length += insertionLength; | |
379 setRange(index + insertionLength, this.length, this, index); | |
380 setAll(index, iterable); | |
381 } | |
382 void setAll(int index, Iterable<E> iterable) { | |
383 if (iterable is List) { | |
384 setRange(index, index + iterable.length, iterable); | |
385 } | |
386 else { | |
387 for (E element in iterable) { | |
388 this[index++] = element; | |
389 } | |
390 } | |
391 } | |
392 Iterable<E> get reversed => new ReversedListIterable<E>(this); | |
393 String toString() => IterableBase.iterableToFullString(this, '[', ']'); | |
394 } | |
OLD | NEW |