| 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 /// The underlying data buffer. |
| 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 |
| 25 /// for here. For example, for a `Uint8Buffer`, this is a `Uint8List`. |
| 26 List<E> _buffer; |
| 27 |
| 28 /// Returns a view of [_buffer] as a [TypedData]. |
| 29 TypedData get _typedBuffer => _buffer as TypedData; |
| 30 |
| 25 /// The length of the list being built. | 31 /// The length of the list being built. |
| 26 int _length; | 32 int _length; |
| 27 | 33 |
| 28 _TypedDataBuffer(List<E> buffer) | 34 _TypedDataBuffer(List<E> buffer) |
| 29 : this._buffer = buffer, this._length = buffer.length; | 35 : this._buffer = buffer, |
| 36 this._length = buffer.length; |
| 30 | 37 |
| 31 int get length => _length; | 38 int get length => _length; |
| 32 E operator[](int index) { | 39 E operator[](int index) { |
| 33 if (index >= length) throw new RangeError.index(index, this); | 40 if (index >= length) throw new RangeError.index(index, this); |
| 34 return _buffer[index]; | 41 return _buffer[index]; |
| 35 } | 42 } |
| 36 | 43 |
| 37 void operator[]=(int index, E value) { | 44 void operator[]=(int index, E value) { |
| 38 if (index >= length) throw new RangeError.index(index, this); | 45 if (index >= length) throw new RangeError.index(index, this); |
| 39 _buffer[index] = value; | 46 _buffer[index] = value; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 | 154 |
| 148 // Swap [index.._length) and [_length..writeIndex) by double-reversing. | 155 // Swap [index.._length) and [_length..writeIndex) by double-reversing. |
| 149 _reverse(_buffer, index, _length); | 156 _reverse(_buffer, index, _length); |
| 150 _reverse(_buffer, _length, writeIndex); | 157 _reverse(_buffer, _length, writeIndex); |
| 151 _reverse(_buffer, index, writeIndex); | 158 _reverse(_buffer, index, writeIndex); |
| 152 _length = writeIndex; | 159 _length = writeIndex; |
| 153 return; | 160 return; |
| 154 } | 161 } |
| 155 | 162 |
| 156 // Reverses the range [start..end) of buffer. | 163 // Reverses the range [start..end) of buffer. |
| 157 static void _reverse(List<int> buffer, int start, int end) { | 164 static void _reverse(List buffer, int start, int end) { |
| 158 end--; // Point to last element, not after last element. | 165 end--; // Point to last element, not after last element. |
| 159 while (start < end) { | 166 while (start < end) { |
| 160 var first = buffer[start]; | 167 var first = buffer[start]; |
| 161 var last = buffer[end]; | 168 var last = buffer[end]; |
| 162 buffer[end] = first; | 169 buffer[end] = first; |
| 163 buffer[start] = last; | 170 buffer[start] = last; |
| 164 start++; | 171 start++; |
| 165 end--; | 172 end--; |
| 166 } | 173 } |
| 167 } | 174 } |
| (...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) { | 279 void _setRange(int start, int end, Iterable<E> source, int skipCount) { |
| 273 if (source is _TypedDataBuffer<E>) { | 280 if (source is _TypedDataBuffer<E>) { |
| 274 _buffer.setRange(start, end, source._buffer, skipCount); | 281 _buffer.setRange(start, end, source._buffer, skipCount); |
| 275 } else { | 282 } else { |
| 276 _buffer.setRange(start, end, source, skipCount); | 283 _buffer.setRange(start, end, source, skipCount); |
| 277 } | 284 } |
| 278 } | 285 } |
| 279 | 286 |
| 280 // TypedData. | 287 // TypedData. |
| 281 | 288 |
| 282 int get elementSizeInBytes => _buffer.elementSizeInBytes; | 289 int get elementSizeInBytes => _typedBuffer.elementSizeInBytes; |
| 283 | 290 |
| 284 int get lengthInBytes => _length * _buffer.elementSizeInBytes; | 291 int get lengthInBytes => _length * _typedBuffer.elementSizeInBytes; |
| 285 | 292 |
| 286 int get offsetInBytes => _buffer.offsetInBytes; | 293 int get offsetInBytes => _typedBuffer.offsetInBytes; |
| 287 | 294 |
| 288 /// Returns the underlying [ByteBuffer]. | 295 /// Returns the underlying [ByteBuffer]. |
| 289 /// | 296 /// |
| 290 /// The returned buffer may be replaced by operations that change the [length] | 297 /// The returned buffer may be replaced by operations that change the [length] |
| 291 /// of this list. | 298 /// of this list. |
| 292 /// | 299 /// |
| 293 /// The buffer may be larger than [lengthInBytes] bytes, but never smaller. | 300 /// The buffer may be larger than [lengthInBytes] bytes, but never smaller. |
| 294 ByteBuffer get buffer => _buffer.buffer; | 301 ByteBuffer get buffer => _typedBuffer.buffer; |
| 295 | 302 |
| 296 // Specialization for the specific type. | 303 // Specialization for the specific type. |
| 297 | 304 |
| 298 // Return zero for integers, 0.0 for floats, etc. | 305 // Return zero for integers, 0.0 for floats, etc. |
| 299 // Used to fill buffer when changing length. | 306 // Used to fill buffer when changing length. |
| 300 E get _defaultValue; | 307 E get _defaultValue; |
| 301 | 308 |
| 302 // Create a new typed list to use as buffer. | 309 // Create a new typed list to use as buffer. |
| 303 List<E> _createBuffer(int size); | 310 List<E> _createBuffer(int size); |
| 304 } | 311 } |
| 305 | 312 |
| 306 abstract class _IntBuffer extends _TypedDataBuffer<int> { | 313 abstract class _IntBuffer extends _TypedDataBuffer<int> { |
| 307 _IntBuffer(buffer): super(buffer); | 314 _IntBuffer(List<int> buffer): super(buffer); |
| 315 |
| 308 int get _defaultValue => 0; | 316 int get _defaultValue => 0; |
| 309 } | 317 } |
| 310 | 318 |
| 311 abstract class _FloatBuffer extends _TypedDataBuffer<double> { | 319 abstract class _FloatBuffer extends _TypedDataBuffer<double> { |
| 312 _FloatBuffer(buffer): super(buffer); | 320 _FloatBuffer(List<double> buffer): super(buffer); |
| 321 |
| 313 double get _defaultValue => 0.0; | 322 double get _defaultValue => 0.0; |
| 314 } | 323 } |
| 315 | 324 |
| 316 class Uint8Buffer extends _IntBuffer { | 325 class Uint8Buffer extends _IntBuffer { |
| 317 Uint8Buffer([int initialLength = 0]) : super(new Uint8List(initialLength)); | 326 Uint8Buffer([int initialLength = 0]) : super(new Uint8List(initialLength)); |
| 318 Uint8List _createBuffer(int size) => new Uint8List(size); | 327 Uint8List _createBuffer(int size) => new Uint8List(size); |
| 319 } | 328 } |
| 320 | 329 |
| 321 class Int8Buffer extends _IntBuffer { | 330 class Int8Buffer extends _IntBuffer { |
| 322 Int8Buffer([int initialLength = 0]) : super(new Int8List(initialLength)); | 331 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; | 387 Int32x4 get _defaultValue => _zero; |
| 379 Int32x4List _createBuffer(int size) => new Int32x4List(size); | 388 Int32x4List _createBuffer(int size) => new Int32x4List(size); |
| 380 } | 389 } |
| 381 | 390 |
| 382 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { | 391 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { |
| 383 Float32x4Buffer([int initialLength = 0]) | 392 Float32x4Buffer([int initialLength = 0]) |
| 384 : super(new Float32x4List(initialLength)); | 393 : super(new Float32x4List(initialLength)); |
| 385 Float32x4 get _defaultValue => new Float32x4.zero(); | 394 Float32x4 get _defaultValue => new Float32x4.zero(); |
| 386 Float32x4List _createBuffer(int size) => new Float32x4List(size); | 395 Float32x4List _createBuffer(int size) => new Float32x4List(size); |
| 387 } | 396 } |
| OLD | NEW |