OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 class _GrowableObjectArray<T> implements List<T> { | 5 class _GrowableObjectArray<T> implements List<T> { |
6 factory _GrowableObjectArray._uninstantiable() { | 6 factory _GrowableObjectArray._uninstantiable() { |
7 throw new UnsupportedError( | 7 throw new UnsupportedError( |
8 "GrowableObjectArray can only be allocated by the VM"); | 8 "GrowableObjectArray can only be allocated by the VM"); |
9 } | 9 } |
10 | 10 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 _grow(len * 2); | 124 _grow(len * 2); |
125 } | 125 } |
126 _setLength(len + 1); | 126 _setLength(len + 1); |
127 this[len] = value; | 127 this[len] = value; |
128 } | 128 } |
129 | 129 |
130 void addLast(T element) { | 130 void addLast(T element) { |
131 add(element); | 131 add(element); |
132 } | 132 } |
133 | 133 |
134 void addAll(Collection<T> collection) { | 134 void addAll(Iterable<T> iterable) { |
135 for (T elem in collection) { | 135 for (T elem in iterable) { |
136 add(elem); | 136 add(elem); |
137 } | 137 } |
138 } | 138 } |
139 | 139 |
140 T removeLast() { | 140 T removeLast() { |
141 var len = length - 1; | 141 var len = length - 1; |
142 var elem = this[len]; | 142 var elem = this[len]; |
143 this[len] = null; | 143 this[len] = null; |
144 _setLength(len); | 144 _setLength(len); |
145 return elem; | 145 return elem; |
146 } | 146 } |
147 | 147 |
148 T get first { | 148 T get first { |
149 return this[0]; | 149 if (length > 0) return this[0]; |
| 150 throw new StateError("No elements"); |
150 } | 151 } |
151 | 152 |
152 T get last { | 153 T get last { |
153 return this[length - 1]; | 154 if (length > 0) return this[length - 1]; |
| 155 throw new StateError("No elements"); |
154 } | 156 } |
155 | 157 |
| 158 T get single { |
| 159 if (length == 1) return this[0]; |
| 160 if (length == 0) throw new StateError("No elements"); |
| 161 throw new StateError("More than one element"); |
| 162 } |
| 163 |
| 164 T min([int compare(T a, T b)]) => Collections.min(this, compare); |
| 165 |
| 166 T max([int compare(T a, T b)]) => Collections.max(this, compare); |
| 167 |
156 int indexOf(T element, [int start = 0]) { | 168 int indexOf(T element, [int start = 0]) { |
157 return Arrays.indexOf(this, element, start, length); | 169 return Arrays.indexOf(this, element, start, length); |
158 } | 170 } |
159 | 171 |
160 int lastIndexOf(T element, [int start = null]) { | 172 int lastIndexOf(T element, [int start = null]) { |
161 if (start == null) start = length - 1; | 173 if (start == null) start = length - 1; |
162 return Arrays.lastIndexOf(this, element, start); | 174 return Arrays.lastIndexOf(this, element, start); |
163 } | 175 } |
164 | 176 |
165 void _grow(int new_length) { | 177 void _grow(int new_length) { |
166 var new_data = new _ObjectArray<T>(new_length); | 178 var new_data = new _ObjectArray<T>(new_length); |
167 for (int i = 0; i < length; i++) { | 179 for (int i = 0; i < length; i++) { |
168 new_data[i] = this[i]; | 180 new_data[i] = this[i]; |
169 } | 181 } |
170 _setData(new_data); | 182 _setData(new_data); |
171 } | 183 } |
172 | 184 |
173 // Collection interface. | 185 // Collection interface. |
174 | 186 |
175 bool contains(T element) => Collections.contains(this, element); | 187 bool contains(T element) { |
| 188 return Collections.contains(this, element); |
| 189 } |
176 | 190 |
177 void forEach(f(T element)) { | 191 void forEach(f(T element)) { |
178 // TODO(srdjan): Use Collections.forEach(this, f); | 192 // TODO(srdjan): Use Collections.forEach(this, f); |
179 // Accessing the list directly improves DeltaBlue performance by 25%. | 193 // Accessing the list directly improves DeltaBlue performance by 25%. |
180 for (int i = 0; i < length; i++) { | 194 for (int i = 0; i < length; i++) { |
181 f(this[i]); | 195 f(this[i]); |
182 } | 196 } |
183 } | 197 } |
184 | 198 |
185 Collection map(f(T element)) { | 199 String join([String separator]) { |
186 return Collections.map(this, | 200 if (isEmpty) return ""; |
187 new _GrowableObjectArray.withCapacity(length), f); | 201 if (this.length == 1) return "${this[0]}"; |
| 202 StringBuffer buffer = new StringBuffer(); |
| 203 if (separator == null || separator == "") { |
| 204 for (int i = 0; i < this.length; i++) { |
| 205 buffer.add("${this[i]}"); |
| 206 } |
| 207 } else { |
| 208 buffer.add("${this[0]}"); |
| 209 for (int i = 1; i < this.length; i++) { |
| 210 buffer.add(separator); |
| 211 buffer.add("${this[i]}"); |
| 212 } |
| 213 } |
| 214 return buffer.toString(); |
| 215 } |
| 216 |
| 217 List mappedBy(f(T element)) { |
| 218 return new MappedList<T, dynamic>(this, f); |
188 } | 219 } |
189 | 220 |
190 reduce(initialValue, combine(previousValue, T element)) { | 221 reduce(initialValue, combine(previousValue, T element)) { |
191 return Collections.reduce(this, initialValue, combine); | 222 return Collections.reduce(this, initialValue, combine); |
192 } | 223 } |
193 | 224 |
194 Collection<T> filter(bool f(T element)) { | 225 Iterable<T> where(bool f(T element)) { |
195 return Collections.filter(this, new _GrowableObjectArray<T>(), f); | 226 return new WhereIterable<T>(this, f); |
| 227 } |
| 228 |
| 229 List<T> take(int n) { |
| 230 return new ListView<T>(this, 0, n); |
| 231 } |
| 232 |
| 233 Iterable<T> takeWhile(bool test(T value)) { |
| 234 return new TakeWhileIterable<T>(this, test); |
| 235 } |
| 236 |
| 237 List<T> skip(int n) { |
| 238 return new ListView<T>(this, n, null); |
| 239 } |
| 240 |
| 241 Iterable<T> skipWhile(bool test(T value)) { |
| 242 return new SkipWhileIterable<T>(this, test); |
196 } | 243 } |
197 | 244 |
198 bool every(bool f(T element)) { | 245 bool every(bool f(T element)) { |
199 return Collections.every(this, f); | 246 return Collections.every(this, f); |
200 } | 247 } |
201 | 248 |
202 bool some(bool f(T element)) { | 249 bool any(bool f(T element)) { |
203 return Collections.some(this, f); | 250 return Collections.any(this, f); |
| 251 } |
| 252 |
| 253 T firstMatching(bool test(T value), {T orElse()}) { |
| 254 return Collections.firstMatching(this, test, orElse); |
| 255 } |
| 256 |
| 257 T lastMatching(bool test(T value), {T orElse()}) { |
| 258 return Collections.lastMatchingInList(this, test, orElse); |
| 259 } |
| 260 |
| 261 T singleMatching(bool test(T value)) { |
| 262 return Collections.singleMatching(this, test); |
| 263 } |
| 264 |
| 265 T elementAt(int index) { |
| 266 return this[index]; |
204 } | 267 } |
205 | 268 |
206 bool get isEmpty { | 269 bool get isEmpty { |
207 return this.length == 0; | 270 return this.length == 0; |
208 } | 271 } |
209 | 272 |
210 void clear() { | 273 void clear() { |
211 this.length = 0; | 274 this.length = 0; |
212 } | 275 } |
213 | 276 |
214 void sort([int compare(T a, T b)]) { | 277 void sort([int compare(T a, T b)]) { |
215 if (compare == null) compare = Comparable.compare; | 278 if (compare == null) compare = Comparable.compare; |
216 _Sort.sort(this, compare); | 279 _Sort.sort(this, compare); |
217 } | 280 } |
218 | 281 |
219 String toString() { | 282 String toString() { |
220 return Collections.collectionToString(this); | 283 return Collections.collectionToString(this); |
221 } | 284 } |
222 | 285 |
223 Iterator<T> iterator() { | 286 Iterator<T> get iterator { |
224 return new SequenceIterator<T>(this); | 287 return new ListIterator<T>(this); |
| 288 } |
| 289 |
| 290 List<T> toList() { |
| 291 return new List<T>.from(this); |
| 292 } |
| 293 |
| 294 Set<T> toSet() { |
| 295 return new Set<T>.from(this); |
225 } | 296 } |
226 } | 297 } |
OLD | NEW |