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> implements List<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 } |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 | 96 |
97 void fillRange(int start, int end, [T fillValue]) { | 97 void fillRange(int start, int end, [T fillValue]) { |
98 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); | 98 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); |
99 } | 99 } |
100 | 100 |
101 List<T> sublist(int start, [int end]) { | 101 List<T> 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 <T>[]; | 105 if (start == end) return <T>[]; |
106 List list = new _GrowableList<T>.withCapacity(length); | 106 List list = new _List(length); |
107 list.length = length; | |
108 Lists.copy(this, start, list, 0, length); | 107 Lists.copy(this, start, list, 0, length); |
109 return list; | 108 var result = new _GrowableList<T>.withData(list); |
109 result._setLength(length); | |
110 return result; | |
110 } | 111 } |
111 | 112 |
112 static const int _kDefaultCapacity = 2; | 113 static const int _kDefaultCapacity = 2; |
113 | 114 |
114 factory _GrowableList(int length) { | 115 factory _GrowableList(int length) { |
115 var data = new _List((length == 0) ? _kDefaultCapacity : length); | 116 var data = new _List((length == 0) ? _kDefaultCapacity : length); |
116 var result = new _GrowableList<T>.withData(data); | 117 var result = new _GrowableList<T>.withData(data); |
117 if (length > 0) { | 118 if (length > 0) { |
118 result._setLength(length); | 119 result._setLength(length); |
119 } | 120 } |
(...skipping 24 matching lines...) Expand all Loading... | |
144 } else { | 145 } else { |
145 for (int i = new_length; i < length; i++) { | 146 for (int i = new_length; i < length; i++) { |
146 this[i] = null; | 147 this[i] = null; |
147 } | 148 } |
148 } | 149 } |
149 _setLength(new_length); | 150 _setLength(new_length); |
150 } | 151 } |
151 | 152 |
152 void _setLength(int new_length) native "GrowableList_setLength"; | 153 void _setLength(int new_length) native "GrowableList_setLength"; |
153 | 154 |
154 void _setData(_List array) native "GrowableList_setData"; | 155 void _setData(_List array) native "GrowableList_setData"; |
Lasse Reichstein Nielsen
2014/08/19 12:48:30
If there was a _getData, we could use _List._copyF
| |
155 | 156 |
156 T operator [](int index) native "GrowableList_getIndexed"; | 157 T operator [](int index) native "GrowableList_getIndexed"; |
157 | 158 |
158 void operator []=(int index, T value) native "GrowableList_setIndexed"; | 159 void operator []=(int index, T value) native "GrowableList_setIndexed"; |
159 | 160 |
160 // The length of this growable array. It is always less than or equal to the | 161 // The length of this growable array. It is always less than or equal to the |
161 // length of the object array, which itself is always greater than 0, so that | 162 // length of the object array, which itself is always greater than 0, so that |
162 // grow() does not have to check for a zero length object array before | 163 // grow() does not have to check for a zero length object array before |
163 // doubling its size. | 164 // doubling its size. |
164 void add(T value) { | 165 void add(T value) { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
329 IterableMixinWorkaround.shuffleList(this, random); | 330 IterableMixinWorkaround.shuffleList(this, random); |
330 } | 331 } |
331 | 332 |
332 String toString() => ListBase.listToString(this); | 333 String toString() => ListBase.listToString(this); |
333 | 334 |
334 Iterator<T> get iterator { | 335 Iterator<T> get iterator { |
335 return new ListIterator<T>(this); | 336 return new ListIterator<T>(this); |
336 } | 337 } |
337 | 338 |
338 List<T> toList({ bool growable: true }) { | 339 List<T> toList({ bool growable: true }) { |
339 return new List<T>.from(this, growable: growable); | 340 var length = this.length; |
341 var result = growable ? new _GrowableList<T>(length) : new _List<T>(length); | |
342 Lists.copy(this, 0, result, 0, length); | |
343 return result; | |
340 } | 344 } |
341 | 345 |
342 Set<T> toSet() { | 346 Set<T> toSet() { |
343 return new Set<T>.from(this); | 347 return new Set<T>.from(this); |
344 } | 348 } |
345 | 349 |
346 Map<int, T> asMap() { | 350 Map<int, T> asMap() { |
347 return new IterableMixinWorkaround<T>().asMapList(this); | 351 return new IterableMixinWorkaround<T>().asMapList(this); |
348 } | 352 } |
349 } | 353 } |
OLD | NEW |