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 _GrowableList<T> implements List<T> { | 5 class _GrowableList<T> extends ListBase<T> { |
6 | 6 |
7 void insert(int index, T element) { | 7 void insert(int index, T element) { |
8 if ((index < 0) || (index > length)) { | 8 if ((index < 0) || (index > length)) { |
9 throw new RangeError.range(index, 0, length); | 9 throw new RangeError.range(index, 0, length); |
10 } | 10 } |
11 if (index == this.length) { | 11 if (index == this.length) { |
12 add(element); | 12 add(element); |
13 return; | 13 return; |
14 } | 14 } |
15 int oldLength = this.length; | 15 int oldLength = this.length; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 void setAll(int index, Iterable<T> iterable) { | 60 void setAll(int index, Iterable<T> iterable) { |
61 if (iterable is List) { | 61 if (iterable is List) { |
62 setRange(index, index + iterable.length, iterable); | 62 setRange(index, index + iterable.length, iterable); |
63 } else { | 63 } else { |
64 for (T element in iterable) { | 64 for (T element in iterable) { |
65 this[index++] = element; | 65 this[index++] = element; |
66 } | 66 } |
67 } | 67 } |
68 } | 68 } |
69 | 69 |
70 void removeWhere(bool test(T element)) { | |
71 IterableMixinWorkaround.removeWhereList(this, test); | |
72 } | |
73 | |
74 void retainWhere(bool test(T element)) { | |
75 IterableMixinWorkaround.removeWhereList(this, | |
76 (T element) => !test(element)); | |
77 } | |
78 | |
79 Iterable<T> getRange(int start, int end) { | |
80 return new IterableMixinWorkaround<T>().getRangeList(this, start, end); | |
81 } | |
82 | |
83 void setRange(int start, int end, Iterable<T> iterable, [int skipCount = 0]) { | |
84 IterableMixinWorkaround.setRangeList(this, start, end, iterable, skipCount); | |
85 } | |
86 | |
87 void removeRange(int start, int end) { | 70 void removeRange(int start, int end) { |
88 Lists.indicesCheck(this, start, end); | 71 Lists.indicesCheck(this, start, end); |
89 Lists.copy(this, end, this, start, this.length - end); | 72 Lists.copy(this, end, this, start, this.length - end); |
90 this.length = this.length - (end - start); | 73 this.length = this.length - (end - start); |
91 } | 74 } |
92 | 75 |
93 void replaceRange(int start, int end, Iterable<T> iterable) { | |
94 IterableMixinWorkaround.replaceRangeList(this, start, end, iterable); | |
95 } | |
96 | |
97 void fillRange(int start, int end, [T fillValue]) { | |
98 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); | |
99 } | |
100 | |
101 List<T> sublist(int start, [int end]) { | 76 List<T> sublist(int start, [int end]) { |
102 Lists.indicesCheck(this, start, end); | 77 Lists.indicesCheck(this, start, end); |
103 if (end == null) end = this.length; | 78 if (end == null) end = this.length; |
104 int length = end - start; | 79 int length = end - start; |
105 if (length == 0) return <T>[]; | 80 if (length == 0) return <T>[]; |
106 List list = new _List(length); | 81 List list = new _List(length); |
107 for (int i = 0; i < length; i++) { | 82 for (int i = 0; i < length; i++) { |
108 list[i] = this[start + i]; | 83 list[i] = this[start + i]; |
109 } | 84 } |
110 var result = new _GrowableList<T>.withData(list); | 85 var result = new _GrowableList<T>.withData(list); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 if (length > 0) return this[length - 1]; | 194 if (length > 0) return this[length - 1]; |
220 throw IterableElementError.noElement(); | 195 throw IterableElementError.noElement(); |
221 } | 196 } |
222 | 197 |
223 T get single { | 198 T get single { |
224 if (length == 1) return this[0]; | 199 if (length == 1) return this[0]; |
225 if (length == 0) throw IterableElementError.noElement(); | 200 if (length == 0) throw IterableElementError.noElement(); |
226 throw IterableElementError.tooMany();; | 201 throw IterableElementError.tooMany();; |
227 } | 202 } |
228 | 203 |
229 int indexOf(Object element, [int start = 0]) { | |
230 return IterableMixinWorkaround.indexOfList(this, element, start); | |
231 } | |
232 | |
233 int lastIndexOf(Object element, [int start = null]) { | |
234 return IterableMixinWorkaround.lastIndexOfList(this, element, start); | |
235 } | |
236 | |
237 void _grow(int new_length) { | 204 void _grow(int new_length) { |
238 var new_data = new _List(new_length); | 205 var new_data = new _List(new_length); |
239 for (int i = 0; i < length; i++) { | 206 for (int i = 0; i < length; i++) { |
240 new_data[i] = this[i]; | 207 new_data[i] = this[i]; |
241 } | 208 } |
242 _setData(new_data); | 209 _setData(new_data); |
243 } | 210 } |
244 | 211 |
245 // Iterable interface. | 212 // Iterable interface. |
246 | 213 |
247 bool contains(Object element) { | |
248 return IterableMixinWorkaround.contains(this, element); | |
249 } | |
250 | |
251 void forEach(f(T element)) { | 214 void forEach(f(T element)) { |
252 int initialLength = length; | 215 int initialLength = length; |
253 for (int i = 0; i < length; i++) { | 216 for (int i = 0; i < length; i++) { |
254 f(this[i]); | 217 f(this[i]); |
255 if (length != initialLength) throw new ConcurrentModificationError(this); | 218 if (length != initialLength) throw new ConcurrentModificationError(this); |
256 } | 219 } |
257 } | 220 } |
258 | 221 |
259 String join([String separator = ""]) { | 222 String join([String separator = ""]) { |
260 final int length = this.length; | 223 final int length = this.length; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 String _joinWithSeparator(String separator) { | 280 String _joinWithSeparator(String separator) { |
318 StringBuffer buffer = new StringBuffer(); | 281 StringBuffer buffer = new StringBuffer(); |
319 buffer.write(this[0]); | 282 buffer.write(this[0]); |
320 for (int i = 1; i < this.length; i++) { | 283 for (int i = 1; i < this.length; i++) { |
321 buffer.write(separator); | 284 buffer.write(separator); |
322 buffer.write(this[i]); | 285 buffer.write(this[i]); |
323 } | 286 } |
324 return buffer.toString(); | 287 return buffer.toString(); |
325 } | 288 } |
326 | 289 |
327 Iterable map(f(T element)) { | |
328 return IterableMixinWorkaround.mapList(this, f); | |
329 } | |
330 | |
331 T reduce(T combine(T value, T element)) { | |
332 return IterableMixinWorkaround.reduce(this, combine); | |
333 } | |
334 | |
335 fold(initialValue, combine(previousValue, T element)) { | |
336 return IterableMixinWorkaround.fold(this, initialValue, combine); | |
337 } | |
338 | |
339 Iterable<T> where(bool f(T element)) { | |
340 return new IterableMixinWorkaround<T>().where(this, f); | |
341 } | |
342 | |
343 Iterable expand(Iterable f(T element)) { | |
344 return IterableMixinWorkaround.expand(this, f); | |
345 } | |
346 | |
347 Iterable<T> take(int n) { | |
348 return new IterableMixinWorkaround<T>().takeList(this, n); | |
349 } | |
350 | |
351 Iterable<T> takeWhile(bool test(T value)) { | |
352 return new IterableMixinWorkaround<T>().takeWhile(this, test); | |
353 } | |
354 | |
355 Iterable<T> skip(int n) { | |
356 return new IterableMixinWorkaround<T>().skipList(this, n); | |
357 } | |
358 | |
359 Iterable<T> skipWhile(bool test(T value)) { | |
360 return new IterableMixinWorkaround<T>().skipWhile(this, test); | |
361 } | |
362 | |
363 bool every(bool f(T element)) { | |
364 return IterableMixinWorkaround.every(this, f); | |
365 } | |
366 | |
367 bool any(bool f(T element)) { | |
368 return IterableMixinWorkaround.any(this, f); | |
369 } | |
370 | |
371 T firstWhere(bool test(T value), {T orElse()}) { | |
372 return IterableMixinWorkaround.firstWhere(this, test, orElse); | |
373 } | |
374 | |
375 T lastWhere(bool test(T value), {T orElse()}) { | |
376 return IterableMixinWorkaround.lastWhereList(this, test, orElse); | |
377 } | |
378 | |
379 T singleWhere(bool test(T value)) { | |
380 return IterableMixinWorkaround.singleWhere(this, test); | |
381 } | |
382 | |
383 T elementAt(int index) { | 290 T elementAt(int index) { |
384 return this[index]; | 291 return this[index]; |
385 } | 292 } |
386 | 293 |
387 bool get isEmpty { | 294 bool get isEmpty { |
388 return this.length == 0; | 295 return this.length == 0; |
389 } | 296 } |
390 | 297 |
391 bool get isNotEmpty => !isEmpty; | 298 bool get isNotEmpty => !isEmpty; |
392 | 299 |
393 void clear() { | 300 void clear() { |
394 this.length = 0; | 301 this.length = 0; |
395 } | 302 } |
396 | 303 |
397 Iterable<T> get reversed => | |
398 new IterableMixinWorkaround<T>().reversedList(this); | |
399 | |
400 void sort([int compare(T a, T b)]) { | |
401 IterableMixinWorkaround.sortList(this, compare); | |
402 } | |
403 | |
404 void shuffle([Random random]) { | |
405 IterableMixinWorkaround.shuffleList(this, random); | |
406 } | |
407 | |
408 String toString() => ListBase.listToString(this); | 304 String toString() => ListBase.listToString(this); |
409 | 305 |
410 Iterator<T> get iterator { | 306 Iterator<T> get iterator { |
411 return new ListIterator<T>(this); | 307 return new ListIterator<T>(this); |
412 } | 308 } |
413 | 309 |
414 List<T> toList({ bool growable: true }) { | 310 List<T> toList({ bool growable: true }) { |
415 var length = this.length; | 311 var length = this.length; |
416 if (length > 0) { | 312 if (length > 0) { |
417 List list = growable ? new _List(length) : new _List<T>(length); | 313 List list = growable ? new _List(length) : new _List<T>(length); |
418 for (int i = 0; i < length; i++) { | 314 for (int i = 0; i < length; i++) { |
419 list[i] = this[i]; | 315 list[i] = this[i]; |
420 } | 316 } |
421 if (!growable) return list; | 317 if (!growable) return list; |
422 var result = new _GrowableList<T>.withData(list); | 318 var result = new _GrowableList<T>.withData(list); |
423 result._setLength(length); | 319 result._setLength(length); |
424 return result; | 320 return result; |
425 } | 321 } |
426 return growable ? <T>[] : new List<T>(0); | 322 return growable ? <T>[] : new List<T>(0); |
427 } | 323 } |
428 | 324 |
429 Set<T> toSet() { | 325 Set<T> toSet() { |
430 return new Set<T>.from(this); | 326 return new Set<T>.from(this); |
431 } | 327 } |
432 | |
433 Map<int, T> asMap() { | |
434 return new IterableMixinWorkaround<T>().asMapList(this); | |
435 } | |
436 } | 328 } |
OLD | NEW |