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 /** A reusable set used to identify cyclic lists during toString() calls. */ | |
8 Set _toStringVisiting = new HashSet.identity(); | |
9 | |
10 /** | 7 /** |
11 * Abstract implementation of a list. | 8 * Abstract implementation of a list. |
12 * | 9 * |
13 * All operations are defined in terms of `length`, `operator[]`, | 10 * All operations are defined in terms of `length`, `operator[]`, |
14 * `operator[]=` and `length=`, which need to be implemented. | 11 * `operator[]=` and `length=`, which need to be implemented. |
15 * | 12 * |
16 * *NOTICE*: Forwarding just these four operations to a normal growable [List] | 13 * *NOTICE*: Forwarding just these four operations to a normal growable [List] |
17 * (as created by `new List()`) will give very bad performance for `add` and | 14 * (as created by `new List()`) will give very bad performance for `add` and |
18 * `addAll` operations of `ListBase`. These operations are implemented by | 15 * `addAll` operations of `ListBase`. These operations are implemented by |
19 * increasing the length of the list by one for each `add` operation, and | 16 * increasing the length of the list by one for each `add` operation, and |
20 * repeatedly increasing the length of a growable list is not efficient. | 17 * repeatedly increasing the length of a growable list is not efficient. |
21 * To avoid this, either override 'add' and 'addAll' to also forward directly | 18 * To avoid this, either override 'add' and 'addAll' to also forward directly |
22 * to the growable list, or, preferably, use `DelegatingList` from | 19 * to the growable list, or, preferably, use `DelegatingList` from |
23 * "package:collection/wrappers.dart" instead. | 20 * "package:collection/wrappers.dart" instead. |
24 */ | 21 */ |
25 abstract class ListBase<E> = Object with ListMixin<E>; | 22 abstract class ListBase<E> extends Object with ListMixin<E> { |
| 23 /** |
| 24 * Convert a `List` to a string as `[each, element, as, string]`. |
| 25 * |
| 26 * Handles circular references where converting one of the elements |
| 27 * to a string ends up converting [list] to a string again. |
| 28 */ |
| 29 static String listToString(List list) => |
| 30 IterableBase.iterableToFullString(list, '[', ']'); |
| 31 } |
26 | 32 |
27 /** | 33 /** |
28 * Base implementation of a [List] class. | 34 * Base implementation of a [List] class. |
29 * | 35 * |
30 * This class can be used as a mixin. | 36 * This class can be used as a mixin. |
31 * | 37 * |
32 * This implements all read operations using only the `length` and | 38 * This implements all read operations using only the `length` and |
33 * `operator[]` members. It implements write operations using those and | 39 * `operator[]` members. It implements write operations using those and |
34 * `length=` and `operator[]=` | 40 * `length=` and `operator[]=` |
35 * | 41 * |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 setRange(index, index + iterable.length, iterable); | 500 setRange(index, index + iterable.length, iterable); |
495 } else { | 501 } else { |
496 for (E element in iterable) { | 502 for (E element in iterable) { |
497 this[index++] = element; | 503 this[index++] = element; |
498 } | 504 } |
499 } | 505 } |
500 } | 506 } |
501 | 507 |
502 Iterable<E> get reversed => new ReversedListIterable(this); | 508 Iterable<E> get reversed => new ReversedListIterable(this); |
503 | 509 |
504 String toString() { | 510 String toString() => IterableBase.iterableToFullString(this, '[', ']'); |
505 if (_toStringVisiting.contains(this)) { | |
506 return '[...]'; | |
507 } | |
508 | |
509 var result = new StringBuffer(); | |
510 try { | |
511 _toStringVisiting.add(this); | |
512 result.write('['); | |
513 result.writeAll(this, ', '); | |
514 result.write(']'); | |
515 } finally { | |
516 _toStringVisiting.remove(this); | |
517 } | |
518 | |
519 return result.toString(); | |
520 } | |
521 } | 511 } |
OLD | NEW |