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 GrowableObjectArray<T> implements List<T> { | 5 class _GrowableObjectArray<T> implements List<T> { |
6 factory GrowableObjectArray._uninstantiable() { | 6 factory _GrowableObjectArray._uninstantiable() { |
7 throw const UnsupportedOperationException( | 7 throw const UnsupportedOperationException( |
8 "GrowableObjectArray can only be allocated by the VM"); | 8 "GrowableObjectArray can only be allocated by the VM"); |
9 } | 9 } |
10 | 10 |
11 T removeAt(int index) { | 11 T removeAt(int index) { |
12 if (index is! int) throw new ArgumentError(index); | 12 if (index is! int) throw new ArgumentError(index); |
13 T result = this[index]; | 13 T result = this[index]; |
14 int newLength = this.length - 1; | 14 int newLength = this.length - 1; |
15 Arrays.copy(this, | 15 Arrays.copy(this, |
16 index + 1, | 16 index + 1, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 start + length, | 59 start + length, |
60 old_length - start); | 60 old_length - start); |
61 for (int i = start; i < start + length; i++) { | 61 for (int i = start; i < start + length; i++) { |
62 this[i] = initialValue; | 62 this[i] = initialValue; |
63 } | 63 } |
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 GrowableObjectArray<T>.withCapacity(length); | 69 List list = new _GrowableObjectArray<T>.withCapacity(length); |
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 factory GrowableObjectArray() { | 75 factory _GrowableObjectArray() { |
76 var data = new ObjectArray<T>(4); | 76 var data = new _ObjectArray<T>(4); |
77 return new GrowableObjectArray<T>.fromObjectArray(data); | 77 return new _GrowableObjectArray<T>.fromObjectArray(data); |
78 } | 78 } |
79 | 79 |
80 factory GrowableObjectArray.withCapacity(int capacity) { | 80 factory _GrowableObjectArray.withCapacity(int capacity) { |
81 var data = new ObjectArray<T>((capacity == 0)? 4 : capacity); | 81 var data = new _ObjectArray<T>((capacity == 0)? 4 : capacity); |
82 return new GrowableObjectArray<T>.fromObjectArray(data); | 82 return new _GrowableObjectArray<T>.fromObjectArray(data); |
83 } | 83 } |
84 | 84 |
85 factory GrowableObjectArray.from(Collection<T> other) { | 85 factory _GrowableObjectArray.from(Collection<T> other) { |
86 List<T> result = new GrowableObjectArray<T>(); | 86 List<T> result = new _GrowableObjectArray<T>(); |
87 result.addAll(other); | 87 result.addAll(other); |
88 return result; | 88 return result; |
89 } | 89 } |
90 | 90 |
91 factory GrowableObjectArray.fromObjectArray(ObjectArray<T> data) | 91 factory _GrowableObjectArray.fromObjectArray(_ObjectArray<T> data) |
92 native "GrowableObjectArray_allocate"; | 92 native "GrowableObjectArray_allocate"; |
93 | 93 |
94 int get length native "GrowableObjectArray_getLength"; | 94 int get length native "GrowableObjectArray_getLength"; |
95 | 95 |
96 int get capacity native "GrowableObjectArray_getCapacity"; | 96 int get capacity native "GrowableObjectArray_getCapacity"; |
97 | 97 |
98 void set length(int new_length) { | 98 void set length(int new_length) { |
99 if (new_length > capacity) { | 99 if (new_length > capacity) { |
100 _grow(new_length); | 100 _grow(new_length); |
101 } else { | 101 } else { |
102 for (int i = new_length; i < length; i++) { | 102 for (int i = new_length; i < length; i++) { |
103 this[i] = null; | 103 this[i] = null; |
104 } | 104 } |
105 } | 105 } |
106 _setLength(new_length); | 106 _setLength(new_length); |
107 } | 107 } |
108 | 108 |
109 void _setLength(int new_length) native "GrowableObjectArray_setLength"; | 109 void _setLength(int new_length) native "GrowableObjectArray_setLength"; |
110 | 110 |
111 void _setData(ObjectArray<T> array) native "GrowableObjectArray_setData"; | 111 void _setData(_ObjectArray<T> array) native "GrowableObjectArray_setData"; |
112 | 112 |
113 T operator [](int index) native "GrowableObjectArray_getIndexed"; | 113 T operator [](int index) native "GrowableObjectArray_getIndexed"; |
114 | 114 |
115 void operator []=(int index, T value) native "GrowableObjectArray_setIndexed"; | 115 void operator []=(int index, T value) native "GrowableObjectArray_setIndexed"; |
116 | 116 |
117 // The length of this growable array. It is always less than or equal to the | 117 // The length of this growable array. It is always less than or equal to the |
118 // length of the object array, which itself is always greater than 0, so that | 118 // length of the object array, which itself is always greater than 0, so that |
119 // grow() does not have to check for a zero length object array before | 119 // grow() does not have to check for a zero length object array before |
120 // doubling its size. | 120 // doubling its size. |
121 void add(T value) { | 121 void add(T value) { |
(...skipping 30 matching lines...) Expand all Loading... |
152 int indexOf(T element, [int start = 0]) { | 152 int indexOf(T element, [int start = 0]) { |
153 return Arrays.indexOf(this, element, start, length); | 153 return Arrays.indexOf(this, element, start, length); |
154 } | 154 } |
155 | 155 |
156 int lastIndexOf(T element, [int start = null]) { | 156 int lastIndexOf(T element, [int start = null]) { |
157 if (start === null) start = length - 1; | 157 if (start === null) start = length - 1; |
158 return Arrays.lastIndexOf(this, element, start); | 158 return Arrays.lastIndexOf(this, element, start); |
159 } | 159 } |
160 | 160 |
161 void _grow(int new_length) { | 161 void _grow(int new_length) { |
162 var new_data = new ObjectArray<T>(new_length); | 162 var new_data = new _ObjectArray<T>(new_length); |
163 for (int i = 0; i < length; i++) { | 163 for (int i = 0; i < length; i++) { |
164 new_data[i] = this[i]; | 164 new_data[i] = this[i]; |
165 } | 165 } |
166 _setData(new_data); | 166 _setData(new_data); |
167 } | 167 } |
168 | 168 |
169 /** | 169 /** |
170 * Collection interface. | 170 * Collection interface. |
171 */ | 171 */ |
172 | 172 |
173 void forEach(f(T element)) { | 173 void forEach(f(T element)) { |
174 // TODO(srdjan): Use Collections.forEach(this, f); | 174 // TODO(srdjan): Use Collections.forEach(this, f); |
175 // Accessing the list directly improves DeltaBlue performance by 25%. | 175 // Accessing the list directly improves DeltaBlue performance by 25%. |
176 for (int i = 0; i < length; i++) { | 176 for (int i = 0; i < length; i++) { |
177 f(this[i]); | 177 f(this[i]); |
178 } | 178 } |
179 } | 179 } |
180 | 180 |
181 Collection map(f(T element)) { | 181 Collection map(f(T element)) { |
182 return Collections.map(this, | 182 return Collections.map(this, |
183 new GrowableObjectArray.withCapacity(length), f); | 183 new _GrowableObjectArray.withCapacity(length), f); |
184 } | 184 } |
185 | 185 |
186 reduce(initialValue, combine(previousValue, T element)) { | 186 reduce(initialValue, combine(previousValue, T element)) { |
187 return Collections.reduce(this, initialValue, combine); | 187 return Collections.reduce(this, initialValue, combine); |
188 } | 188 } |
189 | 189 |
190 Collection<T> filter(bool f(T element)) { | 190 Collection<T> filter(bool f(T element)) { |
191 return Collections.filter(this, new GrowableObjectArray<T>(), f); | 191 return Collections.filter(this, new _GrowableObjectArray<T>(), f); |
192 } | 192 } |
193 | 193 |
194 bool every(bool f(T element)) { | 194 bool every(bool f(T element)) { |
195 return Collections.every(this, f); | 195 return Collections.every(this, f); |
196 } | 196 } |
197 | 197 |
198 bool some(bool f(T element)) { | 198 bool some(bool f(T element)) { |
199 return Collections.some(this, f); | 199 return Collections.some(this, f); |
200 } | 200 } |
201 | 201 |
(...skipping 14 matching lines...) Expand all Loading... |
216 } | 216 } |
217 | 217 |
218 Iterator<T> iterator() { | 218 Iterator<T> iterator() { |
219 return new VariableSizeArrayIterator<T>(this); | 219 return new VariableSizeArrayIterator<T>(this); |
220 } | 220 } |
221 } | 221 } |
222 | 222 |
223 | 223 |
224 // Iterator for arrays with variable size. | 224 // Iterator for arrays with variable size. |
225 class VariableSizeArrayIterator<T> implements Iterator<T> { | 225 class VariableSizeArrayIterator<T> implements Iterator<T> { |
226 VariableSizeArrayIterator(GrowableObjectArray<T> array) | 226 VariableSizeArrayIterator(_GrowableObjectArray<T> array) |
227 : _array = array, _pos = 0 { | 227 : _array = array, _pos = 0 { |
228 } | 228 } |
229 | 229 |
230 bool hasNext() { | 230 bool hasNext() { |
231 return _array.length > _pos; | 231 return _array.length > _pos; |
232 } | 232 } |
233 | 233 |
234 T next() { | 234 T next() { |
235 if (!hasNext()) { | 235 if (!hasNext()) { |
236 throw const NoMoreElementsException(); | 236 throw const NoMoreElementsException(); |
237 } | 237 } |
238 return _array[_pos++]; | 238 return _array[_pos++]; |
239 } | 239 } |
240 | 240 |
241 final GrowableObjectArray<T> _array; | 241 final _GrowableObjectArray<T> _array; |
242 int _pos; | 242 int _pos; |
243 } | 243 } |
OLD | NEW |