| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.collection; | |
| 6 | |
| 7 /** | |
| 8 * Abstract implementation of a list. | |
| 9 * | |
| 10 * `ListBase` can be used as a base class for implementing the `List` interface. | |
| 11 * | |
| 12 * All operations are defined in terms of `length`, `operator[]`, | |
| 13 * `operator[]=` and `length=`, which need to be implemented. | |
| 14 * | |
| 15 * *NOTICE*: Forwarding just these four operations to a normal growable [List] | |
| 16 * (as created by `new List()`) will give very bad performance for `add` and | |
| 17 * `addAll` operations of `ListBase`. These operations are implemented by | |
| 18 * increasing the length of the list by one for each `add` operation, and | |
| 19 * repeatedly increasing the length of a growable list is not efficient. | |
| 20 * To avoid this, either override 'add' and 'addAll' to also forward directly | |
| 21 * to the growable list, or, preferably, use `DelegatingList` from | |
| 22 * "package:collection/wrappers.dart" instead. | |
| 23 */ | |
| 24 abstract class ListBase<E> extends Object with ListMixin<E> { | |
| 25 /** | |
| 26 * Convert a `List` to a string as `[each, element, as, string]`. | |
| 27 * | |
| 28 * Handles circular references where converting one of the elements | |
| 29 * to a string ends up converting [list] to a string again. | |
| 30 */ | |
| 31 static String listToString(List list) => | |
| 32 IterableBase.iterableToFullString(list, '[', ']'); | |
| 33 } | |
| 34 | |
| 35 /** | |
| 36 * Base implementation of a [List] class. | |
| 37 * | |
| 38 * `ListMixin` can be used as a mixin to make a class implement | |
| 39 * the `List` interface. | |
| 40 * | |
| 41 * This implements all read operations using only the `length` and | |
| 42 * `operator[]` members. It implements write operations using those and | |
| 43 * `length=` and `operator[]=` | |
| 44 * | |
| 45 * *NOTICE*: Forwarding just these four operations to a normal growable [List] | |
| 46 * (as created by `new List()`) will give very bad performance for `add` and | |
| 47 * `addAll` operations of `ListBase`. These operations are implemented by | |
| 48 * increasing the length of the list by one for each `add` operation, and | |
| 49 * repeatedly increasing the length of a growable list is not efficient. | |
| 50 * To avoid this, either override 'add' and 'addAll' to also forward directly | |
| 51 * to the growable list, or, if possible, use `DelegatingList` from | |
| 52 * "package:collection/wrappers.dart" instead. | |
| 53 */ | |
| 54 abstract class ListMixin<E> implements List<E> { | |
| 55 // Iterable interface. | |
| 56 Iterator<E> get iterator => new ListIterator<E>(this); | |
| 57 | |
| 58 E elementAt(int index) => this[index]; | |
| 59 | |
| 60 void forEach(void action(E element)) { | |
| 61 int length = this.length; | |
| 62 for (int i = 0; i < length; i++) { | |
| 63 action(this[i]); | |
| 64 if (length != this.length) { | |
| 65 throw new ConcurrentModificationError(this); | |
| 66 } | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 bool get isEmpty => length == 0; | |
| 71 | |
| 72 bool get isNotEmpty => !isEmpty; | |
| 73 | |
| 74 E get first { | |
| 75 if (length == 0) throw IterableElementError.noElement(); | |
| 76 return this[0]; | |
| 77 } | |
| 78 | |
| 79 E get last { | |
| 80 if (length == 0) throw IterableElementError.noElement(); | |
| 81 return this[length - 1]; | |
| 82 } | |
| 83 | |
| 84 E get single { | |
| 85 if (length == 0) throw IterableElementError.noElement(); | |
| 86 if (length > 1) throw IterableElementError.tooMany(); | |
| 87 return this[0]; | |
| 88 } | |
| 89 | |
| 90 bool contains(Object element) { | |
| 91 int length = this.length; | |
| 92 for (int i = 0; i < this.length; i++) { | |
| 93 if (this[i] == element) return true; | |
| 94 if (length != this.length) { | |
| 95 throw new ConcurrentModificationError(this); | |
| 96 } | |
| 97 } | |
| 98 return false; | |
| 99 } | |
| 100 | |
| 101 bool every(bool test(E element)) { | |
| 102 int length = this.length; | |
| 103 for (int i = 0; i < length; i++) { | |
| 104 if (!test(this[i])) return false; | |
| 105 if (length != this.length) { | |
| 106 throw new ConcurrentModificationError(this); | |
| 107 } | |
| 108 } | |
| 109 return true; | |
| 110 } | |
| 111 | |
| 112 bool any(bool test(E element)) { | |
| 113 int length = this.length; | |
| 114 for (int i = 0; i < length; i++) { | |
| 115 if (test(this[i])) return true; | |
| 116 if (length != this.length) { | |
| 117 throw new ConcurrentModificationError(this); | |
| 118 } | |
| 119 } | |
| 120 return false; | |
| 121 } | |
| 122 | |
| 123 E firstWhere(bool test(E element), { E orElse() }) { | |
| 124 int length = this.length; | |
| 125 for (int i = 0; i < length; i++) { | |
| 126 E element = this[i]; | |
| 127 if (test(element)) return element; | |
| 128 if (length != this.length) { | |
| 129 throw new ConcurrentModificationError(this); | |
| 130 } | |
| 131 } | |
| 132 if (orElse != null) return orElse(); | |
| 133 throw IterableElementError.noElement(); | |
| 134 } | |
| 135 | |
| 136 E lastWhere(bool test(E element), { E orElse() }) { | |
| 137 int length = this.length; | |
| 138 for (int i = length - 1; i >= 0; i--) { | |
| 139 E element = this[i]; | |
| 140 if (test(element)) return element; | |
| 141 if (length != this.length) { | |
| 142 throw new ConcurrentModificationError(this); | |
| 143 } | |
| 144 } | |
| 145 if (orElse != null) return orElse(); | |
| 146 throw IterableElementError.noElement(); | |
| 147 } | |
| 148 | |
| 149 E singleWhere(bool test(E element)) { | |
| 150 int length = this.length; | |
| 151 E match = null; | |
| 152 bool matchFound = false; | |
| 153 for (int i = 0; i < length; i++) { | |
| 154 E element = this[i]; | |
| 155 if (test(element)) { | |
| 156 if (matchFound) { | |
| 157 throw IterableElementError.tooMany(); | |
| 158 } | |
| 159 matchFound = true; | |
| 160 match = element; | |
| 161 } | |
| 162 if (length != this.length) { | |
| 163 throw new ConcurrentModificationError(this); | |
| 164 } | |
| 165 } | |
| 166 if (matchFound) return match; | |
| 167 throw IterableElementError.noElement(); | |
| 168 } | |
| 169 | |
| 170 String join([String separator = ""]) { | |
| 171 if (length == 0) return ""; | |
| 172 StringBuffer buffer = new StringBuffer()..writeAll(this, separator); | |
| 173 return buffer.toString(); | |
| 174 } | |
| 175 | |
| 176 Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test); | |
| 177 | |
| 178 Iterable map(f(E element)) => new MappedListIterable(this, f); | |
| 179 | |
| 180 Iterable expand(Iterable f(E element)) => | |
| 181 new ExpandIterable<E, dynamic>(this, f); | |
| 182 | |
| 183 E reduce(E combine(E previousValue, E element)) { | |
| 184 int length = this.length; | |
| 185 if (length == 0) throw IterableElementError.noElement(); | |
| 186 E value = this[0]; | |
| 187 for (int i = 1; i < length; i++) { | |
| 188 value = combine(value, this[i]); | |
| 189 if (length != this.length) { | |
| 190 throw new ConcurrentModificationError(this); | |
| 191 } | |
| 192 } | |
| 193 return value; | |
| 194 } | |
| 195 | |
| 196 fold(var initialValue, combine(var previousValue, E element)) { | |
| 197 var value = initialValue; | |
| 198 int length = this.length; | |
| 199 for (int i = 0; i < length; i++) { | |
| 200 value = combine(value, this[i]); | |
| 201 if (length != this.length) { | |
| 202 throw new ConcurrentModificationError(this); | |
| 203 } | |
| 204 } | |
| 205 return value; | |
| 206 } | |
| 207 | |
| 208 Iterable<E> skip(int count) => new SubListIterable<E>(this, count, null); | |
| 209 | |
| 210 Iterable<E> skipWhile(bool test(E element)) { | |
| 211 return new SkipWhileIterable<E>(this, test); | |
| 212 } | |
| 213 | |
| 214 Iterable<E> take(int count) => new SubListIterable<E>(this, 0, count); | |
| 215 | |
| 216 Iterable<E> takeWhile(bool test(E element)) { | |
| 217 return new TakeWhileIterable<E>(this, test); | |
| 218 } | |
| 219 | |
| 220 List<E> toList({ bool growable: true }) { | |
| 221 List<E> result; | |
| 222 if (growable) { | |
| 223 result = new List<E>()..length = length; | |
| 224 } else { | |
| 225 result = new List<E>(length); | |
| 226 } | |
| 227 for (int i = 0; i < length; i++) { | |
| 228 result[i] = this[i]; | |
| 229 } | |
| 230 return result; | |
| 231 } | |
| 232 | |
| 233 Set<E> toSet() { | |
| 234 Set<E> result = new Set<E>(); | |
| 235 for (int i = 0; i < length; i++) { | |
| 236 result.add(this[i]); | |
| 237 } | |
| 238 return result; | |
| 239 } | |
| 240 | |
| 241 // Collection interface. | |
| 242 void add(E element) { | |
| 243 this[this.length++] = element; | |
| 244 } | |
| 245 | |
| 246 void addAll(Iterable<E> iterable) { | |
| 247 for (E element in iterable) { | |
| 248 this[this.length++] = element; | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 bool remove(Object element) { | |
| 253 for (int i = 0; i < this.length; i++) { | |
| 254 if (this[i] == element) { | |
| 255 this.setRange(i, this.length - 1, this, i + 1); | |
| 256 this.length -= 1; | |
| 257 return true; | |
| 258 } | |
| 259 } | |
| 260 return false; | |
| 261 } | |
| 262 | |
| 263 void removeWhere(bool test(E element)) { | |
| 264 _filter(this, test, false); | |
| 265 } | |
| 266 | |
| 267 void retainWhere(bool test(E element)) { | |
| 268 _filter(this, test, true); | |
| 269 } | |
| 270 | |
| 271 static void _filter(List source, | |
| 272 bool test(var element), | |
| 273 bool retainMatching) { | |
| 274 List retained = []; | |
| 275 int length = source.length; | |
| 276 for (int i = 0; i < length; i++) { | |
| 277 var element = source[i]; | |
| 278 if (test(element) == retainMatching) { | |
| 279 retained.add(element); | |
| 280 } | |
| 281 if (length != source.length) { | |
| 282 throw new ConcurrentModificationError(source); | |
| 283 } | |
| 284 } | |
| 285 if (retained.length != source.length) { | |
| 286 source.setRange(0, retained.length, retained); | |
| 287 source.length = retained.length; | |
| 288 } | |
| 289 } | |
| 290 | |
| 291 void clear() { this.length = 0; } | |
| 292 | |
| 293 // List interface. | |
| 294 | |
| 295 E removeLast() { | |
| 296 if (length == 0) { | |
| 297 throw IterableElementError.noElement(); | |
| 298 } | |
| 299 E result = this[length - 1]; | |
| 300 length--; | |
| 301 return result; | |
| 302 } | |
| 303 | |
| 304 void sort([int compare(E a, E b)]) { | |
| 305 Sort.sort(this, compare == null ? Comparable.compare : compare); | |
| 306 } | |
| 307 | |
| 308 void shuffle([Random random]) { | |
| 309 if (random == null) random = new Random(); | |
| 310 int length = this.length; | |
| 311 while (length > 1) { | |
| 312 int pos = random.nextInt(length); | |
| 313 length -= 1; | |
| 314 var tmp = this[length]; | |
| 315 this[length] = this[pos]; | |
| 316 this[pos] = tmp; | |
| 317 } | |
| 318 } | |
| 319 | |
| 320 Map<int, E> asMap() { | |
| 321 return new ListMapView<E>(this); | |
| 322 } | |
| 323 | |
| 324 List<E> sublist(int start, [int end]) { | |
| 325 int listLength = this.length; | |
| 326 if (end == null) end = listLength; | |
| 327 RangeError.checkValidRange(start, end, listLength); | |
| 328 int length = end - start; | |
| 329 List<E> result = new List<E>()..length = length; | |
| 330 for (int i = 0; i < length; i++) { | |
| 331 result[i] = this[start + i]; | |
| 332 } | |
| 333 return result; | |
| 334 } | |
| 335 | |
| 336 Iterable<E> getRange(int start, int end) { | |
| 337 RangeError.checkValidRange(start, end, this.length); | |
| 338 return new SubListIterable<E>(this, start, end); | |
| 339 } | |
| 340 | |
| 341 void removeRange(int start, int end) { | |
| 342 RangeError.checkValidRange(start, end, this.length); | |
| 343 int length = end - start; | |
| 344 setRange(start, this.length - length, this, end); | |
| 345 this.length -= length; | |
| 346 } | |
| 347 | |
| 348 void fillRange(int start, int end, [E fill]) { | |
| 349 RangeError.checkValidRange(start, end, this.length); | |
| 350 for (int i = start; i < end; i++) { | |
| 351 this[i] = fill; | |
| 352 } | |
| 353 } | |
| 354 | |
| 355 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { | |
| 356 RangeError.checkValidRange(start, end, this.length); | |
| 357 int length = end - start; | |
| 358 if (length == 0) return; | |
| 359 RangeError.checkNotNegative(skipCount, "skipCount"); | |
| 360 | |
| 361 List otherList; | |
| 362 int otherStart; | |
| 363 // TODO(floitsch): Make this accept more. | |
| 364 if (iterable is List) { | |
| 365 otherList = iterable; | |
| 366 otherStart = skipCount; | |
| 367 } else { | |
| 368 otherList = iterable.skip(skipCount).toList(growable: false); | |
| 369 otherStart = 0; | |
| 370 } | |
| 371 if (otherStart + length > otherList.length) { | |
| 372 throw IterableElementError.tooFew(); | |
| 373 } | |
| 374 if (otherStart < start) { | |
| 375 // Copy backwards to ensure correct copy if [from] is this. | |
| 376 for (int i = length - 1; i >= 0; i--) { | |
| 377 this[start + i] = otherList[otherStart + i]; | |
| 378 } | |
| 379 } else { | |
| 380 for (int i = 0; i < length; i++) { | |
| 381 this[start + i] = otherList[otherStart + i]; | |
| 382 } | |
| 383 } | |
| 384 } | |
| 385 | |
| 386 void replaceRange(int start, int end, Iterable<E> newContents) { | |
| 387 RangeError.checkValidRange(start, end, this.length); | |
| 388 if (newContents is! EfficientLength) { | |
| 389 newContents = newContents.toList(); | |
| 390 } | |
| 391 int removeLength = end - start; | |
| 392 int insertLength = newContents.length; | |
| 393 if (removeLength >= insertLength) { | |
| 394 int delta = removeLength - insertLength; | |
| 395 int insertEnd = start + insertLength; | |
| 396 int newLength = this.length - delta; | |
| 397 this.setRange(start, insertEnd, newContents); | |
| 398 if (delta != 0) { | |
| 399 this.setRange(insertEnd, newLength, this, end); | |
| 400 this.length = newLength; | |
| 401 } | |
| 402 } else { | |
| 403 int delta = insertLength - removeLength; | |
| 404 int newLength = this.length + delta; | |
| 405 int insertEnd = start + insertLength; // aka. end + delta. | |
| 406 this.length = newLength; | |
| 407 this.setRange(insertEnd, newLength, this, end); | |
| 408 this.setRange(start, insertEnd, newContents); | |
| 409 } | |
| 410 } | |
| 411 | |
| 412 int indexOf(Object element, [int startIndex = 0]) { | |
| 413 if (startIndex >= this.length) { | |
| 414 return -1; | |
| 415 } | |
| 416 if (startIndex < 0) { | |
| 417 startIndex = 0; | |
| 418 } | |
| 419 for (int i = startIndex; i < this.length; i++) { | |
| 420 if (this[i] == element) { | |
| 421 return i; | |
| 422 } | |
| 423 } | |
| 424 return -1; | |
| 425 } | |
| 426 | |
| 427 /** | |
| 428 * Returns the last index in the list [a] of the given [element], starting | |
| 429 * the search at index [startIndex] to 0. | |
| 430 * Returns -1 if [element] is not found. | |
| 431 */ | |
| 432 int lastIndexOf(Object element, [int startIndex]) { | |
| 433 if (startIndex == null) { | |
| 434 startIndex = this.length - 1; | |
| 435 } else { | |
| 436 if (startIndex < 0) { | |
| 437 return -1; | |
| 438 } | |
| 439 if (startIndex >= this.length) { | |
| 440 startIndex = this.length - 1; | |
| 441 } | |
| 442 } | |
| 443 for (int i = startIndex; i >= 0; i--) { | |
| 444 if (this[i] == element) { | |
| 445 return i; | |
| 446 } | |
| 447 } | |
| 448 return -1; | |
| 449 } | |
| 450 | |
| 451 void insert(int index, E element) { | |
| 452 RangeError.checkValueInInterval(index, 0, length, "index"); | |
| 453 if (index == this.length) { | |
| 454 add(element); | |
| 455 return; | |
| 456 } | |
| 457 // We are modifying the length just below the is-check. Without the check | |
| 458 // Array.copy could throw an exception, leaving the list in a bad state | |
| 459 // (with a length that has been increased, but without a new element). | |
| 460 if (index is! int) throw new ArgumentError(index); | |
| 461 this.length++; | |
| 462 setRange(index + 1, this.length, this, index); | |
| 463 this[index] = element; | |
| 464 } | |
| 465 | |
| 466 E removeAt(int index) { | |
| 467 E result = this[index]; | |
| 468 setRange(index, this.length - 1, this, index + 1); | |
| 469 length--; | |
| 470 return result; | |
| 471 } | |
| 472 | |
| 473 void insertAll(int index, Iterable<E> iterable) { | |
| 474 RangeError.checkValueInInterval(index, 0, length, "index"); | |
| 475 if (iterable is EfficientLength) { | |
| 476 iterable = iterable.toList(); | |
| 477 } | |
| 478 int insertionLength = iterable.length; | |
| 479 // There might be errors after the length change, in which case the list | |
| 480 // will end up being modified but the operation not complete. Unless we | |
| 481 // always go through a "toList" we can't really avoid that. | |
| 482 this.length += insertionLength; | |
| 483 setRange(index + insertionLength, this.length, this, index); | |
| 484 setAll(index, iterable); | |
| 485 } | |
| 486 | |
| 487 void setAll(int index, Iterable<E> iterable) { | |
| 488 if (iterable is List) { | |
| 489 setRange(index, index + iterable.length, iterable); | |
| 490 } else { | |
| 491 for (E element in iterable) { | |
| 492 this[index++] = element; | |
| 493 } | |
| 494 } | |
| 495 } | |
| 496 | |
| 497 Iterable<E> get reversed => new ReversedListIterable<E>(this); | |
| 498 | |
| 499 String toString() => IterableBase.iterableToFullString(this, '[', ']'); | |
| 500 } | |
| OLD | NEW |