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

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

Issue 14246008: Allow Object when doing lookups. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Forgot to upload before committing Created 7 years, 6 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
« no previous file with comments | « sdk/lib/collection/hash_set.dart ('k') | sdk/lib/collection/linked_hash_map.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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`.
11 */ 11 */
12 abstract class IterableMixin<E> implements Iterable<E> { 12 abstract class IterableMixin<E> implements Iterable<E> {
13 Iterable map(f(E element)) => new MappedIterable<E, dynamic>(this, f); 13 Iterable map(f(E element)) => new MappedIterable<E, dynamic>(this, f);
14 14
15 Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f); 15 Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f);
16 16
17 Iterable expand(Iterable f(E element)) => 17 Iterable expand(Iterable f(E element)) =>
18 new ExpandIterable<E, dynamic>(this, f); 18 new ExpandIterable<E, dynamic>(this, f);
19 19
20 bool contains(E element) { 20 bool contains(Object element) {
21 for (E e in this) { 21 for (E e in this) {
22 if (e == element) return true; 22 if (e == element) return true;
23 } 23 }
24 return false; 24 return false;
25 } 25 }
26 26
27 void forEach(void f(E element)) { 27 void forEach(void f(E element)) {
28 for (E element in this) f(element); 28 for (E element in this) f(element);
29 } 29 }
30 30
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 } 134 }
135 135
136 E get single { 136 E get single {
137 Iterator it = iterator; 137 Iterator it = iterator;
138 if (!it.moveNext()) throw new StateError("No elements"); 138 if (!it.moveNext()) throw new StateError("No elements");
139 E result = it.current; 139 E result = it.current;
140 if (it.moveNext()) throw new StateError("More than one element"); 140 if (it.moveNext()) throw new StateError("More than one element");
141 return result; 141 return result;
142 } 142 }
143 143
144 E firstWhere(bool test(E value), { E orElse() }) { 144 dynamic firstWhere(bool test(E value), { Object orElse() }) {
145 // TODO(floitsch): check that arguments are of correct type?
146 for (E element in this) { 145 for (E element in this) {
147 if (test(element)) return element; 146 if (test(element)) return element;
148 } 147 }
149 if (orElse != null) return orElse(); 148 if (orElse != null) return orElse();
150 throw new StateError("No matching element"); 149 throw new StateError("No matching element");
151 } 150 }
152 151
153 E lastWhere(bool test(E value), {E orElse()}) { 152 dynamic lastWhere(bool test(E value), { Object orElse() }) {
154 // TODO(floitsch): check that arguments are of correct type?
155 E result = null; 153 E result = null;
156 bool foundMatching = false; 154 bool foundMatching = false;
157 for (E element in this) { 155 for (E element in this) {
158 if (test(element)) { 156 if (test(element)) {
159 result = element; 157 result = element;
160 foundMatching = true; 158 foundMatching = true;
161 } 159 }
162 } 160 }
163 if (foundMatching) return result; 161 if (foundMatching) return result;
164 if (orElse != null) return orElse(); 162 if (orElse != null) return orElse();
165 throw new StateError("No matching element"); 163 throw new StateError("No matching element");
166 } 164 }
167 165
168 E singleWhere(bool test(E value)) { 166 E singleWhere(bool test(E value)) {
169 // TODO(floitsch): check that argument is of correct type?
170 E result = null; 167 E result = null;
171 bool foundMatching = false; 168 bool foundMatching = false;
172 for (E element in this) { 169 for (E element in this) {
173 if (test(element)) { 170 if (test(element)) {
174 if (foundMatching) { 171 if (foundMatching) {
175 throw new StateError("More than one matching element"); 172 throw new StateError("More than one matching element");
176 } 173 }
177 result = element; 174 result = element;
178 foundMatching = true; 175 foundMatching = true;
179 } 176 }
(...skipping 24 matching lines...) Expand all
204 // to combine const constructors and mixins. 201 // to combine const constructors and mixins.
205 const IterableBase(); 202 const IterableBase();
206 203
207 Iterable map(f(E element)) => new MappedIterable<E, dynamic>(this, f); 204 Iterable map(f(E element)) => new MappedIterable<E, dynamic>(this, f);
208 205
209 Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f); 206 Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f);
210 207
211 Iterable expand(Iterable f(E element)) => 208 Iterable expand(Iterable f(E element)) =>
212 new ExpandIterable<E, dynamic>(this, f); 209 new ExpandIterable<E, dynamic>(this, f);
213 210
214 bool contains(E element) { 211 bool contains(Object element) {
215 for (E e in this) { 212 for (E e in this) {
216 if (e == element) return true; 213 if (e == element) return true;
217 } 214 }
218 return false; 215 return false;
219 } 216 }
220 217
221 void forEach(void f(E element)) { 218 void forEach(void f(E element)) {
222 for (E element in this) f(element); 219 for (E element in this) f(element);
223 } 220 }
224 221
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 } 325 }
329 326
330 E get single { 327 E get single {
331 Iterator it = iterator; 328 Iterator it = iterator;
332 if (!it.moveNext()) throw new StateError("No elements"); 329 if (!it.moveNext()) throw new StateError("No elements");
333 E result = it.current; 330 E result = it.current;
334 if (it.moveNext()) throw new StateError("More than one element"); 331 if (it.moveNext()) throw new StateError("More than one element");
335 return result; 332 return result;
336 } 333 }
337 334
338 E firstWhere(bool test(E value), { E orElse() }) { 335 dynamic firstWhere(bool test(E value), { Object orElse() }) {
339 // TODO(floitsch): check that arguments are of correct type?
340 for (E element in this) { 336 for (E element in this) {
341 if (test(element)) return element; 337 if (test(element)) return element;
342 } 338 }
343 if (orElse != null) return orElse(); 339 if (orElse != null) return orElse();
344 throw new StateError("No matching element"); 340 throw new StateError("No matching element");
345 } 341 }
346 342
347 E lastWhere(bool test(E value), {E orElse()}) { 343 dynamic lastWhere(bool test(E value), { Object orElse() }) {
348 // TODO(floitsch): check that arguments are of correct type?
349 E result = null; 344 E result = null;
350 bool foundMatching = false; 345 bool foundMatching = false;
351 for (E element in this) { 346 for (E element in this) {
352 if (test(element)) { 347 if (test(element)) {
353 result = element; 348 result = element;
354 foundMatching = true; 349 foundMatching = true;
355 } 350 }
356 } 351 }
357 if (foundMatching) return result; 352 if (foundMatching) return result;
358 if (orElse != null) return orElse(); 353 if (orElse != null) return orElse();
359 throw new StateError("No matching element"); 354 throw new StateError("No matching element");
360 } 355 }
361 356
362 E singleWhere(bool test(E value)) { 357 E singleWhere(bool test(E value)) {
363 // TODO(floitsch): check that argument is of correct type?
364 E result = null; 358 E result = null;
365 bool foundMatching = false; 359 bool foundMatching = false;
366 for (E element in this) { 360 for (E element in this) {
367 if (test(element)) { 361 if (test(element)) {
368 if (foundMatching) { 362 if (foundMatching) {
369 throw new StateError("More than one matching element"); 363 throw new StateError("More than one matching element");
370 } 364 }
371 result = element; 365 result = element;
372 foundMatching = true; 366 foundMatching = true;
373 } 367 }
374 } 368 }
375 if (foundMatching) return result; 369 if (foundMatching) return result;
376 throw new StateError("No matching element"); 370 throw new StateError("No matching element");
377 } 371 }
378 372
379 E elementAt(int index) { 373 E elementAt(int index) {
380 if (index is! int || index < 0) throw new RangeError.value(index); 374 if (index is! int || index < 0) throw new RangeError.value(index);
381 int remaining = index; 375 int remaining = index;
382 for (E element in this) { 376 for (E element in this) {
383 if (remaining == 0) return element; 377 if (remaining == 0) return element;
384 remaining--; 378 remaining--;
385 } 379 }
386 throw new RangeError.value(index); 380 throw new RangeError.value(index);
387 } 381 }
388 } 382 }
OLDNEW
« no previous file with comments | « sdk/lib/collection/hash_set.dart ('k') | sdk/lib/collection/linked_hash_map.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698