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 /// Growable typed-data lists. |
6 * Growable typed-data lists. | 6 /// |
7 * | 7 /// 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. | 8 /// 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 | 9 /// is replaced by a new buffer. |
10 * is replaced by a new buffer. | 10 /// |
11 * | 11 /// That means that using the [TypedDataView.buffer] getter is not guaranteed |
12 * 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 |
13 * to return the same result each time it is used, and that the buffer may | 13 /// be larger than what the list is using. |
14 * be larger than what the list is using. | |
15 */ | |
16 library dart.pkg.typed_data.typed_buffers; | 14 library dart.pkg.typed_data.typed_buffers; |
17 | 15 |
18 import "dart:collection" show ListBase; | 16 import "dart:collection" show ListBase; |
19 import "dart:typed_data"; | 17 import "dart:typed_data"; |
20 | 18 |
21 abstract class _TypedDataBuffer<E> extends ListBase<E> { | 19 abstract class _TypedDataBuffer<E> extends ListBase<E> { |
22 static const int INITIAL_LENGTH = 8; | 20 static const int INITIAL_LENGTH = 8; |
23 | 21 |
24 /// This is a Uint8List for Uint8Buffer. It's both a List<E> and a TypedData, | 22 /// This is a Uint8List for Uint8Buffer. It's both a List<E> and a TypedData, |
25 /// which we don't have a type for here. | 23 /// which we don't have a type for here. |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 /// Ensures that [_buffer] is at least [requiredCapacity] long, | 188 /// Ensures that [_buffer] is at least [requiredCapacity] long, |
191 /// | 189 /// |
192 /// Grows the buffer if necessary, preserving existing data. | 190 /// Grows the buffer if necessary, preserving existing data. |
193 void _ensureCapacity(int requiredCapacity) { | 191 void _ensureCapacity(int requiredCapacity) { |
194 if (requiredCapacity <= _buffer.length) return; | 192 if (requiredCapacity <= _buffer.length) return; |
195 var newBuffer = _createBiggerBuffer(requiredCapacity); | 193 var newBuffer = _createBiggerBuffer(requiredCapacity); |
196 newBuffer.setRange(0, _length, _buffer); | 194 newBuffer.setRange(0, _length, _buffer); |
197 _buffer = newBuffer; | 195 _buffer = newBuffer; |
198 } | 196 } |
199 | 197 |
200 /** | 198 /// Create a bigger buffer. |
201 * Create a bigger buffer. | 199 /// |
202 * | 200 /// This method determines how much bigger a bigger buffer should |
203 * This method determines how much bigger a bigger buffer should | 201 /// be. If [requiredCapacity] is not null, it will be at least that |
204 * be. If [requiredCapacity] is not null, it will be at least that | 202 /// size. It will always have at least have double the capacity of |
205 * size. It will always have at least have double the capacity of | 203 /// the current buffer. |
206 * the current buffer. | |
207 */ | |
208 List<E> _createBiggerBuffer(int requiredCapacity) { | 204 List<E> _createBiggerBuffer(int requiredCapacity) { |
209 int newLength = _buffer.length * 2; | 205 int newLength = _buffer.length * 2; |
210 if (requiredCapacity != null && newLength < requiredCapacity) { | 206 if (requiredCapacity != null && newLength < requiredCapacity) { |
211 newLength = requiredCapacity; | 207 newLength = requiredCapacity; |
212 } else if (newLength < INITIAL_LENGTH) { | 208 } else if (newLength < INITIAL_LENGTH) { |
213 newLength = INITIAL_LENGTH; | 209 newLength = INITIAL_LENGTH; |
214 } | 210 } |
215 return _createBuffer(newLength); | 211 return _createBuffer(newLength); |
216 } | 212 } |
217 | 213 |
(...skipping 16 matching lines...) Expand all Loading... |
234 } | 230 } |
235 | 231 |
236 // TypedData. | 232 // TypedData. |
237 | 233 |
238 int get elementSizeInBytes => _buffer.elementSizeInBytes; | 234 int get elementSizeInBytes => _buffer.elementSizeInBytes; |
239 | 235 |
240 int get lengthInBytes => _length * _buffer.elementSizeInBytes; | 236 int get lengthInBytes => _length * _buffer.elementSizeInBytes; |
241 | 237 |
242 int get offsetInBytes => _buffer.offsetInBytes; | 238 int get offsetInBytes => _buffer.offsetInBytes; |
243 | 239 |
244 /** | 240 /// Returns the underlying [ByteBuffer]. |
245 * Returns the underlying [ByteBuffer]. | 241 /// |
246 * | 242 /// The returned buffer may be replaced by operations that change the [length] |
247 * The returned buffer may be replaced by operations that change the [length] | 243 /// of this list. |
248 * of this list. | 244 /// |
249 * | 245 /// The buffer may be larger than [lengthInBytes] bytes, but never smaller. |
250 * The buffer may be larger than [lengthInBytes] bytes, but never smaller. | |
251 */ | |
252 ByteBuffer get buffer => _buffer.buffer; | 246 ByteBuffer get buffer => _buffer.buffer; |
253 | 247 |
254 // Specialization for the specific type. | 248 // Specialization for the specific type. |
255 | 249 |
256 // Return zero for integers, 0.0 for floats, etc. | 250 // Return zero for integers, 0.0 for floats, etc. |
257 // Used to fill buffer when changing length. | 251 // Used to fill buffer when changing length. |
258 E get _defaultValue; | 252 E get _defaultValue; |
259 | 253 |
260 // Create a new typed list to use as buffer. | 254 // Create a new typed list to use as buffer. |
261 List<E> _createBuffer(int size); | 255 List<E> _createBuffer(int size); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 Int32x4 get _defaultValue => _zero; | 330 Int32x4 get _defaultValue => _zero; |
337 Int32x4List _createBuffer(int size) => new Int32x4List(size); | 331 Int32x4List _createBuffer(int size) => new Int32x4List(size); |
338 } | 332 } |
339 | 333 |
340 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { | 334 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { |
341 Float32x4Buffer([int initialLength = 0]) | 335 Float32x4Buffer([int initialLength = 0]) |
342 : super(new Float32x4List(initialLength)); | 336 : super(new Float32x4List(initialLength)); |
343 Float32x4 get _defaultValue => new Float32x4.zero(); | 337 Float32x4 get _defaultValue => new Float32x4.zero(); |
344 Float32x4List _createBuffer(int size) => new Float32x4List(size); | 338 Float32x4List _createBuffer(int size) => new Float32x4List(size); |
345 } | 339 } |
OLD | NEW |