| 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 // The _GrowableArrayMarker class is used to signal to the List() factory | 5 // The _GrowableArrayMarker class is used to signal to the List() factory |
| 6 // whether a parameter was passed. | 6 // whether a parameter was passed. |
| 7 class _GrowableArrayMarker implements int { | 7 class _GrowableArrayMarker implements int { |
| 8 const _GrowableArrayMarker(); | 8 const _GrowableArrayMarker(); |
| 9 } | 9 } |
| 10 | 10 |
| 11 const _GROWABLE_ARRAY_MARKER = const _GrowableArrayMarker(); | 11 const _GROWABLE_ARRAY_MARKER = const _GrowableArrayMarker(); |
| 12 | 12 |
| 13 @patch | 13 @patch class List<E> { |
| 14 class List<E> { | 14 @patch factory List([int length]) = List<E>._internal; |
| 15 @patch | |
| 16 factory List([int length]) = List<E>._internal; | |
| 17 | 15 |
| 18 @patch | 16 @patch factory List.filled(int length, E fill, {bool growable: false}) { |
| 19 factory List.filled(int length, E fill, {bool growable: false}) { | |
| 20 // All error handling on the length parameter is done at the implementation | 17 // All error handling on the length parameter is done at the implementation |
| 21 // of new _List. | 18 // of new _List. |
| 22 var result = growable ? new _GrowableList<E>(length) : new _List<E>(length); | 19 var result = growable ? new _GrowableList<E>(length) : new _List<E>(length); |
| 23 if (fill != null) { | 20 if (fill != null) { |
| 24 for (int i = 0; i < length; i++) { | 21 for (int i = 0; i < length; i++) { |
| 25 result[i] = fill; | 22 result[i] = fill; |
| 26 } | 23 } |
| 27 } | 24 } |
| 28 return result; | 25 return result; |
| 29 } | 26 } |
| 30 | 27 |
| 31 @patch | 28 @patch factory List.from(Iterable elements, { bool growable: true }) { |
| 32 factory List.from(Iterable elements, {bool growable: true}) { | |
| 33 if (elements is EfficientLengthIterable) { | 29 if (elements is EfficientLengthIterable) { |
| 34 int length = elements.length; | 30 int length = elements.length; |
| 35 var list = growable ? new _GrowableList<E>(length) : new _List<E>(length); | 31 var list = growable ? new _GrowableList<E>(length) : new _List<E>(length); |
| 36 if (length > 0) { | 32 if (length > 0) { // Avoid creating iterator unless necessary. |
| 37 // Avoid creating iterator unless necessary. | |
| 38 int i = 0; | 33 int i = 0; |
| 39 for (var element in elements) { | 34 for (var element in elements) { list[i++] = element; } |
| 40 list[i++] = element; | |
| 41 } | |
| 42 } | 35 } |
| 43 return list; | 36 return list; |
| 44 } | 37 } |
| 45 List<E> list = new _GrowableList<E>(0); | 38 List<E> list = new _GrowableList<E>(0); |
| 46 for (E e in elements) { | 39 for (E e in elements) { |
| 47 list.add(e); | 40 list.add(e); |
| 48 } | 41 } |
| 49 if (growable) return list; | 42 if (growable) return list; |
| 50 if (list.length == 0) { | 43 if (list.length == 0) { |
| 51 // Avoid getting an immutable list from makeListFixedLength. | 44 // Avoid getting an immutable list from makeListFixedLength. |
| 52 return new _List<E>(0); | 45 return new _List<E>(0); |
| 53 } | 46 } |
| 54 return makeListFixedLength(list); | 47 return makeListFixedLength(list); |
| 55 } | 48 } |
| 56 | 49 |
| 57 @patch | 50 @patch factory List.unmodifiable(Iterable elements) { |
| 58 factory List.unmodifiable(Iterable elements) { | |
| 59 List result = new List<E>.from(elements, growable: false); | 51 List result = new List<E>.from(elements, growable: false); |
| 60 return makeFixedListUnmodifiable(result); | 52 return makeFixedListUnmodifiable(result); |
| 61 } | 53 } |
| 62 | 54 |
| 63 // The List factory constructor redirects to this one so that we can change | 55 // The List factory constructor redirects to this one so that we can change |
| 64 // length's default value from the one in the SDK's implementation. | 56 // length's default value from the one in the SDK's implementation. |
| 65 factory List._internal([int length = _GROWABLE_ARRAY_MARKER]) { | 57 factory List._internal([int length = _GROWABLE_ARRAY_MARKER]) { |
| 66 if (identical(length, _GROWABLE_ARRAY_MARKER)) { | 58 if (identical(length, _GROWABLE_ARRAY_MARKER)) { |
| 67 return new _GrowableList<E>(0); | 59 return new _GrowableList<E>(0); |
| 68 } | 60 } |
| 69 // All error handling on the length parameter is done at the implementation | 61 // All error handling on the length parameter is done at the implementation |
| 70 // of new _List. | 62 // of new _List. |
| 71 return new _List<E>(length); | 63 return new _List<E>(length); |
| 72 } | 64 } |
| 73 | 65 |
| 74 // Factory constructing a mutable List from a parser generated List literal. | 66 // Factory constructing a mutable List from a parser generated List literal. |
| 75 // [elements] contains elements that are already type checked. | 67 // [elements] contains elements that are already type checked. |
| 76 factory List._fromLiteral(List elements) { | 68 factory List._fromLiteral(List elements) { |
| 77 if (elements.isEmpty) { | 69 if (elements.isEmpty) { |
| 78 return new _GrowableList<E>(0); | 70 return new _GrowableList<E>(0); |
| 79 } | 71 } |
| 80 var result = new _GrowableList<E>.withData(elements); | 72 var result = new _GrowableList<E>.withData(elements); |
| 81 result._setLength(elements.length); | 73 result._setLength(elements.length); |
| 82 return result; | 74 return result; |
| 83 } | 75 } |
| 84 } | 76 } |
| OLD | NEW |