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