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 { | 5 class ListFactory { |
6 factory List<E>.from(Iterable<E> other) { | 6 factory List<E>.from(Iterable<E> other) { |
7 List<E> list = new List<E>(); | 7 List<E> list = new List<E>(); |
8 for (final e in other) { | 8 for (final e in other) { |
9 list.add(e); | 9 list.add(e); |
10 } | 10 } |
11 return list; | 11 return list; |
12 } | 12 } |
13 | 13 |
14 factory List<E>.fromList(List<E> other, int startIndex, int endIndex) { | |
15 List list = new List<E>(); | |
16 if (endIndex > other.length) endIndex = other.length; | |
17 if (startIndex < 0) startIndex = 0; | |
18 int count = endIndex - startIndex; | |
19 if (count > 0) { | |
20 list.length = count; | |
21 Arrays.copy(other, startIndex, list, 0, count); | |
22 } | |
23 return list; | |
24 } | |
25 | |
26 factory List<E>([int length = null]) { | 14 factory List<E>([int length = null]) { |
27 bool isFixed = true; | 15 bool isFixed = true; |
28 if (length === null) { | 16 if (length === null) { |
29 length = 0; | 17 length = 0; |
30 isFixed = false; | 18 isFixed = false; |
31 } else if (length < 0) { | 19 } else if (length < 0) { |
32 throw new IllegalArgumentException("negative length $length"); | 20 throw new IllegalArgumentException("negative length $length"); |
33 } | 21 } |
34 // TODO(floitsch): make list creation more efficient. Currently we allocate | 22 // TODO(floitsch): make list creation more efficient. Currently we allocate |
35 // a new TypeToken at every allocation. Either we can optimize them away, | 23 // a new TypeToken at every allocation. Either we can optimize them away, |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 } | 134 } |
147 if (start < 0 || start > this.length) { | 135 if (start < 0 || start > this.length) { |
148 throw new IndexOutOfRangeException(start); | 136 throw new IndexOutOfRangeException(start); |
149 } | 137 } |
150 _insertRange(start, length, initialValue); | 138 _insertRange(start, length, initialValue); |
151 } | 139 } |
152 | 140 |
153 List<T> getRange(int start, int length) { | 141 List<T> getRange(int start, int length) { |
154 if (length == 0) return []; | 142 if (length == 0) return []; |
155 Arrays.rangeCheck(this, start, length); | 143 Arrays.rangeCheck(this, start, length); |
156 return new List<T>.fromList(this, start, start + length); | 144 List list = new List<T>(); |
| 145 list.length = length; |
| 146 Arrays.copy(this, start, list, 0, length); |
| 147 return list; |
157 } | 148 } |
158 | 149 |
159 int indexOf(T element, int startIndex) { | 150 int indexOf(T element, int startIndex) { |
160 return Arrays.indexOf(this, element, startIndex, this.length); | 151 return Arrays.indexOf(this, element, startIndex, this.length); |
161 } | 152 } |
162 | 153 |
163 int lastIndexOf(T element, int startIndex) { | 154 int lastIndexOf(T element, int startIndex) { |
164 return Arrays.lastIndexOf(this, element, startIndex); | 155 return Arrays.lastIndexOf(this, element, startIndex); |
165 } | 156 } |
166 | 157 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 } | 258 } |
268 | 259 |
269 static List _newList(int len) native { | 260 static List _newList(int len) native { |
270 return new List(len); | 261 return new List(len); |
271 } | 262 } |
272 | 263 |
273 static void _throwIndexOutOfRangeException(int index) native { | 264 static void _throwIndexOutOfRangeException(int index) native { |
274 throw new IndexOutOfRangeException(index); | 265 throw new IndexOutOfRangeException(index); |
275 } | 266 } |
276 } | 267 } |
OLD | NEW |