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

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

Issue 10905010: Some code cleanups in arrays, fix rangeCheck (issue 2899). (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 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
(...skipping 30 matching lines...) Expand all
41 } 41 }
42 42
43 void insertRange(int start, int length, [E initialValue = null]) { 43 void insertRange(int start, int length, [E initialValue = null]) {
44 throw const UnsupportedOperationException( 44 throw const UnsupportedOperationException(
45 "Cannot insert range in a non-extendable array"); 45 "Cannot insert range in a non-extendable array");
46 } 46 }
47 47
48 List<E> getRange(int start, int length) { 48 List<E> getRange(int start, int length) {
49 if (length == 0) return []; 49 if (length == 0) return [];
50 Arrays.rangeCheck(this, start, length); 50 Arrays.rangeCheck(this, start, length);
51 List list = new List<E>(); 51 List list = new GrowableObjectArray<E>.withCapacity(length);
52 list.length = length; 52 list.length = length;
53 Arrays.copy(this, start, list, 0, length); 53 Arrays.copy(this, start, list, 0, length);
54 return list; 54 return list;
55 } 55 }
56 56
57 /** 57 /**
58 * Collection interface. 58 * Collection interface.
59 */ 59 */
60 60
61 void forEach(f(E element)) { 61 void forEach(f(E element)) {
62 Collections.forEach(this, f); 62 Collections.forEach(this, f);
63 } 63 }
64 64
65 Collection map(f(E element)) { 65 Collection map(f(E element)) {
66 return Collections.map(this, new GrowableObjectArray.withCapacity(length), f ); 66 return Collections.map(this, new GrowableObjectArray.withCapacity(length), f );
67 } 67 }
68 68
69 Dynamic reduce(Dynamic initialValue, 69 reduce(initialValue, combine(previousValue, E element)) {
70 Dynamic combine(Dynamic previousValue, E element)) {
71 return Collections.reduce(this, initialValue, combine); 70 return Collections.reduce(this, initialValue, combine);
72 } 71 }
73 72
74 Collection<E> filter(bool f(E element)) { 73 Collection<E> filter(bool f(E element)) {
75 return Collections.filter(this, new GrowableObjectArray<E>(), f); 74 return Collections.filter(this, new GrowableObjectArray<E>(), f);
76 } 75 }
77 76
78 bool every(bool f(E element)) { 77 bool every(bool f(E element)) {
79 return Collections.every(this, f); 78 return Collections.every(this, f);
80 } 79 }
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 */ 195 */
197 196
198 void forEach(f(E element)) { 197 void forEach(f(E element)) {
199 Collections.forEach(this, f); 198 Collections.forEach(this, f);
200 } 199 }
201 200
202 Collection map(f(E element)) { 201 Collection map(f(E element)) {
203 return Collections.map(this, new GrowableObjectArray.withCapacity(length), f ); 202 return Collections.map(this, new GrowableObjectArray.withCapacity(length), f );
204 } 203 }
205 204
206 Dynamic reduce(Dynamic initialValue, 205 reduce(initialValue, combine(previousValue, E element)) {
207 Dynamic combine(Dynamic previousValue, E element)) {
208 return Collections.reduce(this, initialValue, combine); 206 return Collections.reduce(this, initialValue, combine);
209 } 207 }
210 208
211 Collection<E> filter(bool f(E element)) { 209 Collection<E> filter(bool f(E element)) {
212 return Collections.filter(this, new GrowableObjectArray<E>(), f); 210 return Collections.filter(this, new GrowableObjectArray<E>(), f);
213 } 211 }
214 212
215 bool every(bool f(E element)) { 213 bool every(bool f(E element)) {
216 return Collections.every(this, f); 214 return Collections.every(this, f);
217 } 215 }
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 if (!hasNext()) { 294 if (!hasNext()) {
297 throw const NoMoreElementsException(); 295 throw const NoMoreElementsException();
298 } 296 }
299 return _array[_pos++]; 297 return _array[_pos++];
300 } 298 }
301 299
302 final List<E> _array; 300 final List<E> _array;
303 final int _length; // Cache array length for faster access. 301 final int _length; // Cache array length for faster access.
304 int _pos; 302 int _pos;
305 } 303 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698