| 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 | |
| 7 void insert(int index, T element) { | 6 void insert(int index, T element) { |
| 8 if ((index < 0) || (index > length)) { | 7 if ((index < 0) || (index > length)) { |
| 9 throw new RangeError.range(index, 0, length); | 8 throw new RangeError.range(index, 0, length); |
| 10 } | 9 } |
| 11 if (index == this.length) { | 10 if (index == this.length) { |
| 12 add(element); | 11 add(element); |
| 13 return; | 12 return; |
| 14 } | 13 } |
| 15 int oldLength = this.length; | 14 int oldLength = this.length; |
| 16 // We are modifying the length just below the is-check. Without the check | 15 // We are modifying the length just below the is-check. Without the check |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 factory _GrowableList(int length) { | 92 factory _GrowableList(int length) { |
| 94 var data = new _List((length == 0) ? _kDefaultCapacity : length); | 93 var data = new _List((length == 0) ? _kDefaultCapacity : length); |
| 95 var result = new _GrowableList<T>.withData(data); | 94 var result = new _GrowableList<T>.withData(data); |
| 96 if (length > 0) { | 95 if (length > 0) { |
| 97 result._setLength(length); | 96 result._setLength(length); |
| 98 } | 97 } |
| 99 return result; | 98 return result; |
| 100 } | 99 } |
| 101 | 100 |
| 102 factory _GrowableList.withCapacity(int capacity) { | 101 factory _GrowableList.withCapacity(int capacity) { |
| 103 var data = new _List((capacity == 0)? _kDefaultCapacity : capacity); | 102 var data = new _List((capacity == 0) ? _kDefaultCapacity : capacity); |
| 104 return new _GrowableList<T>.withData(data); | 103 return new _GrowableList<T>.withData(data); |
| 105 } | 104 } |
| 106 | 105 |
| 107 factory _GrowableList.withData(_List data) | 106 factory _GrowableList.withData(_List data) native "GrowableList_allocate"; |
| 108 native "GrowableList_allocate"; | |
| 109 | 107 |
| 110 int get _capacity native "GrowableList_getCapacity"; | 108 int get _capacity native "GrowableList_getCapacity"; |
| 111 | 109 |
| 112 int get length native "GrowableList_getLength"; | 110 int get length native "GrowableList_getLength"; |
| 113 | 111 |
| 114 void set length(int new_length) { | 112 void set length(int new_length) { |
| 115 int old_capacity = _capacity; | 113 int old_capacity = _capacity; |
| 116 int new_capacity = new_length; | 114 int new_capacity = new_length; |
| 117 if (new_length == 0) { | 115 if (new_length == 0) { |
| 118 // Ensure that we use _kDefaultCapacity only when the old_capacity | 116 // Ensure that we use _kDefaultCapacity only when the old_capacity |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 if (len == _capacity) { | 161 if (len == _capacity) { |
| 164 _grow(len * 2); | 162 _grow(len * 2); |
| 165 } | 163 } |
| 166 _setLength(len + 1); | 164 _setLength(len + 1); |
| 167 this[len] = value; | 165 this[len] = value; |
| 168 } | 166 } |
| 169 | 167 |
| 170 void addAll(Iterable<T> iterable) { | 168 void addAll(Iterable<T> iterable) { |
| 171 var len = length; | 169 var len = length; |
| 172 final cid = ClassID.getID(iterable); | 170 final cid = ClassID.getID(iterable); |
| 173 final isVMList = | 171 final isVMList = (cid == ClassID.cidArray) || |
| 174 (cid == ClassID.cidArray) || | |
| 175 (cid == ClassID.cidGrowableObjectArray) || | 172 (cid == ClassID.cidGrowableObjectArray) || |
| 176 (cid == ClassID.cidImmutableArray); | 173 (cid == ClassID.cidImmutableArray); |
| 177 if (isVMList || (iterable is EfficientLengthIterable)) { | 174 if (isVMList || (iterable is EfficientLengthIterable)) { |
| 178 var cap = _capacity; | 175 var cap = _capacity; |
| 179 // Pregrow if we know iterable.length. | 176 // Pregrow if we know iterable.length. |
| 180 var iterLen = iterable.length; | 177 var iterLen = iterable.length; |
| 181 var newLen = len + iterLen; | 178 var newLen = len + iterLen; |
| 182 if (newLen > cap) { | 179 if (newLen > cap) { |
| 183 do { | 180 do { |
| 184 cap *= 2; | 181 cap *= 2; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 } | 221 } |
| 225 | 222 |
| 226 T get last { | 223 T get last { |
| 227 if (length > 0) return this[length - 1]; | 224 if (length > 0) return this[length - 1]; |
| 228 throw IterableElementError.noElement(); | 225 throw IterableElementError.noElement(); |
| 229 } | 226 } |
| 230 | 227 |
| 231 T get single { | 228 T get single { |
| 232 if (length == 1) return this[0]; | 229 if (length == 1) return this[0]; |
| 233 if (length == 0) throw IterableElementError.noElement(); | 230 if (length == 0) throw IterableElementError.noElement(); |
| 234 throw IterableElementError.tooMany();; | 231 throw IterableElementError.tooMany(); |
| 232 ; |
| 235 } | 233 } |
| 236 | 234 |
| 237 void _grow(int new_capacity) { | 235 void _grow(int new_capacity) { |
| 238 var new_data = new _List(new_capacity); | 236 var new_data = new _List(new_capacity); |
| 239 for (int i = 0; i < length; i++) { | 237 for (int i = 0; i < length; i++) { |
| 240 new_data[i] = this[i]; | 238 new_data[i] = this[i]; |
| 241 } | 239 } |
| 242 _setData(new_data); | 240 _setData(new_data); |
| 243 } | 241 } |
| 244 | 242 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 void clear() { | 339 void clear() { |
| 342 this.length = 0; | 340 this.length = 0; |
| 343 } | 341 } |
| 344 | 342 |
| 345 String toString() => ListBase.listToString(this); | 343 String toString() => ListBase.listToString(this); |
| 346 | 344 |
| 347 Iterator<T> get iterator { | 345 Iterator<T> get iterator { |
| 348 return new ListIterator<T>(this); | 346 return new ListIterator<T>(this); |
| 349 } | 347 } |
| 350 | 348 |
| 351 List<T> toList({ bool growable: true }) { | 349 List<T> toList({bool growable: true}) { |
| 352 var length = this.length; | 350 var length = this.length; |
| 353 if (length > 0) { | 351 if (length > 0) { |
| 354 List list = growable ? new _List(length) : new _List<T>(length); | 352 List list = growable ? new _List(length) : new _List<T>(length); |
| 355 for (int i = 0; i < length; i++) { | 353 for (int i = 0; i < length; i++) { |
| 356 list[i] = this[i]; | 354 list[i] = this[i]; |
| 357 } | 355 } |
| 358 if (!growable) return list; | 356 if (!growable) return list; |
| 359 var result = new _GrowableList<T>.withData(list); | 357 var result = new _GrowableList<T>.withData(list); |
| 360 result._setLength(length); | 358 result._setLength(length); |
| 361 return result; | 359 return result; |
| 362 } | 360 } |
| 363 return growable ? <T>[] : new List<T>(0); | 361 return growable ? <T>[] : new List<T>(0); |
| 364 } | 362 } |
| 365 | 363 |
| 366 Set<T> toSet() { | 364 Set<T> toSet() { |
| 367 return new Set<T>.from(this); | 365 return new Set<T>.from(this); |
| 368 } | 366 } |
| 369 } | 367 } |
| OLD | NEW |