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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 var len = length; | 145 var len = length; |
| 146 if (len == _capacity) { | 146 if (len == _capacity) { |
| 147 _grow(len * 2); | 147 _grow(len * 2); |
| 148 } | 148 } |
| 149 _setLength(len + 1); | 149 _setLength(len + 1); |
| 150 this[len] = value; | 150 this[len] = value; |
| 151 } | 151 } |
| 152 | 152 |
| 153 void addAll(Iterable<T> iterable) { | 153 void addAll(Iterable<T> iterable) { |
| 154 var len = length; | 154 var len = length; |
| 155 if (iterable is EfficientLength) { | 155 final cid = ClassID.getID(iterable); |
| 156 final isVMList = | |
| 157 (cid == ClassID.cidArray) || | |
| 158 (cid == ClassID.cidGrowableObjectArray) || | |
| 159 (cid == ClassID.cidImmutableArray); | |
|
Lasse Reichstein Nielsen
2015/01/27 15:13:57
Would it be worth it to recognize growable list as
srdjan
2015/01/29 17:42:19
I think so, but we would need to move it here sinc
| |
| 160 if (isVMList || (iterable is EfficientLength)) { | |
| 156 var cap = _capacity; | 161 var cap = _capacity; |
| 157 // Pregrow if we know iterable.length. | 162 // Pregrow if we know iterable.length. |
| 158 var iterLen = iterable.length; | 163 var iterLen = iterable.length; |
| 159 var newLen = len + iterLen; | 164 var newLen = len + iterLen; |
| 160 if (newLen > cap) { | 165 if (newLen > cap) { |
| 161 do { | 166 do { |
| 162 cap *= 2; | 167 cap *= 2; |
| 163 } while (newLen > cap); | 168 } while (newLen > cap); |
| 164 _grow(cap); | 169 _grow(cap); |
| 165 } | 170 } |
| 171 if (isVMList) { | |
| 172 if (identical(iterable, this)) { | |
| 173 throw new ConcurrentModificationError(this); | |
|
Lasse Reichstein Nielsen
2015/01/27 15:13:57
This does change behavior (it now throws before ad
srdjan
2015/01/29 17:42:19
I do not understand. 'this' is always GrowableObje
| |
| 174 } | |
| 175 this._setLength(newLen); | |
| 176 for (int i = 0; i < iterLen; i++) { | |
| 177 this[len++] = iterable[i]; | |
| 178 } | |
| 179 return; | |
| 180 } | |
| 166 } | 181 } |
| 167 Iterator it = iterable.iterator; | 182 Iterator it = iterable.iterator; |
| 168 if (!it.moveNext()) return; | 183 if (!it.moveNext()) return; |
| 169 do { | 184 do { |
| 170 while (len < _capacity) { | 185 while (len < _capacity) { |
| 171 int newLen = len + 1; | 186 int newLen = len + 1; |
| 172 this._setLength(newLen); | 187 this._setLength(newLen); |
| 173 this[len] = it.current; | 188 this[len] = it.current; |
| 174 if (!it.moveNext()) return; | 189 if (!it.moveNext()) return; |
| 175 if (this.length != newLen) throw new ConcurrentModificationError(this); | 190 if (this.length != newLen) throw new ConcurrentModificationError(this); |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 321 result._setLength(length); | 336 result._setLength(length); |
| 322 return result; | 337 return result; |
| 323 } | 338 } |
| 324 return growable ? <T>[] : new List<T>(0); | 339 return growable ? <T>[] : new List<T>(0); |
| 325 } | 340 } |
| 326 | 341 |
| 327 Set<T> toSet() { | 342 Set<T> toSet() { |
| 328 return new Set<T>.from(this); | 343 return new Set<T>.from(this); |
| 329 } | 344 } |
| 330 } | 345 } |
| OLD | NEW |