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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 if (length > 0) { |
| 256 var result = growable ? new _List(length) : new _List<E>(length); |
| 257 result._copyFromObjectArray(this, 0, 0, length); |
| 258 if (growable) { |
| 259 result = new _GrowableList<E>.withData(result); |
| 260 result._setLength(length); |
| 261 } |
| 262 return result; |
| 263 } |
| 264 // _GrowableList.withData must not be called with empty list. |
| 265 return growable ? <E>[] : new List<E>(0); |
254 } | 266 } |
255 | 267 |
256 Set<E> toSet() { | 268 Set<E> toSet() { |
257 return new Set<E>.from(this); | 269 return new Set<E>.from(this); |
258 } | 270 } |
259 | 271 |
260 Map<int, E> asMap() { | 272 Map<int, E> asMap() { |
261 return new IterableMixinWorkaround<E>().asMapList(this); | 273 return new IterableMixinWorkaround<E>().asMapList(this); |
262 } | 274 } |
263 } | 275 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 } | 345 } |
334 | 346 |
335 void replaceRange(int start, int end, Iterable<E> iterable) { | 347 void replaceRange(int start, int end, Iterable<E> iterable) { |
336 throw UnmodifiableListError.change(); | 348 throw UnmodifiableListError.change(); |
337 } | 349 } |
338 | 350 |
339 List<E> sublist(int start, [int end]) { | 351 List<E> sublist(int start, [int end]) { |
340 Lists.indicesCheck(this, start, end); | 352 Lists.indicesCheck(this, start, end); |
341 if (end == null) end = this.length; | 353 if (end == null) end = this.length; |
342 int length = end - start; | 354 int length = end - start; |
343 if (start == end) return []; | 355 if (length == 0) return <E>[]; |
344 List list = new List<E>(); | 356 List list = new _List(length); |
345 list.length = length; | 357 for (int i = 0; i < length; i++) { |
346 Lists.copy(this, start, list, 0, length); | 358 list[i] = this[start + i]; |
347 return list; | 359 } |
| 360 var result = new _GrowableList<E>.withData(list); |
| 361 result._setLength(length); |
| 362 return result; |
348 } | 363 } |
349 | 364 |
350 Iterable<E> getRange(int start, int end) { | 365 Iterable<E> getRange(int start, int end) { |
351 return new IterableMixinWorkaround<E>().getRangeList(this, start, end); | 366 return new IterableMixinWorkaround<E>().getRangeList(this, start, end); |
352 } | 367 } |
353 | 368 |
354 // Collection interface. | 369 // Collection interface. |
355 | 370 |
356 bool contains(Object element) { | 371 bool contains(Object element) { |
357 return IterableMixinWorkaround.contains(this, element); | 372 return IterableMixinWorkaround.contains(this, element); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 throw IterableElementError.noElement(); | 504 throw IterableElementError.noElement(); |
490 } | 505 } |
491 | 506 |
492 E get single { | 507 E get single { |
493 if (length == 1) return this[0]; | 508 if (length == 1) return this[0]; |
494 if (length == 0) throw IterableElementError.noElement(); | 509 if (length == 0) throw IterableElementError.noElement(); |
495 throw IterableElementError.tooMany(); | 510 throw IterableElementError.tooMany(); |
496 } | 511 } |
497 | 512 |
498 List<E> toList({ bool growable: true }) { | 513 List<E> toList({ bool growable: true }) { |
499 return new List<E>.from(this, growable: growable); | 514 var length = this.length; |
| 515 if (length > 0) { |
| 516 List list = growable ? new _List(length) : new _List<E>(length); |
| 517 for (int i = 0; i < length; i++) { |
| 518 list[i] = this[i]; |
| 519 } |
| 520 if (!growable) return list; |
| 521 var result = new _GrowableList<E>.withData(list); |
| 522 result._setLength(length); |
| 523 return result; |
| 524 } |
| 525 return growable ? <E>[] : new _List<E>(0); |
500 } | 526 } |
501 | 527 |
502 Set<E> toSet() { | 528 Set<E> toSet() { |
503 return new Set<E>.from(this); | 529 return new Set<E>.from(this); |
504 } | 530 } |
505 | 531 |
506 Map<int, E> asMap() { | 532 Map<int, E> asMap() { |
507 return new IterableMixinWorkaround<E>().asMapList(this); | 533 return new IterableMixinWorkaround<E>().asMapList(this); |
508 } | 534 } |
509 } | 535 } |
(...skipping 20 matching lines...) Expand all Loading... |
530 } | 556 } |
531 _position = _length; | 557 _position = _length; |
532 _current = null; | 558 _current = null; |
533 return false; | 559 return false; |
534 } | 560 } |
535 | 561 |
536 E get current { | 562 E get current { |
537 return _current; | 563 return _current; |
538 } | 564 } |
539 } | 565 } |
OLD | NEW |