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

Side by Side Diff: test/generated_sdk/lib/collection/iterable.dart

Issue 1162723007: remove generated_sdk from checked in code (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 6 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
(Empty)
1 // Copyright (c) 2012, 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 part of dart.collection;
6
7 /**
8 * This [Iterable] mixin implements all [Iterable] members except `iterator`.
9 *
10 * All other methods are implemented in terms of `iterator`.
11 */
12 abstract class IterableMixin<E> implements Iterable<E> {
13 // This class has methods copied verbatim into:
14 // - IterableBase
15 // - SetMixin
16 // If changing a method here, also change the other copies.
17
18 Iterable map(f(E element)) => new MappedIterable<E, dynamic>(this, f);
19
20 Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f);
21
22 Iterable expand(Iterable f(E element)) =>
23 new ExpandIterable<E, dynamic>(this, f);
24
25 bool contains(Object element) {
26 for (E e in this) {
27 if (e == element) return true;
28 }
29 return false;
30 }
31
32 void forEach(void f(E element)) {
33 for (E element in this) f(element);
34 }
35
36 E reduce(E combine(E value, E element)) {
37 Iterator<E> iterator = this.iterator;
38 if (!iterator.moveNext()) {
39 throw IterableElementError.noElement();
40 }
41 E value = iterator.current;
42 while (iterator.moveNext()) {
43 value = combine(value, iterator.current);
44 }
45 return value;
46 }
47
48 dynamic fold(var initialValue,
49 dynamic combine(var previousValue, E element)) {
50 var value = initialValue;
51 for (E element in this) value = combine(value, element);
52 return value;
53 }
54
55 bool every(bool f(E element)) {
56 for (E element in this) {
57 if (!f(element)) return false;
58 }
59 return true;
60 }
61
62 String join([String separator = ""]) {
63 Iterator<E> iterator = this.iterator;
64 if (!iterator.moveNext()) return "";
65 StringBuffer buffer = new StringBuffer();
66 if (separator == null || separator == "") {
67 do {
68 buffer.write("${iterator.current}");
69 } while (iterator.moveNext());
70 } else {
71 buffer.write("${iterator.current}");
72 while (iterator.moveNext()) {
73 buffer.write(separator);
74 buffer.write("${iterator.current}");
75 }
76 }
77 return buffer.toString();
78 }
79
80 bool any(bool f(E element)) {
81 for (E element in this) {
82 if (f(element)) return true;
83 }
84 return false;
85 }
86
87 List<E> toList({ bool growable: true }) =>
88 new List<E>.from(this, growable: growable);
89
90 Set<E> toSet() => new Set<E>.from(this);
91
92 int get length {
93 assert(this is! EfficientLength);
94 int count = 0;
95 Iterator it = iterator;
96 while (it.moveNext()) {
97 count++;
98 }
99 return count;
100 }
101
102 bool get isEmpty => !iterator.moveNext();
103
104 bool get isNotEmpty => !isEmpty;
105
106 Iterable<E> take(int n) {
107 return new TakeIterable<E>(this, n);
108 }
109
110 Iterable<E> takeWhile(bool test(E value)) {
111 return new TakeWhileIterable<E>(this, test);
112 }
113
114 Iterable<E> skip(int n) {
115 return new SkipIterable<E>(this, n);
116 }
117
118 Iterable<E> skipWhile(bool test(E value)) {
119 return new SkipWhileIterable<E>(this, test);
120 }
121
122 E get first {
123 Iterator<E> it = iterator;
124 if (!it.moveNext()) {
125 throw IterableElementError.noElement();
126 }
127 return it.current;
128 }
129
130 E get last {
131 Iterator<E> it = iterator;
132 if (!it.moveNext()) {
133 throw IterableElementError.noElement();
134 }
135 E result;
136 do {
137 result = it.current;
138 } while(it.moveNext());
139 return result;
140 }
141
142 E get single {
143 Iterator<E> it = iterator;
144 if (!it.moveNext()) throw IterableElementError.noElement();
145 E result = it.current;
146 if (it.moveNext()) throw IterableElementError.tooMany();
147 return result;
148 }
149
150 E firstWhere(bool test(E value), { E orElse() }) {
151 for (E element in this) {
152 if (test(element)) return element;
153 }
154 if (orElse != null) return orElse();
155 throw IterableElementError.noElement();
156 }
157
158 E lastWhere(bool test(E value), { E orElse() }) {
159 E result = null;
160 bool foundMatching = false;
161 for (E element in this) {
162 if (test(element)) {
163 result = element;
164 foundMatching = true;
165 }
166 }
167 if (foundMatching) return result;
168 if (orElse != null) return orElse();
169 throw IterableElementError.noElement();
170 }
171
172 E singleWhere(bool test(E value)) {
173 E result = null;
174 bool foundMatching = false;
175 for (E element in this) {
176 if (test(element)) {
177 if (foundMatching) {
178 throw IterableElementError.tooMany();
179 }
180 result = element;
181 foundMatching = true;
182 }
183 }
184 if (foundMatching) return result;
185 throw IterableElementError.noElement();
186 }
187
188 E elementAt(int index) {
189 if (index is! int) throw new ArgumentError.notNull("index");
190 RangeError.checkNotNegative(index, "index");
191 int elementIndex = 0;
192 for (E element in this) {
193 if (index == elementIndex) return element;
194 elementIndex++;
195 }
196 throw new RangeError.index(index, this, "index", null, elementIndex);
197 }
198
199
200 String toString() => IterableBase.iterableToShortString(this, '(', ')');
201 }
202
203 /**
204 * Base class for implementing [Iterable].
205 *
206 * This class implements all methods of [Iterable] except [Iterable.iterator]
207 * in terms of `iterator`.
208 */
209 abstract class IterableBase<E> implements Iterable<E> {
210 // TODO(lrn): Base this on IterableMixin if there ever becomes a way
211 // to combine const constructors and mixins.
212 const IterableBase();
213
214 Iterable map(f(E element)) => new MappedIterable<E, dynamic>(this, f);
215
216 Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f);
217
218 Iterable expand(Iterable f(E element)) =>
219 new ExpandIterable<E, dynamic>(this, f);
220
221 bool contains(Object element) {
222 for (E e in this) {
223 if (e == element) return true;
224 }
225 return false;
226 }
227
228 void forEach(void f(E element)) {
229 for (E element in this) f(element);
230 }
231
232 E reduce(E combine(E value, E element)) {
233 Iterator<E> iterator = this.iterator;
234 if (!iterator.moveNext()) {
235 throw IterableElementError.noElement();
236 }
237 E value = iterator.current;
238 while (iterator.moveNext()) {
239 value = combine(value, iterator.current);
240 }
241 return value;
242 }
243
244 dynamic fold(var initialValue,
245 dynamic combine(var previousValue, E element)) {
246 var value = initialValue;
247 for (E element in this) value = combine(value, element);
248 return value;
249 }
250
251 bool every(bool f(E element)) {
252 for (E element in this) {
253 if (!f(element)) return false;
254 }
255 return true;
256 }
257
258 String join([String separator = ""]) {
259 Iterator<E> iterator = this.iterator;
260 if (!iterator.moveNext()) return "";
261 StringBuffer buffer = new StringBuffer();
262 if (separator == null || separator == "") {
263 do {
264 buffer.write("${iterator.current}");
265 } while (iterator.moveNext());
266 } else {
267 buffer.write("${iterator.current}");
268 while (iterator.moveNext()) {
269 buffer.write(separator);
270 buffer.write("${iterator.current}");
271 }
272 }
273 return buffer.toString();
274 }
275
276 bool any(bool f(E element)) {
277 for (E element in this) {
278 if (f(element)) return true;
279 }
280 return false;
281 }
282
283 List<E> toList({ bool growable: true }) =>
284 new List<E>.from(this, growable: growable);
285
286 Set<E> toSet() => new Set<E>.from(this);
287
288 int get length {
289 assert(this is! EfficientLength);
290 int count = 0;
291 Iterator<E> it = iterator;
292 while (it.moveNext()) {
293 count++;
294 }
295 return count;
296 }
297
298 bool get isEmpty => !iterator.moveNext();
299
300 bool get isNotEmpty => !isEmpty;
301
302 Iterable<E> take(int n) {
303 return new TakeIterable<E>(this, n);
304 }
305
306 Iterable<E> takeWhile(bool test(E value)) {
307 return new TakeWhileIterable<E>(this, test);
308 }
309
310 Iterable<E> skip(int n) {
311 return new SkipIterable<E>(this, n);
312 }
313
314 Iterable<E> skipWhile(bool test(E value)) {
315 return new SkipWhileIterable<E>(this, test);
316 }
317
318 E get first {
319 Iterator<E> it = iterator;
320 if (!it.moveNext()) {
321 throw IterableElementError.noElement();
322 }
323 return it.current;
324 }
325
326 E get last {
327 Iterator<E> it = iterator;
328 if (!it.moveNext()) {
329 throw IterableElementError.noElement();
330 }
331 E result;
332 do {
333 result = it.current;
334 } while(it.moveNext());
335 return result;
336 }
337
338 E get single {
339 Iterator<E> it = iterator;
340 if (!it.moveNext()) throw IterableElementError.noElement();
341 E result = it.current;
342 if (it.moveNext()) throw IterableElementError.tooMany();
343 return result;
344 }
345
346 E firstWhere(bool test(E value), { E orElse() }) {
347 for (E element in this) {
348 if (test(element)) return element;
349 }
350 if (orElse != null) return orElse();
351 throw IterableElementError.noElement();
352 }
353
354 E lastWhere(bool test(E value), { E orElse() }) {
355 E result = null;
356 bool foundMatching = false;
357 for (E element in this) {
358 if (test(element)) {
359 result = element;
360 foundMatching = true;
361 }
362 }
363 if (foundMatching) return result;
364 if (orElse != null) return orElse();
365 throw IterableElementError.noElement();
366 }
367
368 E singleWhere(bool test(E value)) {
369 E result = null;
370 bool foundMatching = false;
371 for (E element in this) {
372 if (test(element)) {
373 if (foundMatching) {
374 throw IterableElementError.tooMany();
375 }
376 result = element;
377 foundMatching = true;
378 }
379 }
380 if (foundMatching) return result;
381 throw IterableElementError.noElement();
382 }
383
384 E elementAt(int index) {
385 if (index is! int) throw new ArgumentError.notNull("index");
386 RangeError.checkNotNegative(index, "index");
387 int elementIndex = 0;
388 for (E element in this) {
389 if (index == elementIndex) return element;
390 elementIndex++;
391 }
392 throw new RangeError.index(index, this, "index", null, elementIndex);
393 }
394
395 /**
396 * Returns a string representation of (some of) the elements of `this`.
397 *
398 * Elements are represented by their own `toString` results.
399 *
400 * The representation always contains the first three elements.
401 * If there are less than a hundred elements in the iterable, it also
402 * contains the last two elements.
403 *
404 * If the resulting string isn't above 80 characters, more elements are
405 * included from the start of the iterable.
406 *
407 * The conversion may omit calling `toString` on some elements if they
408 * are known to not occur in the output, and it may stop iterating after
409 * a hundred elements.
410 */
411 String toString() => iterableToShortString(this, '(', ')');
412
413 /**
414 * Convert an `Iterable` to a string like [IterableBase.toString].
415 *
416 * Allows using other delimiters than '(' and ')'.
417 *
418 * Handles circular references where converting one of the elements
419 * to a string ends up converting [iterable] to a string again.
420 */
421 static String iterableToShortString(Iterable iterable,
422 [String leftDelimiter = '(',
423 String rightDelimiter = ')']) {
424 if (_isToStringVisiting(iterable)) {
425 if (leftDelimiter == "(" && rightDelimiter == ")") {
426 // Avoid creating a new string in the "common" case.
427 return "(...)";
428 }
429 return "$leftDelimiter...$rightDelimiter";
430 }
431 List parts = [];
432 _toStringVisiting.add(iterable);
433 try {
434 _iterablePartsToStrings(iterable, parts);
435 } finally {
436 assert(identical(_toStringVisiting.last, iterable));
437 _toStringVisiting.removeLast();
438 }
439 return (new StringBuffer(leftDelimiter)
440 ..writeAll(parts, ", ")
441 ..write(rightDelimiter)).toString();
442 }
443
444 /**
445 * Converts an `Iterable` to a string.
446 *
447 * Converts each elements to a string, and separates the results by ", ".
448 * Then wraps the result in [leftDelimiter] and [rightDelimiter].
449 *
450 * Unlike [iterableToShortString], this conversion doesn't omit any
451 * elements or puts any limit on the size of the result.
452 *
453 * Handles circular references where converting one of the elements
454 * to a string ends up converting [iterable] to a string again.
455 */
456 static String iterableToFullString(Iterable iterable,
457 [String leftDelimiter = '(',
458 String rightDelimiter = ')']) {
459 if (_isToStringVisiting(iterable)) {
460 return "$leftDelimiter...$rightDelimiter";
461 }
462 StringBuffer buffer = new StringBuffer(leftDelimiter);
463 _toStringVisiting.add(iterable);
464 try {
465 buffer.writeAll(iterable, ", ");
466 } finally {
467 assert(identical(_toStringVisiting.last, iterable));
468 _toStringVisiting.removeLast();
469 }
470 buffer.write(rightDelimiter);
471 return buffer.toString();
472 }
473
474 /** A set used to identify cyclic lists during toString() calls. */
475 static final List _toStringVisiting = [];
476
477 /** Check if we are currently visiting `o` in a toString call. */
478 static bool _isToStringVisiting(Object o) {
479 for (int i = 0; i < _toStringVisiting.length; i++) {
480 if (identical(o, _toStringVisiting[i])) return true;
481 }
482 return false;
483 }
484
485 /**
486 * Convert elments of [iterable] to strings and store them in [parts].
487 */
488 static void _iterablePartsToStrings(Iterable iterable, List parts) {
489 /*
490 * This is the complicated part of [iterableToShortString].
491 * It is extracted as a separate function to avoid having too much code
492 * inside the try/finally.
493 */
494 /// Try to stay below this many characters.
495 const int LENGTH_LIMIT = 80;
496 /// Always at least this many elements at the start.
497 const int HEAD_COUNT = 3;
498 /// Always at least this many elements at the end.
499 const int TAIL_COUNT = 2;
500 /// Stop iterating after this many elements. Iterables can be infinite.
501 const int MAX_COUNT = 100;
502 // Per entry length overhead. It's for ", " for all after the first entry,
503 // and for "(" and ")" for the initial entry. By pure luck, that's the same
504 // number.
505 const int OVERHEAD = 2;
506 const int ELLIPSIS_SIZE = 3; // "...".length.
507
508 int length = 0;
509 int count = 0;
510 Iterator it = iterable.iterator;
511 // Initial run of elements, at least HEAD_COUNT, and then continue until
512 // passing at most LENGTH_LIMIT characters.
513 while (length < LENGTH_LIMIT || count < HEAD_COUNT) {
514 if (!it.moveNext()) return;
515 String next = "${it.current}";
516 parts.add(next);
517 length += next.length + OVERHEAD;
518 count++;
519 }
520
521 String penultimateString;
522 String ultimateString;
523
524 // Find last two elements. One or more of them may already be in the
525 // parts array. Include their length in `length`.
526 var penultimate = null;
527 var ultimate = null;
528 if (!it.moveNext()) {
529 if (count <= HEAD_COUNT + TAIL_COUNT) return;
530 ultimateString = parts.removeLast();
531 penultimateString = parts.removeLast();
532 } else {
533 penultimate = it.current;
534 count++;
535 if (!it.moveNext()) {
536 if (count <= HEAD_COUNT + 1) {
537 parts.add("$penultimate");
538 return;
539 }
540 ultimateString = "$penultimate";
541 penultimateString = parts.removeLast();
542 length += ultimateString.length + OVERHEAD;
543 } else {
544 ultimate = it.current;
545 count++;
546 // Then keep looping, keeping the last two elements in variables.
547 assert(count < MAX_COUNT);
548 while (it.moveNext()) {
549 penultimate = ultimate;
550 ultimate = it.current;
551 count++;
552 if (count > MAX_COUNT) {
553 // If we haven't found the end before MAX_COUNT, give up.
554 // This cannot happen in the code above because each entry
555 // increases length by at least two, so there is no way to
556 // visit more than ~40 elements before this loop.
557
558 // Remove any surplus elements until length, including ", ...)",
559 // is at most LENGTH_LIMIT.
560 while (length > LENGTH_LIMIT - ELLIPSIS_SIZE - OVERHEAD &&
561 count > HEAD_COUNT) {
562 length -= parts.removeLast().length + OVERHEAD;
563 count--;
564 }
565 parts.add("...");
566 return;
567 }
568 }
569 penultimateString = "$penultimate";
570 ultimateString = "$ultimate";
571 length +=
572 ultimateString.length + penultimateString.length + 2 * OVERHEAD;
573 }
574 }
575
576 // If there is a gap between the initial run and the last two,
577 // prepare to add an ellipsis.
578 String elision = null;
579 if (count > parts.length + TAIL_COUNT) {
580 elision = "...";
581 length += ELLIPSIS_SIZE + OVERHEAD;
582 }
583
584 // If the last two elements were very long, and we have more than
585 // HEAD_COUNT elements in the initial run, drop some to make room for
586 // the last two.
587 while (length > LENGTH_LIMIT && parts.length > HEAD_COUNT) {
588 length -= parts.removeLast().length + OVERHEAD;
589 if (elision == null) {
590 elision = "...";
591 length += ELLIPSIS_SIZE + OVERHEAD;
592 }
593 }
594 if (elision != null) {
595 parts.add(elision);
596 }
597 parts.add(penultimateString);
598 parts.add(ultimateString);
599 }
600 }
OLDNEW
« no previous file with comments | « test/generated_sdk/lib/collection/hash_set.dart ('k') | test/generated_sdk/lib/collection/iterator.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698