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 * The [Collections] class implements static methods useful when | 8 * The [Collections] class implements static methods useful when |
9 * writing a class that implements [Collection] and the [iterator] | 9 * writing a class that implements [Collection] and the [iterator] |
10 * method. | 10 * method. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 if (!it.moveNext()) { | 63 if (!it.moveNext()) { |
64 throw new StateError("No elements"); | 64 throw new StateError("No elements"); |
65 } | 65 } |
66 dynamic result; | 66 dynamic result; |
67 do { | 67 do { |
68 result = it.current; | 68 result = it.current; |
69 } while(it.moveNext()); | 69 } while(it.moveNext()); |
70 return result; | 70 return result; |
71 } | 71 } |
72 | 72 |
73 static dynamic min(Iterable iterable, [int compare(var a, var b)]) { | |
74 if (compare == null) compare = Comparable.compare; | |
75 Iterator it = iterable.iterator; | |
76 if (!it.moveNext()) { | |
77 return null; | |
Anders Johnsen
2013/01/02 11:04:05
State error? [].min() -> null?
Lasse Reichstein Nielsen
2013/01/02 11:49:33
It's deliberate. We can argue whether it's the rig
| |
78 } | |
79 var min = it.current; | |
80 while (it.moveNext()) { | |
81 if (compare(min, it.current) > 0) min = it.current; | |
82 } | |
83 return min; | |
84 } | |
85 | |
86 static dynamic max(Iterable iterable, [int compare(var a, var b)]) { | |
87 if (compare == null) compare = Comparable.compare; | |
88 Iterator it = iterable.iterator; | |
89 if (!it.moveNext()) { | |
90 return null; | |
91 } | |
92 var max = it.current; | |
93 while (it.moveNext()) { | |
94 if (compare(max, it.current) < 0) max = it.current; | |
95 } | |
96 return max; | |
97 } | |
98 | |
73 static dynamic single(Iterable iterable) { | 99 static dynamic single(Iterable iterable) { |
74 Iterator it = iterable.iterator; | 100 Iterator it = iterable.iterator; |
75 if (!it.moveNext()) throw new StateError("No elements"); | 101 if (!it.moveNext()) throw new StateError("No elements"); |
76 dynamic result = it.current; | 102 dynamic result = it.current; |
77 if (it.moveNext()) throw new StateError("More than one element"); | 103 if (it.moveNext()) throw new StateError("More than one element"); |
78 return result; | 104 return result; |
79 } | 105 } |
80 | 106 |
81 static dynamic firstMatching(Iterable iterable, | 107 static dynamic firstMatching(Iterable iterable, |
82 bool test(dynamic value), | 108 bool test(dynamic value), |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
271 * Returns true if the specified collection contains the specified object | 297 * Returns true if the specified collection contains the specified object |
272 * reference. | 298 * reference. |
273 */ | 299 */ |
274 static _containsRef(Collection c, Object ref) { | 300 static _containsRef(Collection c, Object ref) { |
275 for (var e in c) { | 301 for (var e in c) { |
276 if (identical(e, ref)) return true; | 302 if (identical(e, ref)) return true; |
277 } | 303 } |
278 return false; | 304 return false; |
279 } | 305 } |
280 } | 306 } |
OLD | NEW |