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> extends ListBase<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 } |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 } | 105 } |
106 | 106 |
107 factory _GrowableList.withData(_List data) | 107 factory _GrowableList.withData(_List data) |
108 native "GrowableList_allocate"; | 108 native "GrowableList_allocate"; |
109 | 109 |
110 int get _capacity native "GrowableList_getCapacity"; | 110 int get _capacity native "GrowableList_getCapacity"; |
111 | 111 |
112 int get length native "GrowableList_getLength"; | 112 int get length native "GrowableList_getLength"; |
113 | 113 |
114 void set length(int new_length) { | 114 void set length(int new_length) { |
115 int new_capacity = (new_length == 0) ? _kDefaultCapacity : new_length; | 115 int old_capacity = _capacity; |
116 if (new_capacity > _capacity) { | 116 int new_capacity = new_length; |
| 117 if (new_length == 0) { |
| 118 // Ensure that we use _kDefaultCapacity only when the old_capacity |
| 119 // is greater than _kDefaultCapacity otherwise we end up growing the |
| 120 // the array. |
| 121 if (old_capacity < _kDefaultCapacity) { |
| 122 new_capacity = old_capacity; |
| 123 } else { |
| 124 new_capacity = _kDefaultCapacity; |
| 125 } |
| 126 } |
| 127 if (new_capacity > old_capacity) { |
117 _grow(new_capacity); | 128 _grow(new_capacity); |
118 _setLength(new_length); | 129 _setLength(new_length); |
119 return; | 130 return; |
120 } | 131 } |
121 // We are shrinking. Pick the method which has fewer writes. | 132 // We are shrinking. Pick the method which has fewer writes. |
122 // In the shrink-to-fit path, we write |new_capacity + new_length| words | 133 // In the shrink-to-fit path, we write |new_capacity + new_length| words |
123 // (null init + copy). | 134 // (null init + copy). |
124 // In the non-shrink-to-fit path, we write |length - new_length| words | 135 // In the non-shrink-to-fit path, we write |length - new_length| words |
125 // (null overwrite). | 136 // (null overwrite). |
126 final bool shouldShrinkToFit = | 137 final bool shouldShrinkToFit = |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 if (this.length != newLen) throw new ConcurrentModificationError(this); | 207 if (this.length != newLen) throw new ConcurrentModificationError(this); |
197 len = newLen; | 208 len = newLen; |
198 } | 209 } |
199 _grow(_capacity * 2); | 210 _grow(_capacity * 2); |
200 } while (true); | 211 } while (true); |
201 } | 212 } |
202 | 213 |
203 T removeLast() { | 214 T removeLast() { |
204 var len = length - 1; | 215 var len = length - 1; |
205 var elem = this[len]; | 216 var elem = this[len]; |
206 this[len] = null; | 217 this.length = len; |
207 _setLength(len); | |
208 return elem; | 218 return elem; |
209 } | 219 } |
210 | 220 |
211 T get first { | 221 T get first { |
212 if (length > 0) return this[0]; | 222 if (length > 0) return this[0]; |
213 throw IterableElementError.noElement(); | 223 throw IterableElementError.noElement(); |
214 } | 224 } |
215 | 225 |
216 T get last { | 226 T get last { |
217 if (length > 0) return this[length - 1]; | 227 if (length > 0) return this[length - 1]; |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 result._setLength(length); | 360 result._setLength(length); |
351 return result; | 361 return result; |
352 } | 362 } |
353 return growable ? <T>[] : new List<T>(0); | 363 return growable ? <T>[] : new List<T>(0); |
354 } | 364 } |
355 | 365 |
356 Set<T> toSet() { | 366 Set<T> toSet() { |
357 return new Set<T>.from(this); | 367 return new Set<T>.from(this); |
358 } | 368 } |
359 } | 369 } |
OLD | NEW |