OLD | NEW |
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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 } | 83 } |
84 return false; | 84 return false; |
85 } | 85 } |
86 | 86 |
87 List<E> toList({ bool growable: true }) => | 87 List<E> toList({ bool growable: true }) => |
88 new List<E>.from(this, growable: growable); | 88 new List<E>.from(this, growable: growable); |
89 | 89 |
90 Set<E> toSet() => new Set<E>.from(this); | 90 Set<E> toSet() => new Set<E>.from(this); |
91 | 91 |
92 int get length { | 92 int get length { |
93 assert(this is! EfficientLengthIterable); | 93 assert(this is! EfficientLength); |
94 int count = 0; | 94 int count = 0; |
95 Iterator it = iterator; | 95 Iterator it = iterator; |
96 while (it.moveNext()) { | 96 while (it.moveNext()) { |
97 count++; | 97 count++; |
98 } | 98 } |
99 return count; | 99 return count; |
100 } | 100 } |
101 | 101 |
102 bool get isEmpty => !iterator.moveNext(); | 102 bool get isEmpty => !iterator.moveNext(); |
103 | 103 |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 elision = "..."; | 390 elision = "..."; |
391 length += ELLIPSIS_SIZE + OVERHEAD; | 391 length += ELLIPSIS_SIZE + OVERHEAD; |
392 } | 392 } |
393 } | 393 } |
394 if (elision != null) { | 394 if (elision != null) { |
395 parts.add(elision); | 395 parts.add(elision); |
396 } | 396 } |
397 parts.add(penultimateString); | 397 parts.add(penultimateString); |
398 parts.add(ultimateString); | 398 parts.add(ultimateString); |
399 } | 399 } |
400 | |
401 | |
402 /** | |
403 * Marker interface for [Iterable] implementations that have an efficient | |
404 * [length] getter. | |
405 * | |
406 * An iterable implementing this interface should have an "efficient" | |
407 * implementation of `length` that doesn't trigger iteration of the | |
408 * iterable. Being efficient doesn't necessarily require being constant | |
409 * time, but should at least attempt to have have execution time that is | |
410 * better than linear in the elements of the iterable. | |
411 * | |
412 * Methods that worry about reading the length of an `Iterable` because it | |
413 * may be inefficient or may trigger side-effects, or may even never complete, | |
414 * can first check if the iterable `is EfficientLengthIterable`, | |
415 * and if so, use [length] without those concerns. | |
416 * | |
417 * The `EfficientLengthIterable` type should never be used as a type | |
418 * assertion - neither as argument type or return type of a function. | |
419 * Always use [Iterable] for the type and just document the performance if it | |
420 * is relevant. This avoids needlessly restricting the values that can be used. | |
421 */ | |
422 abstract class EfficientLengthIterable<T> implements Iterable<T> { | |
423 /** | |
424 * Returns the number of elements in the iterable. | |
425 * | |
426 * This is an efficient operation that doesn't iterate through | |
427 * the elements. | |
428 */ | |
429 int get length; | |
430 } | |
OLD | NEW |