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 class provides default implementations for Iterables (including Lists). | 8 * This class provides default implementations for Iterables (including Lists). |
9 * | 9 * |
10 * Once Dart receives Mixins it will be replaced with mixin classes. | 10 * Once Dart receives Mixins it will be replaced with mixin classes. |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 | 311 |
312 static Iterable where(Iterable iterable, bool f(var element)) { | 312 static Iterable where(Iterable iterable, bool f(var element)) { |
313 return new WhereIterable(iterable, f); | 313 return new WhereIterable(iterable, f); |
314 } | 314 } |
315 | 315 |
316 static Iterable map(Iterable iterable, f(var element)) { | 316 static Iterable map(Iterable iterable, f(var element)) { |
317 return new MappedIterable(iterable, f); | 317 return new MappedIterable(iterable, f); |
318 } | 318 } |
319 | 319 |
320 static Iterable mapList(List list, f(var element)) { | 320 static Iterable mapList(List list, f(var element)) { |
321 return new MappedListIterable(list, f, 0, null); | 321 return new MappedListIterable(list, f); |
322 } | |
323 | |
324 static List mappedByList(List list, f(var element)) { | |
325 // This is currently a List as well as an Iterable. | |
326 return new MappedList(list, f); | |
327 } | 322 } |
328 | 323 |
329 static Iterable expand(Iterable iterable, Iterable f(var element)) { | 324 static Iterable expand(Iterable iterable, Iterable f(var element)) { |
330 return new ExpandIterable(iterable, f); | 325 return new ExpandIterable(iterable, f); |
331 } | 326 } |
332 | 327 |
333 static Iterable takeList(List list, int n) { | 328 static Iterable takeList(List list, int n) { |
334 // The generic type is currently lost. It will be fixed with mixins. | 329 // The generic type is currently lost. It will be fixed with mixins. |
335 // This is currently a List as well as an Iterable. | 330 // This is currently a List as well as an Iterable. |
336 return new ListView(list, 0, n); | 331 return new SubListIterable(list, 0, n); |
337 } | 332 } |
338 | 333 |
339 static Iterable takeWhile(Iterable iterable, bool test(var value)) { | 334 static Iterable takeWhile(Iterable iterable, bool test(var value)) { |
340 // The generic type is currently lost. It will be fixed with mixins. | 335 // The generic type is currently lost. It will be fixed with mixins. |
341 return new TakeWhileIterable(iterable, test); | 336 return new TakeWhileIterable(iterable, test); |
342 } | 337 } |
343 | 338 |
344 static Iterable skipList(List list, int n) { | 339 static Iterable skipList(List list, int n) { |
345 // The generic type is currently lost. It will be fixed with mixins. | 340 // The generic type is currently lost. It will be fixed with mixins. |
346 // This is currently a List as well as an Iterable. | 341 // This is currently a List as well as an Iterable. |
347 return new ListView(list, n, null); | 342 return new SubListIterable(list, n, null); |
348 } | 343 } |
349 | 344 |
350 static Iterable skipWhile(Iterable iterable, bool test(var value)) { | 345 static Iterable skipWhile(Iterable iterable, bool test(var value)) { |
351 // The generic type is currently lost. It will be fixed with mixins. | 346 // The generic type is currently lost. It will be fixed with mixins. |
352 return new SkipWhileIterable(iterable, test); | 347 return new SkipWhileIterable(iterable, test); |
353 } | 348 } |
354 | 349 |
355 static List reversedList(List l) { | 350 static Iterable reversedList(List l) { |
356 return new ReversedListView(l, 0, null); | 351 return new ReversedListIterable(l); |
357 } | 352 } |
358 | 353 |
359 static void sortList(List l, int compare(a, b)) { | 354 static void sortList(List l, int compare(a, b)) { |
360 if (compare == null) compare = Comparable.compare; | 355 if (compare == null) compare = Comparable.compare; |
361 Sort.sort(l, compare); | 356 Sort.sort(l, compare); |
362 } | 357 } |
363 } | 358 } |
364 | 359 |
365 /** | |
366 * The [Collections] class implements static methods useful when | |
367 * writing a class that implements [Collection] and the [iterator] | |
368 * method. | |
369 */ | |
370 class Collections { | 360 class Collections { |
371 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
372 @deprecated | |
373 static bool contains(Iterable iterable, var element) | |
374 => IterableMixinWorkaround.contains(iterable, element); | |
375 | |
376 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
377 @deprecated | |
378 static void forEach(Iterable iterable, void f(o)) { | |
379 IterableMixinWorkaround.forEach(iterable, f); | |
380 } | |
381 | |
382 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
383 @deprecated | |
384 static bool any(Iterable iterable, bool f(o)) | |
385 => IterableMixinWorkaround.any(iterable, f); | |
386 | |
387 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
388 @deprecated | |
389 static bool every(Iterable iterable, bool f(o)) | |
390 => IterableMixinWorkaround.every(iterable, f); | |
391 | |
392 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
393 @deprecated | |
394 static dynamic reduce(Iterable iterable, | |
395 dynamic initialValue, | |
396 dynamic combine(dynamic previousValue, element)) | |
397 => IterableMixinWorkaround.reduce(iterable, initialValue, combine); | |
398 | |
399 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
400 @deprecated | |
401 static bool isEmpty(Iterable iterable) | |
402 => IterableMixinWorkaround.isEmpty(iterable); | |
403 | |
404 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
405 @deprecated | |
406 static dynamic first(Iterable iterable) | |
407 => IterableMixinWorkaround.first(iterable); | |
408 | |
409 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
410 @deprecated | |
411 static dynamic last(Iterable iterable) | |
412 => IterableMixinWorkaround.last(iterable); | |
413 | |
414 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
415 @deprecated | |
416 static dynamic min(Iterable iterable, [int compare(var a, var b)]) | |
417 => IterableMixinWorkaround.min(iterable, compare); | |
418 | |
419 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
420 @deprecated | |
421 static dynamic max(Iterable iterable, [int compare(var a, var b)]) | |
422 => IterableMixinWorkaround.max(iterable, compare); | |
423 | |
424 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
425 @deprecated | |
426 static dynamic single(Iterable iterable) | |
427 => IterableMixinWorkaround.single(iterable); | |
428 | |
429 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
430 @deprecated | |
431 static dynamic firstMatching(Iterable iterable, | |
432 bool test(dynamic value), | |
433 dynamic orElse()) | |
434 => IterableMixinWorkaround.firstMatching(iterable, test, orElse); | |
435 | |
436 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
437 @deprecated | |
438 static dynamic lastMatching(Iterable iterable, | |
439 bool test(dynamic value), | |
440 dynamic orElse()) | |
441 => IterableMixinWorkaround.lastMatching(iterable, test, orElse); | |
442 | |
443 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
444 @deprecated | |
445 static dynamic lastMatchingInList(List list, | |
446 bool test(dynamic value), | |
447 dynamic orElse()) | |
448 => IterableMixinWorkaround.lastMatchingInList(list, test, orElse); | |
449 | |
450 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
451 @deprecated | |
452 static dynamic singleMatching(Iterable iterable, bool test(dynamic value)) | |
453 => IterableMixinWorkaround.singleMatching(iterable, test); | |
454 | |
455 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
456 @deprecated | |
457 static dynamic elementAt(Iterable iterable, int index) | |
458 => IterableMixinWorkaround.elementAt(iterable, index); | |
459 | |
460 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
461 @deprecated | |
462 static String join(Iterable iterable, [String separator]) | |
463 => IterableMixinWorkaround.join(iterable, separator); | |
464 | |
465 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
466 @deprecated | |
467 static String joinList(List list, [String separator]) | |
468 => IterableMixinWorkaround.joinList(list, separator); | |
469 | |
470 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
471 @deprecated | |
472 static Iterable where(Iterable iterable, bool f(var element)) | |
473 => IterableMixinWorkaround.where(iterable, f); | |
474 | |
475 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
476 @deprecated | |
477 static List mappedByList(List list, f(var element)) | |
478 => IterableMixinWorkaround.mappedByList(list, f); | |
479 | |
480 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
481 @deprecated | |
482 static Iterable takeList(List list, int n) | |
483 => IterableMixinWorkaround.takeList(list, n); | |
484 | |
485 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
486 @deprecated | |
487 static Iterable takeWhile(Iterable iterable, bool test(var value)) | |
488 => IterableMixinWorkaround.takeWhile(iterable, test); | |
489 | |
490 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
491 @deprecated | |
492 static Iterable skipList(List list, int n) | |
493 => IterableMixinWorkaround.skipList(list, n); | |
494 | |
495 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
496 @deprecated | |
497 static Iterable skipWhile(Iterable iterable, bool test(var value)) | |
498 => IterableMixinWorkaround.skipWhile(iterable, test); | |
499 | |
500 static String collectionToString(Collection c) | 361 static String collectionToString(Collection c) |
501 => ToString.collectionToString(c); | 362 => ToString.collectionToString(c); |
502 } | 363 } |
| 364 |
| 365 /** |
| 366 * An unmodifiable [List] view of another List. |
| 367 * |
| 368 * The source of the elements may be a [List] or any [Iterable] with |
| 369 * efficient [Iterable.length] and [Iterable.elementAt]. |
| 370 */ |
| 371 class UnmodifiableListView<E> extends UnmodifiableListBase<E> { |
| 372 Iterable<E> _source; |
| 373 /** Create an unmodifiable list backed by [source]. */ |
| 374 UnmodifiableListView(Iterable<E> source) : _source = source; |
| 375 int get length => _source.length; |
| 376 E operator[](int index) => _source.elementAt(index); |
| 377 } |
OLD | NEW |