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 _List<E> implements List<E> { | 7 class _List<E> implements List<E> { |
8 static final int _classId = (new _List(0))._cid; | 8 static final int _classId = (new _List(0))._cid; |
9 | 9 |
10 factory _List(length) native "List_allocate"; | 10 factory _List(length) native "List_allocate"; |
11 | 11 |
12 E operator [](int index) native "List_getIndexed"; | 12 E operator [](int index) native "List_getIndexed"; |
13 | 13 |
14 void operator []=(int index, E value) native "List_setIndexed"; | 14 void operator []=(int index, E value) native "List_setIndexed"; |
15 | 15 |
16 String toString() { | 16 String toString() { |
17 return IterableMixinWorkaround.toStringIterable(this,'[' , ']'); | 17 return IterableMixinWorkaround.toStringIterable(this,'[' , ']'); |
18 } | 18 } |
19 | 19 |
20 int get length native "List_getLength"; | 20 int get length native "List_getLength"; |
21 | 21 |
22 void _copyFromObjectArray(_List src, | 22 void _copyFromObjectArray(_List src, |
23 int srcStart, | 23 int srcStart, |
24 int dstStart, | 24 int dstStart, |
25 int count) | 25 int count) |
26 native "List_copyFromObjectArray"; | 26 native "List_copyFromObjectArray"; |
27 | 27 |
28 void insert(int index, E element) { | 28 void insert(int index, E element) { |
29 throw new UnsupportedError( | 29 throw NonGrowableListError.add(); |
30 "Cannot insert into a fixed-length list"); | |
31 } | 30 } |
32 | 31 |
33 void insertAll(int index, Iterable<E> iterable) { | 32 void insertAll(int index, Iterable<E> iterable) { |
34 throw new UnsupportedError( | 33 throw NonGrowableListError.add(); |
35 "Cannot insert into a fixed-length list"); | |
36 } | 34 } |
37 | 35 |
38 void setAll(int index, Iterable<E> iterable) { | 36 void setAll(int index, Iterable<E> iterable) { |
39 IterableMixinWorkaround.setAllList(this, index, iterable); | 37 IterableMixinWorkaround.setAllList(this, index, iterable); |
40 } | 38 } |
41 | 39 |
42 E removeAt(int index) { | 40 E removeAt(int index) { |
43 throw new UnsupportedError( | 41 throw NonGrowableListError.remove(); |
44 "Cannot remove from a fixed-length list"); | |
45 } | 42 } |
46 | 43 |
47 bool remove(Object element) { | 44 bool remove(Object element) { |
48 throw new UnsupportedError( | 45 throw NonGrowableListError.remove(); |
49 "Cannot remove from a fixed-length list"); | |
50 } | 46 } |
51 | 47 |
52 void removeWhere(bool test(E element)) { | 48 void removeWhere(bool test(E element)) { |
53 throw new UnsupportedError( | 49 throw NonGrowableListError.remove(); |
54 "Cannot remove from a fixed-length list"); | |
55 } | 50 } |
56 | 51 |
57 void retainWhere(bool test(E element)) { | 52 void retainWhere(bool test(E element)) { |
58 throw new UnsupportedError( | 53 throw NonGrowableListError.remove(); |
59 "Cannot remove from a fixed-length list"); | |
60 } | 54 } |
61 | 55 |
62 Iterable<E> getRange(int start, [int end]) { | 56 Iterable<E> getRange(int start, [int end]) { |
63 return IterableMixinWorkaround.getRangeList(this, start, end); | 57 return IterableMixinWorkaround.getRangeList(this, start, end); |
64 } | 58 } |
65 | 59 |
66 // List interface. | 60 // List interface. |
67 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { | 61 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { |
68 if (start < 0 || start > this.length) { | 62 if (start < 0 || start > this.length) { |
69 throw new RangeError.range(start, 0, this.length); | 63 throw new RangeError.range(start, 0, this.length); |
(...skipping 17 matching lines...) Expand all Loading... |
87 } | 81 } |
88 for (int i = start; i < end; i++) { | 82 for (int i = start; i < end; i++) { |
89 if (!it.moveNext()) return; | 83 if (!it.moveNext()) return; |
90 this[i] = it.current; | 84 this[i] = it.current; |
91 } | 85 } |
92 } | 86 } |
93 } | 87 } |
94 } | 88 } |
95 | 89 |
96 void removeRange(int start, int end) { | 90 void removeRange(int start, int end) { |
97 throw new UnsupportedError( | 91 throw NonGrowableListError.remove(); |
98 "Cannot remove range from a fixed-length list"); | |
99 } | 92 } |
100 | 93 |
101 void replaceRange(int start, int end, Iterable<E> iterable) { | 94 void replaceRange(int start, int end, Iterable<E> iterable) { |
102 throw new UnsupportedError( | 95 throw NonGrowableListError.remove(); |
103 "Cannot remove range from a fixed-length list"); | |
104 } | 96 } |
105 | 97 |
106 void fillRange(int start, int end, [E fillValue]) { | 98 void fillRange(int start, int end, [E fillValue]) { |
107 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); | 99 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); |
108 } | 100 } |
109 | 101 |
110 List<E> sublist(int start, [int end]) { | 102 List<E> sublist(int start, [int end]) { |
111 Lists.indicesCheck(this, start, end); | 103 Lists.indicesCheck(this, start, end); |
112 if (end == null) end = this.length; | 104 if (end == null) end = this.length; |
113 int length = end - start; | 105 int length = end - start; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 int lastIndexOf(Object element, [int start = null]) { | 207 int lastIndexOf(Object element, [int start = null]) { |
216 if (start == null) start = length - 1; | 208 if (start == null) start = length - 1; |
217 return Lists.lastIndexOf(this, element, start); | 209 return Lists.lastIndexOf(this, element, start); |
218 } | 210 } |
219 | 211 |
220 Iterator<E> get iterator { | 212 Iterator<E> get iterator { |
221 return new _FixedSizeArrayIterator<E>(this); | 213 return new _FixedSizeArrayIterator<E>(this); |
222 } | 214 } |
223 | 215 |
224 void add(E element) { | 216 void add(E element) { |
225 throw new UnsupportedError( | 217 throw NonGrowableListError.add(); |
226 "Cannot add to a fixed-length list"); | |
227 } | 218 } |
228 | 219 |
229 void addAll(Iterable<E> iterable) { | 220 void addAll(Iterable<E> iterable) { |
230 throw new UnsupportedError( | 221 throw NonGrowableListError.add(); |
231 "Cannot add to a fixed-length list"); | |
232 } | 222 } |
233 | 223 |
234 void clear() { | 224 void clear() { |
235 throw new UnsupportedError( | 225 throw NonGrowableListError.remove(); |
236 "Cannot clear a fixed-length list"); | |
237 } | 226 } |
238 | 227 |
239 void set length(int length) { | 228 void set length(int length) { |
240 throw new UnsupportedError( | 229 throw NonGrowableListError.length(); |
241 "Cannot resize a fixed-length list"); | |
242 } | 230 } |
243 | 231 |
244 E removeLast() { | 232 E removeLast() { |
245 throw new UnsupportedError( | 233 throw NonGrowableListError.remove(); |
246 "Cannot remove from a fixed-length list"); | |
247 } | 234 } |
248 | 235 |
249 E get first { | 236 E get first { |
250 if (length > 0) return this[0]; | 237 if (length > 0) return this[0]; |
251 throw new StateError("No elements"); | 238 throw IterableElementError.noElement(); |
252 } | 239 } |
253 | 240 |
254 E get last { | 241 E get last { |
255 if (length > 0) return this[length - 1]; | 242 if (length > 0) return this[length - 1]; |
256 throw new StateError("No elements"); | 243 throw IterableElementError.noElement(); |
257 } | 244 } |
258 | 245 |
259 E get single { | 246 E get single { |
260 if (length == 1) return this[0]; | 247 if (length == 1) return this[0]; |
261 if (length == 0) throw new StateError("No elements"); | 248 if (length == 0) throw IterableElementError.noElement(); |
262 throw new StateError("More than one element"); | 249 throw IterableElementError.tooMany(); |
263 } | 250 } |
264 | 251 |
265 List<E> toList({ bool growable: true}) { | 252 List<E> toList({ bool growable: true}) { |
266 return new List<E>.from(this, growable: growable); | 253 return new List<E>.from(this, growable: growable); |
267 } | 254 } |
268 | 255 |
269 Set<E> toSet() { | 256 Set<E> toSet() { |
270 return new Set<E>.from(this); | 257 return new Set<E>.from(this); |
271 } | 258 } |
272 | 259 |
(...skipping 17 matching lines...) Expand all Loading... |
290 throw new UnsupportedError( | 277 throw new UnsupportedError( |
291 "ImmutableArray can only be allocated by the VM"); | 278 "ImmutableArray can only be allocated by the VM"); |
292 } | 279 } |
293 | 280 |
294 factory _ImmutableList._from(List from, int offset, int length) | 281 factory _ImmutableList._from(List from, int offset, int length) |
295 native "ImmutableList_from"; | 282 native "ImmutableList_from"; |
296 | 283 |
297 E operator [](int index) native "List_getIndexed"; | 284 E operator [](int index) native "List_getIndexed"; |
298 | 285 |
299 void operator []=(int index, E value) { | 286 void operator []=(int index, E value) { |
300 throw new UnsupportedError( | 287 throw UnmodifiableListError.change(); |
301 "Cannot modify an immutable array"); | |
302 } | 288 } |
303 | 289 |
304 int get length native "List_getLength"; | 290 int get length native "List_getLength"; |
305 | 291 |
306 void insert(int index, E element) { | 292 void insert(int index, E element) { |
307 throw new UnsupportedError( | 293 throw UnmodifiableListError.add(); |
308 "Cannot add to an immutable array"); | |
309 } | 294 } |
310 | 295 |
311 void insertAll(int index, Iterable<E> iterable) { | 296 void insertAll(int index, Iterable<E> iterable) { |
312 throw new UnsupportedError( | 297 throw UnmodifiableListError.add(); |
313 "Cannot add to an immutable array"); | |
314 } | 298 } |
315 | 299 |
316 void setAll(int index, Iterable<E> iterable) { | 300 void setAll(int index, Iterable<E> iterable) { |
317 throw new UnsupportedError( | 301 throw UnmodifiableListError.change(); |
318 "Cannot modify an immutable array"); | |
319 } | 302 } |
320 | 303 |
321 E removeAt(int index) { | 304 E removeAt(int index) { |
322 throw new UnsupportedError( | 305 throw UnmodifiableListError.remove(); |
323 "Cannot modify an immutable array"); | |
324 } | 306 } |
325 | 307 |
326 bool remove(Object element) { | 308 bool remove(Object element) { |
327 throw new UnsupportedError( | 309 throw UnmodifiableListError.remove(); |
328 "Cannot modify an immutable array"); | |
329 } | 310 } |
330 | 311 |
331 void removeWhere(bool test(E element)) { | 312 void removeWhere(bool test(E element)) { |
332 throw new UnsupportedError( | 313 throw UnmodifiableListError.remove(); |
333 "Cannot modify an immutable array"); | |
334 } | 314 } |
335 | 315 |
336 void retainWhere(bool test(E element)) { | 316 void retainWhere(bool test(E element)) { |
337 throw new UnsupportedError( | 317 throw UnmodifiableListError.remove(); |
338 "Cannot modify an immutable array"); | |
339 } | 318 } |
340 | 319 |
341 void copyFrom(List src, int srcStart, int dstStart, int count) { | 320 void copyFrom(List src, int srcStart, int dstStart, int count) { |
342 throw new UnsupportedError( | 321 throw UnmodifiableListError.change(); |
343 "Cannot modify an immutable array"); | |
344 } | 322 } |
345 | 323 |
346 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { | 324 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { |
347 throw new UnsupportedError( | 325 throw UnmodifiableListError.change(); |
348 "Cannot modify an immutable array"); | |
349 } | 326 } |
350 | 327 |
351 void removeRange(int start, int end) { | 328 void removeRange(int start, int end) { |
352 throw new UnsupportedError( | 329 throw UnmodifiableListError.remove(); |
353 "Cannot remove range of an immutable array"); | |
354 } | 330 } |
355 | 331 |
356 void fillRange(int start, int end, [E fillValue]) { | 332 void fillRange(int start, int end, [E fillValue]) { |
357 throw new UnsupportedError( | 333 throw UnmodifiableListError.change(); |
358 "Cannot modify an immutable array"); | |
359 } | 334 } |
360 | 335 |
361 void replaceRange(int start, int end, Iterable<E> iterable) { | 336 void replaceRange(int start, int end, Iterable<E> iterable) { |
362 throw new UnsupportedError( | 337 throw UnmodifiableListError.change(); |
363 "Cannot modify an immutable array"); | |
364 } | 338 } |
365 | 339 |
366 List<E> sublist(int start, [int end]) { | 340 List<E> sublist(int start, [int end]) { |
367 Lists.indicesCheck(this, start, end); | 341 Lists.indicesCheck(this, start, end); |
368 if (end == null) end = this.length; | 342 if (end == null) end = this.length; |
369 int length = end - start; | 343 int length = end - start; |
370 if (start == end) return []; | 344 if (start == end) return []; |
371 List list = new List<E>(); | 345 List list = new List<E>(); |
372 list.length = length; | 346 list.length = length; |
373 Lists.copy(this, start, list, 0, length); | 347 Lists.copy(this, start, list, 0, length); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 | 428 |
455 bool get isEmpty { | 429 bool get isEmpty { |
456 return this.length == 0; | 430 return this.length == 0; |
457 } | 431 } |
458 | 432 |
459 bool get isNotEmpty => !isEmpty; | 433 bool get isNotEmpty => !isEmpty; |
460 | 434 |
461 Iterable<E> get reversed => IterableMixinWorkaround.reversedList(this); | 435 Iterable<E> get reversed => IterableMixinWorkaround.reversedList(this); |
462 | 436 |
463 void sort([int compare(E a, E b)]) { | 437 void sort([int compare(E a, E b)]) { |
464 throw new UnsupportedError( | 438 throw UnmodifiableListError.change(); |
465 "Cannot modify an immutable array"); | |
466 } | 439 } |
467 | 440 |
468 void shuffle([Random random]) { | 441 void shuffle([Random random]) { |
469 throw new UnsupportedError( | 442 throw UnmodifiableListError.change(); |
470 "Cannot modify an immutable array"); | |
471 } | 443 } |
472 | 444 |
473 String toString() { | 445 String toString() { |
474 return IterableMixinWorkaround.toStringIterable(this, '[', ']'); | 446 return IterableMixinWorkaround.toStringIterable(this, '[', ']'); |
475 } | 447 } |
476 | 448 |
477 int indexOf(Object element, [int start = 0]) { | 449 int indexOf(Object element, [int start = 0]) { |
478 return Lists.indexOf(this, element, start, this.length); | 450 return Lists.indexOf(this, element, start, this.length); |
479 } | 451 } |
480 | 452 |
481 int lastIndexOf(Object element, [int start = null]) { | 453 int lastIndexOf(Object element, [int start = null]) { |
482 if (start == null) start = length - 1; | 454 if (start == null) start = length - 1; |
483 return Lists.lastIndexOf(this, element, start); | 455 return Lists.lastIndexOf(this, element, start); |
484 } | 456 } |
485 | 457 |
486 Iterator<E> get iterator { | 458 Iterator<E> get iterator { |
487 return new _FixedSizeArrayIterator<E>(this); | 459 return new _FixedSizeArrayIterator<E>(this); |
488 } | 460 } |
489 | 461 |
490 void add(E element) { | 462 void add(E element) { |
491 throw new UnsupportedError( | 463 throw UnmodifiableListError.add(); |
492 "Cannot add to an immutable array"); | |
493 } | 464 } |
494 | 465 |
495 void addAll(Iterable<E> elements) { | 466 void addAll(Iterable<E> elements) { |
496 throw new UnsupportedError( | 467 throw UnmodifiableListError.add(); |
497 "Cannot add to an immutable array"); | |
498 } | 468 } |
499 | 469 |
500 void clear() { | 470 void clear() { |
501 throw new UnsupportedError( | 471 throw UnmodifiableListError.remove(); |
502 "Cannot clear an immutable array"); | |
503 } | 472 } |
504 | 473 |
505 void set length(int length) { | 474 void set length(int length) { |
506 throw new UnsupportedError( | 475 throw UnmodifiableListError.length(); |
507 "Cannot change the length of an immutable array"); | |
508 } | 476 } |
509 | 477 |
510 E removeLast() { | 478 E removeLast() { |
511 throw new UnsupportedError( | 479 throw UnmodifiableListError.remove(); |
512 "Cannot remove from a fixed-length list"); | |
513 } | 480 } |
514 | 481 |
515 E get first { | 482 E get first { |
516 if (length > 0) return this[0]; | 483 if (length > 0) return this[0]; |
517 throw new StateError("No elements"); | 484 throw IterableElementError.noElement(); |
518 } | 485 } |
519 | 486 |
520 E get last { | 487 E get last { |
521 if (length > 0) return this[length - 1]; | 488 if (length > 0) return this[length - 1]; |
522 throw new StateError("No elements"); | 489 throw IterableElementError.noElement(); |
523 } | 490 } |
524 | 491 |
525 E get single { | 492 E get single { |
526 if (length == 1) return this[0]; | 493 if (length == 1) return this[0]; |
527 if (length == 0) throw new StateError("No elements"); | 494 if (length == 0) throw IterableElementError.noElement(); |
528 throw new StateError("More than one element"); | 495 throw IterableElementError.tooMany(); |
529 } | 496 } |
530 | 497 |
531 List<E> toList({ bool growable: true }) { | 498 List<E> toList({ bool growable: true }) { |
532 return new List<E>.from(this, growable: growable); | 499 return new List<E>.from(this, growable: growable); |
533 } | 500 } |
534 | 501 |
535 Set<E> toSet() { | 502 Set<E> toSet() { |
536 return new Set<E>.from(this); | 503 return new Set<E>.from(this); |
537 } | 504 } |
538 | 505 |
(...skipping 24 matching lines...) Expand all Loading... |
563 } | 530 } |
564 _position = _length; | 531 _position = _length; |
565 _current = null; | 532 _current = null; |
566 return false; | 533 return false; |
567 } | 534 } |
568 | 535 |
569 E get current { | 536 E get current { |
570 return _current; | 537 return _current; |
571 } | 538 } |
572 } | 539 } |
OLD | NEW |