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 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 static Iterable mapList(List list, f(var element)) { | 320 static Iterable mapList(List list, f(var element)) { |
321 return new MappedListIterable(list, f); | 321 return new MappedListIterable(list, f); |
322 } | 322 } |
323 | 323 |
324 static Iterable expand(Iterable iterable, Iterable f(var element)) { | 324 static Iterable expand(Iterable iterable, Iterable f(var element)) { |
325 return new ExpandIterable(iterable, f); | 325 return new ExpandIterable(iterable, f); |
326 } | 326 } |
327 | 327 |
328 static Iterable takeList(List list, int n) { | 328 static Iterable takeList(List list, int n) { |
329 // 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. |
330 // This is currently a List as well as an Iterable. | |
331 return new SubListIterable(list, 0, n); | 330 return new SubListIterable(list, 0, n); |
332 } | 331 } |
333 | 332 |
334 static Iterable takeWhile(Iterable iterable, bool test(var value)) { | 333 static Iterable takeWhile(Iterable iterable, bool test(var value)) { |
335 // The generic type is currently lost. It will be fixed with mixins. | 334 // The generic type is currently lost. It will be fixed with mixins. |
336 return new TakeWhileIterable(iterable, test); | 335 return new TakeWhileIterable(iterable, test); |
337 } | 336 } |
338 | 337 |
339 static Iterable skipList(List list, int n) { | 338 static Iterable skipList(List list, int n) { |
340 // The generic type is currently lost. It will be fixed with mixins. | 339 // The generic type is currently lost. It will be fixed with mixins. |
341 // This is currently a List as well as an Iterable. | |
342 return new SubListIterable(list, n, null); | 340 return new SubListIterable(list, n, null); |
343 } | 341 } |
344 | 342 |
345 static Iterable skipWhile(Iterable iterable, bool test(var value)) { | 343 static Iterable skipWhile(Iterable iterable, bool test(var value)) { |
346 // The generic type is currently lost. It will be fixed with mixins. | 344 // The generic type is currently lost. It will be fixed with mixins. |
347 return new SkipWhileIterable(iterable, test); | 345 return new SkipWhileIterable(iterable, test); |
348 } | 346 } |
349 | 347 |
350 static Iterable reversedList(List list) { | 348 static Iterable reversedList(List list) { |
351 return new ReversedListIterable(list); | 349 return new ReversedListIterable(list); |
352 } | 350 } |
353 | 351 |
354 static void sortList(List list, int compare(a, b)) { | 352 static void sortList(List list, int compare(a, b)) { |
355 if (compare == null) compare = Comparable.compare; | 353 if (compare == null) compare = Comparable.compare; |
356 Sort.sort(list, compare); | 354 Sort.sort(list, compare); |
357 } | 355 } |
358 | 356 |
359 static int indexOfList(List list, var element, int start) { | 357 static int indexOfList(List list, var element, int start) { |
360 return Arrays.indexOf(list, element, start, list.length); | 358 return Arrays.indexOf(list, element, start, list.length); |
361 } | 359 } |
362 | 360 |
363 static int lastIndexOfList(List list, var element, int start) { | 361 static int lastIndexOfList(List list, var element, int start) { |
364 if (start == null) start = list.length - 1; | 362 if (start == null) start = list.length - 1; |
365 return Arrays.lastIndexOf(list, element, start); | 363 return Arrays.lastIndexOf(list, element, start); |
366 } | 364 } |
367 | 365 |
| 366 static Iterable getRangeList(List list, int start, int end) { |
| 367 if (start < 0 || start > list.length) { |
| 368 throw new RangeError.range(start, 0, list.length); |
| 369 } |
| 370 if (end < start || end > list.length) { |
| 371 throw new RangeError.range(end, start, list.length); |
| 372 } |
| 373 // The generic type is currently lost. It will be fixed with mixins. |
| 374 return new SubListIterable(list, start, end); |
| 375 } |
| 376 |
368 static void setRangeList(List list, int start, int length, | 377 static void setRangeList(List list, int start, int length, |
369 List from, int startFrom) { | 378 List from, int startFrom) { |
370 if (length == 0) return; | 379 if (length == 0) return; |
371 | 380 |
372 if (length < 0) throw new ArgumentError(length); | 381 if (length < 0) throw new ArgumentError(length); |
373 if (start < 0) throw new RangeError.value(start); | 382 if (start < 0) throw new RangeError.value(start); |
374 if (start + length > list.length) { | 383 if (start + length > list.length) { |
375 throw new RangeError.value(start + length); | 384 throw new RangeError.value(start + length); |
376 } | 385 } |
377 | 386 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 * The source of the elements may be a [List] or any [Iterable] with | 443 * The source of the elements may be a [List] or any [Iterable] with |
435 * efficient [Iterable.length] and [Iterable.elementAt]. | 444 * efficient [Iterable.length] and [Iterable.elementAt]. |
436 */ | 445 */ |
437 class UnmodifiableListView<E> extends UnmodifiableListBase<E> { | 446 class UnmodifiableListView<E> extends UnmodifiableListBase<E> { |
438 Iterable<E> _source; | 447 Iterable<E> _source; |
439 /** Create an unmodifiable list backed by [source]. */ | 448 /** Create an unmodifiable list backed by [source]. */ |
440 UnmodifiableListView(Iterable<E> source) : _source = source; | 449 UnmodifiableListView(Iterable<E> source) : _source = source; |
441 int get length => _source.length; | 450 int get length => _source.length; |
442 E operator[](int index) => _source.elementAt(index); | 451 E operator[](int index) => _source.elementAt(index); |
443 } | 452 } |
OLD | NEW |