| 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.core; | 5 part of dart.core; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * An indexable collection of objects with a length. | 8 * An indexable collection of objects with a length. |
| 9 * | 9 * |
| 10 * Subclasses of this class implement different kinds of lists. | 10 * Subclasses of this class implement different kinds of lists. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 * temporarily and is restored before continuing the iteration, the iterator | 44 * temporarily and is restored before continuing the iteration, the iterator |
| 45 * does not detect it. | 45 * does not detect it. |
| 46 * | 46 * |
| 47 * It is generally not allowed to modify the list's length (adding or removing | 47 * It is generally not allowed to modify the list's length (adding or removing |
| 48 * elements) while an operation on the list is being performed, | 48 * elements) while an operation on the list is being performed, |
| 49 * for example during a call to [forEach] or [sort]. | 49 * for example during a call to [forEach] or [sort]. |
| 50 * Changing the list's length while it is being iterated, either by iterating it | 50 * Changing the list's length while it is being iterated, either by iterating it |
| 51 * directly or through iterating an [Iterable] that is backed by the list, will | 51 * directly or through iterating an [Iterable] that is backed by the list, will |
| 52 * break the iteration. | 52 * break the iteration. |
| 53 */ | 53 */ |
| 54 @SupportJsExtensionMethod() | 54 abstract class List<E> implements Iterable<E> { |
| 55 @JsPeerInterface(name: 'Array') | |
| 56 abstract class List<E> implements Iterable<E>, EfficientLength { | |
| 57 /** | 55 /** |
| 58 * Creates a list of the given length. | 56 * Creates a list of the given length. |
| 59 * | 57 * |
| 60 * The created list is fixed-length if [length] is provided. | 58 * The created list is fixed-length if [length] is provided. |
| 61 * | 59 * |
| 62 * List fixedLengthList = new List(3); | 60 * List fixedLengthList = new List(3); |
| 63 * fixedLengthList.length; // 3 | 61 * fixedLengthList.length; // 3 |
| 64 * fixedLengthList.length = 1; // Error | 62 * fixedLengthList.length = 1; // Error |
| 65 * | 63 * |
| 66 * The list has length 0 and is growable if [length] is omitted. | 64 * The list has length 0 and is growable if [length] is omitted. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 79 factory List([int length]) { | 77 factory List([int length]) { |
| 80 dynamic list; | 78 dynamic list; |
| 81 if (length == null) { | 79 if (length == null) { |
| 82 list = JS('', '[]'); | 80 list = JS('', '[]'); |
| 83 } else { | 81 } else { |
| 84 // Explicit type test is necessary to guard against JavaScript conversions | 82 // Explicit type test is necessary to guard against JavaScript conversions |
| 85 // in unchecked mode. | 83 // in unchecked mode. |
| 86 if ((length is !int) || (length < 0)) { | 84 if ((length is !int) || (length < 0)) { |
| 87 throw new ArgumentError("Length must be a non-negative integer: $length"
); | 85 throw new ArgumentError("Length must be a non-negative integer: $length"
); |
| 88 } | 86 } |
| 89 list = JS('', 'new Array(#)', length); | 87 list = JSArray.markFixedList(JS('', 'new Array(#)', length)); |
| 90 // TODO(jmesserly): consider a fixed array subclass instead. | |
| 91 JS('void', r'#.fixed$length = Array', list); | |
| 92 } | 88 } |
| 93 // TODO(jmesserly): skip this when E is dynamic and Object. | 89 return new JSArray<E>.typed(list); |
| 94 JS('void', 'dart.setType(#, List\$(#))', list, E); | |
| 95 // TODO(jmesserly): compiler creates a bogus type check here. | |
| 96 return list; | |
| 97 } | 90 } |
| 98 | 91 |
| 99 /** | 92 /** |
| 100 * Creates a fixed-length list of the given length, and initializes the | 93 * Creates a fixed-length list of the given length, and initializes the |
| 101 * value at each position with [fill]: | 94 * value at each position with [fill]: |
| 102 * | 95 * |
| 103 * new List<int>.filled(3, 0); // [0, 0, 0] | 96 * new List<int>.filled(3, 0); // [0, 0, 0] |
| 104 * | 97 * |
| 105 * The [length] must not be negative or null. | 98 * The [length] must not be negative or null. |
| 106 */ | 99 */ |
| (...skipping 29 matching lines...) Expand all Loading... |
| 136 * | 129 * |
| 137 * Creates a list with [length] positions and fills it with values created by | 130 * Creates a list with [length] positions and fills it with values created by |
| 138 * calling [generator] for each index in the range `0` .. `length - 1` | 131 * calling [generator] for each index in the range `0` .. `length - 1` |
| 139 * in increasing order. | 132 * in increasing order. |
| 140 * | 133 * |
| 141 * new List<int>.generate(3, (int index) => index * index); // [0, 1, 4] | 134 * new List<int>.generate(3, (int index) => index * index); // [0, 1, 4] |
| 142 * | 135 * |
| 143 * The created list is fixed-length unless [growable] is true. | 136 * The created list is fixed-length unless [growable] is true. |
| 144 */ | 137 */ |
| 145 factory List.generate(int length, E generator(int index), | 138 factory List.generate(int length, E generator(int index), |
| 146 { bool growable: true }) { | 139 { bool growable: true }) { |
| 147 List<E> result; | 140 List<E> result; |
| 148 if (growable) { | 141 if (growable) { |
| 149 result = <E>[]..length = length; | 142 result = <E>[]..length = length; |
| 150 } else { | 143 } else { |
| 151 result = new List<E>(length); | 144 result = new List<E>(length); |
| 152 } | 145 } |
| 153 for (int i = 0; i < length; i++) { | 146 for (int i = 0; i < length; i++) { |
| 154 result[i] = generator(i); | 147 result[i] = generator(i); |
| 155 } | 148 } |
| 156 return result; | 149 return result; |
| 157 } | 150 } |
| 158 | 151 |
| 159 checkMutable(reason) { | |
| 160 /* TODO(jacobr): implement. | |
| 161 if (this is !JSMutableArray) { | |
| 162 throw new UnsupportedError(reason); | |
| 163 } | |
| 164 * */ | |
| 165 } | |
| 166 | |
| 167 checkGrowable(reason) { | |
| 168 /* TODO(jacobr): implement | |
| 169 if (this is !JSExtendableArray) { | |
| 170 throw new UnsupportedError(reason); | |
| 171 } | |
| 172 * */ | |
| 173 } | |
| 174 | |
| 175 Iterable<E> where(bool f(E element)) { | |
| 176 return new IterableMixinWorkaround<E>().where(this, f); | |
| 177 } | |
| 178 | |
| 179 Iterable expand(Iterable f(E element)) { | |
| 180 return IterableMixinWorkaround.expand(this, f); | |
| 181 } | |
| 182 | |
| 183 void forEach(void f(E element)) { | |
| 184 int length = this.length; | |
| 185 for (int i = 0; i < length; i++) { | |
| 186 f(JS('', '#[#]', this, i)); | |
| 187 if (length != this.length) { | |
| 188 throw new ConcurrentModificationError(this); | |
| 189 } | |
| 190 } | |
| 191 } | |
| 192 | |
| 193 Iterable map(f(E element)) { | |
| 194 return IterableMixinWorkaround.mapList(this, f); | |
| 195 } | |
| 196 | |
| 197 String join([String separator = ""]) { | |
| 198 var list = new List(this.length); | |
| 199 for (int i = 0; i < this.length; i++) { | |
| 200 list[i] = "${this[i]}"; | |
| 201 } | |
| 202 return JS('String', "#.join(#)", list, separator); | |
| 203 } | |
| 204 | |
| 205 Iterable<E> take(int n) { | |
| 206 return new IterableMixinWorkaround<E>().takeList(this, n); | |
| 207 } | |
| 208 | |
| 209 Iterable<E> takeWhile(bool test(E value)) { | |
| 210 return new IterableMixinWorkaround<E>().takeWhile(this, test); | |
| 211 } | |
| 212 | |
| 213 Iterable<E> skip(int n) { | |
| 214 return new IterableMixinWorkaround<E>().skipList(this, n); | |
| 215 } | |
| 216 | |
| 217 Iterable<E> skipWhile(bool test(E value)) { | |
| 218 return new IterableMixinWorkaround<E>().skipWhile(this, test); | |
| 219 } | |
| 220 | |
| 221 E reduce(E combine(E value, E element)) { | |
| 222 return IterableMixinWorkaround.reduce(this, combine); | |
| 223 } | |
| 224 | |
| 225 fold(initialValue, combine(previousValue, E element)) { | |
| 226 return IterableMixinWorkaround.fold(this, initialValue, combine); | |
| 227 } | |
| 228 | |
| 229 E firstWhere(bool test(E value), {E orElse()}) { | |
| 230 return IterableMixinWorkaround.firstWhere(this, test, orElse); | |
| 231 } | |
| 232 | |
| 233 E lastWhere(bool test(E value), {E orElse()}) { | |
| 234 return IterableMixinWorkaround.lastWhereList(this, test, orElse); | |
| 235 } | |
| 236 | |
| 237 E singleWhere(bool test(E value)) { | |
| 238 return IterableMixinWorkaround.singleWhere(this, test); | |
| 239 } | |
| 240 | |
| 241 E elementAt(int index) { | |
| 242 return this[index]; | |
| 243 } | |
| 244 | |
| 245 E get first { | |
| 246 if (length > 0) return this[0]; | |
| 247 throw new StateError("No elements"); | |
| 248 } | |
| 249 | |
| 250 E get last { | |
| 251 if (length > 0) return this[length - 1]; | |
| 252 throw new StateError("No elements"); | |
| 253 } | |
| 254 | |
| 255 E get single { | |
| 256 if (length == 1) return this[0]; | |
| 257 if (length == 0) throw new StateError("No elements"); | |
| 258 throw new StateError("More than one element"); | |
| 259 } | |
| 260 | |
| 261 bool any(bool f(E element)) => IterableMixinWorkaround.any(this, f); | |
| 262 | |
| 263 bool every(bool f(E element)) => IterableMixinWorkaround.every(this, f); | |
| 264 | |
| 265 bool contains(Object other) { | |
| 266 for (int i = 0; i < length; i++) { | |
| 267 if (this[i] == other) return true; | |
| 268 } | |
| 269 return false; | |
| 270 } | |
| 271 | |
| 272 bool get isEmpty => length == 0; | |
| 273 | |
| 274 bool get isNotEmpty => !isEmpty; | |
| 275 | |
| 276 String toString() => ListBase.listToString(this); | |
| 277 | |
| 278 List<E> toList({ bool growable: true }) { | |
| 279 // TODO(vsm): Enforce growable / non-growable. | |
| 280 // See: https://github.com/dart-lang/dev_compiler/issues/175 | |
| 281 return JS('', 'dart.setType(#.slice(), core.List\$(#))', this, E); | |
| 282 } | |
| 283 | |
| 284 Set<E> toSet() => new Set<E>.from(this); | |
| 285 | |
| 286 Iterator<E> get iterator => new ListIterator<E>(this); | |
| 287 | |
| 288 int get hashCode => Primitives.objectHashCode(this); | |
| 289 | |
| 290 // BORDER XXXX | |
| 291 | |
| 292 /** | 152 /** |
| 293 * Returns the object at the given [index] in the list | 153 * Returns the object at the given [index] in the list |
| 294 * or throws a [RangeError] if [index] is out of bounds. | 154 * or throws a [RangeError] if [index] is out of bounds. |
| 295 */ | 155 */ |
| 296 E operator [](int index) { | 156 E operator [](int index); |
| 297 if (index is !int) throw new ArgumentError(index); | |
| 298 if (index >= length || index < 0) throw new RangeError.value(index); | |
| 299 return JS('var', '#[#]', this, index); | |
| 300 } | |
| 301 | 157 |
| 302 /** | 158 /** |
| 303 * Sets the value at the given [index] in the list to [value] | 159 * Sets the value at the given [index] in the list to [value] |
| 304 * or throws a [RangeError] if [index] is out of bounds. | 160 * or throws a [RangeError] if [index] is out of bounds. |
| 305 */ | 161 */ |
| 306 void operator []=(int index, E value) { | 162 void operator []=(int index, E value); |
| 307 checkMutable('indexed set'); | |
| 308 if (index is !int) throw new ArgumentError(index); | |
| 309 if (index >= length || index < 0) throw new RangeError.value(index); | |
| 310 JS('void', r'#[#] = #', this, index, value); | |
| 311 } | |
| 312 | 163 |
| 313 /** | 164 /** |
| 314 * Returns the number of objects in this list. | 165 * Returns the number of objects in this list. |
| 315 * | 166 * |
| 316 * The valid indices for a list are `0` through `length - 1`. | 167 * The valid indices for a list are `0` through `length - 1`. |
| 317 */ | 168 */ |
| 318 int get length => JS('JSUInt32', r'#.length', this); | 169 int get length; |
| 319 | 170 |
| 320 /** | 171 /** |
| 321 * Changes the length of this list. | 172 * Changes the length of this list. |
| 322 * | 173 * |
| 323 * If [newLength] is greater than | 174 * If [newLength] is greater than |
| 324 * the current length, entries are initialized to [:null:]. | 175 * the current length, entries are initialized to [:null:]. |
| 325 * | 176 * |
| 326 * Throws an [UnsupportedError] if the list is fixed-length. | 177 * Throws an [UnsupportedError] if the list is fixed-length. |
| 327 */ | 178 */ |
| 328 void set length(int newLength) { | 179 void set length(int newLength); |
| 329 if (newLength is !int) throw new ArgumentError(newLength); | |
| 330 if (newLength < 0) throw new RangeError.value(newLength); | |
| 331 checkGrowable('set length'); | |
| 332 JS('void', r'#.length = #', this, newLength); | |
| 333 } | |
| 334 | 180 |
| 335 /** | 181 /** |
| 336 * Adds [value] to the end of this list, | 182 * Adds [value] to the end of this list, |
| 337 * extending the length by one. | 183 * extending the length by one. |
| 338 * | 184 * |
| 339 * Throws an [UnsupportedError] if the list is fixed-length. | 185 * Throws an [UnsupportedError] if the list is fixed-length. |
| 340 */ | 186 */ |
| 341 void add(E value) { | 187 void add(E value); |
| 342 checkGrowable('add'); | |
| 343 JS('void', r'#.push(#)', this, value); | |
| 344 } | |
| 345 | 188 |
| 346 /** | 189 /** |
| 347 * Appends all objects of [iterable] to the end of this list. | 190 * Appends all objects of [iterable] to the end of this list. |
| 348 * | 191 * |
| 349 * Extends the length of the list by the number of objects in [iterable]. | 192 * Extends the length of the list by the number of objects in [iterable]. |
| 350 * Throws an [UnsupportedError] if this list is fixed-length. | 193 * Throws an [UnsupportedError] if this list is fixed-length. |
| 351 */ | 194 */ |
| 352 void addAll(Iterable<E> iterable) { | 195 void addAll(Iterable<E> iterable); |
| 353 for (E e in iterable) { | |
| 354 this.add(e); | |
| 355 } | |
| 356 } | |
| 357 | 196 |
| 358 /** | 197 /** |
| 359 * Returns an [Iterable] of the objects in this list in reverse order. | 198 * Returns an [Iterable] of the objects in this list in reverse order. |
| 360 */ | 199 */ |
| 361 Iterable<E> get reversed => | 200 Iterable<E> get reversed; |
| 362 new IterableMixinWorkaround<E>().reversedList(this); | |
| 363 | 201 |
| 364 /** | 202 /** |
| 365 * Sorts this list according to the order specified by the [compare] function. | 203 * Sorts this list according to the order specified by the [compare] function. |
| 366 * | 204 * |
| 367 * The [compare] function must act as a [Comparator]. | 205 * The [compare] function must act as a [Comparator]. |
| 368 | |
| 369 * List<String> numbers = ['one', 'two', 'three', 'four']; | 206 * List<String> numbers = ['one', 'two', 'three', 'four']; |
| 370 * // Sort from shortest to longest. | 207 * // Sort from shortest to longest. |
| 371 * numbers.sort((x, y) => x.length.compareTo(y.length)); | 208 * numbers.sort((x, y) => x.length.compareTo(y.length)); |
| 372 * numbers.join(', '); // 'one, two, four, three' | 209 * numbers.join(', '); // 'one, two, four, three' |
| 373 * | 210 * |
| 374 * The default List implementations use [Comparable.compare] if | 211 * The default List implementations use [Comparable.compare] if |
| 375 * [compare] is omitted. | 212 * [compare] is omitted. |
| 376 * | 213 * |
| 377 * List<int> nums = [13, 2, -11]; | 214 * List<int> nums = [13, 2, -11]; |
| 378 * nums.sort(); | 215 * nums.sort(); |
| 379 nums.join(', '); // '-11, 2, 13' | 216 nums.join(', '); // '-11, 2, 13' |
| 380 */ | 217 */ |
| 381 void sort([int compare(E a, E b)]) { | 218 void sort([int compare(E a, E b)]); |
| 382 checkMutable('sort'); | |
| 383 IterableMixinWorkaround.sortList(this, compare); | |
| 384 } | |
| 385 | 219 |
| 386 /** | 220 /** |
| 387 * Shuffles the elements of this list randomly. | 221 * Shuffles the elements of this list randomly. |
| 388 */ | 222 */ |
| 389 void shuffle([Random random]) { | 223 void shuffle([Random random]); |
| 390 IterableMixinWorkaround.shuffleList(this, random); | |
| 391 } | |
| 392 | 224 |
| 393 /** | 225 /** |
| 394 * Returns the first index of [element] in this list. | 226 * Returns the first index of [element] in this list. |
| 395 * | 227 * |
| 396 * Searches the list from index [start] to the end of the list. | 228 * Searches the list from index [start] to the end of the list. |
| 397 * The first time an object [:o:] is encountered so that [:o == element:], | 229 * The first time an object [:o:] is encountered so that [:o == element:], |
| 398 * the index of [:o:] is returned. | 230 * the index of [:o:] is returned. |
| 399 * | 231 * |
| 400 * List<String> notes = ['do', 're', 'mi', 're']; | 232 * List<String> notes = ['do', 're', 'mi', 're']; |
| 401 * notes.indexOf('re'); // 1 | 233 * notes.indexOf('re'); // 1 |
| 402 * notes.indexOf('re', 2); // 3 | 234 * notes.indexOf('re', 2); // 3 |
| 403 * | 235 * |
| 404 * Returns -1 if [element] is not found. | 236 * Returns -1 if [element] is not found. |
| 405 * | 237 * |
| 406 * notes.indexOf('fa'); // -1 | 238 * notes.indexOf('fa'); // -1 |
| 407 */ | 239 */ |
| 408 int indexOf(E element, [int start = 0]) { | 240 int indexOf(E element, [int start = 0]); |
| 409 return IterableMixinWorkaround.indexOfList(this, element, start); | |
| 410 } | |
| 411 | 241 |
| 412 /** | 242 /** |
| 413 * Returns the last index of [element] in this list. | 243 * Returns the last index of [element] in this list. |
| 414 * | 244 * |
| 415 * Searches the list backwards from index [start] to 0. | 245 * Searches the list backwards from index [start] to 0. |
| 416 * | 246 * |
| 417 * The first time an object [:o:] is encountered so that [:o == element:], | 247 * The first time an object [:o:] is encountered so that [:o == element:], |
| 418 * the index of [:o:] is returned. | 248 * the index of [:o:] is returned. |
| 419 * | 249 * |
| 420 * List<String> notes = ['do', 're', 'mi', 're']; | 250 * List<String> notes = ['do', 're', 'mi', 're']; |
| 421 * notes.lastIndexOf('re', 2); // 1 | 251 * notes.lastIndexOf('re', 2); // 1 |
| 422 * | 252 * |
| 423 * If [start] is not provided, this method searches from the end of the | 253 * If [start] is not provided, this method searches from the end of the |
| 424 * list./Returns | 254 * list./Returns |
| 425 * | 255 * |
| 426 * notes.lastIndexOf('re'); // 3 | 256 * notes.lastIndexOf('re'); // 3 |
| 427 * | 257 * |
| 428 * Returns -1 if [element] is not found. | 258 * Returns -1 if [element] is not found. |
| 429 * | 259 * |
| 430 * notes.lastIndexOf('fa'); // -1 | 260 * notes.lastIndexOf('fa'); // -1 |
| 431 */ | 261 */ |
| 432 int lastIndexOf(E element, [int start]) { | 262 int lastIndexOf(E element, [int start]); |
| 433 return IterableMixinWorkaround.lastIndexOfList(this, element, start); | |
| 434 } | |
| 435 | 263 |
| 436 /** | 264 /** |
| 437 * Removes all objects from this list; | 265 * Removes all objects from this list; |
| 438 * the length of the list becomes zero. | 266 * the length of the list becomes zero. |
| 439 * | 267 * |
| 440 * Throws an [UnsupportedError], and retains all objects, if this | 268 * Throws an [UnsupportedError], and retains all objects, if this |
| 441 * is a fixed-length list. | 269 * is a fixed-length list. |
| 442 */ | 270 */ |
| 443 void clear() { | 271 void clear(); |
| 444 length = 0; | |
| 445 } | |
| 446 | 272 |
| 447 /** | 273 /** |
| 448 * Inserts the object at position [index] in this list. | 274 * Inserts the object at position [index] in this list. |
| 449 * | 275 * |
| 450 * This increases the length of the list by one and shifts all objects | 276 * This increases the length of the list by one and shifts all objects |
| 451 * at or after the index towards the end of the list. | 277 * at or after the index towards the end of the list. |
| 452 * | 278 * |
| 453 * An error occurs if the [index] is less than 0 or greater than length. | 279 * An error occurs if the [index] is less than 0 or greater than length. |
| 454 * An [UnsupportedError] occurs if the list is fixed-length. | 280 * An [UnsupportedError] occurs if the list is fixed-length. |
| 455 */ | 281 */ |
| 456 void insert(int index, E element) { | 282 void insert(int index, E element); |
| 457 if (index is !int) throw new ArgumentError(index); | |
| 458 if (index < 0 || index > length) { | |
| 459 throw new RangeError.value(index); | |
| 460 } | |
| 461 checkGrowable('insert'); | |
| 462 JS('void', r'#.splice(#, 0, #)', this, index, element); | |
| 463 } | |
| 464 | |
| 465 | 283 |
| 466 /** | 284 /** |
| 467 * Inserts all objects of [iterable] at position [index] in this list. | 285 * Inserts all objects of [iterable] at position [index] in this list. |
| 468 * | 286 * |
| 469 * This increases the length of the list by the length of [iterable] and | 287 * This increases the length of the list by the length of [iterable] and |
| 470 * shifts all later objects towards the end of the list. | 288 * shifts all later objects towards the end of the list. |
| 471 * | 289 * |
| 472 * An error occurs if the [index] is less than 0 or greater than length. | 290 * An error occurs if the [index] is less than 0 or greater than length. |
| 473 * An [UnsupportedError] occurs if the list is fixed-length. | 291 * An [UnsupportedError] occurs if the list is fixed-length. |
| 474 */ | 292 */ |
| 475 void insertAll(int index, Iterable<E> iterable) { | 293 void insertAll(int index, Iterable<E> iterable); |
| 476 checkGrowable('insertAll'); | |
| 477 IterableMixinWorkaround.insertAllList(this, index, iterable); | |
| 478 } | |
| 479 | 294 |
| 480 /** | 295 /** |
| 481 * Overwrites objects of `this` with the objects of [iterable], starting | 296 * Overwrites objects of `this` with the objects of [iterable], starting |
| 482 * at position [index] in this list. | 297 * at position [index] in this list. |
| 483 * | 298 * |
| 484 * List<String> list = ['a', 'b', 'c']; | 299 * List<String> list = ['a', 'b', 'c']; |
| 485 * list.setAll(1, ['bee', 'sea']); | 300 * list.setAll(1, ['bee', 'sea']); |
| 486 * list.join(', '); // 'a, bee, sea' | 301 * list.join(', '); // 'a, bee, sea' |
| 487 * | 302 * |
| 488 * This operation does not increase the length of `this`. | 303 * This operation does not increase the length of `this`. |
| 489 * | 304 * |
| 490 * The [index] must be non-negative and no greater than [length]. | 305 * The [index] must be non-negative and no greater than [length]. |
| 491 * | 306 * |
| 492 * The [iterable] must not have more elements than what can fit from [index] | 307 * The [iterable] must not have more elements than what can fit from [index] |
| 493 * to [length]. | 308 * to [length]. |
| 494 * | 309 * |
| 495 * If `iterable` is based on this list, its values may change /during/ the | 310 * If `iterable` is based on this list, its values may change /during/ the |
| 496 * `setAll` operation. | 311 * `setAll` operation. |
| 497 */ | 312 */ |
| 498 void setAll(int index, Iterable<E> iterable) { | 313 void setAll(int index, Iterable<E> iterable); |
| 499 checkMutable('setAll'); | |
| 500 IterableMixinWorkaround.setAllList(this, index, iterable); | |
| 501 } | |
| 502 | 314 |
| 503 /** | 315 /** |
| 504 * Removes the first occurence of [value] from this list. | 316 * Removes the first occurence of [value] from this list. |
| 505 * | 317 * |
| 506 * Returns true if [value] was in the list, false otherwise. | 318 * Returns true if [value] was in the list, false otherwise. |
| 507 * | 319 * |
| 508 * List<String> parts = ['head', 'shoulders', 'knees', 'toes']; | 320 * List<String> parts = ['head', 'shoulders', 'knees', 'toes']; |
| 509 * parts.remove('head'); // true | 321 * parts.remove('head'); // true |
| 510 * parts.join(', '); // 'shoulders, knees, toes' | 322 * parts.join(', '); // 'shoulders, knees, toes' |
| 511 * | 323 * |
| 512 * The method has no effect if [value] was not in the list. | 324 * The method has no effect if [value] was not in the list. |
| 513 * | 325 * |
| 514 * // Note: 'head' has already been removed. | 326 * // Note: 'head' has already been removed. |
| 515 * parts.remove('head'); // false | 327 * parts.remove('head'); // false |
| 516 * parts.join(', '); // 'shoulders, knees, toes' | 328 * parts.join(', '); // 'shoulders, knees, toes' |
| 517 * | 329 * |
| 518 * An [UnsupportedError] occurs if the list is fixed-length. | 330 * An [UnsupportedError] occurs if the list is fixed-length. |
| 519 */ | 331 */ |
| 520 bool remove(Object element) { | 332 bool remove(Object value); |
| 521 checkGrowable('remove'); | |
| 522 for (int i = 0; i < this.length; i++) { | |
| 523 if (this[i] == value) { | |
| 524 JS('var', r'#.splice(#, 1)', this, i); | |
| 525 return true; | |
| 526 } | |
| 527 } | |
| 528 return false; | |
| 529 } | |
| 530 | 333 |
| 531 /** | 334 /** |
| 532 * Removes the object at position [index] from this list. | 335 * Removes the object at position [index] from this list. |
| 533 * | 336 * |
| 534 * This method reduces the length of `this` by one and moves all later objects | 337 * This method reduces the length of `this` by one and moves all later objects |
| 535 * down by one position. | 338 * down by one position. |
| 536 * | 339 * |
| 537 * Returns the removed object. | 340 * Returns the removed object. |
| 538 * | 341 * |
| 539 * The [index] must be in the range `0 ≤ index < length`. | 342 * The [index] must be in the range `0 ≤ index < length`. |
| 540 * | 343 * |
| 541 * Throws an [UnsupportedError] if this is a fixed-length list. In that case | 344 * Throws an [UnsupportedError] if this is a fixed-length list. In that case |
| 542 * the list is not modified. | 345 * the list is not modified. |
| 543 */ | 346 */ |
| 544 E removeAt(int index) { | 347 E removeAt(int index); |
| 545 if (index is !int) throw new ArgumentError(index); | |
| 546 if (index < 0 || index >= length) { | |
| 547 throw new RangeError.value(index); | |
| 548 } | |
| 549 checkGrowable('removeAt'); | |
| 550 return JS('var', r'#.splice(#, 1)[0]', this, index); | |
| 551 } | |
| 552 | 348 |
| 553 /** | 349 /** |
| 554 * Pops and returns the last object in this list. | 350 * Pops and returns the last object in this list. |
| 555 * | 351 * |
| 556 * Throws an [UnsupportedError] if this is a fixed-length list. | 352 * Throws an [UnsupportedError] if this is a fixed-length list. |
| 557 */ | 353 */ |
| 558 E removeLast() { | 354 E removeLast(); |
| 559 checkGrowable('removeLast'); | |
| 560 if (length == 0) throw new RangeError.value(-1); | |
| 561 return JS('var', r'#.pop()', this); | |
| 562 } | |
| 563 | 355 |
| 564 /** | 356 /** |
| 565 * Removes all objects from this list that satisfy [test]. | 357 * Removes all objects from this list that satisfy [test]. |
| 566 * | 358 * |
| 567 * An object [:o:] satisfies [test] if [:test(o):] is true. | 359 * An object [:o:] satisfies [test] if [:test(o):] is true. |
| 568 * | 360 * |
| 569 * List<String> numbers = ['one', 'two', 'three', 'four']; | 361 * List<String> numbers = ['one', 'two', 'three', 'four']; |
| 570 * numbers.removeWhere((item) => item.length == 3); | 362 * numbers.removeWhere((item) => item.length == 3); |
| 571 * numbers.join(', '); // 'three, four' | 363 * numbers.join(', '); // 'three, four' |
| 572 * | 364 * |
| 573 * Throws an [UnsupportedError] if this is a fixed-length list. | 365 * Throws an [UnsupportedError] if this is a fixed-length list. |
| 574 */ | 366 */ |
| 575 void removeWhere(bool test(E element)) { | 367 void removeWhere(bool test(E element)); |
| 576 // This could, and should, be optimized. | |
| 577 IterableMixinWorkaround.removeWhereList(this, test); | |
| 578 } | |
| 579 | |
| 580 | 368 |
| 581 /** | 369 /** |
| 582 * Removes all objects from this list that fail to satisfy [test]. | 370 * Removes all objects from this list that fail to satisfy [test]. |
| 583 * | 371 * |
| 584 * An object [:o:] satisfies [test] if [:test(o):] is true. | 372 * An object [:o:] satisfies [test] if [:test(o):] is true. |
| 585 * | 373 * |
| 586 * List<String> numbers = ['one', 'two', 'three', 'four']; | 374 * List<String> numbers = ['one', 'two', 'three', 'four']; |
| 587 * numbers.retainWhere((item) => item.length == 3); | 375 * numbers.retainWhere((item) => item.length == 3); |
| 588 * numbers.join(', '); // 'one, two' | 376 * numbers.join(', '); // 'one, two' |
| 589 * | 377 * |
| 590 * Throws an [UnsupportedError] if this is a fixed-length list. | 378 * Throws an [UnsupportedError] if this is a fixed-length list. |
| 591 */ | 379 */ |
| 592 void retainWhere(bool test(E element)) { | 380 void retainWhere(bool test(E element)); |
| 593 IterableMixinWorkaround.removeWhereList(this, | |
| 594 (E element) => !test(element)); | |
| 595 } | |
| 596 | 381 |
| 597 /** | 382 /** |
| 598 * Returns a new list containing the objects from [start] inclusive to [end] | 383 * Returns a new list containing the objects from [start] inclusive to [end] |
| 599 * exclusive. | 384 * exclusive. |
| 600 * | 385 * |
| 601 * List<String> colors = ['red', 'green', 'blue', 'orange', 'pink']; | 386 * List<String> colors = ['red', 'green', 'blue', 'orange', 'pink']; |
| 602 * colors.sublist(1, 3); // ['green', 'blue'] | 387 * colors.sublist(1, 3); // ['green', 'blue'] |
| 603 * | 388 * |
| 604 * If [end] is omitted, the [length] of `this` is used. | 389 * If [end] is omitted, the [length] of `this` is used. |
| 605 * | 390 * |
| 606 * colors.sublist(1); // ['green', 'blue', 'orange', 'pink'] | 391 * colors.sublist(1); // ['green', 'blue', 'orange', 'pink'] |
| 607 * | 392 * |
| 608 * An error occurs if [start] is outside the range `0` .. `length` or if | 393 * An error occurs if [start] is outside the range `0` .. `length` or if |
| 609 * [end] is outside the range `start` .. `length`. | 394 * [end] is outside the range `start` .. `length`. |
| 610 */ | 395 */ |
| 611 List<E> sublist(int start, [int end]) { | 396 List<E> sublist(int start, [int end]); |
| 612 checkNull(start); // TODO(ahe): This is not specified but co19 tests it. | |
| 613 if (start is !int) throw new ArgumentError(start); | |
| 614 if (start < 0 || start > length) { | |
| 615 throw new RangeError.range(start, 0, length); | |
| 616 } | |
| 617 if (end == null) { | |
| 618 end = length; | |
| 619 } else { | |
| 620 if (end is !int) throw new ArgumentError(end); | |
| 621 if (end < start || end > length) { | |
| 622 throw new RangeError.range(end, start, length); | |
| 623 } | |
| 624 } | |
| 625 if (start == end) return <E>[]; | |
| 626 return new JSArray<E>.markGrowable( | |
| 627 JS('', r'#.slice(#, #)', this, start, end)); | |
| 628 } | |
| 629 | 397 |
| 630 /** | 398 /** |
| 631 * Returns an [Iterable] that iterates over the objects in the range | 399 * Returns an [Iterable] that iterates over the objects in the range |
| 632 * [start] inclusive to [end] exclusive. | 400 * [start] inclusive to [end] exclusive. |
| 633 * | 401 * |
| 634 * An error occurs if [end] is before [start]. | 402 * An error occurs if [end] is before [start]. |
| 635 * | 403 * |
| 636 * An error occurs if the [start] and [end] are not valid ranges at the time | 404 * An error occurs if the [start] and [end] are not valid ranges at the time |
| 637 * of the call to this method. The returned [Iterable] behaves like | 405 * of the call to this method. The returned [Iterable] behaves like |
| 638 * `skip(start).take(end - start)`. That is, it does not throw exceptions | 406 * `skip(start).take(end - start)`. That is, it does not throw exceptions |
| 639 * if `this` changes size. | 407 * if `this` changes size. |
| 640 * | 408 * |
| 641 * List<String> colors = ['red', 'green', 'blue', 'orange', 'pink']; | 409 * List<String> colors = ['red', 'green', 'blue', 'orange', 'pink']; |
| 642 * Iterable<String> range = colors.getRange(1, 4); | 410 * Iterable<String> range = colors.getRange(1, 4); |
| 643 * range.join(', '); // 'green, blue, orange' | 411 * range.join(', '); // 'green, blue, orange' |
| 644 * colors.length = 3; | 412 * colors.length = 3; |
| 645 * range.join(', '); // 'green, blue' | 413 * range.join(', '); // 'green, blue' |
| 646 */ | 414 */ |
| 647 Iterable<E> getRange(int start, int end) { | 415 Iterable<E> getRange(int start, int end); |
| 648 return new IterableMixinWorkaround<E>().getRangeList(this, start, end); | |
| 649 } | |
| 650 | |
| 651 | 416 |
| 652 /** | 417 /** |
| 653 * Copies the objects of [iterable], skipping [skipCount] objects first, | 418 * Copies the objects of [iterable], skipping [skipCount] objects first, |
| 654 * into the range [start], inclusive, to [end], exclusive, of the list. | 419 * into the range [start], inclusive, to [end], exclusive, of the list. |
| 655 * | 420 * |
| 656 * List<int> list1 = [1, 2, 3, 4]; | 421 * List<int> list1 = [1, 2, 3, 4]; |
| 657 * List<int> list2 = [5, 6, 7, 8, 9]; | 422 * List<int> list2 = [5, 6, 7, 8, 9]; |
| 658 * // Copies the 4th and 5th items in list2 as the 2nd and 3rd items | 423 * // Copies the 4th and 5th items in list2 as the 2nd and 3rd items |
| 659 * // of list1. | 424 * // of list1. |
| 660 * list1.setRange(1, 3, list2, 3); | 425 * list1.setRange(1, 3, list2, 3); |
| 661 * list1.join(', '); // '1, 8, 9, 4' | 426 * list1.join(', '); // '1, 8, 9, 4' |
| 662 * | 427 * |
| 663 * The [start] and [end] indices must satisfy `0 ≤ start ≤ end ≤ length`. | 428 * The [start] and [end] indices must satisfy `0 ≤ start ≤ end ≤ length`. |
| 664 * If [start] equals [end], this method has no effect. | 429 * If [start] equals [end], this method has no effect. |
| 665 * | 430 * |
| 666 * The [iterable] must have enough objects to fill the range from `start` | 431 * The [iterable] must have enough objects to fill the range from `start` |
| 667 * to `end` after skipping [skipCount] objects. | 432 * to `end` after skipping [skipCount] objects. |
| 668 * | 433 * |
| 669 * If `iterable` is this list, the operation will copy the elements originally | 434 * If `iterable` is this list, the operation will copy the elements originally |
| 670 * in the range from `skipCount` to `skipCount + (end - start)` to the | 435 * in the range from `skipCount` to `skipCount + (end - start)` to the |
| 671 * range `start` to `end`, even if the two ranges overlap. | 436 * range `start` to `end`, even if the two ranges overlap. |
| 672 * | 437 * |
| 673 * If `iterable` depends on this list in some other way, no guarantees are | 438 * If `iterable` depends on this list in some other way, no guarantees are |
| 674 * made. | 439 * made. |
| 675 */ | 440 */ |
| 676 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { | 441 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]); |
| 677 checkMutable('set range'); | |
| 678 IterableMixinWorkaround.setRangeList(this, start, end, iterable, skipCount); | |
| 679 } | |
| 680 | 442 |
| 681 /** | 443 /** |
| 682 * Removes the objects in the range [start] inclusive to [end] exclusive. | 444 * Removes the objects in the range [start] inclusive to [end] exclusive. |
| 683 * | 445 * |
| 684 * The [start] and [end] indices must be in the range | 446 * The [start] and [end] indices must be in the range |
| 685 * `0 ≤ index ≤ length`, and `start ≤ end`. | 447 * `0 ≤ index ≤ length`, and `start ≤ end`. |
| 686 * | 448 * |
| 687 * Throws an [UnsupportedError] if this is a fixed-length list. In that case | 449 * Throws an [UnsupportedError] if this is a fixed-length list. In that case |
| 688 * the list is not modified. | 450 * the list is not modified. |
| 689 */ | 451 */ |
| 690 void removeRange(int start, int end) { | 452 void removeRange(int start, int end); |
| 691 checkGrowable('removeRange'); | |
| 692 int receiverLength = this.length; | |
| 693 if (start < 0 || start > receiverLength) { | |
| 694 throw new RangeError.range(start, 0, receiverLength); | |
| 695 } | |
| 696 if (end < start || end > receiverLength) { | |
| 697 throw new RangeError.range(end, start, receiverLength); | |
| 698 } | |
| 699 Lists.copy(this, | |
| 700 end, | |
| 701 this, | |
| 702 start, | |
| 703 receiverLength - end); | |
| 704 this.length = receiverLength - (end - start); | |
| 705 } | |
| 706 | 453 |
| 707 /** | 454 /** |
| 708 * Sets the objects in the range [start] inclusive to [end] exclusive | 455 * Sets the objects in the range [start] inclusive to [end] exclusive |
| 709 * to the given [fillValue]. | 456 * to the given [fillValue]. |
| 710 * | 457 * |
| 711 * An error occurs if [start]..[end] is not a valid range for `this`. | 458 * An error occurs if [start]..[end] is not a valid range for `this`. |
| 712 */ | 459 */ |
| 713 void fillRange(int start, int end, [E fillValue]) { | 460 void fillRange(int start, int end, [E fillValue]); |
| 714 checkMutable('fill range'); | |
| 715 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); | |
| 716 } | |
| 717 | 461 |
| 718 /** | 462 /** |
| 719 * Removes the objects in the range [start] inclusive to [end] exclusive | 463 * Removes the objects in the range [start] inclusive to [end] exclusive |
| 720 * and inserts the contents of [replacement] in its place. | 464 * and inserts the contents of [replacement] in its place. |
| 721 * | 465 * |
| 722 * List<int> list = [1, 2, 3, 4, 5]; | 466 * List<int> list = [1, 2, 3, 4, 5]; |
| 723 * list.replaceRange(1, 4, [6, 7]); | 467 * list.replaceRange(1, 4, [6, 7]); |
| 724 * list.join(', '); // '1, 6, 7, 5' | 468 * list.join(', '); // '1, 6, 7, 5' |
| 725 * | 469 * |
| 726 * An error occurs if [start]..[end] is not a valid range for `this`. | 470 * An error occurs if [start]..[end] is not a valid range for `this`. |
| 471 * |
| 472 * This method does not work on fixed-length lists, even when [replacement] |
| 473 * has the same number of elements as the replaced range. In that case use |
| 474 * [setRange] instead. |
| 727 */ | 475 */ |
| 728 void replaceRange(int start, int end, Iterable<E> replacement) { | 476 void replaceRange(int start, int end, Iterable<E> replacement); |
| 729 checkGrowable('removeRange'); | |
| 730 IterableMixinWorkaround.replaceRangeList(this, start, end, replacement); | |
| 731 } | |
| 732 | 477 |
| 733 /** | 478 /** |
| 734 * Returns an unmodifiable [Map] view of `this`. | 479 * Returns an unmodifiable [Map] view of `this`. |
| 735 * | 480 * |
| 736 * The map uses the indices of this list as keys and the corresponding objects | 481 * The map uses the indices of this list as keys and the corresponding objects |
| 737 * as values. The `Map.keys` [Iterable] iterates the indices of this list | 482 * as values. The `Map.keys` [Iterable] iterates the indices of this list |
| 738 * in numerical order. | 483 * in numerical order. |
| 739 * | 484 * |
| 740 * List<String> words = ['fee', 'fi', 'fo', 'fum']; | 485 * List<String> words = ['fee', 'fi', 'fo', 'fum']; |
| 741 * Map<int, String> map = words.asMap(); | 486 * Map<int, String> map = words.asMap(); |
| 742 * map[0] + map[1]; // 'feefi'; | 487 * map[0] + map[1]; // 'feefi'; |
| 743 * map.keys.toList(); // [0, 1, 2, 3] | 488 * map.keys.toList(); // [0, 1, 2, 3] |
| 744 */ | 489 */ |
| 745 Map<int, E> asMap() { | 490 Map<int, E> asMap(); |
| 746 return new IterableMixinWorkaround<E>().asMapList(this); | |
| 747 } | |
| 748 } | 491 } |
| OLD | NEW |