| OLD | NEW |
| 1 // Copyright (c) 2012, 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.core; | 5 part of dart.collection.dev; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * A [List] is an indexable collection with a length. It can be of | 8 * Skeleton class for an unmodifiable [List]. |
| 9 * fixed size or extendable. | |
| 10 */ | |
| 11 abstract class List<E> implements Collection<E> { | |
| 12 /** | |
| 13 * Creates a list of the given [length]. | |
| 14 * | |
| 15 * The length of the returned list is not fixed. | |
| 16 */ | |
| 17 external factory List([int length = 0]); | |
| 18 | |
| 19 /** | |
| 20 * Creates a fixed-sized list of the given [length] where each entry is | |
| 21 * filled with [fill]. | |
| 22 */ | |
| 23 external factory List.fixedLength(int length, {E fill: null}); | |
| 24 | |
| 25 /** | |
| 26 * Creates an list of the given [length] where each entry is | |
| 27 * filled with [fill]. | |
| 28 * | |
| 29 * The length of the returned list is not fixed. | |
| 30 */ | |
| 31 external factory List.filled(int length, E fill); | |
| 32 | |
| 33 /** | |
| 34 * Creates an list with the elements of [other]. The order in | |
| 35 * the list will be the order provided by the iterator of [other]. | |
| 36 * | |
| 37 * The length of the returned list is not fixed. | |
| 38 */ | |
| 39 factory List.from(Iterable other) { | |
| 40 var list = new List<E>(); | |
| 41 for (E e in other) { | |
| 42 list.add(e); | |
| 43 } | |
| 44 return list; | |
| 45 } | |
| 46 | |
| 47 /** | |
| 48 * Returns the element at the given [index] in the list or throws | |
| 49 * an [RangeError] if [index] is out of bounds. | |
| 50 */ | |
| 51 E operator [](int index); | |
| 52 | |
| 53 /** | |
| 54 * Sets the entry at the given [index] in the list to [value]. | |
| 55 * Throws an [RangeError] if [index] is out of bounds. | |
| 56 */ | |
| 57 void operator []=(int index, E value); | |
| 58 | |
| 59 /** | |
| 60 * Changes the length of the list. If [newLength] is greater than | |
| 61 * the current [length], entries are initialized to [:null:]. Throws | |
| 62 * an [UnsupportedError] if the list is not extendable. | |
| 63 */ | |
| 64 void set length(int newLength); | |
| 65 | |
| 66 /** | |
| 67 * Adds [value] at the end of the list, extending the length by | |
| 68 * one. Throws an [UnsupportedError] if the list is not | |
| 69 * extendable. | |
| 70 */ | |
| 71 void add(E value); | |
| 72 | |
| 73 /** | |
| 74 * Adds [value] at the end of the list, extending the length by | |
| 75 * one. Throws an [UnsupportedError] if the list is not | |
| 76 * extendable. | |
| 77 */ | |
| 78 void addLast(E value); | |
| 79 | |
| 80 /** | |
| 81 * Appends all elements of the [iterable] to the end of this list. | |
| 82 * Extends the length of the list by the number of elements in [iterable]. | |
| 83 * Throws an [UnsupportedError] if this list is not extensible. | |
| 84 */ | |
| 85 void addAll(Iterable<E> iterable); | |
| 86 | |
| 87 /** | |
| 88 * Sorts the list according to the order specified by the [compare] function. | |
| 89 * | |
| 90 * The [compare] function must act as a [Comparator]. | |
| 91 * The default [List] implementations use [Comparable.compare] if | |
| 92 * [compare] is omitted. | |
| 93 */ | |
| 94 void sort([int compare(E a, E b)]); | |
| 95 | |
| 96 /** | |
| 97 * Returns the first index of [element] in the list. | |
| 98 * | |
| 99 * Searches the list from index [start] to the length of the list. | |
| 100 * The first time an element [:e:] is encountered so that [:e == element:], | |
| 101 * the index of [:e:] is returned. | |
| 102 * Returns -1 if [element] is not found. | |
| 103 */ | |
| 104 int indexOf(E element, [int start = 0]); | |
| 105 | |
| 106 /** | |
| 107 * Returns the last index of [element] in the list. | |
| 108 * | |
| 109 * Searches the list backwards from index [start] (inclusive) to 0. | |
| 110 * The first time an element [:e:] is encountered so that [:e == element:], | |
| 111 * the index of [:e:] is returned. | |
| 112 * If start is not provided, it defaults to [:this.length - 1:] . | |
| 113 * Returns -1 if [element] is not found. | |
| 114 */ | |
| 115 int lastIndexOf(E element, [int start]); | |
| 116 | |
| 117 /** | |
| 118 * Removes all elements in the list. | |
| 119 * | |
| 120 * The length of the list becomes zero. | |
| 121 * Throws an [UnsupportedError], and retains all elements, if the | |
| 122 * length of the list cannot be changed. | |
| 123 */ | |
| 124 void clear(); | |
| 125 | |
| 126 /** | |
| 127 * Removes the element at position[index] from the list. | |
| 128 * | |
| 129 * This reduces the length of the list by one and moves all later elements | |
| 130 * down by one position. | |
| 131 * Returns the removed element. | |
| 132 * Throws an [ArgumentError] if [index] is not an [int]. | |
| 133 * Throws an [RangeError] if the [index] does not point inside | |
| 134 * the list. | |
| 135 * Throws an [UnsupportedError], and doesn't remove the element, | |
| 136 * if the length of the list cannot be changed. | |
| 137 */ | |
| 138 E removeAt(int index); | |
| 139 | |
| 140 /** | |
| 141 * Pops and returns the last element of the list. | |
| 142 * Throws a [UnsupportedError] if the length of the | |
| 143 * list cannot be changed. | |
| 144 */ | |
| 145 E removeLast(); | |
| 146 | |
| 147 /** | |
| 148 * Returns a new list containing [length] elements from the list, | |
| 149 * starting at [start]. | |
| 150 * Returns an empty list if [length] is 0. | |
| 151 * Throws an [ArgumentError] if [length] is negative. | |
| 152 * Throws an [RangeError] if [start] or | |
| 153 * [:start + length - 1:] are out of range. | |
| 154 */ | |
| 155 List<E> getRange(int start, int length); | |
| 156 | |
| 157 /** | |
| 158 * Copies [length] elements of [from], starting | |
| 159 * at [startFrom], into the list, starting at [start]. | |
| 160 * If [length] is 0, this method does not do anything. | |
| 161 * Throws an [ArgumentError] if [length] is negative. | |
| 162 * Throws an [RangeError] if [start] or | |
| 163 * [:start + length - 1:] are out of range for [:this:], or if | |
| 164 * [startFrom] or [:startFrom + length - 1:] are out of range for [from]. | |
| 165 */ | |
| 166 void setRange(int start, int length, List<E> from, [int startFrom]); | |
| 167 | |
| 168 /** | |
| 169 * Removes [length] elements from the list, beginning at [start]. | |
| 170 * Throws an [UnsupportedError] if the list is | |
| 171 * not extendable. | |
| 172 * If [length] is 0, this method does not do anything. | |
| 173 * Throws an [ArgumentError] if [length] is negative. | |
| 174 * Throws an [RangeError] if [start] or | |
| 175 * [:start + length: - 1] are out of range. | |
| 176 */ | |
| 177 void removeRange(int start, int length); | |
| 178 | |
| 179 /** | |
| 180 * Inserts a new range into the list, starting from [start] to | |
| 181 * [:start + length - 1:]. The entries are filled with [fill]. | |
| 182 * Throws an [UnsupportedError] if the list is | |
| 183 * not extendable. | |
| 184 * If [length] is 0, this method does not do anything. | |
| 185 * If [start] is the length of the list, this method inserts the | |
| 186 * range at the end of the list. | |
| 187 * Throws an [ArgumentError] if [length] is negative. | |
| 188 * Throws an [RangeError] if [start] is negative or if | |
| 189 * [start] is greater than the length of the list. | |
| 190 */ | |
| 191 void insertRange(int start, int length, [E fill]); | |
| 192 } | |
| 193 | |
| 194 /** | |
| 195 * An unmodifiable [List]. | |
| 196 */ | 9 */ |
| 197 abstract class NonExtensibleListMixin<E> | 10 abstract class NonExtensibleListMixin<E> |
| 198 extends Iterable<E> implements List<E> { | 11 extends Iterable<E> implements List<E> { |
| 199 | 12 |
| 200 Iterator<E> get iterator => new ListIterator(this); | 13 Iterator<E> get iterator => new ListIterator(this); |
| 201 | 14 |
| 202 void forEach(f(E element)) { | 15 void forEach(f(E element)) { |
| 203 for (int i = 0; i < this.length; i++) f(this[i]); | 16 for (int i = 0; i < this.length; i++) f(this[i]); |
| 204 } | 17 } |
| 205 | 18 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 | 129 |
| 317 void retainAll(Iterable elements) { | 130 void retainAll(Iterable elements) { |
| 318 throw new UnsupportedError( | 131 throw new UnsupportedError( |
| 319 "Cannot remove from an unmodifiable list"); | 132 "Cannot remove from an unmodifiable list"); |
| 320 } | 133 } |
| 321 | 134 |
| 322 void removeMatching(bool test(E element)) { | 135 void removeMatching(bool test(E element)) { |
| 323 throw new UnsupportedError( | 136 throw new UnsupportedError( |
| 324 "Cannot remove from an unmodifiable list"); | 137 "Cannot remove from an unmodifiable list"); |
| 325 } | 138 } |
| 326 | |
| 327 void sort([Comparator<E> compare]) { | 139 void sort([Comparator<E> compare]) { |
| 328 throw new UnsupportedError( | 140 throw new UnsupportedError( |
| 329 "Cannot modify an unmodifiable list"); | 141 "Cannot modify an unmodifiable list"); |
| 330 } | 142 } |
| 331 | 143 |
| 332 void clear() { | 144 void clear() { |
| 333 throw new UnsupportedError( | 145 throw new UnsupportedError( |
| 334 "Cannot clear an unmodifiable list"); | 146 "Cannot clear an unmodifiable list"); |
| 335 } | 147 } |
| 336 | 148 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 354 "Cannot remove from an unmodifiable list"); | 166 "Cannot remove from an unmodifiable list"); |
| 355 } | 167 } |
| 356 | 168 |
| 357 void insertRange(int start, int length, [E initialValue]) { | 169 void insertRange(int start, int length, [E initialValue]) { |
| 358 throw new UnsupportedError( | 170 throw new UnsupportedError( |
| 359 "Cannot insert range in an unmodifiable list"); | 171 "Cannot insert range in an unmodifiable list"); |
| 360 } | 172 } |
| 361 } | 173 } |
| 362 | 174 |
| 363 /** | 175 /** |
| 364 * Iterates over a [Sequence] in growing index order. | 176 * Iterates over a [List] in growing index order. |
| 365 */ | 177 */ |
| 366 class ListIterator<E> implements Iterator<E> { | 178 class ListIterator<E> implements Iterator<E> { |
| 367 final List<E> _list; | 179 final List<E> _list; |
| 368 int _position; | 180 int _position; |
| 369 E _current; | 181 E _current; |
| 370 | 182 |
| 371 ListIterator(this._list) : _position = -1; | 183 ListIterator(this._list) : _position = -1; |
| 372 | 184 |
| 373 bool moveNext() { | 185 bool moveNext() { |
| 374 int nextPosition = _position + 1; | 186 int nextPosition = _position + 1; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 | 258 |
| 447 ListView<E> take(int takeCount) { | 259 ListView<E> take(int takeCount) { |
| 448 if (takeCount is! int || takeCount < 0) { | 260 if (takeCount is! int || takeCount < 0) { |
| 449 throw new ArgumentError(takeCount); | 261 throw new ArgumentError(takeCount); |
| 450 } | 262 } |
| 451 int newLength = takeCount; | 263 int newLength = takeCount; |
| 452 if (_length != null && takeCount > _length) newLength = _length; | 264 if (_length != null && takeCount > _length) newLength = _length; |
| 453 return new ListView(_list, _offset, newLength); | 265 return new ListView(_list, _offset, newLength); |
| 454 } | 266 } |
| 455 } | 267 } |
| OLD | NEW |