| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 GrowableObjectArray<T> implements List<T> { | 5 class GrowableObjectArray<T> implements List<T> { |
| 6 ObjectArray<T> backingArray; | 6 ObjectArray<T> backingArray; |
| 7 | 7 |
| 8 void copyFrom(List<Object> src, int srcStart, int dstStart, int count) { | 8 void copyFrom(List<Object> src, int srcStart, int dstStart, int count) { |
| 9 Arrays.copy(src, srcStart, this, dstStart, count); | 9 Arrays.copy(src, srcStart, this, dstStart, count); |
| 10 } | 10 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 void insertRange(int start, int length, [T initialValue = null]) { | 40 void insertRange(int start, int length, [T initialValue = null]) { |
| 41 if (length == 0) { | 41 if (length == 0) { |
| 42 return; | 42 return; |
| 43 } | 43 } |
| 44 if (length < 0) { | 44 if (length < 0) { |
| 45 throw new IllegalArgumentException("negative length $length"); | 45 throw new IllegalArgumentException("negative length $length"); |
| 46 } | 46 } |
| 47 if (start < 0 || start > this.length) { | 47 if (start < 0 || start > this.length) { |
| 48 throw new IndexOutOfRangeException(start); | 48 throw new IndexOutOfRangeException(start); |
| 49 } | 49 } |
| 50 if (this.length + length >= backingArray.length) { | 50 if (this.length + length > backingArray.length) { |
| 51 grow(backingArray.length + length); | 51 grow(backingArray.length + length); |
| 52 } | 52 } |
| 53 Arrays.copy(backingArray, | 53 Arrays.copy(backingArray, |
| 54 start, | 54 start, |
| 55 backingArray, | 55 backingArray, |
| 56 start + length, | 56 start + length, |
| 57 this.length - start); | 57 this.length - start); |
| 58 if (initialValue !== null) { | 58 if (initialValue !== null) { |
| 59 for (int i = start; i < start + length; i++) { | 59 for (int i = start; i < start + length; i++) { |
| 60 backingArray[i] = initialValue; | 60 backingArray[i] = initialValue; |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 this.length = this.length + length; | 63 this.length = this.length + length; |
| 64 } | 64 } |
| 65 | 65 |
| 66 List<T> getRange(int start, int length) { | 66 List<T> getRange(int start, int length) { |
| 67 if (length == 0) return []; | 67 if (length == 0) return []; |
| 68 Arrays.rangeCheck(this, start, length); | 68 Arrays.rangeCheck(this, start, length); |
| 69 List list = new List<T>(); | 69 List list = new List<T>(); |
| 70 list.length = length; | 70 list.length = length; |
| 71 Arrays.copy(this, start, list, 0, length); | 71 Arrays.copy(this, start, list, 0, length); |
| 72 return list; | 72 return list; |
| 73 } | 73 } |
| 74 | 74 |
| 75 // The length of this growable array. It is always less than the | 75 // The length of this growable array. It is always less than or equal to the |
| 76 // length of the backing array. | 76 // length of the backing array, which itself is always greater than 0, so that |
| 77 // grow() does not have to check for a zero length backing array before |
| 78 // doubling its size. |
| 77 int _length; | 79 int _length; |
| 78 | 80 |
| 79 GrowableObjectArray() | 81 GrowableObjectArray() |
| 80 : _length = 0, backingArray = new ObjectArray<T>(4) {} | 82 : _length = 0, backingArray = new ObjectArray<T>(4) {} |
| 81 | 83 |
| 82 GrowableObjectArray.withCapacity(int capacity) { | 84 GrowableObjectArray.withCapacity(int capacity) { |
| 83 _length = 0; | 85 _length = 0; |
| 84 if (capacity <= 0) { | 86 if (capacity <= 0) { |
| 85 capacity = 4; | 87 capacity = 4; |
| 86 } | 88 } |
| 87 backingArray = new ObjectArray<T>(capacity); | 89 backingArray = new ObjectArray<T>(capacity); |
| 88 } | 90 } |
| 89 | 91 |
| 90 GrowableObjectArray._usingArray(List<T> array) { | |
| 91 backingArray = array; | |
| 92 _length = array.length; | |
| 93 if (_length == 0) { | |
| 94 grow(4); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 factory GrowableObjectArray<T>.from(Collection<T> other) { | 92 factory GrowableObjectArray<T>.from(Collection<T> other) { |
| 99 List<T> result = new GrowableObjectArray<T>(); | 93 List<T> result = new GrowableObjectArray<T>(); |
| 100 result.addAll(other); | 94 result.addAll(other); |
| 101 return result; | 95 return result; |
| 102 } | 96 } |
| 103 | 97 |
| 104 int get length() { | 98 int get length() { |
| 105 return _length; | 99 return _length; |
| 106 } | 100 } |
| 107 | 101 |
| 108 void set length(int new_length) { | 102 void set length(int new_length) { |
| 109 if (new_length >= backingArray.length) { | 103 if (new_length > backingArray.length) { |
| 110 grow(new_length); | 104 grow(new_length); |
| 111 } else { | 105 } else { |
| 112 for (int i = new_length; i < _length; i++) { | 106 for (int i = new_length; i < _length; i++) { |
| 113 backingArray[i] = null; | 107 backingArray[i] = null; |
| 114 } | 108 } |
| 115 } | 109 } |
| 116 _length = new_length; | 110 _length = new_length; |
| 117 } | 111 } |
| 118 | 112 |
| 119 T operator [](int index) { | 113 T operator [](int index) { |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 if (!hasNext()) { | 234 if (!hasNext()) { |
| 241 throw const NoMoreElementsException(); | 235 throw const NoMoreElementsException(); |
| 242 } | 236 } |
| 243 return _array[_pos++]; | 237 return _array[_pos++]; |
| 244 } | 238 } |
| 245 | 239 |
| 246 final GrowableObjectArray<T> _array; | 240 final GrowableObjectArray<T> _array; |
| 247 int _pos; | 241 int _pos; |
| 248 } | 242 } |
| 249 | 243 |
| OLD | NEW |