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