OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 /** | |
6 * An indexed sequence of elements of the same type. | |
7 * | |
8 * This is a primitive interface that any finite integer-indexable | |
9 * sequence can implement. | |
10 * It is intended for data structures where access by index is | |
11 * the most efficient way to access the data. | |
12 */ | |
13 abstract class Sequence<T> { | |
14 /** | |
15 * The limit of valid indices of the sequence. | |
16 * | |
17 * The length getter should be efficient. | |
18 */ | |
19 int get length; | |
20 | |
21 /** | |
22 * Returns the value at the given [index]. | |
23 * | |
24 * Valid indices must be in the range [:0..length - 1:]. | |
25 * The lookup operator should be efficient. | |
26 */ | |
27 T operator[](int index); | |
28 } | |
29 | |
30 /** | |
31 * A skeleton class for a [Collection] that is also a [Sequence]. | |
32 */ | |
33 abstract class SequenceCollection<E> implements Collection<E>, Sequence<E> { | |
34 // The class is intended for use as a mixin as well. | |
35 | |
36 Iterator<E> iterator() => new SequenceIterator(sequence); | |
37 | |
38 void forEach(f(E element)) { | |
39 for (int i = 0; i < this.length; i++) f(this[i]); | |
40 } | |
41 | |
42 Collection map(f(E element)) { | |
43 List result = new List(); | |
44 for (int i = 0; i < this.length; i++) { | |
45 result.add(f(this[i])); | |
46 } | |
47 return result; | |
48 } | |
49 | |
50 bool contains(E value) { | |
51 for (int i = 0; i < sequence.length; i++) { | |
52 if (sequence[i] == value) return true; | |
53 } | |
54 return false; | |
55 } | |
56 | |
57 reduce(initialValue, combine(previousValue, E element)) { | |
58 var value = initialValue; | |
59 for (int i = 0; i < this.length; i++) { | |
60 value = combine(value, this[i]); | |
61 } | |
62 return value; | |
63 } | |
64 | |
65 Collection<E> filter(bool f(E element)) { | |
66 List<E> result = <E>[]; | |
67 for (int i = 0; i < this.length; i++) { | |
68 E element = this[i]; | |
69 if (f(element)) result.add(element); | |
70 } | |
71 return result; | |
72 } | |
73 | |
74 bool every(bool f(E element)) { | |
75 for (int i = 0; i < this.length; i++) { | |
76 if (!f(this[i])) return false; | |
77 } | |
78 return true; | |
79 } | |
80 | |
81 bool some(bool f(E element)) { | |
82 for (int i = 0; i < this.length; i++) { | |
83 if (f(this[i])) return true; | |
84 } | |
85 return false; | |
86 } | |
87 | |
88 bool isEmpty() { | |
89 return this.length == 0; | |
90 } | |
91 } | |
92 | |
93 | |
94 /** | |
95 * An unmodifiable [List] backed by a [Sequence]. | |
96 */ | |
97 class SequenceList<E> extends SequenceCollection<E> implements List<E> { | |
98 Sequence<E> sequence; | |
99 SequenceList(this.sequence); | |
100 | |
101 int get length => sequence.length; | |
102 | |
103 T operator[](int index) => sequence[index]; | |
104 | |
105 int indexOf(E value, [int start = 0]) { | |
106 for (int i = start; i < sequence.length; i++) { | |
107 if (sequence[i] == value) return i; | |
108 } | |
109 return -1; | |
110 } | |
111 | |
112 int lastIndexOf(E value, [int start]) { | |
113 if (start == null) start = sequence.length - 1; | |
114 for (int i = start; i >= 0; i--) { | |
115 if (sequence[i] == value) return i; | |
116 } | |
117 return -1; | |
118 } | |
119 | |
120 E last() => sequence[sequence.length - 1]; | |
121 | |
122 List<E> getRange(int start, int length) { | |
123 List<E> result = <E>[]; | |
124 for (int i = 0; i < length; i++) { | |
125 result.add(sequence[start + i]); | |
126 } | |
127 return result; | |
128 } | |
129 | |
130 void operator []=(int index, E value) { | |
131 throw new UnsupportedOperationException( | |
132 "Cannot modify an unmodifiable list"); | |
133 } | |
134 | |
135 void set length(int newLength) { | |
136 throw new UnsupportedOperationException( | |
137 "Cannot change the length of an unmodifiable list"); | |
138 } | |
139 | |
140 void add(E value) { | |
141 throw new UnsupportedOperationException( | |
142 "Cannot add to an unmodifiable list"); | |
143 } | |
144 | |
145 void addLast(E value) { | |
146 throw new UnsupportedOperationException( | |
147 "Cannot add to an unmodifiable list"); | |
148 } | |
149 | |
150 void addAll(Collection<E> collection) { | |
151 throw new UnsupportedOperationException( | |
152 "Cannot add to an unmodifiable list"); | |
153 } | |
154 | |
155 void sort([Comparator<E> compare]) { | |
156 throw new UnsupportedOperationException( | |
157 "Cannot modify an unmodifiable list"); | |
158 } | |
159 | |
160 void clear() { | |
161 throw new UnsupportedOperationException( | |
162 "Cannot clear an unmodifiable list"); | |
163 } | |
164 | |
165 E removeAt(int index) { | |
166 throw new UnsupportedOperationException( | |
167 "Cannot remove in an unmodifiable list"); | |
168 } | |
169 | |
170 E removeLast() { | |
171 throw new UnsupportedOperationException( | |
172 "Cannot remove in an unmodifiable list"); | |
173 } | |
174 | |
175 void setRange(int start, int length, List<E> from, [int startFrom]) { | |
176 throw new UnsupportedOperationException( | |
177 "Cannot modify an unmodifiable list"); | |
178 } | |
179 | |
180 void removeRange(int start, int length) { | |
181 throw new UnsupportedOperationException( | |
182 "Cannot remove in an unmodifiable list"); | |
183 } | |
184 | |
185 void insertRange(int start, int length, [E initialValue]) { | |
186 throw new UnsupportedOperationException( | |
187 "Cannot insert range in an unmodifiable list"); | |
188 } | |
189 } | |
190 | |
191 /** | |
192 * Iterates over a [Sequence] in growing index order. | |
193 */ | |
194 class SequenceIterator<E> implements Iterator<E> { | |
195 Sequence<E> _sequence; | |
196 int _position; | |
197 SequenceIterator(this._sequence) : _position = 0; | |
198 bool hasNext() => _position < _sequence.length; | |
199 E next() { | |
200 if (hasNext()) return _sequence[_position++]; | |
201 throw new NoMoreElementsException(); | |
202 } | |
203 } | |
204 | |
OLD | NEW |