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

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

Issue 2754013002: Format all dart: library files (Closed)
Patch Set: Created 3 years, 9 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
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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 106
107 Set<E> difference(Set<Object> other) { 107 Set<E> difference(Set<Object> other) {
108 Set<E> result = toSet(); 108 Set<E> result = toSet();
109 for (E element in this) { 109 for (E element in this) {
110 if (other.contains(element)) result.remove(element); 110 if (other.contains(element)) result.remove(element);
111 } 111 }
112 return result; 112 return result;
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 =
117 : new List<E>(length); 117 growable ? (new List<E>()..length = length) : 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)) => new EfficientLengthMappedIterable<E, T>(
124 new EfficientLengthMappedIterable<E, T>(this, f); 124 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>
142 new ExpandIterable<E, T>(this, f); 142 expand<T>(Iterable<T> f(E element)) => 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 T fold<T>(T initialValue, 160 T fold<T>(T initialValue, T combine(T previousValue, E element)) {
161 T combine(T previousValue, E element)) {
162 var value = initialValue; 161 var value = initialValue;
163 for (E element in this) value = combine(value, element); 162 for (E element in this) value = combine(value, element);
164 return value; 163 return value;
165 } 164 }
166 165
167 bool every(bool f(E element)) { 166 bool every(bool f(E element)) {
168 for (E element in this) { 167 for (E element in this) {
169 if (!f(element)) return false; 168 if (!f(element)) return false;
170 } 169 }
171 return true; 170 return true;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 } 220 }
222 221
223 E get last { 222 E get last {
224 Iterator<E> it = iterator; 223 Iterator<E> it = iterator;
225 if (!it.moveNext()) { 224 if (!it.moveNext()) {
226 throw IterableElementError.noElement(); 225 throw IterableElementError.noElement();
227 } 226 }
228 E result; 227 E result;
229 do { 228 do {
230 result = it.current; 229 result = it.current;
231 } while(it.moveNext()); 230 } while (it.moveNext());
232 return result; 231 return result;
233 } 232 }
234 233
235 E firstWhere(bool test(E value), { E orElse() }) { 234 E firstWhere(bool test(E value), {E orElse()}) {
236 for (E element in this) { 235 for (E element in this) {
237 if (test(element)) return element; 236 if (test(element)) return element;
238 } 237 }
239 if (orElse != null) return orElse(); 238 if (orElse != null) return orElse();
240 throw IterableElementError.noElement(); 239 throw IterableElementError.noElement();
241 } 240 }
242 241
243 E lastWhere(bool test(E value), { E orElse() }) { 242 E lastWhere(bool test(E value), {E orElse()}) {
244 E result = null; 243 E result = null;
245 bool foundMatching = false; 244 bool foundMatching = false;
246 for (E element in this) { 245 for (E element in this) {
247 if (test(element)) { 246 if (test(element)) {
248 result = element; 247 result = element;
249 foundMatching = true; 248 foundMatching = true;
250 } 249 }
251 } 250 }
252 if (foundMatching) return result; 251 if (foundMatching) return result;
253 if (orElse != null) return orElse(); 252 if (orElse != null) return orElse();
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 abstract class SetBase<E> extends SetMixin<E> { 301 abstract class SetBase<E> extends SetMixin<E> {
303 /** 302 /**
304 * Convert a `Set` to a string as `{each, element, as, string}`. 303 * Convert a `Set` to a string as `{each, element, as, string}`.
305 * 304 *
306 * Handles circular references where converting one of the elements 305 * Handles circular references where converting one of the elements
307 * to a string ends up converting [set] to a string again. 306 * to a string ends up converting [set] to a string again.
308 */ 307 */
309 static String setToString(Set set) => 308 static String setToString(Set set) =>
310 IterableBase.iterableToFullString(set, '{', '}'); 309 IterableBase.iterableToFullString(set, '{', '}');
311 } 310 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698