OLD | NEW |
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 // Note that the optimizing compiler depends on the algorithm which | 5 // Note that the optimizing compiler depends on the algorithm which |
6 // returns a _GrowableObjectArray if length is null, otherwise returns | 6 // returns a _GrowableObjectArray if length is null, otherwise returns |
7 // fixed size array. | 7 // fixed size array. |
8 patch class _ListImpl<E> { | 8 patch class _ListImpl<E> { |
9 /* patch */ factory List([int length = null]) { | 9 /* patch */ factory List([int length = 0]) { |
10 if (length == null) { | 10 if (length is! int || length < 0) { |
11 return new _GrowableObjectArray<E>(); | 11 throw new ArgumentError("Length must be a positive integer: $length."); |
12 } else { | |
13 return new _ObjectArray<E>(length); | |
14 } | 12 } |
| 13 _GrowableObjectArray<E> result = new _GrowableObjectArray<E>(); |
| 14 result.length = length; |
| 15 return result; |
| 16 } |
| 17 |
| 18 /* patch */ factory List.fixedLength(int length, {E fill: null}) { |
| 19 if (length is! int || length < 0) { |
| 20 throw new ArgumentError("Length must be a positive integer: $length."); |
| 21 } |
| 22 _ObjectArray<E> result = new _ObjectArray<E>(length); |
| 23 if (fill != null) { |
| 24 for (int i = 0; i < length; i++) { |
| 25 result[i] = fill; |
| 26 } |
| 27 } |
| 28 return result; |
| 29 } |
| 30 |
| 31 /* patch */ factory List.filled(int length, E fill) { |
| 32 if (length is! int || length < 0) { |
| 33 throw new ArgumentError("Length must be a positive integer: $length."); |
| 34 } |
| 35 _GrowableObjectArray<E> result = |
| 36 new _GrowableObjectArray<E>.withCapacity(length < 4 ? 4 : length); |
| 37 if (length != 0) { |
| 38 result.length = length; |
| 39 if (fill != null) { |
| 40 for (int i = 0; i < length; i++) { |
| 41 result[i] = fill; |
| 42 } |
| 43 } |
| 44 } |
| 45 return result; |
15 } | 46 } |
16 | 47 |
17 /* patch */ factory List.from(Iterable<E> other) { | 48 /* patch */ factory List.from(Iterable<E> other) { |
18 _GrowableObjectArray<E> list = new _GrowableObjectArray<E>(); | 49 _GrowableObjectArray<E> list = new _GrowableObjectArray<E>(); |
19 for (final e in other) { | 50 for (final e in other) { |
20 list.add(e); | 51 list.add(e); |
21 } | 52 } |
22 return list; | 53 return list; |
23 } | 54 } |
24 | 55 |
25 // Factory constructing a mutable List from a parser generated List literal. | 56 // Factory constructing a mutable List from a parser generated List literal. |
26 // [elements] contains elements that are already type checked. | 57 // [elements] contains elements that are already type checked. |
27 factory List._fromLiteral(List elements) { | 58 factory List._fromLiteral(List elements) { |
28 var list = new List<E>(); | 59 var list = new List<E>(); |
29 if (elements.length > 0) { | 60 if (elements.length > 0) { |
30 list._setData(elements); | 61 list._setData(elements); |
31 list.length = elements.length; | 62 list.length = elements.length; |
32 } | 63 } |
33 return list; | 64 return list; |
34 } | 65 } |
35 } | 66 } |
OLD | NEW |