Chromium Code Reviews| 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 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 } | 167 } |
| 168 } | 168 } |
| 169 return buffer.toString(); | 169 return buffer.toString(); |
| 170 } | 170 } |
| 171 } | 171 } |
| 172 | 172 |
| 173 Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test); | 173 Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test); |
| 174 | 174 |
| 175 Iterable map(f(E element)) => new MappedListIterable(this, f); | 175 Iterable map(f(E element)) => new MappedListIterable(this, f); |
| 176 | 176 |
| 177 Iterable expand(Iterable f(E element)) => | |
|
floitsch
2013/04/15 16:22:48
Extracted into a separate CL.
I need to commit bef
| |
| 178 new ExpandIterable<E, dynamic>(this, f); | |
| 179 | |
| 177 E reduce(E combine(E previousValue, E element)) { | 180 E reduce(E combine(E previousValue, E element)) { |
| 178 if (length == 0) throw new StateError("No elements"); | 181 if (length == 0) throw new StateError("No elements"); |
| 179 E value = this[0]; | 182 E value = this[0]; |
| 180 for (int i = 1; i < length; i++) { | 183 for (int i = 1; i < length; i++) { |
| 181 value = combine(value, this[i]); | 184 value = combine(value, this[i]); |
| 182 } | 185 } |
| 183 return value; | 186 return value; |
| 184 } | 187 } |
| 185 | 188 |
| 186 fold(var initialValue, combine(var previousValue, E element)) { | 189 fold(var initialValue, combine(var previousValue, E element)) { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 } | 294 } |
| 292 | 295 |
| 293 void sort([Comparator<E> compare]) { | 296 void sort([Comparator<E> compare]) { |
| 294 Sort.sort(this, compare); | 297 Sort.sort(this, compare); |
| 295 } | 298 } |
| 296 | 299 |
| 297 Map<int, E> asMap() { | 300 Map<int, E> asMap() { |
| 298 return new ListMapView(this); | 301 return new ListMapView(this); |
| 299 } | 302 } |
| 300 | 303 |
| 301 List<E> sublist(int start, [int end]) { | 304 void _rangeCheck(int start, int end) { |
| 302 if (end == null) end = length; | |
| 303 if (start < 0 || start > this.length) { | 305 if (start < 0 || start > this.length) { |
| 304 throw new RangeError.range(start, 0, this.length); | 306 throw new RangeError.range(start, 0, this.length); |
| 305 } | 307 } |
| 306 if (end < start || end > this.length) { | 308 if (end < start || end > this.length) { |
| 307 throw new RangeError.range(end, start, this.length); | 309 throw new RangeError.range(end, start, this.length); |
| 308 } | 310 } |
| 311 } | |
| 312 | |
| 313 List<E> sublist(int start, [int end]) { | |
| 314 if (end == null) end = length; | |
| 315 _rangeCheck(start, end); | |
| 309 int length = end - start; | 316 int length = end - start; |
| 310 List<E> result = new List<E>()..length = length; | 317 List<E> result = new List<E>()..length = length; |
| 311 for (int i = 0; i < length; i++) { | 318 for (int i = 0; i < length; i++) { |
| 312 result[i] = this[start + i]; | 319 result[i] = this[start + i]; |
| 313 } | 320 } |
| 314 return result; | 321 return result; |
| 315 } | 322 } |
| 316 | 323 |
| 317 Iterable<E> getRange(int start, int end) { | 324 Iterable<E> getRange(int start, int end) { |
| 318 if (start < 0 || start > this.length) { | 325 _rangeCheck(start, end); |
| 319 throw new RangeError.range(start, 0, this.length); | |
| 320 } | |
| 321 if (end < start || end > this.length) { | |
| 322 throw new RangeError.range(end, start, this.length); | |
| 323 } | |
| 324 return new SubListIterable(this, start, end); | 326 return new SubListIterable(this, start, end); |
| 325 } | 327 } |
| 326 | 328 |
| 327 void removeRange(int start, int end) { | 329 void removeRange(int start, int end) { |
| 328 if (start < 0 || start > this.length) { | 330 _rangeCheck(start, end); |
| 329 throw new RangeError.range(start, 0, this.length); | |
| 330 } | |
| 331 if (end < start || end > this.length) { | |
| 332 throw new RangeError.range(end, start, this.length); | |
| 333 } | |
| 334 int length = end - start; | 331 int length = end - start; |
| 335 setRange(start, this.length - length, this, end); | 332 setRange(start, this.length - length, this, end); |
| 336 this.length -= length; | 333 this.length -= length; |
| 337 } | 334 } |
| 338 | 335 |
| 339 void clearRange(int start, int length, [E fill]) { | 336 void fillRange(int start, int end, [E fill]) { |
| 340 for (int i = 0; i < length; i++) { | 337 _rangeCheck(start, end); |
| 341 this[start + i] = fill; | 338 for (int i = start; i < end; i++) { |
| 339 this[i] = fill; | |
| 342 } | 340 } |
| 343 } | 341 } |
| 344 | 342 |
| 345 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { | 343 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { |
| 346 if (start < 0 || start > this.length) { | 344 _rangeCheck(start, end); |
| 347 throw new RangeError.range(start, 0, this.length); | |
| 348 } | |
| 349 if (end < 0 || end > this.length) { | |
| 350 throw new RangeError.range(end, start, this.length); | |
| 351 } | |
| 352 int length = end - start; | 345 int length = end - start; |
| 353 if (length == 0) return; | 346 if (length == 0) return; |
| 354 | 347 |
| 355 if (skipCount < 0) throw new ArgumentError(skipCount); | 348 if (skipCount < 0) throw new ArgumentError(skipCount); |
| 356 | 349 |
| 357 List otherList; | 350 List otherList; |
| 358 int otherStart; | 351 int otherStart; |
| 359 // TODO(floitsch): Make this accept more. | 352 // TODO(floitsch): Make this accept more. |
| 360 if (iterable is List) { | 353 if (iterable is List) { |
| 361 otherList = iterable; | 354 otherList = iterable; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 372 for (int i = length - 1; i >= 0; i--) { | 365 for (int i = length - 1; i >= 0; i--) { |
| 373 this[start + i] = otherList[otherStart + i]; | 366 this[start + i] = otherList[otherStart + i]; |
| 374 } | 367 } |
| 375 } else { | 368 } else { |
| 376 for (int i = 0; i < length; i++) { | 369 for (int i = 0; i < length; i++) { |
| 377 this[start + i] = otherList[otherStart + i]; | 370 this[start + i] = otherList[otherStart + i]; |
| 378 } | 371 } |
| 379 } | 372 } |
| 380 } | 373 } |
| 381 | 374 |
| 375 void replaceRange(int start, int end, Iterable<E> newContents) { | |
| 376 // TODO(floitsch): Optimize this. | |
| 377 removeRange(start, end); | |
| 378 insertAll(start, newContents); | |
| 379 } | |
| 380 | |
| 382 int indexOf(E element, [int startIndex = 0]) { | 381 int indexOf(E element, [int startIndex = 0]) { |
| 383 if (startIndex >= this.length) { | 382 if (startIndex >= this.length) { |
| 384 return -1; | 383 return -1; |
| 385 } | 384 } |
| 386 if (startIndex < 0) { | 385 if (startIndex < 0) { |
| 387 startIndex = 0; | 386 startIndex = 0; |
| 388 } | 387 } |
| 389 for (int i = startIndex; i < this.length; i++) { | 388 for (int i = startIndex; i < this.length; i++) { |
| 390 if (this[i] == element) { | 389 if (this[i] == element) { |
| 391 return i; | 390 return i; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 411 } | 410 } |
| 412 } | 411 } |
| 413 for (int i = startIndex; i >= 0; i--) { | 412 for (int i = startIndex; i >= 0; i--) { |
| 414 if (this[i] == element) { | 413 if (this[i] == element) { |
| 415 return i; | 414 return i; |
| 416 } | 415 } |
| 417 } | 416 } |
| 418 return -1; | 417 return -1; |
| 419 } | 418 } |
| 420 | 419 |
| 420 void insert(int index, E element) { | |
|
floitsch
2013/04/15 16:22:48
Extracted into a separate CL.
I need to commit bef
| |
| 421 if (index < 0 || index > length) { | |
| 422 throw new RangeError.range(index, 0, length); | |
| 423 } | |
| 424 if (index == this.length) { | |
| 425 add(element); | |
| 426 return; | |
| 427 } | |
| 428 // We are modifying the length just below the is-check. Without the check | |
| 429 // Array.copy could throw an exception, leaving the list in a bad state | |
| 430 // (with a length that has been increased, but without a new element). | |
| 431 if (index is! int) throw new ArgumentError(index); | |
| 432 this.length++; | |
| 433 setRange(index + 1, this.length, this, index); | |
| 434 this[index] = element; | |
| 435 } | |
| 436 | |
| 437 E removeAt(int index) { | |
|
floitsch
2013/04/15 16:22:48
Extracted into a separate CL.
I need to commit bef
| |
| 438 E result = this[index]; | |
| 439 setRange(index, this.length - 1, this, index + 1); | |
| 440 length--; | |
| 441 return result; | |
| 442 } | |
| 443 | |
| 444 void insertAll(int index, Iterable<E> iterable) { | |
| 445 if (index < 0 || index > length) { | |
| 446 throw new RangeError.range(index, 0, length); | |
| 447 } | |
| 448 // TODO(floitsch): we can probably detect more cases. | |
| 449 if (iterable is! List && iterable is! Set && iterable is! SubListIterable) { | |
| 450 iterable = iterable.toList(); | |
| 451 } | |
| 452 int insertionLength = iterable.length; | |
| 453 // There might be errors after the length change, in which case the list | |
| 454 // will end up being modified but the operation not complete. Unless we | |
| 455 // always go through a "toList" we can't really avoid that. | |
| 456 this.length += insertionLength; | |
| 457 setRange(index + insertionLength, this.length, this, index); | |
| 458 setAll(index, iterable); | |
| 459 } | |
| 460 | |
| 461 void setAll(int index, Iterable<E> iterable) { | |
| 462 if (iterable is List) { | |
| 463 setRange(index, index + iterable.length, iterable); | |
| 464 } else { | |
| 465 for (E element in iterable) { | |
| 466 this[index++] = element; | |
| 467 } | |
| 468 } | |
| 469 } | |
| 470 | |
| 421 Iterable<E> get reversed => new ReversedListIterable(this); | 471 Iterable<E> get reversed => new ReversedListIterable(this); |
| 422 } | 472 } |
| OLD | NEW |