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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 _buffer = newBuffer; | 58 _buffer = newBuffer; |
| 59 } | 59 } |
| 60 _length = newLength; | 60 _length = newLength; |
| 61 } | 61 } |
| 62 | 62 |
| 63 void _add(E value) { | 63 void _add(E value) { |
| 64 if (_length == _buffer.length) _grow(); | 64 if (_length == _buffer.length) _grow(); |
| 65 _buffer[_length++] = value; | 65 _buffer[_length++] = value; |
| 66 } | 66 } |
| 67 | 67 |
| 68 // We override the default implementation of `add` and `addAll` because | 68 // We override the default implementation of `add` because it grows the list |
| 69 // they grow by setting the length in increments of one. We want to grow | 69 // by setting the length in increments of one. We want to grow by doubling |
| 70 // by doubling capacity in most cases. | 70 // capacity in most cases. |
| 71 void add(E value) { _add(value); } | 71 void add(E value) { _add(value); } |
| 72 | 72 |
| 73 void addAll(Iterable<E> values) { | 73 /// This adds values from [start] (inclusive) to [end] (exclusive) in |
| 74 for (E value in values) _add(value); | 74 /// [values]. [start] defaults to `0` and [end] defaults to `values.length`. |
| 75 void addAll(Iterable<E> values, [int start, int end]) { | |
|
Lasse Reichstein Nielsen
2015/10/15 10:58:25
Use [int start = 0, int end] like other range para
nweiz
2015/10/15 20:14:10
Done for consistency, although in general default
| |
| 76 start ??= 0; | |
| 77 if (start == 0 && end == null) { | |
| 78 // `ListBase.insertAll` bottoms out on `setRange`, which has good handling | |
| 79 // of lists, typed buffers, and typed data lists. | |
| 80 insertAll(_length, values); | |
|
Lasse Reichstein Nielsen
2015/10/15 10:58:25
You could also update insertAll to also take start
nweiz
2015/10/15 20:14:10
Done.
| |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 var list = values is List ? values : values.toList(growable: false); | |
|
Lasse Reichstein Nielsen
2015/10/15 10:58:25
I still don't want to convert the iterable to a li
nweiz
2015/10/15 20:14:10
Done.
| |
| 85 end = RangeError.checkValidRange(start, end, list.length); | |
|
Lasse Reichstein Nielsen
2015/10/15 10:58:25
That requires the end to be inside the iterable. W
nweiz
2015/10/15 20:14:10
List.getRange doesn't allow too large a start or e
Lasse Reichstein Nielsen
2015/10/20 08:09:49
Generally we check the limits on lists and not on
nweiz
2015/10/20 21:24:39
Wouldn't this be a breaking change? The error is d
floitsch
2015/10/20 22:30:46
Since one can leave "end" empty I prefer to check,
| |
| 86 | |
| 87 var newLength = _length + end - start; | |
| 88 _growBuffer(newLength); | |
|
Lasse Reichstein Nielsen
2015/10/15 10:58:25
I prefer _ensureCapacity. The function doesn't act
nweiz
2015/10/15 20:14:10
Done.
| |
| 89 _setRange(_length, newLength, list, start); | |
| 90 _length = newLength; | |
| 75 } | 91 } |
| 76 | 92 |
| 77 void insert(int index, E element) { | 93 void insert(int index, E element) { |
| 78 if (index < 0 || index > _length) { | 94 if (index < 0 || index > _length) { |
| 79 throw new RangeError.range(index, 0, _length); | 95 throw new RangeError.range(index, 0, _length); |
| 80 } | 96 } |
| 81 if (_length < _buffer.length) { | 97 if (_length < _buffer.length) { |
| 82 _buffer.setRange(index + 1, _length + 1, _buffer, index); | 98 _buffer.setRange(index + 1, _length + 1, _buffer, index); |
| 83 _buffer[index] = element; | 99 _buffer[index] = element; |
| 84 _length++; | 100 _length++; |
| 85 return; | 101 return; |
| 86 } | 102 } |
| 87 List<E> newBuffer = _createBiggerBuffer(null); | 103 List<E> newBuffer = _createBiggerBuffer(null); |
| 88 newBuffer.setRange(0, index, _buffer); | 104 newBuffer.setRange(0, index, _buffer); |
| 89 newBuffer.setRange(index + 1, _length + 1, _buffer, index); | 105 newBuffer.setRange(index + 1, _length + 1, _buffer, index); |
| 90 newBuffer[index] = element; | 106 newBuffer[index] = element; |
| 91 _length++; | 107 _length++; |
| 92 _buffer = newBuffer; | 108 _buffer = newBuffer; |
| 93 } | 109 } |
| 94 | 110 |
| 111 /// Grows [_buffer] so that it's at least [requiredLength] long, preserving | |
| 112 /// the existing data. | |
|
Lasse Reichstein Nielsen
2015/10/15 10:58:25
/// Ensures that [_buffer] is at least [requiredCa
nweiz
2015/10/15 20:14:10
Done.
| |
| 113 void _growBuffer(int requiredLength) { | |
| 114 if (requiredLength < _buffer.length) return; | |
| 115 var newBuffer = _createBiggerBuffer(null); | |
| 116 newBuffer.setRange(0, _length, _buffer); | |
| 117 _buffer = newBuffer; | |
| 118 } | |
| 119 | |
| 95 /** | 120 /** |
| 96 * Create a bigger buffer. | 121 * Create a bigger buffer. |
| 97 * | 122 * |
| 98 * This method determines how much bigger a bigger buffer should | 123 * This method determines how much bigger a bigger buffer should |
| 99 * be. If [requiredLength] is not null, it will be at least that | 124 * be. If [requiredLength] is not null, it will be at least that |
| 100 * size. It will always have at least have double the capacity of | 125 * size. It will always have at least have double the capacity of |
| 101 * the current buffer. | 126 * the current buffer. |
| 102 */ | 127 */ |
| 103 List<E> _createBiggerBuffer(int requiredLength) { | 128 List<E> _createBiggerBuffer(int requiredLength) { |
| 104 int newLength = _buffer.length * 2; | 129 int newLength = _buffer.length * 2; |
| 105 if (requiredLength != null && newLength < requiredLength) { | 130 if (requiredLength != null && newLength < requiredLength) { |
| 106 newLength = requiredLength; | 131 newLength = requiredLength; |
| 107 } else if (newLength < INITIAL_LENGTH) { | 132 } else if (newLength < INITIAL_LENGTH) { |
| 108 newLength = INITIAL_LENGTH; | 133 newLength = INITIAL_LENGTH; |
| 109 } | 134 } |
| 110 return _createBuffer(newLength); | 135 return _createBuffer(newLength); |
| 111 } | 136 } |
| 112 | 137 |
| 113 void _grow() { | 138 void _grow() { |
| 114 _buffer = _createBiggerBuffer(null)..setRange(0, _length, _buffer); | 139 _buffer = _createBiggerBuffer(null)..setRange(0, _length, _buffer); |
| 115 } | 140 } |
| 116 | 141 |
| 117 void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { | 142 void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { |
| 118 if (end > _length) throw new RangeError.range(end, 0, _length); | 143 if (end > _length) throw new RangeError.range(end, 0, _length); |
| 144 _setRange(start, end, source, skipCount); | |
| 145 } | |
| 146 | |
| 147 /// Like [setRange], but with no bounds checking. | |
| 148 void _setRange(int start, int end, Iterable<E> source, int skipCount) { | |
| 119 if (source is _TypedDataBuffer<E>) { | 149 if (source is _TypedDataBuffer<E>) { |
| 120 _buffer.setRange(start, end, source._buffer, skipCount); | 150 _buffer.setRange(start, end, source._buffer, skipCount); |
| 121 } else { | 151 } else { |
| 122 _buffer.setRange(start, end, source, skipCount); | 152 _buffer.setRange(start, end, source, skipCount); |
| 123 } | 153 } |
| 124 } | 154 } |
| 125 | 155 |
| 126 // TypedData. | 156 // TypedData. |
| 127 | 157 |
| 128 int get elementSizeInBytes => _buffer.elementSizeInBytes; | 158 int get elementSizeInBytes => _buffer.elementSizeInBytes; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 226 Int32x4 get _defaultValue => _zero; | 256 Int32x4 get _defaultValue => _zero; |
| 227 Int32x4List _createBuffer(int size) => new Int32x4List(size); | 257 Int32x4List _createBuffer(int size) => new Int32x4List(size); |
| 228 } | 258 } |
| 229 | 259 |
| 230 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { | 260 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { |
| 231 Float32x4Buffer([int initialLength = 0]) | 261 Float32x4Buffer([int initialLength = 0]) |
| 232 : super(new Float32x4List(initialLength)); | 262 : super(new Float32x4List(initialLength)); |
| 233 Float32x4 get _defaultValue => new Float32x4.zero(); | 263 Float32x4 get _defaultValue => new Float32x4.zero(); |
| 234 Float32x4List _createBuffer(int size) => new Float32x4List(size); | 264 Float32x4List _createBuffer(int size) => new Float32x4List(size); |
| 235 } | 265 } |
| OLD | NEW |