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 | 5 |
6 // TODO(srdjan): Use shared array implementation. | 6 // TODO(srdjan): Use shared array implementation. |
7 class _List<E> implements List<E> { | 7 class _List<E> implements List<E> { |
8 | 8 |
9 factory _List(length) native "List_allocate"; | 9 factory _List(length) native "List_allocate"; |
10 | 10 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { | 60 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { |
61 if (start < 0 || start > this.length) { | 61 if (start < 0 || start > this.length) { |
62 throw new RangeError.range(start, 0, this.length); | 62 throw new RangeError.range(start, 0, this.length); |
63 } | 63 } |
64 if (end < start || end > this.length) { | 64 if (end < start || end > this.length) { |
65 throw new RangeError.range(end, start, this.length); | 65 throw new RangeError.range(end, start, this.length); |
66 } | 66 } |
67 int length = end - start; | 67 int length = end - start; |
68 if (length == 0) return; | 68 if (length == 0) return; |
69 | 69 |
70 if (ClassID.getID(iterable) == ClassID.cidOneByteString) { | 70 if (ClassID.getID(iterable) == ClassID.cidOneByteString) { |
Lasse Reichstein Nielsen
2014/08/19 12:48:30
This looks wrong: OneByteString?
| |
71 _copyFromObjectArray(iterable, skipCount, start, length); | 71 _copyFromObjectArray(iterable, skipCount, start, length); |
72 } else { | 72 } else { |
73 if (iterable is List) { | 73 if (iterable is List) { |
74 Lists.copy(iterable, skipCount, this, start, length); | 74 Lists.copy(iterable, skipCount, this, start, length); |
75 } else { | 75 } else { |
76 Iterator it = iterable.iterator; | 76 Iterator it = iterable.iterator; |
77 while (skipCount > 0) { | 77 while (skipCount > 0) { |
78 if (!it.moveNext()) return; | 78 if (!it.moveNext()) return; |
79 skipCount--; | 79 skipCount--; |
80 } | 80 } |
(...skipping 14 matching lines...) Expand all Loading... | |
95 } | 95 } |
96 | 96 |
97 void fillRange(int start, int end, [E fillValue]) { | 97 void fillRange(int start, int end, [E fillValue]) { |
98 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); | 98 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); |
99 } | 99 } |
100 | 100 |
101 List<E> sublist(int start, [int end]) { | 101 List<E> sublist(int start, [int end]) { |
102 Lists.indicesCheck(this, start, end); | 102 Lists.indicesCheck(this, start, end); |
103 if (end == null) end = this.length; | 103 if (end == null) end = this.length; |
104 int length = end - start; | 104 int length = end - start; |
105 if (start == end) return []; | 105 if (start == end) return <E>[]; |
106 List list = new _GrowableList<E>.withCapacity(length); | 106 List list = new _List(length); |
107 list.length = length; | 107 list._copyFromObjectArray(this, start, 0, length); |
108 Lists.copy(this, start, list, 0, length); | 108 var result = new _GrowableList<E>.withData(list); |
109 return list; | 109 result._setLength(length); |
110 return result; | |
110 } | 111 } |
111 | 112 |
112 // Iterable interface. | 113 // Iterable interface. |
113 | 114 |
114 bool contains(Object element) { | 115 bool contains(Object element) { |
115 return IterableMixinWorkaround.contains(this, element); | 116 return IterableMixinWorkaround.contains(this, element); |
116 } | 117 } |
117 | 118 |
118 void forEach(f(E element)) { | 119 void forEach(f(E element)) { |
119 IterableMixinWorkaround.forEach(this, f); | 120 IterableMixinWorkaround.forEach(this, f); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
242 if (length > 0) return this[length - 1]; | 243 if (length > 0) return this[length - 1]; |
243 throw IterableElementError.noElement(); | 244 throw IterableElementError.noElement(); |
244 } | 245 } |
245 | 246 |
246 E get single { | 247 E get single { |
247 if (length == 1) return this[0]; | 248 if (length == 1) return this[0]; |
248 if (length == 0) throw IterableElementError.noElement(); | 249 if (length == 0) throw IterableElementError.noElement(); |
249 throw IterableElementError.tooMany(); | 250 throw IterableElementError.tooMany(); |
250 } | 251 } |
251 | 252 |
252 List<E> toList({ bool growable: true}) { | 253 List<E> toList({ bool growable: true }) { |
253 return new List<E>.from(this, growable: growable); | 254 var length = this.length; |
255 var result = growable ? new _List(length) : new _List<E>(length); | |
256 result._copyFromObjectArray(this, 0, 0, length); | |
257 if (growable) { | |
258 result = new _GrowableList<E>.withData(result); | |
259 result._setLength(length); | |
260 } | |
261 return result; | |
254 } | 262 } |
255 | 263 |
256 Set<E> toSet() { | 264 Set<E> toSet() { |
257 return new Set<E>.from(this); | 265 return new Set<E>.from(this); |
258 } | 266 } |
259 | 267 |
260 Map<int, E> asMap() { | 268 Map<int, E> asMap() { |
261 return new IterableMixinWorkaround<E>().asMapList(this); | 269 return new IterableMixinWorkaround<E>().asMapList(this); |
262 } | 270 } |
263 } | 271 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
333 } | 341 } |
334 | 342 |
335 void replaceRange(int start, int end, Iterable<E> iterable) { | 343 void replaceRange(int start, int end, Iterable<E> iterable) { |
336 throw UnmodifiableListError.change(); | 344 throw UnmodifiableListError.change(); |
337 } | 345 } |
338 | 346 |
339 List<E> sublist(int start, [int end]) { | 347 List<E> sublist(int start, [int end]) { |
340 Lists.indicesCheck(this, start, end); | 348 Lists.indicesCheck(this, start, end); |
341 if (end == null) end = this.length; | 349 if (end == null) end = this.length; |
342 int length = end - start; | 350 int length = end - start; |
343 if (start == end) return []; | 351 if (start == end) return <E>[]; |
344 List list = new List<E>(); | 352 List list = new _List(length); |
345 list.length = length; | |
346 Lists.copy(this, start, list, 0, length); | 353 Lists.copy(this, start, list, 0, length); |
347 return list; | 354 var result = new _GrowableList<E>.withData(list); |
355 result._setLength(length); | |
356 return result; | |
348 } | 357 } |
349 | 358 |
350 Iterable<E> getRange(int start, int end) { | 359 Iterable<E> getRange(int start, int end) { |
351 return new IterableMixinWorkaround<E>().getRangeList(this, start, end); | 360 return new IterableMixinWorkaround<E>().getRangeList(this, start, end); |
352 } | 361 } |
353 | 362 |
354 // Collection interface. | 363 // Collection interface. |
355 | 364 |
356 bool contains(Object element) { | 365 bool contains(Object element) { |
357 return IterableMixinWorkaround.contains(this, element); | 366 return IterableMixinWorkaround.contains(this, element); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
489 throw IterableElementError.noElement(); | 498 throw IterableElementError.noElement(); |
490 } | 499 } |
491 | 500 |
492 E get single { | 501 E get single { |
493 if (length == 1) return this[0]; | 502 if (length == 1) return this[0]; |
494 if (length == 0) throw IterableElementError.noElement(); | 503 if (length == 0) throw IterableElementError.noElement(); |
495 throw IterableElementError.tooMany(); | 504 throw IterableElementError.tooMany(); |
496 } | 505 } |
497 | 506 |
498 List<E> toList({ bool growable: true }) { | 507 List<E> toList({ bool growable: true }) { |
499 return new List<E>.from(this, growable: growable); | 508 int length = this.length; |
509 List list = growable ? new _GrowableList<E>(length) : new _List<E>(length); | |
510 Lists.copy(this, 0, list, 0, length); | |
511 return list; | |
500 } | 512 } |
501 | 513 |
502 Set<E> toSet() { | 514 Set<E> toSet() { |
503 return new Set<E>.from(this); | 515 return new Set<E>.from(this); |
504 } | 516 } |
505 | 517 |
506 Map<int, E> asMap() { | 518 Map<int, E> asMap() { |
507 return new IterableMixinWorkaround<E>().asMapList(this); | 519 return new IterableMixinWorkaround<E>().asMapList(this); |
508 } | 520 } |
509 } | 521 } |
(...skipping 20 matching lines...) Expand all Loading... | |
530 } | 542 } |
531 _position = _length; | 543 _position = _length; |
532 _current = null; | 544 _current = null; |
533 return false; | 545 return false; |
534 } | 546 } |
535 | 547 |
536 E get current { | 548 E get current { |
537 return _current; | 549 return _current; |
538 } | 550 } |
539 } | 551 } |
OLD | NEW |