| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.core; | |
| 6 | |
| 7 /** | |
| 8 * An indexable collection of objects with a length. | |
| 9 * | |
| 10 * Subclasses of this class implement different kinds of lists. | |
| 11 * The most common kinds of lists are: | |
| 12 * | |
| 13 * * Fixed-length list. | |
| 14 * An error occurs when attempting to use operations | |
| 15 * that can change the length of the list. | |
| 16 * | |
| 17 * * Growable list. Full implementation of the API defined in this class. | |
| 18 * | |
| 19 * The default growable list, as returned by `new List()` or `[]`, keeps | |
| 20 * an internal buffer, and grows that buffer when necessary. This guarantees | |
| 21 * that a sequence of [add] operations will each execute in amortized constant | |
| 22 * time. Setting the length directly may take time proportional to the new | |
| 23 * length, and may change the internal capacity so that a following add | |
| 24 * operation will need to immediately increase the buffer capacity. | |
| 25 * Other list implementations may have different performance behavior. | |
| 26 * | |
| 27 * The following code illustrates that some List implementations support | |
| 28 * only a subset of the API. | |
| 29 * | |
| 30 * List<int> fixedLengthList = new List(5); | |
| 31 * fixedLengthList.length = 0; // Error | |
| 32 * fixedLengthList.add(499); // Error | |
| 33 * fixedLengthList[0] = 87; | |
| 34 * List<int> growableList = [1, 2]; | |
| 35 * growableList.length = 0; | |
| 36 * growableList.add(499); | |
| 37 * growableList[0] = 87; | |
| 38 * | |
| 39 * Lists are [Iterable]. Iteration occurs over values in index order. Changing | |
| 40 * the values does not affect iteration, but changing the valid | |
| 41 * indices—that is, changing the list's length—between iteration | |
| 42 * steps causes a [ConcurrentModificationError]. This means that only growable | |
| 43 * lists can throw ConcurrentModificationError. If the length changes | |
| 44 * temporarily and is restored before continuing the iteration, the iterator | |
| 45 * does not detect it. | |
| 46 * | |
| 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, | |
| 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 | |
| 51 * directly or through iterating an [Iterable] that is backed by the list, will | |
| 52 * break the iteration. | |
| 53 */ | |
| 54 @SupportJsExtensionMethod() | |
| 55 @JsPeerInterface(name: 'Array') | |
| 56 abstract class List<E> implements Iterable<E>, EfficientLength { | |
| 57 /** | |
| 58 * Creates a list of the given length. | |
| 59 * | |
| 60 * The created list is fixed-length if [length] is provided. | |
| 61 * | |
| 62 * List fixedLengthList = new List(3); | |
| 63 * fixedLengthList.length; // 3 | |
| 64 * fixedLengthList.length = 1; // Error | |
| 65 * | |
| 66 * The list has length 0 and is growable if [length] is omitted. | |
| 67 * | |
| 68 * List growableList = new List(); | |
| 69 * growableList.length; // 0; | |
| 70 * growableList.length = 3; | |
| 71 * | |
| 72 * To create a growable list with a given length, just assign the length | |
| 73 * right after creation: | |
| 74 * | |
| 75 * List growableList = new List()..length = 500; | |
| 76 * | |
| 77 * The [length] must not be negative or null, if it is provided. | |
| 78 */ | |
| 79 factory List([int length]) { | |
| 80 dynamic list; | |
| 81 if (length == null) { | |
| 82 list = JS('', '[]'); | |
| 83 } else { | |
| 84 // Explicit type test is necessary to guard against JavaScript conversions | |
| 85 // in unchecked mode. | |
| 86 if ((length is !int) || (length < 0)) { | |
| 87 throw new ArgumentError("Length must be a non-negative integer: $length"
); | |
| 88 } | |
| 89 list = JS('', 'new Array(#)', length); | |
| 90 // TODO(jmesserly): consider a fixed array subclass instead. | |
| 91 JS('void', r'#.fixed$length = Array', list); | |
| 92 } | |
| 93 // TODO(jmesserly): skip this when E is dynamic and Object. | |
| 94 JS('void', 'dart.setType(#, List\$(#))', list, E); | |
| 95 // TODO(jmesserly): compiler creates a bogus type check here. | |
| 96 return list; | |
| 97 } | |
| 98 | |
| 99 /** | |
| 100 * Creates a fixed-length list of the given length, and initializes the | |
| 101 * value at each position with [fill]: | |
| 102 * | |
| 103 * new List<int>.filled(3, 0); // [0, 0, 0] | |
| 104 * | |
| 105 * The [length] must not be negative or null. | |
| 106 */ | |
| 107 factory List.filled(int length, E fill) { | |
| 108 List<E> result = new List<E>(length); | |
| 109 if (length != 0 && fill != null) { | |
| 110 for (int i = 0; i < result.length; i++) { | |
| 111 result[i] = fill; | |
| 112 } | |
| 113 } | |
| 114 return result; | |
| 115 } | |
| 116 | |
| 117 /** | |
| 118 * Creates a list containing all [elements]. | |
| 119 * | |
| 120 * The [Iterator] of [elements] provides the order of the elements. | |
| 121 * | |
| 122 * This constructor returns a growable list when [growable] is true; | |
| 123 * otherwise, it returns a fixed-length list. | |
| 124 */ | |
| 125 factory List.from(Iterable elements, { bool growable: true }) { | |
| 126 List<E> list = new List<E>(); | |
| 127 for (var e in elements) { | |
| 128 list.add(e); | |
| 129 } | |
| 130 if (growable) return list; | |
| 131 return makeListFixedLength(list); | |
| 132 } | |
| 133 | |
| 134 /** | |
| 135 * Generates a list of values. | |
| 136 * | |
| 137 * 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` | |
| 139 * in increasing order. | |
| 140 * | |
| 141 * new List<int>.generate(3, (int index) => index * index); // [0, 1, 4] | |
| 142 * | |
| 143 * The created list is fixed-length unless [growable] is true. | |
| 144 */ | |
| 145 factory List.generate(int length, E generator(int index), | |
| 146 { bool growable: true }) { | |
| 147 List<E> result; | |
| 148 if (growable) { | |
| 149 result = <E>[]..length = length; | |
| 150 } else { | |
| 151 result = new List<E>(length); | |
| 152 } | |
| 153 for (int i = 0; i < length; i++) { | |
| 154 result[i] = generator(i); | |
| 155 } | |
| 156 return result; | |
| 157 } | |
| 158 | |
| 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 /** | |
| 293 * Returns the object at the given [index] in the list | |
| 294 * or throws a [RangeError] if [index] is out of bounds. | |
| 295 */ | |
| 296 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 | |
| 302 /** | |
| 303 * Sets the value at the given [index] in the list to [value] | |
| 304 * or throws a [RangeError] if [index] is out of bounds. | |
| 305 */ | |
| 306 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 | |
| 313 /** | |
| 314 * Returns the number of objects in this list. | |
| 315 * | |
| 316 * The valid indices for a list are `0` through `length - 1`. | |
| 317 */ | |
| 318 int get length => JS('JSUInt32', r'#.length', this); | |
| 319 | |
| 320 /** | |
| 321 * Changes the length of this list. | |
| 322 * | |
| 323 * If [newLength] is greater than | |
| 324 * the current length, entries are initialized to [:null:]. | |
| 325 * | |
| 326 * Throws an [UnsupportedError] if the list is fixed-length. | |
| 327 */ | |
| 328 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 | |
| 335 /** | |
| 336 * Adds [value] to the end of this list, | |
| 337 * extending the length by one. | |
| 338 * | |
| 339 * Throws an [UnsupportedError] if the list is fixed-length. | |
| 340 */ | |
| 341 void add(E value) { | |
| 342 checkGrowable('add'); | |
| 343 JS('void', r'#.push(#)', this, value); | |
| 344 } | |
| 345 | |
| 346 /** | |
| 347 * Appends all objects of [iterable] to the end of this list. | |
| 348 * | |
| 349 * Extends the length of the list by the number of objects in [iterable]. | |
| 350 * Throws an [UnsupportedError] if this list is fixed-length. | |
| 351 */ | |
| 352 void addAll(Iterable<E> iterable) { | |
| 353 for (E e in iterable) { | |
| 354 this.add(e); | |
| 355 } | |
| 356 } | |
| 357 | |
| 358 /** | |
| 359 * Returns an [Iterable] of the objects in this list in reverse order. | |
| 360 */ | |
| 361 Iterable<E> get reversed => | |
| 362 new IterableMixinWorkaround<E>().reversedList(this); | |
| 363 | |
| 364 /** | |
| 365 * Sorts this list according to the order specified by the [compare] function. | |
| 366 * | |
| 367 * The [compare] function must act as a [Comparator]. | |
| 368 | |
| 369 * List<String> numbers = ['one', 'two', 'three', 'four']; | |
| 370 * // Sort from shortest to longest. | |
| 371 * numbers.sort((x, y) => x.length.compareTo(y.length)); | |
| 372 * numbers.join(', '); // 'one, two, four, three' | |
| 373 * | |
| 374 * The default List implementations use [Comparable.compare] if | |
| 375 * [compare] is omitted. | |
| 376 * | |
| 377 * List<int> nums = [13, 2, -11]; | |
| 378 * nums.sort(); | |
| 379 nums.join(', '); // '-11, 2, 13' | |
| 380 */ | |
| 381 void sort([int compare(E a, E b)]) { | |
| 382 checkMutable('sort'); | |
| 383 IterableMixinWorkaround.sortList(this, compare); | |
| 384 } | |
| 385 | |
| 386 /** | |
| 387 * Shuffles the elements of this list randomly. | |
| 388 */ | |
| 389 void shuffle([Random random]) { | |
| 390 IterableMixinWorkaround.shuffleList(this, random); | |
| 391 } | |
| 392 | |
| 393 /** | |
| 394 * Returns the first index of [element] in this list. | |
| 395 * | |
| 396 * 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:], | |
| 398 * the index of [:o:] is returned. | |
| 399 * | |
| 400 * List<String> notes = ['do', 're', 'mi', 're']; | |
| 401 * notes.indexOf('re'); // 1 | |
| 402 * notes.indexOf('re', 2); // 3 | |
| 403 * | |
| 404 * Returns -1 if [element] is not found. | |
| 405 * | |
| 406 * notes.indexOf('fa'); // -1 | |
| 407 */ | |
| 408 int indexOf(E element, [int start = 0]) { | |
| 409 return IterableMixinWorkaround.indexOfList(this, element, start); | |
| 410 } | |
| 411 | |
| 412 /** | |
| 413 * Returns the last index of [element] in this list. | |
| 414 * | |
| 415 * Searches the list backwards from index [start] to 0. | |
| 416 * | |
| 417 * The first time an object [:o:] is encountered so that [:o == element:], | |
| 418 * the index of [:o:] is returned. | |
| 419 * | |
| 420 * List<String> notes = ['do', 're', 'mi', 're']; | |
| 421 * notes.lastIndexOf('re', 2); // 1 | |
| 422 * | |
| 423 * If [start] is not provided, this method searches from the end of the | |
| 424 * list./Returns | |
| 425 * | |
| 426 * notes.lastIndexOf('re'); // 3 | |
| 427 * | |
| 428 * Returns -1 if [element] is not found. | |
| 429 * | |
| 430 * notes.lastIndexOf('fa'); // -1 | |
| 431 */ | |
| 432 int lastIndexOf(E element, [int start]) { | |
| 433 return IterableMixinWorkaround.lastIndexOfList(this, element, start); | |
| 434 } | |
| 435 | |
| 436 /** | |
| 437 * Removes all objects from this list; | |
| 438 * the length of the list becomes zero. | |
| 439 * | |
| 440 * Throws an [UnsupportedError], and retains all objects, if this | |
| 441 * is a fixed-length list. | |
| 442 */ | |
| 443 void clear() { | |
| 444 length = 0; | |
| 445 } | |
| 446 | |
| 447 /** | |
| 448 * Inserts the object at position [index] in this list. | |
| 449 * | |
| 450 * 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. | |
| 452 * | |
| 453 * An error occurs if the [index] is less than 0 or greater than length. | |
| 454 * An [UnsupportedError] occurs if the list is fixed-length. | |
| 455 */ | |
| 456 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 | |
| 466 /** | |
| 467 * Inserts all objects of [iterable] at position [index] in this list. | |
| 468 * | |
| 469 * This increases the length of the list by the length of [iterable] and | |
| 470 * shifts all later objects towards the end of the list. | |
| 471 * | |
| 472 * An error occurs if the [index] is less than 0 or greater than length. | |
| 473 * An [UnsupportedError] occurs if the list is fixed-length. | |
| 474 */ | |
| 475 void insertAll(int index, Iterable<E> iterable) { | |
| 476 checkGrowable('insertAll'); | |
| 477 IterableMixinWorkaround.insertAllList(this, index, iterable); | |
| 478 } | |
| 479 | |
| 480 /** | |
| 481 * Overwrites objects of `this` with the objects of [iterable], starting | |
| 482 * at position [index] in this list. | |
| 483 * | |
| 484 * List<String> list = ['a', 'b', 'c']; | |
| 485 * list.setAll(1, ['bee', 'sea']); | |
| 486 * list.join(', '); // 'a, bee, sea' | |
| 487 * | |
| 488 * This operation does not increase the length of `this`. | |
| 489 * | |
| 490 * The [index] must be non-negative and no greater than [length]. | |
| 491 * | |
| 492 * The [iterable] must not have more elements than what can fit from [index] | |
| 493 * to [length]. | |
| 494 * | |
| 495 * If `iterable` is based on this list, its values may change /during/ the | |
| 496 * `setAll` operation. | |
| 497 */ | |
| 498 void setAll(int index, Iterable<E> iterable) { | |
| 499 checkMutable('setAll'); | |
| 500 IterableMixinWorkaround.setAllList(this, index, iterable); | |
| 501 } | |
| 502 | |
| 503 /** | |
| 504 * Removes the first occurence of [value] from this list. | |
| 505 * | |
| 506 * Returns true if [value] was in the list, false otherwise. | |
| 507 * | |
| 508 * List<String> parts = ['head', 'shoulders', 'knees', 'toes']; | |
| 509 * parts.remove('head'); // true | |
| 510 * parts.join(', '); // 'shoulders, knees, toes' | |
| 511 * | |
| 512 * The method has no effect if [value] was not in the list. | |
| 513 * | |
| 514 * // Note: 'head' has already been removed. | |
| 515 * parts.remove('head'); // false | |
| 516 * parts.join(', '); // 'shoulders, knees, toes' | |
| 517 * | |
| 518 * An [UnsupportedError] occurs if the list is fixed-length. | |
| 519 */ | |
| 520 bool remove(Object element) { | |
| 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 | |
| 531 /** | |
| 532 * Removes the object at position [index] from this list. | |
| 533 * | |
| 534 * This method reduces the length of `this` by one and moves all later objects | |
| 535 * down by one position. | |
| 536 * | |
| 537 * Returns the removed object. | |
| 538 * | |
| 539 * The [index] must be in the range `0 ≤ index < length`. | |
| 540 * | |
| 541 * Throws an [UnsupportedError] if this is a fixed-length list. In that case | |
| 542 * the list is not modified. | |
| 543 */ | |
| 544 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 | |
| 553 /** | |
| 554 * Pops and returns the last object in this list. | |
| 555 * | |
| 556 * Throws an [UnsupportedError] if this is a fixed-length list. | |
| 557 */ | |
| 558 E removeLast() { | |
| 559 checkGrowable('removeLast'); | |
| 560 if (length == 0) throw new RangeError.value(-1); | |
| 561 return JS('var', r'#.pop()', this); | |
| 562 } | |
| 563 | |
| 564 /** | |
| 565 * Removes all objects from this list that satisfy [test]. | |
| 566 * | |
| 567 * An object [:o:] satisfies [test] if [:test(o):] is true. | |
| 568 * | |
| 569 * List<String> numbers = ['one', 'two', 'three', 'four']; | |
| 570 * numbers.removeWhere((item) => item.length == 3); | |
| 571 * numbers.join(', '); // 'three, four' | |
| 572 * | |
| 573 * Throws an [UnsupportedError] if this is a fixed-length list. | |
| 574 */ | |
| 575 void removeWhere(bool test(E element)) { | |
| 576 // This could, and should, be optimized. | |
| 577 IterableMixinWorkaround.removeWhereList(this, test); | |
| 578 } | |
| 579 | |
| 580 | |
| 581 /** | |
| 582 * Removes all objects from this list that fail to satisfy [test]. | |
| 583 * | |
| 584 * An object [:o:] satisfies [test] if [:test(o):] is true. | |
| 585 * | |
| 586 * List<String> numbers = ['one', 'two', 'three', 'four']; | |
| 587 * numbers.retainWhere((item) => item.length == 3); | |
| 588 * numbers.join(', '); // 'one, two' | |
| 589 * | |
| 590 * Throws an [UnsupportedError] if this is a fixed-length list. | |
| 591 */ | |
| 592 void retainWhere(bool test(E element)) { | |
| 593 IterableMixinWorkaround.removeWhereList(this, | |
| 594 (E element) => !test(element)); | |
| 595 } | |
| 596 | |
| 597 /** | |
| 598 * Returns a new list containing the objects from [start] inclusive to [end] | |
| 599 * exclusive. | |
| 600 * | |
| 601 * List<String> colors = ['red', 'green', 'blue', 'orange', 'pink']; | |
| 602 * colors.sublist(1, 3); // ['green', 'blue'] | |
| 603 * | |
| 604 * If [end] is omitted, the [length] of `this` is used. | |
| 605 * | |
| 606 * colors.sublist(1); // ['green', 'blue', 'orange', 'pink'] | |
| 607 * | |
| 608 * An error occurs if [start] is outside the range `0` .. `length` or if | |
| 609 * [end] is outside the range `start` .. `length`. | |
| 610 */ | |
| 611 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 | |
| 630 /** | |
| 631 * Returns an [Iterable] that iterates over the objects in the range | |
| 632 * [start] inclusive to [end] exclusive. | |
| 633 * | |
| 634 * An error occurs if [end] is before [start]. | |
| 635 * | |
| 636 * 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 | |
| 638 * `skip(start).take(end - start)`. That is, it does not throw exceptions | |
| 639 * if `this` changes size. | |
| 640 * | |
| 641 * List<String> colors = ['red', 'green', 'blue', 'orange', 'pink']; | |
| 642 * Iterable<String> range = colors.getRange(1, 4); | |
| 643 * range.join(', '); // 'green, blue, orange' | |
| 644 * colors.length = 3; | |
| 645 * range.join(', '); // 'green, blue' | |
| 646 */ | |
| 647 Iterable<E> getRange(int start, int end) { | |
| 648 return new IterableMixinWorkaround<E>().getRangeList(this, start, end); | |
| 649 } | |
| 650 | |
| 651 | |
| 652 /** | |
| 653 * Copies the objects of [iterable], skipping [skipCount] objects first, | |
| 654 * into the range [start], inclusive, to [end], exclusive, of the list. | |
| 655 * | |
| 656 * List<int> list1 = [1, 2, 3, 4]; | |
| 657 * List<int> list2 = [5, 6, 7, 8, 9]; | |
| 658 * // Copies the 4th and 5th items in list2 as the 2nd and 3rd items | |
| 659 * // of list1. | |
| 660 * list1.setRange(1, 3, list2, 3); | |
| 661 * list1.join(', '); // '1, 8, 9, 4' | |
| 662 * | |
| 663 * The [start] and [end] indices must satisfy `0 ≤ start ≤ end ≤ length`. | |
| 664 * If [start] equals [end], this method has no effect. | |
| 665 * | |
| 666 * The [iterable] must have enough objects to fill the range from `start` | |
| 667 * to `end` after skipping [skipCount] objects. | |
| 668 * | |
| 669 * If `iterable` is this list, the operation will copy the elements originally | |
| 670 * in the range from `skipCount` to `skipCount + (end - start)` to the | |
| 671 * range `start` to `end`, even if the two ranges overlap. | |
| 672 * | |
| 673 * If `iterable` depends on this list in some other way, no guarantees are | |
| 674 * made. | |
| 675 */ | |
| 676 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 | |
| 681 /** | |
| 682 * Removes the objects in the range [start] inclusive to [end] exclusive. | |
| 683 * | |
| 684 * The [start] and [end] indices must be in the range | |
| 685 * `0 ≤ index ≤ length`, and `start ≤ end`. | |
| 686 * | |
| 687 * Throws an [UnsupportedError] if this is a fixed-length list. In that case | |
| 688 * the list is not modified. | |
| 689 */ | |
| 690 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 | |
| 707 /** | |
| 708 * Sets the objects in the range [start] inclusive to [end] exclusive | |
| 709 * to the given [fillValue]. | |
| 710 * | |
| 711 * An error occurs if [start]..[end] is not a valid range for `this`. | |
| 712 */ | |
| 713 void fillRange(int start, int end, [E fillValue]) { | |
| 714 checkMutable('fill range'); | |
| 715 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); | |
| 716 } | |
| 717 | |
| 718 /** | |
| 719 * Removes the objects in the range [start] inclusive to [end] exclusive | |
| 720 * and inserts the contents of [replacement] in its place. | |
| 721 * | |
| 722 * List<int> list = [1, 2, 3, 4, 5]; | |
| 723 * list.replaceRange(1, 4, [6, 7]); | |
| 724 * list.join(', '); // '1, 6, 7, 5' | |
| 725 * | |
| 726 * An error occurs if [start]..[end] is not a valid range for `this`. | |
| 727 */ | |
| 728 void replaceRange(int start, int end, Iterable<E> replacement) { | |
| 729 checkGrowable('removeRange'); | |
| 730 IterableMixinWorkaround.replaceRangeList(this, start, end, replacement); | |
| 731 } | |
| 732 | |
| 733 /** | |
| 734 * Returns an unmodifiable [Map] view of `this`. | |
| 735 * | |
| 736 * 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 | |
| 738 * in numerical order. | |
| 739 * | |
| 740 * List<String> words = ['fee', 'fi', 'fo', 'fum']; | |
| 741 * Map<int, String> map = words.asMap(); | |
| 742 * map[0] + map[1]; // 'feefi'; | |
| 743 * map.keys.toList(); // [0, 1, 2, 3] | |
| 744 */ | |
| 745 Map<int, E> asMap() { | |
| 746 return new IterableMixinWorkaround<E>().asMapList(this); | |
| 747 } | |
| 748 } | |
| OLD | NEW |