OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.collection; | 5 part of dart.collection; |
6 | 6 |
7 /** | 7 /** |
8 * Abstract implementation of a list. | 8 * Abstract implementation of a list. |
9 * | 9 * |
10 * All operations are defined in terms of `length`, `operator[]`, | 10 * All operations are defined in terms of `length`, `operator[]`, |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 | 270 |
271 void addAll(Iterable<E> iterable) { | 271 void addAll(Iterable<E> iterable) { |
272 for (E element in iterable) { | 272 for (E element in iterable) { |
273 this[this.length++] = element; | 273 this[this.length++] = element; |
274 } | 274 } |
275 } | 275 } |
276 | 276 |
277 void remove(Object element) { | 277 void remove(Object element) { |
278 for (int i = 0; i < this.length; i++) { | 278 for (int i = 0; i < this.length; i++) { |
279 if (this[i] == element) { | 279 if (this[i] == element) { |
280 this.setRange(i, this.length - i - 1, this, i + 1); | 280 this.setRange(i, i + this.length - 1, this, i + 1); |
281 this.length -= 1; | 281 this.length -= 1; |
282 return; | 282 return; |
283 } | 283 } |
284 } | 284 } |
285 } | 285 } |
286 | 286 |
287 void removeAll(Iterable<Object> elements) { | 287 void removeAll(Iterable<Object> elements) { |
288 if (elements is! Set) { | 288 if (elements is! Set) { |
289 elements = elements.toSet(); | 289 elements = elements.toSet(); |
290 } | 290 } |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 } | 375 } |
376 | 376 |
377 void insertRange(int start, int length, [E initialValue]) { | 377 void insertRange(int start, int length, [E initialValue]) { |
378 if (start < 0 || start > this.length) { | 378 if (start < 0 || start > this.length) { |
379 throw new RangeError.range(start, 0, this.length); | 379 throw new RangeError.range(start, 0, this.length); |
380 } | 380 } |
381 int oldLength = this.length; | 381 int oldLength = this.length; |
382 int moveLength = oldLength - start; | 382 int moveLength = oldLength - start; |
383 this.length += length; | 383 this.length += length; |
384 if (moveLength > 0) { | 384 if (moveLength > 0) { |
385 this.setRange(start + length, moveLength, this, start); | 385 this.setRange(start + length, oldLength, this, start); |
386 } | 386 } |
387 for (int i = 0; i < length; i++) { | 387 for (int i = 0; i < length; i++) { |
388 this[start + i] = initialValue; | 388 this[start + i] = initialValue; |
389 } | 389 } |
390 } | 390 } |
391 | 391 |
392 void removeRange(int start, int length) { | 392 void removeRange(int start, int length) { |
393 if (start < 0 || start > this.length) { | 393 if (start < 0 || start > this.length) { |
394 throw new RangeError.range(start, 0, this.length); | 394 throw new RangeError.range(start, 0, this.length); |
395 } | 395 } |
396 if (length < 0 || start + length > this.length) { | 396 if (length < 0 || start + length > this.length) { |
397 throw new RangeError.range(length, 0, this.length - start); | 397 throw new RangeError.range(length, 0, this.length - start); |
398 } | 398 } |
399 int end = start + length; | 399 int end = start + length; |
400 setRange(start, this.length - end, this, end); | 400 setRange(start, this.length - length, this, end); |
401 this.length -= length; | 401 this.length -= length; |
402 } | 402 } |
403 | 403 |
404 void clearRange(int start, int length, [E fill]) { | 404 void clearRange(int start, int length, [E fill]) { |
405 for (int i = 0; i < length; i++) { | 405 for (int i = 0; i < length; i++) { |
406 this[start + i] = fill; | 406 this[start + i] = fill; |
407 } | 407 } |
408 } | 408 } |
409 | 409 |
410 void setRange(int start, int length, List<E> from, [int startFrom]) { | 410 void setRange(int start, int end, List<E> from, [int startFrom]) { |
411 if (start < 0 || start > this.length) { | 411 if (start < 0 || start > this.length) { |
412 throw new RangeError.range(start, 0, this.length); | 412 throw new RangeError.range(start, 0, this.length); |
413 } | 413 } |
414 if (length < 0 || start + length > this.length) { | 414 if (end < 0 || end > this.length) { |
415 throw new RangeError.range(length, 0, this.length - start); | 415 throw new RangeError.range(end, start, this.length); |
416 } | 416 } |
417 if (startFrom == null) { | 417 if (startFrom == null) { |
418 startFrom = 0; | 418 startFrom = 0; |
419 } | 419 } |
| 420 int length = end - start; |
420 if (startFrom < 0 || startFrom + length > from.length) { | 421 if (startFrom < 0 || startFrom + length > from.length) { |
421 throw new RangeError.range(startFrom, 0, from.length - length); | 422 throw new RangeError.range(startFrom, 0, from.length - length); |
422 } | 423 } |
423 if (startFrom < start) { | 424 if (startFrom < start) { |
424 // Copy backwards to ensure correct copy if [from] is this. | 425 // Copy backwards to ensure correct copy if [from] is this. |
425 for (int i = length - 1; i >= 0; i--) { | 426 for (int i = length - 1; i >= 0; i--) { |
426 this[start + i] = from[startFrom + i]; | 427 this[start + i] = from[startFrom + i]; |
427 } | 428 } |
428 } else { | 429 } else { |
429 for (int i = 0; i < length; i++) { | 430 for (int i = 0; i < length; i++) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 for (int i = startIndex; i >= 0; i--) { | 467 for (int i = startIndex; i >= 0; i--) { |
467 if (this[i] == element) { | 468 if (this[i] == element) { |
468 return i; | 469 return i; |
469 } | 470 } |
470 } | 471 } |
471 return -1; | 472 return -1; |
472 } | 473 } |
473 | 474 |
474 Iterable<E> get reversed => new ReversedListIterable(this); | 475 Iterable<E> get reversed => new ReversedListIterable(this); |
475 } | 476 } |
OLD | NEW |