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 | 5 |
6 // TODO(srdjan): Use shared array implementation. | 6 // TODO(srdjan): Use shared array implementation. |
7 class _ObjectArray<E> implements List<E> { | 7 class _ObjectArray<E> implements List<E> { |
8 | 8 |
9 factory _ObjectArray(int length) native "ObjectArray_allocate"; | 9 factory _ObjectArray(int length) native "ObjectArray_allocate"; |
10 | 10 |
11 E operator [](int index) native "ObjectArray_getIndexed"; | 11 E operator [](int index) native "ObjectArray_getIndexed"; |
12 | 12 |
13 void operator []=(int index, E value) native "ObjectArray_setIndexed"; | 13 void operator []=(int index, E value) native "ObjectArray_setIndexed"; |
14 | 14 |
15 String toString() { | 15 String toString() { |
16 return Collections.collectionToString(this); | 16 return Collections.collectionToString(this); |
17 } | 17 } |
18 | 18 |
19 int get length native "ObjectArray_getLength"; | 19 int get length native "ObjectArray_getLength"; |
20 | 20 |
21 void _copyFromObjectArray(_ObjectArray src, | 21 void _copyFromObjectArray(_ObjectArray src, |
22 int srcStart, | 22 int srcStart, |
23 int dstStart, | 23 int dstStart, |
24 int count) | 24 int count) |
25 native "ObjectArray_copyFromObjectArray"; | 25 native "ObjectArray_copyFromObjectArray"; |
26 | 26 |
27 E removeAt(int index) { | 27 E removeAt(int index) { |
28 throw const UnsupportedOperationException( | 28 throw new UnsupportedError( |
29 "Cannot remove element of a non-extendable array"); | 29 "Cannot remove element of a non-extendable array"); |
30 } | 30 } |
31 | 31 |
32 void setRange(int start, int length, List<E> from, [int startFrom = 0]) { | 32 void setRange(int start, int length, List<E> from, [int startFrom = 0]) { |
33 if (length < 0) { | 33 if (length < 0) { |
34 throw new ArgumentError("negative length $length"); | 34 throw new ArgumentError("negative length $length"); |
35 } | 35 } |
36 if (from is _ObjectArray) { | 36 if (from is _ObjectArray) { |
37 _copyFromObjectArray(from, startFrom, start, length); | 37 _copyFromObjectArray(from, startFrom, start, length); |
38 } else { | 38 } else { |
39 Arrays.copy(from, startFrom, this, start, length); | 39 Arrays.copy(from, startFrom, this, start, length); |
40 } | 40 } |
41 } | 41 } |
42 | 42 |
43 void removeRange(int start, int length) { | 43 void removeRange(int start, int length) { |
44 throw const UnsupportedOperationException( | 44 throw new UnsupportedError( |
45 "Cannot remove range of a non-extendable array"); | 45 "Cannot remove range of a non-extendable array"); |
46 } | 46 } |
47 | 47 |
48 void insertRange(int start, int length, [E initialValue = null]) { | 48 void insertRange(int start, int length, [E initialValue = null]) { |
49 throw const UnsupportedOperationException( | 49 throw new UnsupportedError( |
50 "Cannot insert range in a non-extendable array"); | 50 "Cannot insert range in a non-extendable array"); |
51 } | 51 } |
52 | 52 |
53 List<E> getRange(int start, int length) { | 53 List<E> getRange(int start, int length) { |
54 if (length == 0) return []; | 54 if (length == 0) return []; |
55 Arrays.rangeCheck(this, start, length); | 55 Arrays.rangeCheck(this, start, length); |
56 List list = new _GrowableObjectArray<E>.withCapacity(length); | 56 List list = new _GrowableObjectArray<E>.withCapacity(length); |
57 list.length = length; | 57 list.length = length; |
58 Arrays.copy(this, start, list, 0, length); | 58 Arrays.copy(this, start, list, 0, length); |
59 return list; | 59 return list; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 int lastIndexOf(E element, [int start = null]) { | 103 int lastIndexOf(E element, [int start = null]) { |
104 if (start === null) start = length - 1; | 104 if (start === null) start = length - 1; |
105 return Arrays.lastIndexOf(this, element, start); | 105 return Arrays.lastIndexOf(this, element, start); |
106 } | 106 } |
107 | 107 |
108 Iterator<E> iterator() { | 108 Iterator<E> iterator() { |
109 return new _FixedSizeArrayIterator<E>(this); | 109 return new _FixedSizeArrayIterator<E>(this); |
110 } | 110 } |
111 | 111 |
112 void add(E element) { | 112 void add(E element) { |
113 throw const UnsupportedOperationException( | 113 throw new UnsupportedError( |
114 "Cannot add to a non-extendable array"); | 114 "Cannot add to a non-extendable array"); |
115 } | 115 } |
116 | 116 |
117 void addLast(E element) { | 117 void addLast(E element) { |
118 add(element); | 118 add(element); |
119 } | 119 } |
120 | 120 |
121 void addAll(Collection<E> elements) { | 121 void addAll(Collection<E> elements) { |
122 throw const UnsupportedOperationException( | 122 throw new UnsupportedError( |
123 "Cannot add to a non-extendable array"); | 123 "Cannot add to a non-extendable array"); |
124 } | 124 } |
125 | 125 |
126 void clear() { | 126 void clear() { |
127 throw const UnsupportedOperationException( | 127 throw new UnsupportedError( |
128 "Cannot clear a non-extendable array"); | 128 "Cannot clear a non-extendable array"); |
129 } | 129 } |
130 | 130 |
131 void set length(int length) { | 131 void set length(int length) { |
132 throw const UnsupportedOperationException( | 132 throw new UnsupportedError( |
133 "Cannot change the length of a non-extendable array"); | 133 "Cannot change the length of a non-extendable array"); |
134 } | 134 } |
135 | 135 |
136 E removeLast() { | 136 E removeLast() { |
137 throw const UnsupportedOperationException( | 137 throw new UnsupportedError( |
138 "Cannot remove in a non-extendable array"); | 138 "Cannot remove in a non-extendable array"); |
139 } | 139 } |
140 | 140 |
141 E last() { | 141 E last() { |
142 return this[length - 1]; | 142 return this[length - 1]; |
143 } | 143 } |
144 } | 144 } |
145 | 145 |
146 | 146 |
147 // This is essentially the same class as _ObjectArray, but it does not | 147 // This is essentially the same class as _ObjectArray, but it does not |
148 // permit any modification of array elements from Dart code. We use | 148 // permit any modification of array elements from Dart code. We use |
149 // this class for arrays constructed from Dart array literals. | 149 // this class for arrays constructed from Dart array literals. |
150 // TODO(hausner): We should consider the trade-offs between two | 150 // TODO(hausner): We should consider the trade-offs between two |
151 // classes (and inline cache misses) versus a field in the native | 151 // classes (and inline cache misses) versus a field in the native |
152 // implementation (checks when modifying). We should keep watching | 152 // implementation (checks when modifying). We should keep watching |
153 // the inline cache misses. | 153 // the inline cache misses. |
154 class _ImmutableArray<E> implements List<E> { | 154 class _ImmutableArray<E> implements List<E> { |
155 | 155 |
156 factory _ImmutableArray._uninstantiable() { | 156 factory _ImmutableArray._uninstantiable() { |
157 throw const UnsupportedOperationException( | 157 throw new UnsupportedError( |
158 "ImmutableArray can only be allocated by the VM"); | 158 "ImmutableArray can only be allocated by the VM"); |
159 } | 159 } |
160 | 160 |
161 E operator [](int index) native "ObjectArray_getIndexed"; | 161 E operator [](int index) native "ObjectArray_getIndexed"; |
162 | 162 |
163 void operator []=(int index, E value) { | 163 void operator []=(int index, E value) { |
164 throw const UnsupportedOperationException( | 164 throw new UnsupportedError( |
165 "Cannot modify an immutable array"); | 165 "Cannot modify an immutable array"); |
166 } | 166 } |
167 | 167 |
168 int get length native "ObjectArray_getLength"; | 168 int get length native "ObjectArray_getLength"; |
169 | 169 |
170 E removeAt(int index) { | 170 E removeAt(int index) { |
171 throw const UnsupportedOperationException( | 171 throw new UnsupportedError( |
172 "Cannot modify an immutable array"); | 172 "Cannot modify an immutable array"); |
173 } | 173 } |
174 | 174 |
175 void copyFrom(List src, int srcStart, int dstStart, int count) { | 175 void copyFrom(List src, int srcStart, int dstStart, int count) { |
176 throw const UnsupportedOperationException( | 176 throw new UnsupportedError( |
177 "Cannot modify an immutable array"); | 177 "Cannot modify an immutable array"); |
178 } | 178 } |
179 | 179 |
180 void setRange(int start, int length, List<E> from, [int startFrom = 0]) { | 180 void setRange(int start, int length, List<E> from, [int startFrom = 0]) { |
181 throw const UnsupportedOperationException( | 181 throw new UnsupportedError( |
182 "Cannot modify an immutable array"); | 182 "Cannot modify an immutable array"); |
183 } | 183 } |
184 | 184 |
185 void removeRange(int start, int length) { | 185 void removeRange(int start, int length) { |
186 throw const UnsupportedOperationException( | 186 throw new UnsupportedError( |
187 "Cannot remove range of an immutable array"); | 187 "Cannot remove range of an immutable array"); |
188 } | 188 } |
189 | 189 |
190 void insertRange(int start, int length, [E initialValue = null]) { | 190 void insertRange(int start, int length, [E initialValue = null]) { |
191 throw const UnsupportedOperationException( | 191 throw new UnsupportedError( |
192 "Cannot insert range in an immutable array"); | 192 "Cannot insert range in an immutable array"); |
193 } | 193 } |
194 | 194 |
195 List<E> getRange(int start, int length) { | 195 List<E> getRange(int start, int length) { |
196 if (length == 0) return []; | 196 if (length == 0) return []; |
197 Arrays.rangeCheck(this, start, length); | 197 Arrays.rangeCheck(this, start, length); |
198 List list = new List<E>(); | 198 List list = new List<E>(); |
199 list.length = length; | 199 list.length = length; |
200 Arrays.copy(this, start, list, 0, length); | 200 Arrays.copy(this, start, list, 0, length); |
201 return list; | 201 return list; |
(...skipping 26 matching lines...) Expand all Loading... |
228 | 228 |
229 bool some(bool f(E element)) { | 229 bool some(bool f(E element)) { |
230 return Collections.some(this, f); | 230 return Collections.some(this, f); |
231 } | 231 } |
232 | 232 |
233 bool isEmpty() { | 233 bool isEmpty() { |
234 return this.length === 0; | 234 return this.length === 0; |
235 } | 235 } |
236 | 236 |
237 void sort([Comparator<E> compare]) { | 237 void sort([Comparator<E> compare]) { |
238 throw const UnsupportedOperationException( | 238 throw new UnsupportedError( |
239 "Cannot modify an immutable array"); | 239 "Cannot modify an immutable array"); |
240 } | 240 } |
241 | 241 |
242 String toString() { | 242 String toString() { |
243 return Collections.collectionToString(this); | 243 return Collections.collectionToString(this); |
244 } | 244 } |
245 | 245 |
246 int indexOf(E element, [int start = 0]) { | 246 int indexOf(E element, [int start = 0]) { |
247 return Arrays.indexOf(this, element, start, this.length); | 247 return Arrays.indexOf(this, element, start, this.length); |
248 } | 248 } |
249 | 249 |
250 int lastIndexOf(E element, [int start = null]) { | 250 int lastIndexOf(E element, [int start = null]) { |
251 if (start === null) start = length - 1; | 251 if (start === null) start = length - 1; |
252 return Arrays.lastIndexOf(this, element, start); | 252 return Arrays.lastIndexOf(this, element, start); |
253 } | 253 } |
254 | 254 |
255 Iterator<E> iterator() { | 255 Iterator<E> iterator() { |
256 return new _FixedSizeArrayIterator<E>(this); | 256 return new _FixedSizeArrayIterator<E>(this); |
257 } | 257 } |
258 | 258 |
259 void add(E element) { | 259 void add(E element) { |
260 throw const UnsupportedOperationException( | 260 throw new UnsupportedError( |
261 "Cannot add to an immutable array"); | 261 "Cannot add to an immutable array"); |
262 } | 262 } |
263 | 263 |
264 void addLast(E element) { | 264 void addLast(E element) { |
265 add(element); | 265 add(element); |
266 } | 266 } |
267 | 267 |
268 void addAll(Collection<E> elements) { | 268 void addAll(Collection<E> elements) { |
269 throw const UnsupportedOperationException( | 269 throw new UnsupportedError( |
270 "Cannot add to an immutable array"); | 270 "Cannot add to an immutable array"); |
271 } | 271 } |
272 | 272 |
273 void clear() { | 273 void clear() { |
274 throw const UnsupportedOperationException( | 274 throw new UnsupportedError( |
275 "Cannot clear an immutable array"); | 275 "Cannot clear an immutable array"); |
276 } | 276 } |
277 | 277 |
278 void set length(int length) { | 278 void set length(int length) { |
279 throw const UnsupportedOperationException( | 279 throw new UnsupportedError( |
280 "Cannot change the length of an immutable array"); | 280 "Cannot change the length of an immutable array"); |
281 } | 281 } |
282 | 282 |
283 E removeLast() { | 283 E removeLast() { |
284 throw const UnsupportedOperationException( | 284 throw new UnsupportedError( |
285 "Cannot remove in a non-extendable array"); | 285 "Cannot remove in a non-extendable array"); |
286 } | 286 } |
287 | 287 |
288 E last() { | 288 E last() { |
289 return this[length - 1]; | 289 return this[length - 1]; |
290 } | 290 } |
291 } | 291 } |
292 | 292 |
293 | 293 |
294 // Iterator for arrays with fixed size. | 294 // Iterator for arrays with fixed size. |
(...skipping 11 matching lines...) Expand all Loading... |
306 if (!hasNext()) { | 306 if (!hasNext()) { |
307 throw const NoMoreElementsException(); | 307 throw const NoMoreElementsException(); |
308 } | 308 } |
309 return _array[_pos++]; | 309 return _array[_pos++]; |
310 } | 310 } |
311 | 311 |
312 final List<E> _array; | 312 final List<E> _array; |
313 final int _length; // Cache array length for faster access. | 313 final int _length; // Cache array length for faster access. |
314 int _pos; | 314 int _pos; |
315 } | 315 } |
OLD | NEW |