Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /** | 5 /** |
| 6 * Growable typed-data lists. | 6 * Growable typed-data lists. |
| 7 * | 7 * |
| 8 * These lists works just as a typed-data list, except that they are growable. | 8 * These lists works just as a typed-data list, except that they are growable. |
| 9 * They use an underlying buffer, and when that buffer becomes too small, it | 9 * They use an underlying buffer, and when that buffer becomes too small, it |
| 10 * is replaced by a new buffer. | 10 * is replaced by a new buffer. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 /// This adds values from [start] (inclusive) to [end] (exclusive) in | 93 /// This adds values from [start] (inclusive) to [end] (exclusive) in |
| 94 /// [values]. If [end] is omitted, it defaults to adding all elements of | 94 /// [values]. If [end] is omitted, it defaults to adding all elements of |
| 95 /// [values] after [start]. | 95 /// [values] after [start]. |
| 96 /// | 96 /// |
| 97 /// The [start] value must be non-negative. The [values] iterable must have at | 97 /// The [start] value must be non-negative. The [values] iterable must have at |
| 98 /// least [start] elements, and if [end] is specified, it must be greater than | 98 /// least [start] elements, and if [end] is specified, it must be greater than |
| 99 /// or equal to [start] and [values] must have at least [end] elements. | 99 /// or equal to [start] and [values] must have at least [end] elements. |
| 100 void insertAll(int index, Iterable<E> values, [int start = 0, int end]) { | 100 void insertAll(int index, Iterable<E> values, [int start = 0, int end]) { |
| 101 RangeError.checkValidIndex(index, this, "index", _length + 1); | 101 RangeError.checkValidIndex(index, this, "index", _length + 1); |
| 102 RangeError.checkNotNegative(start, "start"); | 102 RangeError.checkNotNegative(start, "start"); |
| 103 if (end != null && start > end) { | 103 if (end != null) { |
| 104 throw new RangeError.range(end, start, null, "end"); | 104 if (start > end) { |
| 105 throw new RangeError.range(end, start, null, "end"); | |
| 106 } | |
| 107 if (start == end) return; | |
|
nweiz
2015/10/28 21:43:30
This has the same issue as calling [values.start]:
Lasse Reichstein Nielsen
2015/10/29 09:24:42
I think that's acceptable - you are asking for zer
| |
| 105 } | 108 } |
| 106 | 109 |
| 110 | |
| 107 // If we're adding to the end of the list anyway, use [_addAll]. This lets | 111 // If we're adding to the end of the list anyway, use [_addAll]. This lets |
| 108 // us avoid converting [values] into a list even if [end] is null, since we | 112 // us avoid converting [values] into a list even if [end] is null, since we |
| 109 // can add values iteratively to the end of the list. We can't do so in the | 113 // can add values iteratively to the end of the list. We can't do so in the |
| 110 // center because copying the trailing elements every time is non-linear. | 114 // center because copying the trailing elements every time is non-linear. |
| 111 if (index == _length) { | 115 if (index == _length) { |
| 112 _addAll(values, start, end); | 116 _addAll(values, start, end); |
| 113 return; | 117 return; |
| 114 } | 118 } |
| 115 | 119 |
| 116 // If we don't know how much room to make for [values], convert it to a list | 120 if (end == null && values is List) { |
| 117 // so we can tell. | |
| 118 if (end == null) { | |
| 119 if (values is! List) values = values.toList(growable: false); | |
| 120 end = values.length; | 121 end = values.length; |
| 121 } | 122 } |
| 123 if (end != null) { | |
| 124 _insertKnownLength(index, values, start, end); | |
| 125 return; | |
| 126 } | |
| 122 | 127 |
| 123 _insertKnownLength(index, values, start, end); | 128 // Add elements at end, growing as appropriate, then put them back at |
| 129 // position [index] using flip-by-double-reverse. | |
| 130 if (end != null) values = values.take(end); | |
| 131 int writeIndex = _length; | |
| 132 int skipCount = start; | |
| 133 for (var value in values) { | |
| 134 if (skipCount > 0) { | |
| 135 skipCount--; | |
| 136 continue; | |
| 137 } | |
| 138 if (writeIndex == _buffer.length) { | |
| 139 _grow(); | |
| 140 } | |
| 141 _buffer[writeIndex++] = value; | |
| 142 } | |
| 143 if (skipCount > 0) { | |
| 144 throw new StateError("Too few elements"); | |
| 145 } | |
| 146 if (end != null && writeIndex < end) { | |
| 147 throw new RangeError.range(end, start, writeIndex, "end"); | |
|
floitsch
2015/10/28 18:20:51
Add comment, that throwing now leaves the array in
Lasse Reichstein Nielsen
2015/10/29 09:24:42
Done.
| |
| 148 } | |
| 149 // Swap [index.._length) and [_length..writeIndex) by double-reversing. | |
| 150 _reverse(_buffer, index, _length); | |
| 151 _reverse(_buffer, _length, writeIndex); | |
| 152 _reverse(_buffer, index, writeIndex); | |
| 153 _length = writeIndex; | |
| 154 return; | |
| 155 } | |
| 156 | |
| 157 // Reverses the range [start..end) of buffer. | |
| 158 static void _reverse(List<int> buffer, int start, int end) { | |
| 159 end--; // Point to last element, not after last element. | |
| 160 while (start < end) { | |
| 161 var first = buffer[start]; | |
| 162 var last = buffer[end]; | |
| 163 buffer[end] = first; | |
| 164 buffer[start] = last; | |
| 165 start++; | |
| 166 end--; | |
| 167 } | |
| 124 } | 168 } |
| 125 | 169 |
| 126 /// Does the same thing as [addAll]. | 170 /// Does the same thing as [addAll]. |
| 127 /// | 171 /// |
| 128 /// This allows [addAll] and [insertAll] to share implementation without a | 172 /// This allows [addAll] and [insertAll] to share implementation without a |
| 129 /// subclass unexpectedly overriding both when it intended to only override | 173 /// subclass unexpectedly overriding both when it intended to only override |
| 130 /// [addAll]. | 174 /// [addAll]. |
| 131 void _addAll(Iterable<E> values, [int start = 0, int end]) { | 175 void _addAll(Iterable<E> values, [int start = 0, int end]) { |
| 132 if (values is List) end ??= values.length; | 176 if (values is List) end ??= values.length; |
| 133 | 177 |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 336 Int32x4 get _defaultValue => _zero; | 380 Int32x4 get _defaultValue => _zero; |
| 337 Int32x4List _createBuffer(int size) => new Int32x4List(size); | 381 Int32x4List _createBuffer(int size) => new Int32x4List(size); |
| 338 } | 382 } |
| 339 | 383 |
| 340 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { | 384 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { |
| 341 Float32x4Buffer([int initialLength = 0]) | 385 Float32x4Buffer([int initialLength = 0]) |
| 342 : super(new Float32x4List(initialLength)); | 386 : super(new Float32x4List(initialLength)); |
| 343 Float32x4 get _defaultValue => new Float32x4.zero(); | 387 Float32x4 get _defaultValue => new Float32x4.zero(); |
| 344 Float32x4List _createBuffer(int size) => new Float32x4List(size); | 388 Float32x4List _createBuffer(int size) => new Float32x4List(size); |
| 345 } | 389 } |
| OLD | NEW |