OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 /** | 5 /** |
6 * Base implementations of [Set]. | 6 * Base implementations of [Set]. |
7 */ | 7 */ |
8 part of dart.collection; | 8 part of dart.collection; |
9 | 9 |
10 /** | 10 /** |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 } | 113 } |
114 | 114 |
115 List<E> toList({bool growable: true}) { | 115 List<E> toList({bool growable: true}) { |
116 List<E> result = growable ? (new List<E>()..length = length) | 116 List<E> result = growable ? (new List<E>()..length = length) |
117 : new List<E>(length); | 117 : new List<E>(length); |
118 int i = 0; | 118 int i = 0; |
119 for (E element in this) result[i++] = element; | 119 for (E element in this) result[i++] = element; |
120 return result; | 120 return result; |
121 } | 121 } |
122 | 122 |
123 Iterable/*<T>*/ map/*<T>*/(/*=T*/f(E element)) => | 123 Iterable<T> map<T>(T f(E element)) => |
124 new EfficientLengthMappedIterable<E, dynamic/*=T*/>(this, f); | 124 new EfficientLengthMappedIterable<E, T>(this, f); |
125 | 125 |
126 E get single { | 126 E get single { |
127 if (length > 1) throw IterableElementError.tooMany(); | 127 if (length > 1) throw IterableElementError.tooMany(); |
128 Iterator<E> it = iterator; | 128 Iterator<E> it = iterator; |
129 if (!it.moveNext()) throw IterableElementError.noElement(); | 129 if (!it.moveNext()) throw IterableElementError.noElement(); |
130 E result = it.current; | 130 E result = it.current; |
131 return result; | 131 return result; |
132 } | 132 } |
133 | 133 |
134 String toString() => IterableBase.iterableToFullString(this, '{', '}'); | 134 String toString() => IterableBase.iterableToFullString(this, '{', '}'); |
135 | 135 |
136 // Copied from IterableMixin. | 136 // Copied from IterableMixin. |
137 // Should be inherited if we had multi-level mixins. | 137 // Should be inherited if we had multi-level mixins. |
138 | 138 |
139 Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f); | 139 Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f); |
140 | 140 |
141 Iterable/*<T>*/ expand/*<T>*/(Iterable/*<T>*/ f(E element)) => | 141 Iterable<T> expand<T>(Iterable<T> f(E element)) => |
142 new ExpandIterable<E, dynamic/*=T*/>(this, f); | 142 new ExpandIterable<E, T>(this, f); |
143 | 143 |
144 void forEach(void f(E element)) { | 144 void forEach(void f(E element)) { |
145 for (E element in this) f(element); | 145 for (E element in this) f(element); |
146 } | 146 } |
147 | 147 |
148 E reduce(E combine(E value, E element)) { | 148 E reduce(E combine(E value, E element)) { |
149 Iterator<E> iterator = this.iterator; | 149 Iterator<E> iterator = this.iterator; |
150 if (!iterator.moveNext()) { | 150 if (!iterator.moveNext()) { |
151 throw IterableElementError.noElement(); | 151 throw IterableElementError.noElement(); |
152 } | 152 } |
153 E value = iterator.current; | 153 E value = iterator.current; |
154 while (iterator.moveNext()) { | 154 while (iterator.moveNext()) { |
155 value = combine(value, iterator.current); | 155 value = combine(value, iterator.current); |
156 } | 156 } |
157 return value; | 157 return value; |
158 } | 158 } |
159 | 159 |
160 dynamic/*=T*/ fold/*<T>*/(var/*=T*/ initialValue, | 160 T fold<T>(T initialValue, |
161 dynamic/*=T*/ combine(var/*=T*/ previousValue, E element)) { | 161 T combine(T previousValue, E element)) { |
162 var value = initialValue; | 162 var value = initialValue; |
163 for (E element in this) value = combine(value, element); | 163 for (E element in this) value = combine(value, element); |
164 return value; | 164 return value; |
165 } | 165 } |
166 | 166 |
167 bool every(bool f(E element)) { | 167 bool every(bool f(E element)) { |
168 for (E element in this) { | 168 for (E element in this) { |
169 if (!f(element)) return false; | 169 if (!f(element)) return false; |
170 } | 170 } |
171 return true; | 171 return true; |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 abstract class SetBase<E> extends SetMixin<E> { | 302 abstract class SetBase<E> extends SetMixin<E> { |
303 /** | 303 /** |
304 * Convert a `Set` to a string as `{each, element, as, string}`. | 304 * Convert a `Set` to a string as `{each, element, as, string}`. |
305 * | 305 * |
306 * Handles circular references where converting one of the elements | 306 * Handles circular references where converting one of the elements |
307 * to a string ends up converting [set] to a string again. | 307 * to a string ends up converting [set] to a string again. |
308 */ | 308 */ |
309 static String setToString(Set set) => | 309 static String setToString(Set set) => |
310 IterableBase.iterableToFullString(set, '{', '}'); | 310 IterableBase.iterableToFullString(set, '{', '}'); |
311 } | 311 } |
OLD | NEW |