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