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> implements List<T> { | 5 class _GrowableList<T> implements List<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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 } | 95 } |
96 | 96 |
97 void fillRange(int start, int end, [T fillValue]) { | 97 void fillRange(int start, int end, [T fillValue]) { |
98 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); | 98 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); |
99 } | 99 } |
100 | 100 |
101 List<T> sublist(int start, [int end]) { | 101 List<T> sublist(int start, [int end]) { |
102 Lists.indicesCheck(this, start, end); | 102 Lists.indicesCheck(this, start, end); |
103 if (end == null) end = this.length; | 103 if (end == null) end = this.length; |
104 int length = end - start; | 104 int length = end - start; |
105 if (start == end) return <T>[]; | 105 if (length == 0) return <T>[]; |
106 List list = new _GrowableList<T>.withCapacity(length); | 106 List list = new _List(length); |
107 list.length = length; | 107 for (int i = 0; i < length; i++) { |
108 Lists.copy(this, start, list, 0, length); | 108 list[i] = this[start + i]; |
109 return list; | 109 } |
| 110 var result = new _GrowableList<T>.withData(list); |
| 111 result._setLength(length); |
| 112 return result; |
110 } | 113 } |
111 | 114 |
112 static const int _kDefaultCapacity = 2; | 115 static const int _kDefaultCapacity = 2; |
113 | 116 |
114 factory _GrowableList(int length) { | 117 factory _GrowableList(int length) { |
115 var data = new _List((length == 0) ? _kDefaultCapacity : length); | 118 var data = new _List((length == 0) ? _kDefaultCapacity : length); |
116 var result = new _GrowableList<T>.withData(data); | 119 var result = new _GrowableList<T>.withData(data); |
117 if (length > 0) { | 120 if (length > 0) { |
118 result._setLength(length); | 121 result._setLength(length); |
119 } | 122 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 void add(T value) { | 167 void add(T value) { |
165 var len = length; | 168 var len = length; |
166 if (len == _capacity) { | 169 if (len == _capacity) { |
167 _grow(len * 2); | 170 _grow(len * 2); |
168 } | 171 } |
169 _setLength(len + 1); | 172 _setLength(len + 1); |
170 this[len] = value; | 173 this[len] = value; |
171 } | 174 } |
172 | 175 |
173 void addAll(Iterable<T> iterable) { | 176 void addAll(Iterable<T> iterable) { |
174 for (T elem in iterable) { | 177 var len = length; |
175 add(elem); | 178 if (iterable is EfficientLength) { |
| 179 var cap = _capacity; |
| 180 // Pregrow if we know iterable.length. |
| 181 var iterLen = iterable.length; |
| 182 var newLen = len + iterLen; |
| 183 if (newLen > cap) { |
| 184 do { |
| 185 cap *= 2; |
| 186 } while (newLen > cap); |
| 187 _grow(cap); |
| 188 } |
176 } | 189 } |
| 190 Iterator it = iterable.iterator; |
| 191 if (!it.moveNext()) return; |
| 192 do { |
| 193 while (len < _capacity) { |
| 194 int newLen = len + 1; |
| 195 this._setLength(newLen); |
| 196 this[len] = it.current; |
| 197 if (!it.moveNext()) return; |
| 198 if (this.length != newLen) throw new ConcurrentModificationError(this); |
| 199 len = newLen; |
| 200 } |
| 201 _grow(_capacity * 2); |
| 202 } while (true); |
177 } | 203 } |
178 | 204 |
179 T removeLast() { | 205 T removeLast() { |
180 var len = length - 1; | 206 var len = length - 1; |
181 var elem = this[len]; | 207 var elem = this[len]; |
182 this[len] = null; | 208 this[len] = null; |
183 _setLength(len); | 209 _setLength(len); |
184 return elem; | 210 return elem; |
185 } | 211 } |
186 | 212 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 IterableMixinWorkaround.shuffleList(this, random); | 355 IterableMixinWorkaround.shuffleList(this, random); |
330 } | 356 } |
331 | 357 |
332 String toString() => ListBase.listToString(this); | 358 String toString() => ListBase.listToString(this); |
333 | 359 |
334 Iterator<T> get iterator { | 360 Iterator<T> get iterator { |
335 return new ListIterator<T>(this); | 361 return new ListIterator<T>(this); |
336 } | 362 } |
337 | 363 |
338 List<T> toList({ bool growable: true }) { | 364 List<T> toList({ bool growable: true }) { |
339 return new List<T>.from(this, growable: growable); | 365 var length = this.length; |
| 366 if (length > 0) { |
| 367 List list = growable ? new _List(length) : new _List<T>(length); |
| 368 for (int i = 0; i < length; i++) { |
| 369 list[i] = this[i]; |
| 370 } |
| 371 if (!growable) return list; |
| 372 var result = new _GrowableList<T>.withData(list); |
| 373 result._setLength(length); |
| 374 return result; |
| 375 } |
| 376 return growable ? <T>[] : new List<T>(0); |
340 } | 377 } |
341 | 378 |
342 Set<T> toSet() { | 379 Set<T> toSet() { |
343 return new Set<T>.from(this); | 380 return new Set<T>.from(this); |
344 } | 381 } |
345 | 382 |
346 Map<int, T> asMap() { | 383 Map<int, T> asMap() { |
347 return new IterableMixinWorkaround<T>().asMapList(this); | 384 return new IterableMixinWorkaround<T>().asMapList(this); |
348 } | 385 } |
349 } | 386 } |
OLD | NEW |