| 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 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 | 250 |
| 251 void forEach(f(T element)) { | 251 void forEach(f(T element)) { |
| 252 int initialLength = length; | 252 int initialLength = length; |
| 253 for (int i = 0; i < length; i++) { | 253 for (int i = 0; i < length; i++) { |
| 254 f(this[i]); | 254 f(this[i]); |
| 255 if (length != initialLength) throw new ConcurrentModificationError(this); | 255 if (length != initialLength) throw new ConcurrentModificationError(this); |
| 256 } | 256 } |
| 257 } | 257 } |
| 258 | 258 |
| 259 String join([String separator = ""]) { | 259 String join([String separator = ""]) { |
| 260 if (isEmpty) return ""; | 260 final int length = this.length; |
| 261 if (this.length == 1) return "${this[0]}"; | 261 if (length == 0) return ""; |
| 262 if (length == 1) return "${this[0]}"; |
| 263 if (separator.isNotEmpty) return _joinWithSeparator(separator); |
| 264 var i = 0; |
| 265 var codeUnitCount = 0; |
| 266 while (i < length) { |
| 267 final element = this[i]; |
| 268 final int cid = ClassID.getID(element); |
| 269 // While list contains one-byte strings. |
| 270 if (ClassID.cidOneByteString == cid) { |
| 271 codeUnitCount += element.length; |
| 272 i++; |
| 273 // Loop back while strings are one-byte strings. |
| 274 continue; |
| 275 } |
| 276 // Otherwise, never loop back to the outer loop, and |
| 277 // handle the remaining strings below. |
| 278 |
| 279 // Loop while elements are strings, |
| 280 final int firstNonOneByteStringLimit = i; |
| 281 var nextElement = element; |
| 282 while (nextElement is String) { |
| 283 i++; |
| 284 if (i == length) { |
| 285 return _StringBase._concatRangeNative(this, 0, length); |
| 286 } |
| 287 nextElement = this[i]; |
| 288 } |
| 289 |
| 290 // Not all elements are strings, so allocate a new backing array. |
| 291 final list = new _List(length); |
| 292 for (int copyIndex = 0; copyIndex < i; copyIndex++) { |
| 293 list[copyIndex] = this[copyIndex]; |
| 294 } |
| 295 // Is non-zero if list contains a non-onebyte string. |
| 296 var onebyteCanary = i - firstNonOneByteStringLimit; |
| 297 while (true) { |
| 298 final String elementString = "$nextElement"; |
| 299 onebyteCanary |= |
| 300 (ClassID.getID(elementString) ^ ClassID.cidOneByteString); |
| 301 list[i] = elementString; |
| 302 codeUnitCount += elementString.length; |
| 303 i++; |
| 304 if (i == length) break; |
| 305 nextElement = this[i]; |
| 306 } |
| 307 if (onebyteCanary == 0) { |
| 308 // All elements returned a one-byte string from toString. |
| 309 return _OneByteString._concatAll(list, codeUnitCount); |
| 310 } |
| 311 return _StringBase._concatRangeNative(list, 0, length); |
| 312 } |
| 313 // All elements were one-byte strings. |
| 314 return _OneByteString._concatAll(this, codeUnitCount); |
| 315 } |
| 316 |
| 317 String _joinWithSeparator(String separator) { |
| 262 StringBuffer buffer = new StringBuffer(); | 318 StringBuffer buffer = new StringBuffer(); |
| 263 if (separator.isEmpty) { | 319 buffer.write(this[0]); |
| 264 for (int i = 0; i < this.length; i++) { | 320 for (int i = 1; i < this.length; i++) { |
| 265 buffer.write(this[i]); | 321 buffer.write(separator); |
| 266 } | 322 buffer.write(this[i]); |
| 267 } else { | |
| 268 buffer.write(this[0]); | |
| 269 for (int i = 1; i < this.length; i++) { | |
| 270 buffer.write(separator); | |
| 271 buffer.write(this[i]); | |
| 272 } | |
| 273 } | 323 } |
| 274 return buffer.toString(); | 324 return buffer.toString(); |
| 275 } | 325 } |
| 276 | 326 |
| 277 Iterable map(f(T element)) { | 327 Iterable map(f(T element)) { |
| 278 return IterableMixinWorkaround.mapList(this, f); | 328 return IterableMixinWorkaround.mapList(this, f); |
| 279 } | 329 } |
| 280 | 330 |
| 281 T reduce(T combine(T value, T element)) { | 331 T reduce(T combine(T value, T element)) { |
| 282 return IterableMixinWorkaround.reduce(this, combine); | 332 return IterableMixinWorkaround.reduce(this, combine); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 } | 427 } |
| 378 | 428 |
| 379 Set<T> toSet() { | 429 Set<T> toSet() { |
| 380 return new Set<T>.from(this); | 430 return new Set<T>.from(this); |
| 381 } | 431 } |
| 382 | 432 |
| 383 Map<int, T> asMap() { | 433 Map<int, T> asMap() { |
| 384 return new IterableMixinWorkaround<T>().asMapList(this); | 434 return new IterableMixinWorkaround<T>().asMapList(this); |
| 385 } | 435 } |
| 386 } | 436 } |
| OLD | NEW |