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); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 } | 109 } |
110 | 110 |
111 bool isEmpty() { | 111 bool isEmpty() { |
112 return this.length === 0; | 112 return this.length === 0; |
113 } | 113 } |
114 | 114 |
115 void sort(int compare(T a, T b)) { | 115 void sort(int compare(T a, T b)) { |
116 DualPivotQuicksort.sort(this, compare); | 116 DualPivotQuicksort.sort(this, compare); |
117 } | 117 } |
118 | 118 |
119 int indexOf(T element, int startIndex) { | 119 int indexOf(T element, [int start = 0]) { |
120 return Arrays.indexOf(this, element, startIndex, this.length); | 120 return Arrays.indexOf(this, element, start, this.length); |
121 } | 121 } |
122 | 122 |
123 int lastIndexOf(T element, int startIndex) { | 123 int lastIndexOf(T element, [int start = null]) { |
124 return Arrays.lastIndexOf(this, element, startIndex); | 124 if (start === null) start = length - 1; |
| 125 return Arrays.lastIndexOf(this, element, start); |
125 } | 126 } |
126 | 127 |
127 Iterator<T> iterator() { | 128 Iterator<T> iterator() { |
128 return new FixedSizeArrayIterator<T>(this); | 129 return new FixedSizeArrayIterator<T>(this); |
129 } | 130 } |
130 | 131 |
131 void add(T element) { | 132 void add(T element) { |
132 throw const UnsupportedOperationException( | 133 throw const UnsupportedOperationException( |
133 "Cannot add to a non-extendable array"); | 134 "Cannot add to a non-extendable array"); |
134 } | 135 } |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 | 237 |
237 void sort(int compare(T a, T b)) { | 238 void sort(int compare(T a, T b)) { |
238 throw const UnsupportedOperationException( | 239 throw const UnsupportedOperationException( |
239 "Cannot modify an immutable array"); | 240 "Cannot modify an immutable array"); |
240 } | 241 } |
241 | 242 |
242 String toString() { | 243 String toString() { |
243 return "ImmutableArray"; | 244 return "ImmutableArray"; |
244 } | 245 } |
245 | 246 |
246 int indexOf(T element, int startIndex) { | 247 int indexOf(T element, [int start = 0]) { |
247 return Arrays.indexOf(this, element, startIndex, this.length); | 248 return Arrays.indexOf(this, element, start, this.length); |
248 } | 249 } |
249 | 250 |
250 int lastIndexOf(T element, int startIndex) { | 251 int lastIndexOf(T element, [int start = null]) { |
251 return Arrays.lastIndexOf(this, element, startIndex); | 252 if (start === null) start = length - 1; |
| 253 return Arrays.lastIndexOf(this, element, start); |
252 } | 254 } |
253 | 255 |
254 Iterator<T> iterator() { | 256 Iterator<T> iterator() { |
255 return new FixedSizeArrayIterator<T>(this); | 257 return new FixedSizeArrayIterator<T>(this); |
256 } | 258 } |
257 | 259 |
258 void add(T element) { | 260 void add(T element) { |
259 throw const UnsupportedOperationException( | 261 throw const UnsupportedOperationException( |
260 "Cannot add to an immutable array"); | 262 "Cannot add to an immutable array"); |
261 } | 263 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 if (!hasNext()) { | 307 if (!hasNext()) { |
306 throw const NoMoreElementsException(); | 308 throw const NoMoreElementsException(); |
307 } | 309 } |
308 return _array[_pos++]; | 310 return _array[_pos++]; |
309 } | 311 } |
310 | 312 |
311 final List<T> _array; | 313 final List<T> _array; |
312 final int _length; // Cache array length for faster access. | 314 final int _length; // Cache array length for faster access. |
313 int _pos; | 315 int _pos; |
314 } | 316 } |
OLD | NEW |