| 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 } |
| 11 if (index == this.length) { | 11 if (index == this.length) { |
| 12 add(element); | 12 add(element); |
| 13 return; | 13 return; |
| 14 } | 14 } |
| 15 int oldLength = this.length; | 15 int oldLength = this.length; |
| 16 // 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 |
| 17 // Array.copy could throw an exception, leaving the list in a bad state | 17 // Array.copy could throw an exception, leaving the list in a bad state |
| 18 // (with a length that has been increased, but without a new element). | 18 // (with a length that has been increased, but without a new element). |
| 19 if (index is! int) throw new ArgumentError(index); | 19 if (index is! int) throw new ArgumentError(index); |
| 20 this.length++; | 20 this.length++; |
| 21 Lists.copy(this, index, this, index + 1, oldLength - index); | 21 Lists.copy(this, index, this, index + 1, oldLength - index); |
| 22 this[index] = element; | 22 this[index] = element; |
| 23 } | 23 } |
| 24 | 24 |
| 25 T removeAt(int index) { | 25 T removeAt(int index) { |
| 26 var result = this[index]; | 26 var result = this[index]; |
| 27 int newLength = this.length - 1; | 27 int newLength = this.length - 1; |
| 28 Lists.copy(this, index + 1, this, index, newLength - index); | 28 if (index < newLength) { |
| 29 Lists.copy(this, index + 1, this, index, newLength - index); |
| 30 } |
| 29 this.length = newLength; | 31 this.length = newLength; |
| 30 return result; | 32 return result; |
| 31 } | 33 } |
| 32 | 34 |
| 33 bool remove(Object element) { | 35 bool remove(Object element) { |
| 34 for (int i = 0; i < this.length; i++) { | 36 for (int i = 0; i < this.length; i++) { |
| 35 if (this[i] == element) { | 37 if (this[i] == element) { |
| 36 removeAt(i); | 38 removeAt(i); |
| 37 return true; | 39 return true; |
| 38 } | 40 } |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 result._setLength(length); | 321 result._setLength(length); |
| 320 return result; | 322 return result; |
| 321 } | 323 } |
| 322 return growable ? <T>[] : new List<T>(0); | 324 return growable ? <T>[] : new List<T>(0); |
| 323 } | 325 } |
| 324 | 326 |
| 325 Set<T> toSet() { | 327 Set<T> toSet() { |
| 326 return new Set<T>.from(this); | 328 return new Set<T>.from(this); |
| 327 } | 329 } |
| 328 } | 330 } |
| OLD | NEW |