| 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 Array<T> { | 5 class ListImplementation<T> implements Array<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 } |
| 11 | 11 |
| 12 void setRange(int start, int length, List<T> from, [int startFrom = 0]) { | 12 void setRange(int start, int length, List<T> from, [int startFrom = 0]) { |
| 13 if (length < 0) { | 13 if (length < 0) { |
| 14 throw new IllegalArgumentException("negative length $length"); | 14 throw new IllegalArgumentException("negative length $length"); |
| 15 } | 15 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 return new List<T>.fromList(this, start, start + length); | 69 return new List<T>.fromList(this, start, start + length); |
| 70 } | 70 } |
| 71 | 71 |
| 72 // The length of this growable array. It is always less than the | 72 // The length of this growable array. It is always less than the |
| 73 // length of the backing array. | 73 // length of the backing array. |
| 74 int _length; | 74 int _length; |
| 75 // Constant used by indexOf and lastIndexOf when the element given | 75 // Constant used by indexOf and lastIndexOf when the element given |
| 76 // is not in the array. | 76 // is not in the array. |
| 77 static final int ABSENT = -1; | 77 static final int ABSENT = -1; |
| 78 | 78 |
| 79 GrowableObjectArray() | 79 ListImplementation() |
| 80 : _length = 0, backingArray = new ObjectArray<T>(4) {} | 80 : _length = 0, backingArray = new ObjectArray<T>(4) {} |
| 81 | 81 |
| 82 GrowableObjectArray.withCapacity(int capacity) { | 82 ListImplementation.withCapacity(int capacity) { |
| 83 _length = 0; | 83 _length = 0; |
| 84 if (capacity <= 0) { | 84 if (capacity <= 0) { |
| 85 capacity = 4; | 85 capacity = 4; |
| 86 } | 86 } |
| 87 backingArray = new ObjectArray<T>(capacity); | 87 backingArray = new ObjectArray<T>(capacity); |
| 88 } | 88 } |
| 89 | 89 |
| 90 GrowableObjectArray._usingArray(Array<T> array) { | 90 ListImplementation._usingArray(Array<T> array) { |
| 91 backingArray = array; | 91 backingArray = array; |
| 92 _length = array.length; | 92 _length = array.length; |
| 93 if (_length == 0) { | 93 if (_length == 0) { |
| 94 grow(4); | 94 grow(4); |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 | 97 |
| 98 factory GrowableObjectArray.from(Collection<T> other) { | 98 factory ListImplementation.from(Collection<T> other) { |
| 99 Array result = new GrowableObjectArray(); | 99 List result = new List(); |
| 100 result.addAll(other); | 100 result.addAll(other); |
| 101 return result; | 101 return result; |
| 102 } | 102 } |
| 103 | 103 |
| 104 int get length() { | 104 int get length() { |
| 105 return _length; | 105 return _length; |
| 106 } | 106 } |
| 107 | 107 |
| 108 void set length(int new_length) { | 108 void set length(int new_length) { |
| 109 if (new_length >= backingArray.length) { | 109 if (new_length >= backingArray.length) { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 | 183 |
| 184 void forEach(f(T element)) { | 184 void forEach(f(T element)) { |
| 185 // TODO(srdjan): Use Collections.forEach(this, f); | 185 // TODO(srdjan): Use Collections.forEach(this, f); |
| 186 // Using backingArray directly improves DeltaBlue performance by 25%. | 186 // Using backingArray directly improves DeltaBlue performance by 25%. |
| 187 for (int i = 0; i < _length; i++) { | 187 for (int i = 0; i < _length; i++) { |
| 188 f(backingArray[i]); | 188 f(backingArray[i]); |
| 189 } | 189 } |
| 190 } | 190 } |
| 191 | 191 |
| 192 Collection<T> filter(bool f(T element)) { | 192 Collection<T> filter(bool f(T element)) { |
| 193 return Collections.filter(this, new GrowableObjectArray<T>(), f); | 193 return Collections.filter(this, new List<T>(), f); |
| 194 } | 194 } |
| 195 | 195 |
| 196 bool every(bool f(T element)) { | 196 bool every(bool f(T element)) { |
| 197 return Collections.every(this, f); | 197 return Collections.every(this, f); |
| 198 } | 198 } |
| 199 | 199 |
| 200 bool some(bool f(T element)) { | 200 bool some(bool f(T element)) { |
| 201 return Collections.some(this, f); | 201 return Collections.some(this, f); |
| 202 } | 202 } |
| 203 | 203 |
| 204 bool isEmpty() { | 204 bool isEmpty() { |
| 205 return this.length === 0; | 205 return this.length === 0; |
| 206 } | 206 } |
| 207 | 207 |
| 208 void clear() { | 208 void clear() { |
| 209 this.length = 0; | 209 this.length = 0; |
| 210 } | 210 } |
| 211 | 211 |
| 212 void sort(int compare(T a, T b)) { | 212 void sort(int compare(T a, T b)) { |
| 213 DualPivotQuicksort.sort(this, compare); | 213 DualPivotQuicksort.sort(this, compare); |
| 214 } | 214 } |
| 215 | 215 |
| 216 Iterator<T> iterator() { | 216 Iterator<T> iterator() { |
| 217 return new VariableSizeArrayIterator<T>(this); | 217 return new ListIterator<T>(this); |
| 218 } | 218 } |
| 219 } | 219 } |
| 220 | 220 |
| 221 | 221 |
| 222 // Iterator for arrays with variable size. | 222 // Iterator for lists with variable size. |
| 223 class VariableSizeArrayIterator<T> implements Iterator<T> { | 223 class ListIterator<T> implements Iterator<T> { |
| 224 VariableSizeArrayIterator(GrowableObjectArray<T> array) | 224 ListIterator(List<T> this._list) : _pos = 0; |
| 225 : _array = array, _pos = 0 { | |
| 226 } | |
| 227 | 225 |
| 228 bool hasNext() { | 226 bool hasNext() { |
| 229 return _array._length > _pos; | 227 return _list._length > _pos; |
| 230 } | 228 } |
| 231 | 229 |
| 232 T next() { | 230 T next() { |
| 233 if (!hasNext()) { | 231 if (!hasNext()) { |
| 234 throw const NoMoreElementsException(); | 232 throw const NoMoreElementsException(); |
| 235 } | 233 } |
| 236 return _array[_pos++]; | 234 return _list[_pos++]; |
| 237 } | 235 } |
| 238 | 236 |
| 239 final GrowableObjectArray<T> _array; | 237 final List<T> _list; |
| 240 int _pos; | 238 int _pos; |
| 241 } | 239 } |
| 242 | 240 |
| OLD | NEW |