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

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

Issue 11000025: Revert hiding of VM-only coreimpl list implementation types. While I (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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
« no previous file with comments | « runtime/bin/socket_impl.dart ('k') | runtime/lib/array_patch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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( 71 return Collections.map(this, new GrowableObjectArray.withCapacity(length), f );
72 this, new _GrowableObjectArray.withCapacity(length), f);
73 } 72 }
74 73
75 reduce(initialValue, combine(previousValue, E element)) { 74 reduce(initialValue, combine(previousValue, E element)) {
76 return Collections.reduce(this, initialValue, combine); 75 return Collections.reduce(this, initialValue, combine);
77 } 76 }
78 77
79 Collection<E> filter(bool f(E element)) { 78 Collection<E> filter(bool f(E element)) {
80 return Collections.filter(this, new _GrowableObjectArray<E>(), f); 79 return Collections.filter(this, new GrowableObjectArray<E>(), f);
81 } 80 }
82 81
83 bool every(bool f(E element)) { 82 bool every(bool f(E element)) {
84 return Collections.every(this, f); 83 return Collections.every(this, f);
85 } 84 }
86 85
87 bool some(bool f(E element)) { 86 bool some(bool f(E element)) {
88 return Collections.some(this, f); 87 return Collections.some(this, f);
89 } 88 }
90 89
91 bool isEmpty() { 90 bool isEmpty() {
92 return this.length === 0; 91 return this.length === 0;
93 } 92 }
94 93
95 void sort(int compare(E a, E b)) { 94 void sort(int compare(E a, E b)) {
96 DualPivotQuicksort.sort(this, compare); 95 DualPivotQuicksort.sort(this, compare);
97 } 96 }
98 97
99 int indexOf(E element, [int start = 0]) { 98 int indexOf(E element, [int start = 0]) {
100 return Arrays.indexOf(this, element, start, this.length); 99 return Arrays.indexOf(this, element, start, this.length);
101 } 100 }
102 101
103 int lastIndexOf(E element, [int start = null]) { 102 int lastIndexOf(E element, [int start = null]) {
104 if (start === null) start = length - 1; 103 if (start === null) start = length - 1;
105 return Arrays.lastIndexOf(this, element, start); 104 return Arrays.lastIndexOf(this, element, start);
106 } 105 }
107 106
108 Iterator<E> iterator() { 107 Iterator<E> iterator() {
109 return new _FixedSizeArrayIterator<E>(this); 108 return new FixedSizeArrayIterator<E>(this);
110 } 109 }
111 110
112 void add(E element) { 111 void add(E element) {
113 throw const UnsupportedOperationException( 112 throw const UnsupportedOperationException(
114 "Cannot add to a non-extendable array"); 113 "Cannot add to a non-extendable array");
115 } 114 }
116 115
117 void addLast(E element) { 116 void addLast(E element) {
118 add(element); 117 add(element);
119 } 118 }
(...skipping 17 matching lines...) Expand all
137 throw const UnsupportedOperationException( 136 throw const UnsupportedOperationException(
138 "Cannot remove in a non-extendable array"); 137 "Cannot remove in a non-extendable array");
139 } 138 }
140 139
141 E last() { 140 E last() {
142 return this[length - 1]; 141 return this[length - 1];
143 } 142 }
144 } 143 }
145 144
146 145
147 // 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
148 // permit any modification of array elements from Dart code. We use 147 // permit any modification of array elements from Dart code. We use
149 // this class for arrays constructed from Dart array literals. 148 // this class for arrays constructed from Dart array literals.
150 // TODO(hausner): We should consider the trade-offs between two 149 // TODO(hausner): We should consider the trade-offs between two
151 // classes (and inline cache misses) versus a field in the native 150 // classes (and inline cache misses) versus a field in the native
152 // implementation (checks when modifying). We should keep watching 151 // implementation (checks when modifying). We should keep watching
153 // the inline cache misses. 152 // the inline cache misses.
154 class _ImmutableArray<E> implements List<E> { 153 class ImmutableArray<E> implements List<E> {
155 154
156 factory _ImmutableArray._uninstantiable() { 155 factory ImmutableArray._uninstantiable() {
157 throw const UnsupportedOperationException( 156 throw const UnsupportedOperationException(
158 "ImmutableArray can only be allocated by the VM"); 157 "ImmutableArray can only be allocated by the VM");
159 } 158 }
160 159
161 E operator [](int index) native "ObjectArray_getIndexed"; 160 E operator [](int index) native "ObjectArray_getIndexed";
162 161
163 void operator []=(int index, E value) { 162 void operator []=(int index, E value) {
164 throw const UnsupportedOperationException( 163 throw const UnsupportedOperationException(
165 "Cannot modify an immutable array"); 164 "Cannot modify an immutable array");
166 } 165 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 202
204 /** 203 /**
205 * Collection interface. 204 * Collection interface.
206 */ 205 */
207 206
208 void forEach(f(E element)) { 207 void forEach(f(E element)) {
209 Collections.forEach(this, f); 208 Collections.forEach(this, f);
210 } 209 }
211 210
212 Collection map(f(E element)) { 211 Collection map(f(E element)) {
213 return Collections.map( 212 return Collections.map(this, new GrowableObjectArray.withCapacity(length), f );
214 this, new _GrowableObjectArray.withCapacity(length), f);
215 } 213 }
216 214
217 reduce(initialValue, combine(previousValue, E element)) { 215 reduce(initialValue, combine(previousValue, E element)) {
218 return Collections.reduce(this, initialValue, combine); 216 return Collections.reduce(this, initialValue, combine);
219 } 217 }
220 218
221 Collection<E> filter(bool f(E element)) { 219 Collection<E> filter(bool f(E element)) {
222 return Collections.filter(this, new _GrowableObjectArray<E>(), f); 220 return Collections.filter(this, new GrowableObjectArray<E>(), f);
223 } 221 }
224 222
225 bool every(bool f(E element)) { 223 bool every(bool f(E element)) {
226 return Collections.every(this, f); 224 return Collections.every(this, f);
227 } 225 }
228 226
229 bool some(bool f(E element)) { 227 bool some(bool f(E element)) {
230 return Collections.some(this, f); 228 return Collections.some(this, f);
231 } 229 }
232 230
(...skipping 13 matching lines...) Expand all
246 int indexOf(E element, [int start = 0]) { 244 int indexOf(E element, [int start = 0]) {
247 return Arrays.indexOf(this, element, start, this.length); 245 return Arrays.indexOf(this, element, start, this.length);
248 } 246 }
249 247
250 int lastIndexOf(E element, [int start = null]) { 248 int lastIndexOf(E element, [int start = null]) {
251 if (start === null) start = length - 1; 249 if (start === null) start = length - 1;
252 return Arrays.lastIndexOf(this, element, start); 250 return Arrays.lastIndexOf(this, element, start);
253 } 251 }
254 252
255 Iterator<E> iterator() { 253 Iterator<E> iterator() {
256 return new _FixedSizeArrayIterator<E>(this); 254 return new FixedSizeArrayIterator<E>(this);
257 } 255 }
258 256
259 void add(E element) { 257 void add(E element) {
260 throw const UnsupportedOperationException( 258 throw const UnsupportedOperationException(
261 "Cannot add to an immutable array"); 259 "Cannot add to an immutable array");
262 } 260 }
263 261
264 void addLast(E element) { 262 void addLast(E element) {
265 add(element); 263 add(element);
266 } 264 }
(...skipping 18 matching lines...) Expand all
285 "Cannot remove in a non-extendable array"); 283 "Cannot remove in a non-extendable array");
286 } 284 }
287 285
288 E last() { 286 E last() {
289 return this[length - 1]; 287 return this[length - 1];
290 } 288 }
291 } 289 }
292 290
293 291
294 // Iterator for arrays with fixed size. 292 // Iterator for arrays with fixed size.
295 class _FixedSizeArrayIterator<E> implements Iterator<E> { 293 class FixedSizeArrayIterator<E> implements Iterator<E> {
296 _FixedSizeArrayIterator(List array) 294 FixedSizeArrayIterator(List array)
297 : _array = array, _length = array.length, _pos = 0 { 295 : _array = array, _length = array.length, _pos = 0 {
298 assert(array is _ObjectArray || array is _ImmutableArray); 296 assert(array is ObjectArray || array is ImmutableArray);
299 } 297 }
300 298
301 bool hasNext() { 299 bool hasNext() {
302 return _length > _pos; 300 return _length > _pos;
303 } 301 }
304 302
305 E next() { 303 E next() {
306 if (!hasNext()) { 304 if (!hasNext()) {
307 throw const NoMoreElementsException(); 305 throw const NoMoreElementsException();
308 } 306 }
309 return _array[_pos++]; 307 return _array[_pos++];
310 } 308 }
311 309
312 final List<E> _array; 310 final List<E> _array;
313 final int _length; // Cache array length for faster access. 311 final int _length; // Cache array length for faster access.
314 int _pos; 312 int _pos;
315 } 313 }
OLDNEW
« no previous file with comments | « runtime/bin/socket_impl.dart ('k') | runtime/lib/array_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698