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 List<E> { | 8 patch class List<E> { |
9 /* patch */ factory List([int length = 0]) { | 9 /* patch */ factory List([int length]) { |
| 10 if (!?length) return new _GrowableObjectArray<E>(0); |
10 if ((length is! int) || (length < 0)) { | 11 if ((length is! int) || (length < 0)) { |
11 _throwArgumentError(length); | 12 _throwArgumentError(length); |
12 } | 13 } |
13 _GrowableObjectArray<E> result = new _GrowableObjectArray<E>(length); | 14 _ObjectArray<E> result = new _ObjectArray<E>(length); |
14 return result; | 15 return result; |
15 } | 16 } |
16 | 17 |
17 /* patch */ factory List.fixedLength(int length, {E fill: null}) { | 18 /* patch */ factory List.filled(int length, E fill) { |
18 if ((length is! int) || (length < 0)) { | 19 if ((length is! int) || (length < 0)) { |
19 _throwArgumentError(length); | 20 _throwArgumentError(length); |
20 } | 21 } |
21 _ObjectArray<E> result = new _ObjectArray<E>(length); | 22 _ObjectArray<E> result = new _ObjectArray<E>(length); |
22 if (fill != null) { | 23 if (fill != null) { |
23 for (int i = 0; i < length; i++) { | 24 for (int i = 0; i < length; i++) { |
24 result[i] = fill; | 25 result[i] = fill; |
25 } | 26 } |
26 } | 27 } |
27 return result; | 28 return result; |
28 } | 29 } |
29 | 30 |
30 // Factory constructing a mutable List from a parser generated List literal. | 31 // Factory constructing a mutable List from a parser generated List literal. |
31 // [elements] contains elements that are already type checked. | 32 // [elements] contains elements that are already type checked. |
32 factory List._fromLiteral(List elements) { | 33 factory List._fromLiteral(List elements) { |
33 if (elements.isEmpty) { | 34 if (elements.isEmpty) { |
34 return new _GrowableObjectArray<E>(0); | 35 return new _GrowableObjectArray<E>(0); |
35 } | 36 } |
36 var result = new _GrowableObjectArray<E>.withData(elements); | 37 var result = new _GrowableObjectArray<E>.withData(elements); |
37 result._setLength(elements.length); | 38 result._setLength(elements.length); |
38 return result; | 39 return result; |
39 } | 40 } |
40 | 41 |
41 static void _throwArgumentError(int length) { | 42 static void _throwArgumentError(int length) { |
42 throw new ArgumentError("Length must be a positive integer: $length."); | 43 throw new ArgumentError("Length must be a positive integer: $length."); |
43 } | 44 } |
44 } | 45 } |
OLD | NEW |