OLD | NEW |
| (Empty) |
1 part of dart.collection; | |
2 abstract class IterableMixin<E> implements Iterable<E> {Iterable map(f(E elemen
t)) => new MappedIterable<E, dynamic>(this, f); | |
3 Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f); | |
4 Iterable expand(Iterable f(E element)) => new ExpandIterable<E, dynamic>(this,
f); | |
5 bool contains(Object element) { | |
6 for (E e in this) { | |
7 if (e == element) return true; | |
8 } | |
9 return false; | |
10 } | |
11 void forEach(void f(E element)) { | |
12 for (E element in this) f(element); | |
13 } | |
14 E reduce(E combine(E value, E element)) { | |
15 Iterator<E> iterator = this.iterator; | |
16 if (!iterator.moveNext()) { | |
17 throw IterableElementError.noElement(); | |
18 } | |
19 E value = iterator.current; | |
20 while (iterator.moveNext()) { | |
21 value = combine(value, iterator.current); | |
22 } | |
23 return value; | |
24 } | |
25 dynamic fold(var initialValue, dynamic combine(var previousValue, E element)) { | |
26 var value = initialValue; | |
27 for (E element in this) value = combine(value, element); | |
28 return value; | |
29 } | |
30 bool every(bool f(E element)) { | |
31 for (E element in this) { | |
32 if (!f(element)) return false; | |
33 } | |
34 return true; | |
35 } | |
36 String join([String separator = ""]) { | |
37 Iterator<E> iterator = this.iterator; | |
38 if (!iterator.moveNext()) return ""; | |
39 StringBuffer buffer = new StringBuffer(); | |
40 if (separator == null || separator == "") { | |
41 do { | |
42 buffer.write("${iterator.current}"); | |
43 } | |
44 while (iterator.moveNext());} | |
45 else { | |
46 buffer.write("${iterator.current}"); | |
47 while (iterator.moveNext()) { | |
48 buffer.write(separator); | |
49 buffer.write("${iterator.current}"); | |
50 } | |
51 } | |
52 return buffer.toString(); | |
53 } | |
54 bool any(bool f(E element)) { | |
55 for (E element in this) { | |
56 if (f(element)) return true; | |
57 } | |
58 return false; | |
59 } | |
60 List<E> toList({ | |
61 bool growable : true} | |
62 ) => new List<E>.from(this, growable: growable); | |
63 Set<E> toSet() => new Set<E>.from(this); | |
64 int get length { | |
65 assert (this is! EfficientLength); int count = 0; | |
66 Iterator it = iterator; | |
67 while (it.moveNext()) { | |
68 count++; | |
69 } | |
70 return count; | |
71 } | |
72 bool get isEmpty => !iterator.moveNext(); | |
73 bool get isNotEmpty => !isEmpty; | |
74 Iterable<E> take(int n) { | |
75 return new TakeIterable<E>(this, n); | |
76 } | |
77 Iterable<E> takeWhile(bool test(E value)) { | |
78 return new TakeWhileIterable<E>(this, test); | |
79 } | |
80 Iterable<E> skip(int n) { | |
81 return new SkipIterable<E>(this, n); | |
82 } | |
83 Iterable<E> skipWhile(bool test(E value)) { | |
84 return new SkipWhileIterable<E>(this, test); | |
85 } | |
86 E get first { | |
87 Iterator<E> it = iterator; | |
88 if (!it.moveNext()) { | |
89 throw IterableElementError.noElement(); | |
90 } | |
91 return it.current; | |
92 } | |
93 E get last { | |
94 Iterator<E> it = iterator; | |
95 if (!it.moveNext()) { | |
96 throw IterableElementError.noElement(); | |
97 } | |
98 E result; | |
99 do { | |
100 result = it.current; | |
101 } | |
102 while (it.moveNext()); return result; | |
103 } | |
104 E get single { | |
105 Iterator<E> it = iterator; | |
106 if (!it.moveNext()) throw IterableElementError.noElement(); | |
107 E result = it.current; | |
108 if (it.moveNext()) throw IterableElementError.tooMany(); | |
109 return result; | |
110 } | |
111 E firstWhere(bool test(E value), { | |
112 E orElse()} | |
113 ) { | |
114 for (E element in this) { | |
115 if (test(element)) return element; | |
116 } | |
117 if (orElse != null) return orElse(); | |
118 throw IterableElementError.noElement(); | |
119 } | |
120 E lastWhere(bool test(E value), { | |
121 E orElse()} | |
122 ) { | |
123 E result = null; | |
124 bool foundMatching = false; | |
125 for (E element in this) { | |
126 if (test(element)) { | |
127 result = element; | |
128 foundMatching = true; | |
129 } | |
130 } | |
131 if (foundMatching) return result; | |
132 if (orElse != null) return orElse(); | |
133 throw IterableElementError.noElement(); | |
134 } | |
135 E singleWhere(bool test(E value)) { | |
136 E result = null; | |
137 bool foundMatching = false; | |
138 for (E element in this) { | |
139 if (test(element)) { | |
140 if (foundMatching) { | |
141 throw IterableElementError.tooMany(); | |
142 } | |
143 result = element; | |
144 foundMatching = true; | |
145 } | |
146 } | |
147 if (foundMatching) return result; | |
148 throw IterableElementError.noElement(); | |
149 } | |
150 E elementAt(int index) { | |
151 if (index is! int) throw new ArgumentError.notNull("index"); | |
152 RangeError.checkNotNegative(index, "index"); | |
153 int elementIndex = 0; | |
154 for (E element in this) { | |
155 if (index == elementIndex) return element; | |
156 elementIndex++; | |
157 } | |
158 throw new RangeError.index(index, this, "index", null, elementIndex); | |
159 } | |
160 String toString() => IterableBase.iterableToShortString(this, '(', ')'); | |
161 } | |
162 abstract class IterableBase<E> implements Iterable<E> {const IterableBase(); | |
163 Iterable map(f(E element)) => new MappedIterable<E, dynamic>(this, f); | |
164 Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f); | |
165 Iterable expand(Iterable f(E element)) => new ExpandIterable<E, dynamic>(this,
f); | |
166 bool contains(Object element) { | |
167 for (E e in this) { | |
168 if (e == element) return true; | |
169 } | |
170 return false; | |
171 } | |
172 void forEach(void f(E element)) { | |
173 for (E element in this) f(element); | |
174 } | |
175 E reduce(E combine(E value, E element)) { | |
176 Iterator<E> iterator = this.iterator; | |
177 if (!iterator.moveNext()) { | |
178 throw IterableElementError.noElement(); | |
179 } | |
180 E value = iterator.current; | |
181 while (iterator.moveNext()) { | |
182 value = combine(value, iterator.current); | |
183 } | |
184 return value; | |
185 } | |
186 dynamic fold(var initialValue, dynamic combine(var previousValue, E element)) { | |
187 var value = initialValue; | |
188 for (E element in this) value = combine(value, element); | |
189 return value; | |
190 } | |
191 bool every(bool f(E element)) { | |
192 for (E element in this) { | |
193 if (!f(element)) return false; | |
194 } | |
195 return true; | |
196 } | |
197 String join([String separator = ""]) { | |
198 Iterator<E> iterator = this.iterator; | |
199 if (!iterator.moveNext()) return ""; | |
200 StringBuffer buffer = new StringBuffer(); | |
201 if (separator == null || separator == "") { | |
202 do { | |
203 buffer.write("${iterator.current}"); | |
204 } | |
205 while (iterator.moveNext());} | |
206 else { | |
207 buffer.write("${iterator.current}"); | |
208 while (iterator.moveNext()) { | |
209 buffer.write(separator); | |
210 buffer.write("${iterator.current}"); | |
211 } | |
212 } | |
213 return buffer.toString(); | |
214 } | |
215 bool any(bool f(E element)) { | |
216 for (E element in this) { | |
217 if (f(element)) return true; | |
218 } | |
219 return false; | |
220 } | |
221 List<E> toList({ | |
222 bool growable : true} | |
223 ) => new List<E>.from(this, growable: growable); | |
224 Set<E> toSet() => new Set<E>.from(this); | |
225 int get length { | |
226 assert (this is! EfficientLength); int count = 0; | |
227 Iterator<E> it = iterator; | |
228 while (it.moveNext()) { | |
229 count++; | |
230 } | |
231 return count; | |
232 } | |
233 bool get isEmpty => !iterator.moveNext(); | |
234 bool get isNotEmpty => !isEmpty; | |
235 Iterable<E> take(int n) { | |
236 return new TakeIterable<E>(this, n); | |
237 } | |
238 Iterable<E> takeWhile(bool test(E value)) { | |
239 return new TakeWhileIterable<E>(this, test); | |
240 } | |
241 Iterable<E> skip(int n) { | |
242 return new SkipIterable<E>(this, n); | |
243 } | |
244 Iterable<E> skipWhile(bool test(E value)) { | |
245 return new SkipWhileIterable<E>(this, test); | |
246 } | |
247 E get first { | |
248 Iterator<E> it = iterator; | |
249 if (!it.moveNext()) { | |
250 throw IterableElementError.noElement(); | |
251 } | |
252 return it.current; | |
253 } | |
254 E get last { | |
255 Iterator<E> it = iterator; | |
256 if (!it.moveNext()) { | |
257 throw IterableElementError.noElement(); | |
258 } | |
259 E result; | |
260 do { | |
261 result = it.current; | |
262 } | |
263 while (it.moveNext()); return result; | |
264 } | |
265 E get single { | |
266 Iterator<E> it = iterator; | |
267 if (!it.moveNext()) throw IterableElementError.noElement(); | |
268 E result = it.current; | |
269 if (it.moveNext()) throw IterableElementError.tooMany(); | |
270 return result; | |
271 } | |
272 E firstWhere(bool test(E value), { | |
273 E orElse()} | |
274 ) { | |
275 for (E element in this) { | |
276 if (test(element)) return element; | |
277 } | |
278 if (orElse != null) return orElse(); | |
279 throw IterableElementError.noElement(); | |
280 } | |
281 E lastWhere(bool test(E value), { | |
282 E orElse()} | |
283 ) { | |
284 E result = null; | |
285 bool foundMatching = false; | |
286 for (E element in this) { | |
287 if (test(element)) { | |
288 result = element; | |
289 foundMatching = true; | |
290 } | |
291 } | |
292 if (foundMatching) return result; | |
293 if (orElse != null) return orElse(); | |
294 throw IterableElementError.noElement(); | |
295 } | |
296 E singleWhere(bool test(E value)) { | |
297 E result = null; | |
298 bool foundMatching = false; | |
299 for (E element in this) { | |
300 if (test(element)) { | |
301 if (foundMatching) { | |
302 throw IterableElementError.tooMany(); | |
303 } | |
304 result = element; | |
305 foundMatching = true; | |
306 } | |
307 } | |
308 if (foundMatching) return result; | |
309 throw IterableElementError.noElement(); | |
310 } | |
311 E elementAt(int index) { | |
312 if (index is! int) throw new ArgumentError.notNull("index"); | |
313 RangeError.checkNotNegative(index, "index"); | |
314 int elementIndex = 0; | |
315 for (E element in this) { | |
316 if (index == elementIndex) return element; | |
317 elementIndex++; | |
318 } | |
319 throw new RangeError.index(index, this, "index", null, elementIndex); | |
320 } | |
321 String toString() => iterableToShortString(this, '(', ')'); | |
322 static String iterableToShortString(Iterable iterable, [String leftDelimiter =
'(', String rightDelimiter = ')']) { | |
323 if (_isToStringVisiting(iterable)) { | |
324 if (leftDelimiter == "(" && rightDelimiter == ")") { | |
325 return "(...)"; | |
326 } | |
327 return "$leftDelimiter...$rightDelimiter"; | |
328 } | |
329 List parts = []; | |
330 _toStringVisiting.add(iterable); | |
331 try { | |
332 _iterablePartsToStrings(iterable, parts); | |
333 } | |
334 finally { | |
335 assert (identical(_toStringVisiting.last, iterable)); _toStringVisiting.remove
Last(); | |
336 } | |
337 return (new StringBuffer(leftDelimiter)..writeAll(parts, ", ")..write(rightDeli
miter)).toString(); | |
338 } | |
339 static String iterableToFullString(Iterable iterable, [String leftDelimiter = '
(', String rightDelimiter = ')']) { | |
340 if (_isToStringVisiting(iterable)) { | |
341 return "$leftDelimiter...$rightDelimiter"; | |
342 } | |
343 StringBuffer buffer = new StringBuffer(leftDelimiter); | |
344 _toStringVisiting.add(iterable); | |
345 try { | |
346 buffer.writeAll(iterable, ", "); | |
347 } | |
348 finally { | |
349 assert (identical(_toStringVisiting.last, iterable)); _toStringVisiting.remove
Last(); | |
350 } | |
351 buffer.write(rightDelimiter); | |
352 return buffer.toString(); | |
353 } | |
354 static final List _toStringVisiting = []; | |
355 static bool _isToStringVisiting(Object o) { | |
356 for (int i = 0; i < _toStringVisiting.length; i++) { | |
357 if (identical(o, _toStringVisiting[i])) return true; | |
358 } | |
359 return false; | |
360 } | |
361 static void _iterablePartsToStrings(Iterable iterable, List parts) { | |
362 const int LENGTH_LIMIT = 80; | |
363 const int HEAD_COUNT = 3; | |
364 const int TAIL_COUNT = 2; | |
365 const int MAX_COUNT = 100; | |
366 const int OVERHEAD = 2; | |
367 const int ELLIPSIS_SIZE = 3; | |
368 int length = 0; | |
369 int count = 0; | |
370 Iterator it = iterable.iterator; | |
371 while (length < LENGTH_LIMIT || count < HEAD_COUNT) { | |
372 if (!it.moveNext()) return; String next = "${it.current}"; | |
373 parts.add(next); | |
374 length += next.length + OVERHEAD; | |
375 count++; | |
376 } | |
377 String penultimateString; | |
378 String ultimateString; | |
379 var penultimate = null; | |
380 var ultimate = null; | |
381 if (!it.moveNext()) { | |
382 if (count <= HEAD_COUNT + TAIL_COUNT) return; ultimateString = ((__x0) => DEVC
$RT.cast(__x0, dynamic, String, "DynamicCast", """line 530, column 24 of dart:co
llection/iterable.dart: """, __x0 is String, true))(parts.removeLast()); | |
383 penultimateString = ((__x1) => DEVC$RT.cast(__x1, dynamic, String, "DynamicCa
st", """line 531, column 27 of dart:collection/iterable.dart: """, __x1 is Strin
g, true))(parts.removeLast()); | |
384 } | |
385 else { | |
386 penultimate = it.current; | |
387 count++; | |
388 if (!it.moveNext()) { | |
389 if (count <= HEAD_COUNT + 1) { | |
390 parts.add("$penultimate"); | |
391 return;} | |
392 ultimateString = "$penultimate"; | |
393 penultimateString = ((__x2) => DEVC$RT.cast(__x2, dynamic, String, "Dynamic
Cast", """line 541, column 29 of dart:collection/iterable.dart: """, __x2 is Str
ing, true))(parts.removeLast()); | |
394 length += ultimateString.length + OVERHEAD; | |
395 } | |
396 else { | |
397 ultimate = it.current; | |
398 count++; | |
399 assert (count < MAX_COUNT); while (it.moveNext()) { | |
400 penultimate = ultimate; | |
401 ultimate = it.current; | |
402 count++; | |
403 if (count > MAX_COUNT) { | |
404 while (length > LENGTH_LIMIT - ELLIPSIS_SIZE - OVERHEAD && count > HEAD_
COUNT) { | |
405 length -= ((__x3) => DEVC$RT.cast(__x3, dynamic, int, "DynamicCast", "
""line 562, column 25 of dart:collection/iterable.dart: """, __x3 is int, true))
(parts.removeLast().length + OVERHEAD); | |
406 count--; | |
407 } | |
408 parts.add("..."); | |
409 return;} | |
410 } | |
411 penultimateString = "$penultimate"; | |
412 ultimateString = "$ultimate"; | |
413 length += ultimateString.length + penultimateString.length + 2 * OVERHEAD; | |
414 } | |
415 } | |
416 String elision = null; | |
417 if (count > parts.length + TAIL_COUNT) { | |
418 elision = "..."; | |
419 length += ELLIPSIS_SIZE + OVERHEAD; | |
420 } | |
421 while (length > LENGTH_LIMIT && parts.length > HEAD_COUNT) { | |
422 length -= ((__x4) => DEVC$RT.cast(__x4, dynamic, int, "DynamicCast", """line 5
88, column 17 of dart:collection/iterable.dart: """, __x4 is int, true))(parts.r
emoveLast().length + OVERHEAD); | |
423 if (elision == null) { | |
424 elision = "..."; | |
425 length += ELLIPSIS_SIZE + OVERHEAD; | |
426 } | |
427 } | |
428 if (elision != null) { | |
429 parts.add(elision); | |
430 } | |
431 parts.add(penultimateString); | |
432 parts.add(ultimateString); | |
433 } | |
434 } | |
OLD | NEW |