OLD | NEW |
| (Empty) |
1 // TODO(jmesserly): remove this once we have a subclassable growable list | |
2 // in our libraries. | |
3 | |
4 /// A [List] proxy that you can subclass. | |
5 library list_proxy; | |
6 | |
7 import 'dart:collection'; | |
8 import 'dart:math' show Random; | |
9 | |
10 // TOOD(jmesserly): this needs to be removed, but fixing NodeList is tricky. | |
11 class ListProxy<E> extends IterableBase<E> implements List<E> { | |
12 | |
13 /// The inner [List<T>] with the actual storage. | |
14 final List<E> _list; | |
15 | |
16 /// Creates a list proxy. | |
17 /// You can optionally specify the list to use for [storage] of the items, | |
18 /// otherwise this will create a [List<E>]. | |
19 ListProxy([List<E> storage]) | |
20 : _list = storage != null ? storage : <E>[]; | |
21 | |
22 // TODO(jmesserly): This should be on List. | |
23 // See http://code.google.com/p/dart/issues/detail?id=947 | |
24 bool remove(E item) { | |
25 int i = indexOf(item); | |
26 if (i == -1) return false; | |
27 removeAt(i); | |
28 return true; | |
29 } | |
30 | |
31 void insert(int index, E item) => _list.insert(index, item); | |
32 | |
33 // Override from Iterable to fix performance | |
34 // Length and last become O(1) instead of O(N) | |
35 // The others are just different constant factor. | |
36 int get length => _list.length; | |
37 E get last => _list.last; | |
38 E get first => _list.first; | |
39 E get single => _list.single; | |
40 | |
41 // From Iterable | |
42 Iterator<E> get iterator => _list.iterator; | |
43 | |
44 // From List | |
45 E operator [](int index) => _list[index]; | |
46 operator []=(int index, E value) { _list[index] = value; } | |
47 set length(int value) { _list.length = value; } | |
48 void add(E value) { _list.add(value); } | |
49 | |
50 void addLast(E value) { add(value); } | |
51 void addAll(Iterable<E> collection) { _list.addAll(collection); } | |
52 void sort([int compare(E a, E b)]) { _list.sort(compare); } | |
53 void shuffle([Random random]) { _list.shuffle(random); } | |
54 | |
55 int indexOf(E element, [int start = 0]) => _list.indexOf(element, start); | |
56 int lastIndexOf(E element, [int start]) => _list.lastIndexOf(element, start); | |
57 void clear() { _list.clear(); } | |
58 | |
59 E removeAt(int index) => _list.removeAt(index); | |
60 E removeLast() => _list.removeLast(); | |
61 | |
62 void removeWhere(bool test(E element)) => _list.removeWhere(test); | |
63 void retainWhere(bool test(E element)) => _list.retainWhere(test); | |
64 | |
65 List<E> sublist(int start, [int end]) => _list.sublist(start, end); | |
66 | |
67 List<E> getRange(int start, int end) => _list.getRange(start, end); | |
68 | |
69 void setRange(int start, int length, List<E> from, [int startFrom = 0]) { | |
70 _list.setRange(start, length, from, startFrom); | |
71 } | |
72 void removeRange(int start, int length) { _list.removeRange(start, length); } | |
73 void insertAll(int index, Iterable<E> iterable) { | |
74 _list.insertAll(index, iterable); | |
75 } | |
76 | |
77 Iterable<E> get reversed => _list.reversed; | |
78 | |
79 Map<int, E> asMap() => _list.asMap(); | |
80 | |
81 void replaceRange(int start, int end, Iterable<E> newContents) => | |
82 _list.replaceRange(start, end, newContents); | |
83 | |
84 void setAll(int index, Iterable<E> iterable) => _list.setAll(index, iterable); | |
85 | |
86 void fillRange(int start, int end, [E fillValue]) | |
87 => _list.fillRange(start, end, fillValue); | |
88 } | |
OLD | NEW |