Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(300)

Side by Side Diff: runtime/lib/array.dart

Issue 10990055: Hide VM-only coreimpl List implementation types. These should not be (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix on x64 as well. Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 const UnsupportedOperationException(
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 const UnsupportedOperationException(
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 const UnsupportedOperationException(
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;
60 } 60 }
61 61
62 /** 62 /**
63 * Collection interface. 63 * Collection interface.
64 */ 64 */
65 65
66 void forEach(f(E element)) { 66 void forEach(f(E element)) {
67 Collections.forEach(this, f); 67 Collections.forEach(this, f);
68 } 68 }
69 69
70 Collection map(f(E element)) { 70 Collection map(f(E element)) {
71 return Collections.map(this, new GrowableObjectArray.withCapacity(length), f ); 71 return Collections.map(this, new _GrowableObjectArray.withCapacity(length), f);
72 } 72 }
73 73
74 reduce(initialValue, combine(previousValue, E element)) { 74 reduce(initialValue, combine(previousValue, E element)) {
75 return Collections.reduce(this, initialValue, combine); 75 return Collections.reduce(this, initialValue, combine);
76 } 76 }
77 77
78 Collection<E> filter(bool f(E element)) { 78 Collection<E> filter(bool f(E element)) {
79 return Collections.filter(this, new GrowableObjectArray<E>(), f); 79 return Collections.filter(this, new _GrowableObjectArray<E>(), f);
80 } 80 }
81 81
82 bool every(bool f(E element)) { 82 bool every(bool f(E element)) {
83 return Collections.every(this, f); 83 return Collections.every(this, f);
84 } 84 }
85 85
86 bool some(bool f(E element)) { 86 bool some(bool f(E element)) {
87 return Collections.some(this, f); 87 return Collections.some(this, f);
88 } 88 }
89 89
90 bool isEmpty() { 90 bool isEmpty() {
91 return this.length === 0; 91 return this.length === 0;
92 } 92 }
93 93
94 void sort(int compare(E a, E b)) { 94 void sort(int compare(E a, E b)) {
95 DualPivotQuicksort.sort(this, compare); 95 DualPivotQuicksort.sort(this, compare);
96 } 96 }
97 97
98 int indexOf(E element, [int start = 0]) { 98 int indexOf(E element, [int start = 0]) {
99 return Arrays.indexOf(this, element, start, this.length); 99 return Arrays.indexOf(this, element, start, this.length);
100 } 100 }
101 101
102 int lastIndexOf(E element, [int start = null]) { 102 int lastIndexOf(E element, [int start = null]) {
103 if (start === null) start = length - 1; 103 if (start === null) start = length - 1;
104 return Arrays.lastIndexOf(this, element, start); 104 return Arrays.lastIndexOf(this, element, start);
105 } 105 }
106 106
107 Iterator<E> iterator() { 107 Iterator<E> iterator() {
108 return new FixedSizeArrayIterator<E>(this); 108 return new _FixedSizeArrayIterator<E>(this);
109 } 109 }
110 110
111 void add(E element) { 111 void add(E element) {
112 throw const UnsupportedOperationException( 112 throw const UnsupportedOperationException(
113 "Cannot add to a non-extendable array"); 113 "Cannot add to a non-extendable array");
114 } 114 }
115 115
116 void addLast(E element) { 116 void addLast(E element) {
117 add(element); 117 add(element);
118 } 118 }
(...skipping 17 matching lines...) Expand all
136 throw const UnsupportedOperationException( 136 throw const UnsupportedOperationException(
137 "Cannot remove in a non-extendable array"); 137 "Cannot remove in a non-extendable array");
138 } 138 }
139 139
140 E last() { 140 E last() {
141 return this[length - 1]; 141 return this[length - 1];
142 } 142 }
143 } 143 }
144 144
145 145
146 // This is essentially the same class as ObjectArray, but it does not 146 // This is essentially the same class as _ObjectArray, but it does not
147 // permit any modification of array elements from Dart code. We use 147 // permit any modification of array elements from Dart code. We use
148 // this class for arrays constructed from Dart array literals. 148 // this class for arrays constructed from Dart array literals.
149 // TODO(hausner): We should consider the trade-offs between two 149 // TODO(hausner): We should consider the trade-offs between two
150 // classes (and inline cache misses) versus a field in the native 150 // classes (and inline cache misses) versus a field in the native
151 // implementation (checks when modifying). We should keep watching 151 // implementation (checks when modifying). We should keep watching
152 // the inline cache misses. 152 // the inline cache misses.
153 class ImmutableArray<E> implements List<E> { 153 class _ImmutableArray<E> implements List<E> {
154 154
155 factory ImmutableArray._uninstantiable() { 155 factory _ImmutableArray._uninstantiable() {
156 throw const UnsupportedOperationException( 156 throw const UnsupportedOperationException(
157 "ImmutableArray can only be allocated by the VM"); 157 "ImmutableArray can only be allocated by the VM");
158 } 158 }
159 159
160 E operator [](int index) native "ObjectArray_getIndexed"; 160 E operator [](int index) native "ObjectArray_getIndexed";
161 161
162 void operator []=(int index, E value) { 162 void operator []=(int index, E value) {
163 throw const UnsupportedOperationException( 163 throw const UnsupportedOperationException(
164 "Cannot modify an immutable array"); 164 "Cannot modify an immutable array");
165 } 165 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 202
203 /** 203 /**
204 * Collection interface. 204 * Collection interface.
205 */ 205 */
206 206
207 void forEach(f(E element)) { 207 void forEach(f(E element)) {
208 Collections.forEach(this, f); 208 Collections.forEach(this, f);
209 } 209 }
210 210
211 Collection map(f(E element)) { 211 Collection map(f(E element)) {
212 return Collections.map(this, new GrowableObjectArray.withCapacity(length), f ); 212 return Collections.map(this, new _GrowableObjectArray.withCapacity(length), f);
213 } 213 }
214 214
215 reduce(initialValue, combine(previousValue, E element)) { 215 reduce(initialValue, combine(previousValue, E element)) {
216 return Collections.reduce(this, initialValue, combine); 216 return Collections.reduce(this, initialValue, combine);
217 } 217 }
218 218
219 Collection<E> filter(bool f(E element)) { 219 Collection<E> filter(bool f(E element)) {
220 return Collections.filter(this, new GrowableObjectArray<E>(), f); 220 return Collections.filter(this, new _GrowableObjectArray<E>(), f);
221 } 221 }
222 222
223 bool every(bool f(E element)) { 223 bool every(bool f(E element)) {
224 return Collections.every(this, f); 224 return Collections.every(this, f);
225 } 225 }
226 226
227 bool some(bool f(E element)) { 227 bool some(bool f(E element)) {
228 return Collections.some(this, f); 228 return Collections.some(this, f);
229 } 229 }
230 230
(...skipping 13 matching lines...) Expand all
244 int indexOf(E element, [int start = 0]) { 244 int indexOf(E element, [int start = 0]) {
245 return Arrays.indexOf(this, element, start, this.length); 245 return Arrays.indexOf(this, element, start, this.length);
246 } 246 }
247 247
248 int lastIndexOf(E element, [int start = null]) { 248 int lastIndexOf(E element, [int start = null]) {
249 if (start === null) start = length - 1; 249 if (start === null) start = length - 1;
250 return Arrays.lastIndexOf(this, element, start); 250 return Arrays.lastIndexOf(this, element, start);
251 } 251 }
252 252
253 Iterator<E> iterator() { 253 Iterator<E> iterator() {
254 return new FixedSizeArrayIterator<E>(this); 254 return new _FixedSizeArrayIterator<E>(this);
255 } 255 }
256 256
257 void add(E element) { 257 void add(E element) {
258 throw const UnsupportedOperationException( 258 throw const UnsupportedOperationException(
259 "Cannot add to an immutable array"); 259 "Cannot add to an immutable array");
260 } 260 }
261 261
262 void addLast(E element) { 262 void addLast(E element) {
263 add(element); 263 add(element);
264 } 264 }
(...skipping 18 matching lines...) Expand all
283 "Cannot remove in a non-extendable array"); 283 "Cannot remove in a non-extendable array");
284 } 284 }
285 285
286 E last() { 286 E last() {
287 return this[length - 1]; 287 return this[length - 1];
288 } 288 }
289 } 289 }
290 290
291 291
292 // Iterator for arrays with fixed size. 292 // Iterator for arrays with fixed size.
293 class FixedSizeArrayIterator<E> implements Iterator<E> { 293 class _FixedSizeArrayIterator<E> implements Iterator<E> {
294 FixedSizeArrayIterator(List array) 294 _FixedSizeArrayIterator(List array)
295 : _array = array, _length = array.length, _pos = 0 { 295 : _array = array, _length = array.length, _pos = 0 {
296 assert(array is ObjectArray || array is ImmutableArray); 296 assert(array is _ObjectArray || array is _ImmutableArray);
297 } 297 }
298 298
299 bool hasNext() { 299 bool hasNext() {
300 return _length > _pos; 300 return _length > _pos;
301 } 301 }
302 302
303 E next() { 303 E next() {
304 if (!hasNext()) { 304 if (!hasNext()) {
305 throw const NoMoreElementsException(); 305 throw const NoMoreElementsException();
306 } 306 }
307 return _array[_pos++]; 307 return _array[_pos++];
308 } 308 }
309 309
310 final List<E> _array; 310 final List<E> _array;
311 final int _length; // Cache array length for faster access. 311 final int _length; // Cache array length for faster access.
312 int _pos; 312 int _pos;
313 } 313 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698