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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 the |
76 // length of the backing array. | 76 // length of the backing array. |
77 int _length; | 77 int _length; |
78 // Constant used by indexOf and lastIndexOf when the element given | |
79 // is not in the array. | |
80 static final int ABSENT = -1; | |
81 | 78 |
82 GrowableObjectArray() | 79 GrowableObjectArray() |
83 : _length = 0, backingArray = new ObjectArray<T>(4) {} | 80 : _length = 0, backingArray = new ObjectArray<T>(4) {} |
84 | 81 |
85 GrowableObjectArray.withCapacity(int capacity) { | 82 GrowableObjectArray.withCapacity(int capacity) { |
86 _length = 0; | 83 _length = 0; |
87 if (capacity <= 0) { | 84 if (capacity <= 0) { |
88 capacity = 4; | 85 capacity = 4; |
89 } | 86 } |
90 backingArray = new ObjectArray<T>(capacity); | 87 backingArray = new ObjectArray<T>(capacity); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 return backingArray[_length]; | 162 return backingArray[_length]; |
166 } | 163 } |
167 | 164 |
168 T last() { | 165 T last() { |
169 if (_length === 0) { | 166 if (_length === 0) { |
170 throw new IndexOutOfRangeException(-1); | 167 throw new IndexOutOfRangeException(-1); |
171 } | 168 } |
172 return backingArray[_length - 1]; | 169 return backingArray[_length - 1]; |
173 } | 170 } |
174 | 171 |
175 int indexOf(T element, int startIndex) { | 172 int indexOf(T element, [int start = 0]) { |
176 return Arrays.indexOf(backingArray, element, startIndex, _length); | 173 return Arrays.indexOf(backingArray, element, start, _length); |
177 } | 174 } |
178 | 175 |
179 int lastIndexOf(T element, int startIndex) { | 176 int lastIndexOf(T element, [int start = null]) { |
180 return Arrays.lastIndexOf(backingArray, element, startIndex); | 177 if (start === null) start = length - 1; |
| 178 return Arrays.lastIndexOf(backingArray, element, start); |
181 } | 179 } |
182 | 180 |
183 /** | 181 /** |
184 * Collection interface. | 182 * Collection interface. |
185 */ | 183 */ |
186 | 184 |
187 void forEach(f(T element)) { | 185 void forEach(f(T element)) { |
188 // TODO(srdjan): Use Collections.forEach(this, f); | 186 // TODO(srdjan): Use Collections.forEach(this, f); |
189 // Using backingArray directly improves DeltaBlue performance by 25%. | 187 // Using backingArray directly improves DeltaBlue performance by 25%. |
190 for (int i = 0; i < _length; i++) { | 188 for (int i = 0; i < _length; i++) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 if (!hasNext()) { | 234 if (!hasNext()) { |
237 throw const NoMoreElementsException(); | 235 throw const NoMoreElementsException(); |
238 } | 236 } |
239 return _array[_pos++]; | 237 return _array[_pos++]; |
240 } | 238 } |
241 | 239 |
242 final GrowableObjectArray<T> _array; | 240 final GrowableObjectArray<T> _array; |
243 int _pos; | 241 int _pos; |
244 } | 242 } |
245 | 243 |
OLD | NEW |