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 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 | 235 |
236 void addAll(Iterable<E> iterable) { | 236 void addAll(Iterable<E> iterable) { |
237 for (E element in iterable) { | 237 for (E element in iterable) { |
238 this[this.length++] = element; | 238 this[this.length++] = element; |
239 } | 239 } |
240 } | 240 } |
241 | 241 |
242 void remove(Object element) { | 242 void remove(Object element) { |
243 for (int i = 0; i < this.length; i++) { | 243 for (int i = 0; i < this.length; i++) { |
244 if (this[i] == element) { | 244 if (this[i] == element) { |
245 this.setRange(i, this.length - i - 1, this, i + 1); | 245 this.setRange(i, i + this.length - 1, this, i + 1); |
246 this.length -= 1; | 246 this.length -= 1; |
247 return; | 247 return; |
248 } | 248 } |
249 } | 249 } |
250 } | 250 } |
251 | 251 |
252 void removeWhere(bool test(E element)) { | 252 void removeWhere(bool test(E element)) { |
253 _filter(this, test, false); | 253 _filter(this, test, false); |
254 } | 254 } |
255 | 255 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 } | 325 } |
326 | 326 |
327 void insertRange(int start, int length, [E initialValue]) { | 327 void insertRange(int start, int length, [E initialValue]) { |
328 if (start < 0 || start > this.length) { | 328 if (start < 0 || start > this.length) { |
329 throw new RangeError.range(start, 0, this.length); | 329 throw new RangeError.range(start, 0, this.length); |
330 } | 330 } |
331 int oldLength = this.length; | 331 int oldLength = this.length; |
332 int moveLength = oldLength - start; | 332 int moveLength = oldLength - start; |
333 this.length += length; | 333 this.length += length; |
334 if (moveLength > 0) { | 334 if (moveLength > 0) { |
335 this.setRange(start + length, moveLength, this, start); | 335 this.setRange(start + length, oldLength, this, start); |
336 } | 336 } |
337 for (int i = 0; i < length; i++) { | 337 for (int i = 0; i < length; i++) { |
338 this[start + i] = initialValue; | 338 this[start + i] = initialValue; |
339 } | 339 } |
340 } | 340 } |
341 | 341 |
342 void removeRange(int start, int length) { | 342 void removeRange(int start, int length) { |
343 if (start < 0 || start > this.length) { | 343 if (start < 0 || start > this.length) { |
344 throw new RangeError.range(start, 0, this.length); | 344 throw new RangeError.range(start, 0, this.length); |
345 } | 345 } |
346 if (length < 0 || start + length > this.length) { | 346 if (length < 0 || start + length > this.length) { |
347 throw new RangeError.range(length, 0, this.length - start); | 347 throw new RangeError.range(length, 0, this.length - start); |
348 } | 348 } |
349 int end = start + length; | 349 int end = start + length; |
350 setRange(start, this.length - end, this, end); | 350 setRange(start, this.length - length, this, end); |
351 this.length -= length; | 351 this.length -= length; |
352 } | 352 } |
353 | 353 |
354 void clearRange(int start, int length, [E fill]) { | 354 void clearRange(int start, int length, [E fill]) { |
355 for (int i = 0; i < length; i++) { | 355 for (int i = 0; i < length; i++) { |
356 this[start + i] = fill; | 356 this[start + i] = fill; |
357 } | 357 } |
358 } | 358 } |
359 | 359 |
360 void setRange(int start, int length, List<E> from, [int startFrom]) { | 360 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { |
361 if (start < 0 || start > this.length) { | 361 if (start < 0 || start > this.length) { |
362 throw new RangeError.range(start, 0, this.length); | 362 throw new RangeError.range(start, 0, this.length); |
363 } | 363 } |
364 if (length < 0 || start + length > this.length) { | 364 if (end < 0 || end > this.length) { |
365 throw new RangeError.range(length, 0, this.length - start); | 365 throw new RangeError.range(end, start, this.length); |
366 } | 366 } |
367 if (startFrom == null) { | 367 int length = end - start; |
368 startFrom = 0; | 368 if (length == 0) return; |
| 369 |
| 370 if (skipCount < 0) throw new ArgumentError(skipCount); |
| 371 |
| 372 List otherList; |
| 373 int otherStart; |
| 374 // TODO(floitsch): Make this accept more. |
| 375 if (iterable is List) { |
| 376 otherList = iterable; |
| 377 otherStart = skipCount; |
| 378 } else { |
| 379 otherList = iterable.skip(skipCount).toList(growable: false); |
| 380 otherStart = 0; |
369 } | 381 } |
370 if (startFrom < 0 || startFrom + length > from.length) { | 382 if (otherStart + length > otherList.length) { |
371 throw new RangeError.range(startFrom, 0, from.length - length); | 383 throw new StateError("Not enough elements"); |
372 } | 384 } |
373 if (startFrom < start) { | 385 if (otherStart < start) { |
374 // Copy backwards to ensure correct copy if [from] is this. | 386 // Copy backwards to ensure correct copy if [from] is this. |
375 for (int i = length - 1; i >= 0; i--) { | 387 for (int i = length - 1; i >= 0; i--) { |
376 this[start + i] = from[startFrom + i]; | 388 this[start + i] = otherList[otherStart + i]; |
377 } | 389 } |
378 } else { | 390 } else { |
379 for (int i = 0; i < length; i++) { | 391 for (int i = 0; i < length; i++) { |
380 this[start + i] = from[startFrom + i]; | 392 this[start + i] = otherList[otherStart + i]; |
381 } | 393 } |
382 } | 394 } |
383 } | 395 } |
384 | 396 |
385 int indexOf(E element, [int startIndex = 0]) { | 397 int indexOf(E element, [int startIndex = 0]) { |
386 if (startIndex >= this.length) { | 398 if (startIndex >= this.length) { |
387 return -1; | 399 return -1; |
388 } | 400 } |
389 if (startIndex < 0) { | 401 if (startIndex < 0) { |
390 startIndex = 0; | 402 startIndex = 0; |
(...skipping 27 matching lines...) Expand all Loading... |
418 return i; | 430 return i; |
419 } | 431 } |
420 } | 432 } |
421 return -1; | 433 return -1; |
422 } | 434 } |
423 | 435 |
424 Iterable<E> get reversed => new ReversedListIterable(this); | 436 Iterable<E> get reversed => new ReversedListIterable(this); |
425 | 437 |
426 String toString() => ToString.iterableToString(this); | 438 String toString() => ToString.iterableToString(this); |
427 } | 439 } |
OLD | NEW |