Chromium Code Reviews| 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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 | 106 |
| 107 factory _GrowableList.from(Iterable<T> other) { | 107 factory _GrowableList.from(Iterable<T> other) { |
| 108 List<T> result = new _GrowableList<T>(); | 108 List<T> result = new _GrowableList<T>(); |
| 109 result.addAll(other); | 109 result.addAll(other); |
| 110 return result; | 110 return result; |
| 111 } | 111 } |
| 112 | 112 |
| 113 factory _GrowableList.withData(_List data) | 113 factory _GrowableList.withData(_List data) |
| 114 native "GrowableList_allocate"; | 114 native "GrowableList_allocate"; |
| 115 | 115 |
| 116 int get _capacity native "GrowableList_getCapacity"; | |
| 117 | |
| 116 int get length native "GrowableList_getLength"; | 118 int get length native "GrowableList_getLength"; |
| 117 | 119 |
| 118 int get _capacity native "GrowableList_getCapacity"; | |
| 119 | |
| 120 void set length(int new_length) { | 120 void set length(int new_length) { |
| 121 if (new_length > _capacity) { | 121 if (new_length > _capacity) { |
|
Ivan Posva
2016/02/16 23:54:54
Judging from Srdjan's comment we should not interm
Cutch
2016/02/17 21:12:54
Done.
| |
| 122 _grow(new_length); | 122 _grow(new_length); |
| 123 _setLength(new_length); | |
| 124 return; | |
| 125 } | |
| 126 // We are shrinking. Pick the method which has fewer writes. | |
| 127 // In the shrink-to-fit path, we write 2 * |new_length| words | |
| 128 // (null fill + copy). | |
|
Ivan Posva
2016/02/16 23:54:54
How about "(null init + copy)" and "(null overwrit
Cutch
2016/02/17 21:12:54
Done.
| |
| 129 // In the non-shrink-to-fit path, we write |length - new_length| words | |
| 130 // (null fill). | |
| 131 final bool shouldShrinkToFit = (2 * new_length) < (length - new_length); | |
|
Ivan Posva
2016/02/16 23:54:54
Which would mean here (new_capacity + new_length)
Cutch
2016/02/17 21:12:54
Done.
| |
| 132 if (shouldShrinkToFit) { | |
| 133 _shrink(new_length); | |
| 123 } else { | 134 } else { |
| 124 for (int i = new_length; i < length; i++) { | 135 for (int i = new_length; i < length; i++) { |
| 125 this[i] = null; | 136 this[i] = null; |
| 126 } | 137 } |
| 127 } | 138 } |
| 128 _setLength(new_length); | 139 _setLength(new_length); |
| 129 } | 140 } |
| 130 | 141 |
| 131 void _setLength(int new_length) native "GrowableList_setLength"; | 142 void _setLength(int new_length) native "GrowableList_setLength"; |
| 132 | 143 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 } | 229 } |
| 219 | 230 |
| 220 void _grow(int new_length) { | 231 void _grow(int new_length) { |
| 221 var new_data = new _List(new_length); | 232 var new_data = new _List(new_length); |
| 222 for (int i = 0; i < length; i++) { | 233 for (int i = 0; i < length; i++) { |
| 223 new_data[i] = this[i]; | 234 new_data[i] = this[i]; |
| 224 } | 235 } |
| 225 _setData(new_data); | 236 _setData(new_data); |
| 226 } | 237 } |
| 227 | 238 |
| 239 void _shrink(int new_length) { | |
|
srdjan
2016/02/16 23:45:29
if (new_length == _capacity) return
Ivan Posva
2016/02/16 23:54:54
I don't think this would work here as the code abo
Cutch
2016/02/17 21:12:54
Acknowledged.
Cutch
2016/02/17 21:12:54
Acknowledged.
| |
| 240 var new_data = new _List(new_length == 0 ? _kDefaultCapacity : new_length); | |
| 241 for (int i = 0; i < new_length; i++) { | |
| 242 new_data[i] = this[i]; | |
| 243 } | |
| 244 _setData(new_data); | |
| 245 } | |
| 246 | |
| 228 // Iterable interface. | 247 // Iterable interface. |
| 229 | 248 |
| 230 void forEach(f(T element)) { | 249 void forEach(f(T element)) { |
| 231 int initialLength = length; | 250 int initialLength = length; |
| 232 for (int i = 0; i < length; i++) { | 251 for (int i = 0; i < length; i++) { |
| 233 f(this[i]); | 252 f(this[i]); |
| 234 if (length != initialLength) throw new ConcurrentModificationError(this); | 253 if (length != initialLength) throw new ConcurrentModificationError(this); |
| 235 } | 254 } |
| 236 } | 255 } |
| 237 | 256 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 335 result._setLength(length); | 354 result._setLength(length); |
| 336 return result; | 355 return result; |
| 337 } | 356 } |
| 338 return growable ? <T>[] : new List<T>(0); | 357 return growable ? <T>[] : new List<T>(0); |
| 339 } | 358 } |
| 340 | 359 |
| 341 Set<T> toSet() { | 360 Set<T> toSet() { |
| 342 return new Set<T>.from(this); | 361 return new Set<T>.from(this); |
| 343 } | 362 } |
| 344 } | 363 } |
| OLD | NEW |