OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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.dev; | 5 part of dart._collection.dev; |
6 | 6 |
7 /** | 7 /** |
8 * Class implementing the read-operations on [List]. | 8 * Class implementing the read-operations on [List]. |
9 * | 9 * |
10 * Implements all read-only operations, except [:operator[]:] and [:length:], | 10 * Implements all read-only operations, except [:operator[]:] and [:length:], |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 for (int i = 0; i < length; i++) { | 90 for (int i = 0; i < length; i++) { |
91 result.add(this[start + i]); | 91 result.add(this[start + i]); |
92 } | 92 } |
93 return result; | 93 return result; |
94 } | 94 } |
95 | 95 |
96 Iterable map(f(E element)) { | 96 Iterable map(f(E element)) { |
97 return new MappedIterable(this, f); | 97 return new MappedIterable(this, f); |
98 } | 98 } |
99 | 99 |
| 100 List mappedBy(f(E element)) { |
| 101 return new MappedList(this, f); |
| 102 } |
| 103 |
100 Iterable<E> take(int n) { | 104 Iterable<E> take(int n) { |
101 return new SubListIterable(this, 0, n); | 105 return new SubListIterable(this, 0, n); |
102 } | 106 } |
103 | 107 |
104 Iterable<E> skip(int n) { | 108 Iterable<E> skip(int n) { |
105 return new SubListIterable(this, n, null); | 109 return new SubListIterable(this, n, null); |
106 } | 110 } |
107 | 111 |
108 Iterable<E> get reversed => new ReversedListIterable(this); | 112 Iterable<E> get reversed => new ReversedListIterable(this); |
109 | 113 |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 throw new UnsupportedError( | 279 throw new UnsupportedError( |
276 "Cannot remove from an unmodifiable list"); | 280 "Cannot remove from an unmodifiable list"); |
277 } | 281 } |
278 | 282 |
279 void insertRange(int start, int length, [E initialValue]) { | 283 void insertRange(int start, int length, [E initialValue]) { |
280 throw new UnsupportedError( | 284 throw new UnsupportedError( |
281 "Cannot insert range in an unmodifiable list"); | 285 "Cannot insert range in an unmodifiable list"); |
282 } | 286 } |
283 } | 287 } |
284 | 288 |
| 289 class MappedList<S, T> extends UnmodifiableListBase<T> { |
| 290 final List<S> _list; |
| 291 // TODO(ahe): Restore type when feature is implemented in dart2js |
| 292 // checked mode. http://dartbug.com/7733 |
| 293 final /* _Transformation<S, T> */ _f; |
| 294 |
| 295 MappedList(this._list, T this._f(S element)); |
| 296 |
| 297 T operator[](int index) => _f(_list[index]); |
| 298 int get length => _list.length; |
285 } | 299 } |
286 | 300 |
287 /** An empty fixed-length list. */ | 301 /** An empty fixed-length list. */ |
288 class EmptyList<E> extends FixedLengthListBase<E> { | 302 class EmptyList<E> extends FixedLengthListBase<E> { |
289 int get length => 0; | 303 int get length => 0; |
290 E operator[](int index) { throw new RangeError.value(index); } | 304 E operator[](int index) { throw new RangeError.value(index); } |
291 void operator []=(int index, E value) { throw new RangeError.value(index); } | 305 void operator []=(int index, E value) { throw new RangeError.value(index); } |
292 Iterable<E> skip(int count) => const EmptyIterable(); | 306 Iterable<E> skip(int count) => const EmptyIterable(); |
293 Iterable<E> take(int count) => const EmptyIterable(); | 307 Iterable<E> take(int count) => const EmptyIterable(); |
294 Iterable<E> get reversed => const EmptyIterable(); | 308 Iterable<E> get reversed => const EmptyIterable(); |
295 void sort([int compare(E a, E b)]) {} | 309 void sort([int compare(E a, E b)]) {} |
296 } | 310 } |
297 | 311 |
298 class ReversedListIterable<E> extends ListIterable<E> { | 312 class ReversedListIterable<E> extends ListIterable<E> { |
299 Iterable<E> _source; | 313 Iterable<E> _source; |
300 ReversedListIterable(this._source); | 314 ReversedListIterable(this._source); |
301 | 315 |
302 int get length => _source.length; | 316 int get length => _source.length; |
303 | 317 |
304 E elementAt(int index) => _source.elementAt(_source.length - 1 - index); | 318 E elementAt(int index) => _source.elementAt(_source.length - 1 - index); |
305 } | 319 } |
OLD | NEW |