Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 ListFactory<T> { | 5 class ListFactory<T> { |
| 6 | 6 |
| 7 factory List.from(Iterable<T> other) { | 7 factory List.from(Iterable<T> other) { |
| 8 GrowableObjectArray<T> list = new GrowableObjectArray<T>(); | 8 GrowableObjectArray<T> list = new GrowableObjectArray<T>(); |
| 9 for (final e in other) { | 9 for (final e in other) { |
| 10 list.add(e); | 10 list.add(e); |
| 11 } | 11 } |
| 12 return list; | 12 return list; |
| 13 } | 13 } |
| 14 | 14 |
| 15 factory List.fromList(List<T> other, int startIndex, int endIndex) { | 15 factory List.fromList(List<T> other, int startIndex, int endIndex) { |
|
Ivan Posva
2011/11/02 23:44:07
Since you are cleaning up, please do not forget th
ngeoffray
2011/11/03 07:54:04
Good catch! Follow up CL: http://codereview.chromi
| |
| 16 List list = new List<T>(); | 16 List list = new List<T>(); |
| 17 if (endIndex > other.length) endIndex = other.length; | 17 if (endIndex > other.length) endIndex = other.length; |
| 18 if (startIndex < 0) startIndex = 0; | 18 if (startIndex < 0) startIndex = 0; |
| 19 int count = endIndex - startIndex; | 19 int count = endIndex - startIndex; |
| 20 if (count > 0) { | 20 if (count > 0) { |
| 21 list.length = count; | 21 list.length = count; |
| 22 Arrays.copy(other, startIndex, list, 0, count); | 22 Arrays.copy(other, startIndex, list, 0, count); |
| 23 } | 23 } |
| 24 return list; | 24 return list; |
| 25 } | 25 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 } | 75 } |
| 76 | 76 |
| 77 void insertRange(int start, int length, [T initialValue = null]) { | 77 void insertRange(int start, int length, [T initialValue = null]) { |
| 78 throw const UnsupportedOperationException( | 78 throw const UnsupportedOperationException( |
| 79 "Cannot insert range in a non-extendable array"); | 79 "Cannot insert range in a non-extendable array"); |
| 80 } | 80 } |
| 81 | 81 |
| 82 List<T> getRange(int start, int length) { | 82 List<T> getRange(int start, int length) { |
| 83 if (length == 0) return []; | 83 if (length == 0) return []; |
| 84 Arrays.rangeCheck(this, start, length); | 84 Arrays.rangeCheck(this, start, length); |
| 85 return new List<T>.fromList(this, start, start + length); | 85 List list = new List<T>(); |
| 86 list.length = length; | |
| 87 Arrays.copy(this, start, list, 0, length); | |
| 88 return list; | |
| 86 } | 89 } |
| 87 | 90 |
| 88 /** | 91 /** |
| 89 * Collection interface. | 92 * Collection interface. |
| 90 */ | 93 */ |
| 91 | 94 |
| 92 void forEach(f(T element)) { | 95 void forEach(f(T element)) { |
| 93 Collections.forEach(this, f); | 96 Collections.forEach(this, f); |
| 94 } | 97 } |
| 95 | 98 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 } | 197 } |
| 195 | 198 |
| 196 void insertRange(int start, int length, [T initialValue = null]) { | 199 void insertRange(int start, int length, [T initialValue = null]) { |
| 197 throw const UnsupportedOperationException( | 200 throw const UnsupportedOperationException( |
| 198 "Cannot insert range in an immutable array"); | 201 "Cannot insert range in an immutable array"); |
| 199 } | 202 } |
| 200 | 203 |
| 201 List<T> getRange(int start, int length) { | 204 List<T> getRange(int start, int length) { |
| 202 if (length == 0) return []; | 205 if (length == 0) return []; |
| 203 Arrays.rangeCheck(this, start, length); | 206 Arrays.rangeCheck(this, start, length); |
| 204 return new List<T>.fromList(this, start, start + length); | 207 List list = new List<T>(); |
| 208 list.length = length; | |
| 209 Arrays.copy(this, start, list, 0, length); | |
| 210 return list; | |
| 205 } | 211 } |
| 206 | 212 |
| 207 /** | 213 /** |
| 208 * Collection interface. | 214 * Collection interface. |
| 209 */ | 215 */ |
| 210 | 216 |
| 211 void forEach(f(T element)) { | 217 void forEach(f(T element)) { |
| 212 Collections.forEach(this, f); | 218 Collections.forEach(this, f); |
| 213 } | 219 } |
| 214 | 220 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 if (!hasNext()) { | 305 if (!hasNext()) { |
| 300 throw const NoMoreElementsException(); | 306 throw const NoMoreElementsException(); |
| 301 } | 307 } |
| 302 return _array[_pos++]; | 308 return _array[_pos++]; |
| 303 } | 309 } |
| 304 | 310 |
| 305 final List<T> _array; | 311 final List<T> _array; |
| 306 final int _length; // Cache array length for faster access. | 312 final int _length; // Cache array length for faster access. |
| 307 int _pos; | 313 int _pos; |
| 308 } | 314 } |
| OLD | NEW |