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 /// |
| 11 /// That means that using the [TypedDataView.buffer] getter is not guaranteed | 11 /// That means that using the [TypedDataView.buffer] getter is not guaranteed |
| 12 /// to return the same result each time it is used, and that the buffer may | 12 /// to return the same result each time it is used, and that the buffer may |
| 13 /// be larger than what the list is using. | 13 /// be larger than what the list is using. |
| 14 library dart.pkg.typed_data.typed_buffers; | 14 library dart.pkg.typed_data.typed_buffers; |
| 15 | 15 |
| 16 import "dart:collection" show ListBase; | 16 import "dart:collection" show ListBase; |
| 17 import "dart:typed_data"; | 17 import "dart:typed_data"; |
| 18 | 18 |
| 19 abstract class _TypedDataBuffer<E> extends ListBase<E> { | 19 abstract class _TypedDataBuffer<E> extends ListBase<E> { |
| 20 static const int INITIAL_LENGTH = 8; | 20 static const int INITIAL_LENGTH = 8; |
| 21 | 21 |
| 22 /// This is a Uint8List for Uint8Buffer. It's both a List<E> and a TypedData, | 22 /// This is a Uint8List for Uint8Buffer. |
|
floitsch
2016/05/09 13:20:59
I don't understand this comment.
Also: no need to
nweiz
2016/05/09 18:28:34
Done.
| |
| 23 /// which we don't have a type for here. | 23 /// |
| 24 var _buffer; | 24 /// This is always both a List<E> and a TypedData, which we don't have a type for here. |
|
floitsch
2016/05/09 13:20:59
Long line.
nweiz
2016/05/09 18:28:34
Done.
| |
| 25 List<E> _buffer; | |
| 26 | |
| 27 /// Returns a view of [_buffer] as a [TypedData]. | |
| 28 TypedData get _typedBuffer => _buffer as TypedData; | |
| 29 | |
| 25 /// The length of the list being built. | 30 /// The length of the list being built. |
| 26 int _length; | 31 int _length; |
| 27 | 32 |
| 28 _TypedDataBuffer(List<E> buffer) | 33 _TypedDataBuffer(List<E> buffer) |
| 29 : this._buffer = buffer, this._length = buffer.length; | 34 : this._buffer = buffer, |
| 35 this._length = buffer.length; | |
| 30 | 36 |
| 31 int get length => _length; | 37 int get length => _length; |
| 32 E operator[](int index) { | 38 E operator[](int index) { |
| 33 if (index >= length) throw new RangeError.index(index, this); | 39 if (index >= length) throw new RangeError.index(index, this); |
| 34 return _buffer[index]; | 40 return _buffer[index]; |
| 35 } | 41 } |
| 36 | 42 |
| 37 void operator[]=(int index, E value) { | 43 void operator[]=(int index, E value) { |
| 38 if (index >= length) throw new RangeError.index(index, this); | 44 if (index >= length) throw new RangeError.index(index, this); |
| 39 _buffer[index] = value; | 45 _buffer[index] = value; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 | 153 |
| 148 // Swap [index.._length) and [_length..writeIndex) by double-reversing. | 154 // Swap [index.._length) and [_length..writeIndex) by double-reversing. |
| 149 _reverse(_buffer, index, _length); | 155 _reverse(_buffer, index, _length); |
| 150 _reverse(_buffer, _length, writeIndex); | 156 _reverse(_buffer, _length, writeIndex); |
| 151 _reverse(_buffer, index, writeIndex); | 157 _reverse(_buffer, index, writeIndex); |
| 152 _length = writeIndex; | 158 _length = writeIndex; |
| 153 return; | 159 return; |
| 154 } | 160 } |
| 155 | 161 |
| 156 // Reverses the range [start..end) of buffer. | 162 // Reverses the range [start..end) of buffer. |
| 157 static void _reverse(List<int> buffer, int start, int end) { | 163 static void _reverse(List buffer, int start, int end) { |
| 158 end--; // Point to last element, not after last element. | 164 end--; // Point to last element, not after last element. |
| 159 while (start < end) { | 165 while (start < end) { |
| 160 var first = buffer[start]; | 166 var first = buffer[start]; |
| 161 var last = buffer[end]; | 167 var last = buffer[end]; |
| 162 buffer[end] = first; | 168 buffer[end] = first; |
| 163 buffer[start] = last; | 169 buffer[start] = last; |
| 164 start++; | 170 start++; |
| 165 end--; | 171 end--; |
| 166 } | 172 } |
| 167 } | 173 } |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 void _setRange(int start, int end, Iterable<E> source, int skipCount) { | 278 void _setRange(int start, int end, Iterable<E> source, int skipCount) { |
| 273 if (source is _TypedDataBuffer<E>) { | 279 if (source is _TypedDataBuffer<E>) { |
| 274 _buffer.setRange(start, end, source._buffer, skipCount); | 280 _buffer.setRange(start, end, source._buffer, skipCount); |
| 275 } else { | 281 } else { |
| 276 _buffer.setRange(start, end, source, skipCount); | 282 _buffer.setRange(start, end, source, skipCount); |
| 277 } | 283 } |
| 278 } | 284 } |
| 279 | 285 |
| 280 // TypedData. | 286 // TypedData. |
| 281 | 287 |
| 282 int get elementSizeInBytes => _buffer.elementSizeInBytes; | 288 int get elementSizeInBytes => _typedBuffer.elementSizeInBytes; |
| 283 | 289 |
| 284 int get lengthInBytes => _length * _buffer.elementSizeInBytes; | 290 int get lengthInBytes => _length * _typedBuffer.elementSizeInBytes; |
| 285 | 291 |
| 286 int get offsetInBytes => _buffer.offsetInBytes; | 292 int get offsetInBytes => _typedBuffer.offsetInBytes; |
| 287 | 293 |
| 288 /// Returns the underlying [ByteBuffer]. | 294 /// Returns the underlying [ByteBuffer]. |
| 289 /// | 295 /// |
| 290 /// The returned buffer may be replaced by operations that change the [length] | 296 /// The returned buffer may be replaced by operations that change the [length] |
| 291 /// of this list. | 297 /// of this list. |
| 292 /// | 298 /// |
| 293 /// The buffer may be larger than [lengthInBytes] bytes, but never smaller. | 299 /// The buffer may be larger than [lengthInBytes] bytes, but never smaller. |
| 294 ByteBuffer get buffer => _buffer.buffer; | 300 ByteBuffer get buffer => _typedBuffer.buffer; |
| 295 | 301 |
| 296 // Specialization for the specific type. | 302 // Specialization for the specific type. |
| 297 | 303 |
| 298 // Return zero for integers, 0.0 for floats, etc. | 304 // Return zero for integers, 0.0 for floats, etc. |
| 299 // Used to fill buffer when changing length. | 305 // Used to fill buffer when changing length. |
| 300 E get _defaultValue; | 306 E get _defaultValue; |
| 301 | 307 |
| 302 // Create a new typed list to use as buffer. | 308 // Create a new typed list to use as buffer. |
| 303 List<E> _createBuffer(int size); | 309 List<E> _createBuffer(int size); |
| 304 } | 310 } |
| 305 | 311 |
| 306 abstract class _IntBuffer extends _TypedDataBuffer<int> { | 312 abstract class _IntBuffer extends _TypedDataBuffer<int> { |
| 307 _IntBuffer(buffer): super(buffer); | 313 _IntBuffer(List<int> buffer): super(buffer); |
| 314 | |
| 308 int get _defaultValue => 0; | 315 int get _defaultValue => 0; |
| 309 } | 316 } |
| 310 | 317 |
| 311 abstract class _FloatBuffer extends _TypedDataBuffer<double> { | 318 abstract class _FloatBuffer extends _TypedDataBuffer<double> { |
| 312 _FloatBuffer(buffer): super(buffer); | 319 _FloatBuffer(List<double> buffer): super(buffer); |
| 320 | |
| 313 double get _defaultValue => 0.0; | 321 double get _defaultValue => 0.0; |
| 314 } | 322 } |
| 315 | 323 |
| 316 class Uint8Buffer extends _IntBuffer { | 324 class Uint8Buffer extends _IntBuffer { |
| 317 Uint8Buffer([int initialLength = 0]) : super(new Uint8List(initialLength)); | 325 Uint8Buffer([int initialLength = 0]) : super(new Uint8List(initialLength)); |
| 318 Uint8List _createBuffer(int size) => new Uint8List(size); | 326 Uint8List _createBuffer(int size) => new Uint8List(size); |
| 319 } | 327 } |
| 320 | 328 |
| 321 class Int8Buffer extends _IntBuffer { | 329 class Int8Buffer extends _IntBuffer { |
| 322 Int8Buffer([int initialLength = 0]) : super(new Int8List(initialLength)); | 330 Int8Buffer([int initialLength = 0]) : super(new Int8List(initialLength)); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 378 Int32x4 get _defaultValue => _zero; | 386 Int32x4 get _defaultValue => _zero; |
| 379 Int32x4List _createBuffer(int size) => new Int32x4List(size); | 387 Int32x4List _createBuffer(int size) => new Int32x4List(size); |
| 380 } | 388 } |
| 381 | 389 |
| 382 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { | 390 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { |
| 383 Float32x4Buffer([int initialLength = 0]) | 391 Float32x4Buffer([int initialLength = 0]) |
| 384 : super(new Float32x4List(initialLength)); | 392 : super(new Float32x4List(initialLength)); |
| 385 Float32x4 get _defaultValue => new Float32x4.zero(); | 393 Float32x4 get _defaultValue => new Float32x4.zero(); |
| 386 Float32x4List _createBuffer(int size) => new Float32x4List(size); | 394 Float32x4List _createBuffer(int size) => new Float32x4List(size); |
| 387 } | 395 } |
| OLD | NEW |