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 _GrowableList<T> implements List<T> { | 5 class _GrowableList<T> implements List<T> { |
6 static final int _classId = (new _GrowableList(0))._cid; | 6 static final int _classId = (new _GrowableList(0))._cid; |
7 | 7 |
8 void insert(int index, T element) { | 8 void insert(int index, T element) { |
9 if (index < 0 || index > length) { | 9 if (index < 0 || index > length) { |
10 throw new RangeError.range(index, 0, length); | 10 throw new RangeError.range(index, 0, length); |
11 } | 11 } |
12 if (index == this.length) { | 12 if (index == this.length) { |
13 add(element); | 13 add(element); |
14 return; | 14 return; |
15 } | 15 } |
16 int oldLength = this.length; | 16 int oldLength = this.length; |
17 // We are modifying the length just below the is-check. Without the check | 17 // We are modifying the length just below the is-check. Without the check |
18 // Array.copy could throw an exception, leaving the list in a bad state | 18 // Array.copy could throw an exception, leaving the list in a bad state |
19 // (with a length that has been increased, but without a new element). | 19 // (with a length that has been increased, but without a new element). |
20 if (index is! int) throw new ArgumentError(index); | 20 if (index is! int) throw new ArgumentError(index); |
21 this.length++; | 21 this.length++; |
22 Arrays.copy(this, | 22 Lists.copy(this, |
23 index, | 23 index, |
24 this, | 24 this, |
25 index + 1, | 25 index + 1, |
26 oldLength - index); | 26 oldLength - index); |
27 this[index] = element; | 27 this[index] = element; |
28 } | 28 } |
29 | 29 |
30 T removeAt(int index) { | 30 T removeAt(int index) { |
31 if (index is! int) throw new ArgumentError(index); | 31 if (index is! int) throw new ArgumentError(index); |
32 T result = this[index]; | 32 T result = this[index]; |
33 int newLength = this.length - 1; | 33 int newLength = this.length - 1; |
34 Arrays.copy(this, | 34 Lists.copy(this, |
35 index + 1, | 35 index + 1, |
36 this, | 36 this, |
37 index, | 37 index, |
38 newLength - index); | 38 newLength - index); |
39 this.length = newLength; | 39 this.length = newLength; |
40 return result; | 40 return result; |
41 } | 41 } |
42 | 42 |
43 bool remove(Object element) { | 43 bool remove(Object element) { |
44 for (int i = 0; i < this.length; i++) { | 44 for (int i = 0; i < this.length; i++) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 | 88 |
89 Iterable<T> getRange(int start, int end) { | 89 Iterable<T> getRange(int start, int end) { |
90 return IterableMixinWorkaround.getRangeList(this, start, end); | 90 return IterableMixinWorkaround.getRangeList(this, start, end); |
91 } | 91 } |
92 | 92 |
93 void setRange(int start, int end, Iterable<T> iterable, [int skipCount = 0]) { | 93 void setRange(int start, int end, Iterable<T> iterable, [int skipCount = 0]) { |
94 IterableMixinWorkaround.setRangeList(this, start, end, iterable, skipCount); | 94 IterableMixinWorkaround.setRangeList(this, start, end, iterable, skipCount); |
95 } | 95 } |
96 | 96 |
97 void removeRange(int start, int end) { | 97 void removeRange(int start, int end) { |
98 Arrays.indicesCheck(this, start, end); | 98 Lists.indicesCheck(this, start, end); |
99 Arrays.copy(this, | 99 Lists.copy(this, |
100 end, | 100 end, |
101 this, | 101 this, |
102 start, | 102 start, |
103 this.length - end); | 103 this.length - end); |
104 this.length = this.length - (end - start); | 104 this.length = this.length - (end - start); |
105 } | 105 } |
106 | 106 |
107 void replaceRange(int start, int end, Iterable<T> iterable) { | 107 void replaceRange(int start, int end, Iterable<T> iterable) { |
108 IterableMixinWorkaround.replaceRangeList(this, start, end, iterable); | 108 IterableMixinWorkaround.replaceRangeList(this, start, end, iterable); |
109 } | 109 } |
110 | 110 |
111 void fillRange(int start, int end, [T fillValue]) { | 111 void fillRange(int start, int end, [T fillValue]) { |
112 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); | 112 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); |
113 } | 113 } |
114 | 114 |
115 List<T> sublist(int start, [int end]) { | 115 List<T> sublist(int start, [int end]) { |
116 Arrays.indicesCheck(this, start, end); | 116 Lists.indicesCheck(this, start, end); |
117 if (end == null) end = this.length; | 117 if (end == null) end = this.length; |
118 int length = end - start; | 118 int length = end - start; |
119 if (start == end) return <T>[]; | 119 if (start == end) return <T>[]; |
120 List list = new _GrowableList<T>.withCapacity(length); | 120 List list = new _GrowableList<T>.withCapacity(length); |
121 list.length = length; | 121 list.length = length; |
122 Arrays.copy(this, start, list, 0, length); | 122 Lists.copy(this, start, list, 0, length); |
123 return list; | 123 return list; |
124 } | 124 } |
125 | 125 |
126 factory _GrowableList(int length) { | 126 factory _GrowableList(int length) { |
127 var data = new _List((length == 0) ? 4 : length); | 127 var data = new _List((length == 0) ? 4 : length); |
128 var result = new _GrowableList<T>.withData(data); | 128 var result = new _GrowableList<T>.withData(data); |
129 if (length > 0) { | 129 if (length > 0) { |
130 result._setLength(length); | 130 result._setLength(length); |
131 } | 131 } |
132 return result; | 132 return result; |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 } | 353 } |
354 | 354 |
355 Set<T> toSet() { | 355 Set<T> toSet() { |
356 return new Set<T>.from(this); | 356 return new Set<T>.from(this); |
357 } | 357 } |
358 | 358 |
359 Map<int, T> asMap() { | 359 Map<int, T> asMap() { |
360 return IterableMixinWorkaround.asMapList(this); | 360 return IterableMixinWorkaround.asMapList(this); |
361 } | 361 } |
362 } | 362 } |
OLD | NEW |