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 /// Growable typed-data lists. | 5 /// Growable typed-data lists. |
| 6 /// | 6 /// |
| 7 /// These lists works just as a typed-data list, except that they are growable. | 7 /// These lists works just as a typed-data list, except that they are growable. |
| 8 /// They use an underlying buffer, and when that buffer becomes too small, it | 8 /// They use an underlying buffer, and when that buffer becomes too small, it |
| 9 /// is replaced by a new buffer. | 9 /// is replaced by a new buffer. |
| 10 /// | 10 /// |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 if (end == null && values is List) { | 118 if (end == null && values is List) { |
| 119 end = values.length; | 119 end = values.length; |
| 120 } | 120 } |
| 121 if (end != null) { | 121 if (end != null) { |
| 122 _insertKnownLength(index, values, start, end); | 122 _insertKnownLength(index, values, start, end); |
| 123 return; | 123 return; |
| 124 } | 124 } |
| 125 | 125 |
| 126 // Add elements at end, growing as appropriate, then put them back at | 126 // Add elements at end, growing as appropriate, then put them back at |
| 127 // position [index] using flip-by-double-reverse. | 127 // position [index] using flip-by-double-reverse. |
| 128 if (end != null) values = values.take(end); | 128 var writeIndex = _length; |
| 129 int writeIndex = _length; | 129 var skipCount = start; |
| 130 int skipCount = start; | |
| 131 for (var value in values) { | 130 for (var value in values) { |
| 132 if (skipCount > 0) { | 131 if (skipCount > 0) { |
| 133 skipCount--; | 132 skipCount--; |
| 134 continue; | 133 continue; |
| 135 } | 134 } |
| 136 if (writeIndex == _buffer.length) { | 135 if (writeIndex == _buffer.length) { |
| 137 _grow(); | 136 _grow(writeIndex); |
| 138 } | 137 } |
| 139 _buffer[writeIndex++] = value; | 138 _buffer[writeIndex++] = value; |
| 140 } | 139 } |
| 140 | |
| 141 if (skipCount > 0) { | 141 if (skipCount > 0) { |
| 142 throw new StateError("Too few elements"); | 142 throw new StateError("Too few elements"); |
| 143 } | 143 } |
| 144 if (end != null && writeIndex < end) { | 144 if (end != null && writeIndex < end) { |
| 145 throw new RangeError.range(end, start, writeIndex, "end"); | 145 throw new RangeError.range(end, start, writeIndex, "end"); |
| 146 } | 146 } |
| 147 | |
| 147 // Swap [index.._length) and [_length..writeIndex) by double-reversing. | 148 // Swap [index.._length) and [_length..writeIndex) by double-reversing. |
| 148 _reverse(_buffer, index, _length); | 149 _reverse(_buffer, index, _length); |
| 149 _reverse(_buffer, _length, writeIndex); | 150 _reverse(_buffer, _length, writeIndex); |
| 150 _reverse(_buffer, index, writeIndex); | 151 _reverse(_buffer, index, writeIndex); |
| 151 _length = writeIndex; | 152 _length = writeIndex; |
| 152 return; | 153 return; |
| 153 } | 154 } |
| 154 | 155 |
| 155 // Reverses the range [start..end) of buffer. | 156 // Reverses the range [start..end) of buffer. |
| 156 static void _reverse(List<int> buffer, int start, int end) { | 157 static void _reverse(List<int> buffer, int start, int end) { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 248 List<E> _createBiggerBuffer(int requiredCapacity) { | 249 List<E> _createBiggerBuffer(int requiredCapacity) { |
| 249 int newLength = _buffer.length * 2; | 250 int newLength = _buffer.length * 2; |
| 250 if (requiredCapacity != null && newLength < requiredCapacity) { | 251 if (requiredCapacity != null && newLength < requiredCapacity) { |
| 251 newLength = requiredCapacity; | 252 newLength = requiredCapacity; |
| 252 } else if (newLength < INITIAL_LENGTH) { | 253 } else if (newLength < INITIAL_LENGTH) { |
| 253 newLength = INITIAL_LENGTH; | 254 newLength = INITIAL_LENGTH; |
| 254 } | 255 } |
| 255 return _createBuffer(newLength); | 256 return _createBuffer(newLength); |
| 256 } | 257 } |
| 257 | 258 |
| 258 void _grow() { | 259 /// Grows the buffer. |
| 259 _buffer = _createBiggerBuffer(null)..setRange(0, _length, _buffer); | 260 /// |
| 261 /// This copies the first [length] elements into the new buffer. It defaults | |
| 262 /// to copying [_length] elements. | |
| 263 void _grow([int length]) { | |
|
Lasse Reichstein Nielsen
2016/02/24 11:06:42
I would generally not make internal functions have
nweiz
2016/02/24 18:45:56
Done.
| |
| 264 _buffer = _createBiggerBuffer(null) | |
| 265 ..setRange(0, length ?? _length, _buffer); | |
| 260 } | 266 } |
| 261 | 267 |
| 262 void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { | 268 void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { |
| 263 if (end > _length) throw new RangeError.range(end, 0, _length); | 269 if (end > _length) throw new RangeError.range(end, 0, _length); |
| 264 _setRange(start, end, source, skipCount); | 270 _setRange(start, end, source, skipCount); |
| 265 } | 271 } |
| 266 | 272 |
| 267 /// Like [setRange], but with no bounds checking. | 273 /// Like [setRange], but with no bounds checking. |
| 268 void _setRange(int start, int end, Iterable<E> source, int skipCount) { | 274 void _setRange(int start, int end, Iterable<E> source, int skipCount) { |
| 269 if (source is _TypedDataBuffer<E>) { | 275 if (source is _TypedDataBuffer<E>) { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 374 Int32x4 get _defaultValue => _zero; | 380 Int32x4 get _defaultValue => _zero; |
| 375 Int32x4List _createBuffer(int size) => new Int32x4List(size); | 381 Int32x4List _createBuffer(int size) => new Int32x4List(size); |
| 376 } | 382 } |
| 377 | 383 |
| 378 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { | 384 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { |
| 379 Float32x4Buffer([int initialLength = 0]) | 385 Float32x4Buffer([int initialLength = 0]) |
| 380 : super(new Float32x4List(initialLength)); | 386 : super(new Float32x4List(initialLength)); |
| 381 Float32x4 get _defaultValue => new Float32x4.zero(); | 387 Float32x4 get _defaultValue => new Float32x4.zero(); |
| 382 Float32x4List _createBuffer(int size) => new Float32x4List(size); | 388 Float32x4List _createBuffer(int size) => new Float32x4List(size); |
| 383 } | 389 } |
| OLD | NEW |