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]) : _list = storage != null ? storage : <E>[]; | |
20 | |
21 // TODO(jmesserly): This should be on List. | |
22 // See http://code.google.com/p/dart/issues/detail?id=947 | |
23 bool remove(E item) { | |
24 int i = indexOf(item); | |
25 if (i == -1) return false; | |
26 removeAt(i); | |
27 return true; | |
28 } | |
29 | |
30 void insert(int index, E item) => _list.insert(index, item); | |
31 | |
32 // Override from Iterable to fix performance | |
33 // Length and last become O(1) instead of O(N) | |
34 // The others are just different constant factor. | |
35 int get length => _list.length; | |
36 E get last => _list.last; | |
37 E get first => _list.first; | |
38 E get single => _list.single; | |
39 | |
40 // From Iterable | |
41 Iterator<E> get iterator => _list.iterator; | |
42 | |
43 // From List | |
44 E operator [](int index) => _list[index]; | |
45 operator []=(int index, E value) { | |
46 _list[index] = value; | |
47 } | |
48 set length(int value) { | |
49 _list.length = value; | |
50 } | |
51 void add(E value) { | |
52 _list.add(value); | |
53 } | |
54 | |
55 void addLast(E value) { | |
56 add(value); | |
57 } | |
58 void addAll(Iterable<E> collection) { | |
59 _list.addAll(collection); | |
60 } | |
61 void sort([int compare(E a, E b)]) { | |
62 _list.sort(compare); | |
63 } | |
64 void shuffle([Random random]) { | |
65 _list.shuffle(random); | |
66 } | |
67 | |
68 int indexOf(E element, [int start = 0]) => _list.indexOf(element, start); | |
69 int lastIndexOf(E element, [int start]) => _list.lastIndexOf(element, start); | |
70 void clear() { | |
71 _list.clear(); | |
72 } | |
73 | |
74 E removeAt(int index) => _list.removeAt(index); | |
75 E removeLast() => _list.removeLast(); | |
76 | |
77 void removeWhere(bool test(E element)) => _list.removeWhere(test); | |
78 void retainWhere(bool test(E element)) => _list.retainWhere(test); | |
79 | |
80 List<E> sublist(int start, [int end]) => _list.sublist(start, end); | |
81 | |
82 List<E> getRange(int start, int end) => _list.getRange(start, end); | |
83 | |
84 void setRange(int start, int length, List<E> from, [int startFrom = 0]) { | |
85 _list.setRange(start, length, from, startFrom); | |
86 } | |
87 void removeRange(int start, int length) { | |
88 _list.removeRange(start, length); | |
89 } | |
90 void insertAll(int index, Iterable<E> iterable) { | |
91 _list.insertAll(index, iterable); | |
92 } | |
93 | |
94 Iterable<E> get reversed => _list.reversed; | |
95 | |
96 Map<int, E> asMap() => _list.asMap(); | |
97 | |
98 void replaceRange(int start, int end, Iterable<E> newContents) => | |
99 _list.replaceRange(start, end, newContents); | |
100 | |
101 void setAll(int index, Iterable<E> iterable) => _list.setAll(index, iterable); | |
102 | |
103 void fillRange(int start, int end, [E fillValue]) => | |
104 _list.fillRange(start, end, fillValue); | |
105 } | |
OLD | NEW |