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

Side by Side Diff: sdk/lib/collection/iterable.dart

Issue 1025573005: Move implementation from IterableBase to Iterable. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove extra "static" on helper functions. Created 5 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.collection; 5 part of dart.collection;
6 6
7 /** 7 /**
8 * This [Iterable] mixin implements all [Iterable] members except `iterator`. 8 * This [Iterable] mixin implements all [Iterable] members except `iterator`.
9 * 9 *
10 * All other methods are implemented in terms of `iterator`. 10 * All other methods are implemented in terms of `iterator`.
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 199
200 String toString() => IterableBase.iterableToShortString(this, '(', ')'); 200 String toString() => IterableBase.iterableToShortString(this, '(', ')');
201 } 201 }
202 202
203 /** 203 /**
204 * Base class for implementing [Iterable]. 204 * Base class for implementing [Iterable].
205 * 205 *
206 * This class implements all methods of [Iterable] except [Iterable.iterator] 206 * This class implements all methods of [Iterable] except [Iterable.iterator]
207 * in terms of `iterator`. 207 * in terms of `iterator`.
208 */ 208 */
209 abstract class IterableBase<E> implements Iterable<E> { 209 abstract class IterableBase<E> extends 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(); 210 const IterableBase();
213 211
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 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 it = iterator;
320 if (!it.moveNext()) {
321 throw IterableElementError.noElement();
322 }
323 return it.current;
324 }
325
326 E get last {
327 Iterator 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 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 /** 212 /**
414 * Convert an `Iterable` to a string like [IterableBase.toString]. 213 * Convert an `Iterable` to a string like [IterableBase.toString].
415 * 214 *
416 * Allows using other delimiters than '(' and ')'. 215 * Allows using other delimiters than '(' and ')'.
417 * 216 *
418 * Handles circular references where converting one of the elements 217 * Handles circular references where converting one of the elements
419 * to a string ends up converting [iterable] to a string again. 218 * to a string ends up converting [iterable] to a string again.
420 */ 219 */
421 static String iterableToShortString(Iterable iterable, 220 static String iterableToShortString(Iterable iterable,
422 [String leftDelimiter = '(', 221 [String leftDelimiter = '(',
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 _toStringVisiting.add(iterable); 262 _toStringVisiting.add(iterable);
464 try { 263 try {
465 buffer.writeAll(iterable, ", "); 264 buffer.writeAll(iterable, ", ");
466 } finally { 265 } finally {
467 assert(identical(_toStringVisiting.last, iterable)); 266 assert(identical(_toStringVisiting.last, iterable));
468 _toStringVisiting.removeLast(); 267 _toStringVisiting.removeLast();
469 } 268 }
470 buffer.write(rightDelimiter); 269 buffer.write(rightDelimiter);
471 return buffer.toString(); 270 return buffer.toString();
472 } 271 }
272 }
473 273
474 /** A set used to identify cyclic lists during toString() calls. */ 274 /** A set used to identify cyclic lists during toString() calls. */
475 static final List _toStringVisiting = []; 275 final List _toStringVisiting = [];
476 276
477 /** Check if we are currently visiting `o` in a toString call. */ 277 /** Check if we are currently visiting `o` in a toString call. */
478 static bool _isToStringVisiting(Object o) { 278 bool _isToStringVisiting(Object o) {
479 for (int i = 0; i < _toStringVisiting.length; i++) { 279 for (int i = 0; i < _toStringVisiting.length; i++) {
480 if (identical(o, _toStringVisiting[i])) return true; 280 if (identical(o, _toStringVisiting[i])) return true;
481 } 281 }
482 return false; 282 return false;
283 }
284
285 /**
286 * Convert elments of [iterable] to strings and store them in [parts].
287 */
288 void _iterablePartsToStrings(Iterable iterable, List parts) {
289 /*
290 * This is the complicated part of [iterableToShortString].
291 * It is extracted as a separate function to avoid having too much code
292 * inside the try/finally.
293 */
294 /// Try to stay below this many characters.
295 const int LENGTH_LIMIT = 80;
296 /// Always at least this many elements at the start.
297 const int HEAD_COUNT = 3;
298 /// Always at least this many elements at the end.
299 const int TAIL_COUNT = 2;
300 /// Stop iterating after this many elements. Iterables can be infinite.
301 const int MAX_COUNT = 100;
302 // Per entry length overhead. It's for ", " for all after the first entry,
303 // and for "(" and ")" for the initial entry. By pure luck, that's the same
304 // number.
305 const int OVERHEAD = 2;
306 const int ELLIPSIS_SIZE = 3; // "...".length.
307
308 int length = 0;
309 int count = 0;
310 Iterator it = iterable.iterator;
311 // Initial run of elements, at least HEAD_COUNT, and then continue until
312 // passing at most LENGTH_LIMIT characters.
313 while (length < LENGTH_LIMIT || count < HEAD_COUNT) {
314 if (!it.moveNext()) return;
315 String next = "${it.current}";
316 parts.add(next);
317 length += next.length + OVERHEAD;
318 count++;
483 } 319 }
484 320
485 /** 321 String penultimateString;
486 * Convert elments of [iterable] to strings and store them in [parts]. 322 String ultimateString;
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 323
508 int length = 0; 324 // Find last two elements. One or more of them may already be in the
509 int count = 0; 325 // parts array. Include their length in `length`.
510 Iterator it = iterable.iterator; 326 var penultimate = null;
511 // Initial run of elements, at least HEAD_COUNT, and then continue until 327 var ultimate = null;
512 // passing at most LENGTH_LIMIT characters. 328 if (!it.moveNext()) {
513 while (length < LENGTH_LIMIT || count < HEAD_COUNT) { 329 if (count <= HEAD_COUNT + TAIL_COUNT) return;
514 if (!it.moveNext()) return; 330 ultimateString = parts.removeLast();
515 String next = "${it.current}"; 331 penultimateString = parts.removeLast();
516 parts.add(next); 332 } else {
517 length += next.length + OVERHEAD; 333 penultimate = it.current;
334 count++;
335 if (!it.moveNext()) {
336 if (count <= HEAD_COUNT + 1) {
337 parts.add("$penultimate");
338 return;
339 }
340 ultimateString = "$penultimate";
341 penultimateString = parts.removeLast();
342 length += ultimateString.length + OVERHEAD;
343 } else {
344 ultimate = it.current;
518 count++; 345 count++;
519 } 346 // Then keep looping, keeping the last two elements in variables.
347 assert(count < MAX_COUNT);
348 while (it.moveNext()) {
349 penultimate = ultimate;
350 ultimate = it.current;
351 count++;
352 if (count > MAX_COUNT) {
353 // If we haven't found the end before MAX_COUNT, give up.
354 // This cannot happen in the code above because each entry
355 // increases length by at least two, so there is no way to
356 // visit more than ~40 elements before this loop.
520 357
521 String penultimateString; 358 // Remove any surplus elements until length, including ", ...)",
522 String ultimateString; 359 // is at most LENGTH_LIMIT.
523 360 while (length > LENGTH_LIMIT - ELLIPSIS_SIZE - OVERHEAD &&
524 // Find last two elements. One or more of them may already be in the 361 count > HEAD_COUNT) {
525 // parts array. Include their length in `length`. 362 length -= parts.removeLast().length + OVERHEAD;
526 var penultimate = null; 363 count--;
527 var ultimate = null; 364 }
528 if (!it.moveNext()) { 365 parts.add("...");
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; 366 return;
539 } 367 }
540 ultimateString = "$penultimate"; 368 }
541 penultimateString = parts.removeLast(); 369 penultimateString = "$penultimate";
542 length += ultimateString.length + OVERHEAD; 370 ultimateString = "$ultimate";
543 } else { 371 length +=
544 ultimate = it.current; 372 ultimateString.length + penultimateString.length + 2 * OVERHEAD;
545 count++; 373 }
546 // Then keep looping, keeping the last two elements in variables. 374 }
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 375
558 // Remove any surplus elements until length, including ", ...)", 376 // If there is a gap between the initial run and the last two,
559 // is at most LENGTH_LIMIT. 377 // prepare to add an ellipsis.
560 while (length > LENGTH_LIMIT - ELLIPSIS_SIZE - OVERHEAD && 378 String elision = null;
561 count > HEAD_COUNT) { 379 if (count > parts.length + TAIL_COUNT) {
562 length -= parts.removeLast().length + OVERHEAD; 380 elision = "...";
563 count--; 381 length += ELLIPSIS_SIZE + OVERHEAD;
564 } 382 }
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 383
576 // If there is a gap between the initial run and the last two, 384 // If the last two elements were very long, and we have more than
577 // prepare to add an ellipsis. 385 // HEAD_COUNT elements in the initial run, drop some to make room for
578 String elision = null; 386 // the last two.
579 if (count > parts.length + TAIL_COUNT) { 387 while (length > LENGTH_LIMIT && parts.length > HEAD_COUNT) {
388 length -= parts.removeLast().length + OVERHEAD;
389 if (elision == null) {
580 elision = "..."; 390 elision = "...";
581 length += ELLIPSIS_SIZE + OVERHEAD; 391 length += ELLIPSIS_SIZE + OVERHEAD;
582 } 392 }
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 } 393 }
394 if (elision != null) {
395 parts.add(elision);
396 }
397 parts.add(penultimateString);
398 parts.add(ultimateString);
600 } 399 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/compiler/js_lib/linked_hash_map.dart ('k') | sdk/lib/collection/linked_list.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698