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 /// Appends all objects of [values] to the end of this buffer. |
| 74 for (E value in values) _add(value); | 74 /// |
| 75 /// This adds values from [start] (inclusive) to [end] (exclusive) in | |
| 76 /// [values]. If [end] is omitted, it defaults to adding all elements of | |
| 77 /// [values] after [start]. | |
| 78 /// | |
| 79 /// Throws a [StateError] if [values] isn't long enough to extract a slice | |
| 80 /// between [start] and [end]. | |
| 81 void addAll(Iterable<E> values, [int start = 0, int end]) { | |
| 82 insertAll(_length, values, start, end); | |
|
Lasse Reichstein Nielsen
2015/10/23 08:00:02
I generally prefer to not define one public API me
nweiz
2015/10/26 23:53:57
Done.
| |
| 83 } | |
| 84 | |
| 85 /// Inserts all objects of [values] at position [index] in this list. | |
| 86 /// | |
| 87 /// This adds values from [start] (inclusive) to [end] (exclusive) in | |
| 88 /// [values]. If [end] is omitted, it defaults to adding all elements of | |
| 89 /// [values] after [start]. | |
| 90 /// | |
| 91 /// Throws a [RangeError] if [index] is less than 0 or greater than [length]. | |
| 92 /// Throws a [StateError] if [values] isn't long enough to extract a slice | |
|
Lasse Reichstein Nielsen
2015/10/23 08:00:02
Don't specify the exact errors, just:
The [star
nweiz
2015/10/26 23:53:57
Done, but why? Isn't it good for the user to know
floitsch
2015/10/27 10:26:17
It really matters for exceptions, but not for erro
Lasse Reichstein Nielsen
2015/10/27 10:33:12
What Florian said!
| |
| 93 /// between [start] and [end]. | |
| 94 void insertAll(int index, Iterable<E> values, [int start = 0, int end]) { | |
|
Lasse Reichstein Nielsen
2015/10/23 08:00:02
Maybe split this earlier if [values] is a list:
nweiz
2015/10/26 23:53:57
Done.
| |
| 95 RangeError.checkValidIndex(index, this, "index", _length + 1); | |
| 96 RangeError.checkNotNegative(start, "start"); | |
| 97 if (end != null && start > end) { | |
| 98 throw new RangeError.range(end, start, null, "end"); | |
| 99 } | |
| 100 | |
| 101 // If [values] is an iterable, we can't efficiently get its length. If we | |
| 102 // also don't have [end], we don't know how large to expand [_buffer] so we | |
| 103 // fall back to the default implementation. | |
| 104 if (end == null && values is! List) { | |
| 105 var i = 0; | |
| 106 for (var value in values) { | |
| 107 if (i >= start) insert(index + i - start, value); | |
| 108 i++; | |
| 109 } | |
| 110 if (i < start) throw new StateError("Too few elements"); | |
|
Lasse Reichstein Nielsen
2015/10/23 08:00:02
It's the [values] argument that have too few argum
nweiz
2015/10/26 23:53:57
This is inconsistent with the corresponding error
Lasse Reichstein Nielsen
2015/10/27 10:33:12
So it is.
I'm getting less and less enchanted by t
| |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 if (values is List) { | |
| 115 end ??= values.length; | |
| 116 if (start > values.length || end > values.length) { | |
| 117 throw new StateError("Too few elements"); | |
|
Lasse Reichstein Nielsen
2015/10/23 08:00:02
RangeError.range(end, start, values.length, "end")
| |
| 118 } | |
| 119 } | |
| 120 | |
| 121 var valuesLength = end - start; | |
| 122 var newLength = _length + valuesLength; | |
| 123 _ensureCapacity(newLength); | |
| 124 | |
| 125 _buffer.setRange( | |
| 126 index + valuesLength, _length + valuesLength, _buffer, index); | |
| 127 _buffer.setRange(index, index + valuesLength, values, start); | |
| 128 _length = newLength; | |
| 75 } | 129 } |
| 76 | 130 |
| 77 void insert(int index, E element) { | 131 void insert(int index, E element) { |
| 78 if (index < 0 || index > _length) { | 132 if (index < 0 || index > _length) { |
| 79 throw new RangeError.range(index, 0, _length); | 133 throw new RangeError.range(index, 0, _length); |
| 80 } | 134 } |
| 81 if (_length < _buffer.length) { | 135 if (_length < _buffer.length) { |
| 82 _buffer.setRange(index + 1, _length + 1, _buffer, index); | 136 _buffer.setRange(index + 1, _length + 1, _buffer, index); |
| 83 _buffer[index] = element; | 137 _buffer[index] = element; |
| 84 _length++; | 138 _length++; |
| 85 return; | 139 return; |
| 86 } | 140 } |
| 87 List<E> newBuffer = _createBiggerBuffer(null); | 141 List<E> newBuffer = _createBiggerBuffer(null); |
| 88 newBuffer.setRange(0, index, _buffer); | 142 newBuffer.setRange(0, index, _buffer); |
| 89 newBuffer.setRange(index + 1, _length + 1, _buffer, index); | 143 newBuffer.setRange(index + 1, _length + 1, _buffer, index); |
| 90 newBuffer[index] = element; | 144 newBuffer[index] = element; |
| 91 _length++; | 145 _length++; |
| 92 _buffer = newBuffer; | 146 _buffer = newBuffer; |
| 93 } | 147 } |
| 94 | 148 |
| 149 /// Ensures that [_buffer] is at least [requiredCapacity] long, | |
| 150 /// | |
| 151 /// Grows the buffer if necessary, preserving existing data. | |
| 152 void _ensureCapacity(int requiredCapacity) { | |
| 153 if (requiredCapacity <= _buffer.length) return; | |
| 154 var newBuffer = _createBiggerBuffer(requiredCapacity); | |
| 155 newBuffer.setRange(0, _length, _buffer); | |
| 156 _buffer = newBuffer; | |
| 157 } | |
| 158 | |
| 95 /** | 159 /** |
| 96 * Create a bigger buffer. | 160 * Create a bigger buffer. |
| 97 * | 161 * |
| 98 * This method determines how much bigger a bigger buffer should | 162 * This method determines how much bigger a bigger buffer should |
| 99 * be. If [requiredLength] is not null, it will be at least that | 163 * be. If [requiredCapacity] is not null, it will be at least that |
| 100 * size. It will always have at least have double the capacity of | 164 * size. It will always have at least have double the capacity of |
| 101 * the current buffer. | 165 * the current buffer. |
| 102 */ | 166 */ |
| 103 List<E> _createBiggerBuffer(int requiredLength) { | 167 List<E> _createBiggerBuffer(int requiredCapacity) { |
| 104 int newLength = _buffer.length * 2; | 168 int newLength = _buffer.length * 2; |
| 105 if (requiredLength != null && newLength < requiredLength) { | 169 if (requiredCapacity != null && newLength < requiredCapacity) { |
| 106 newLength = requiredLength; | 170 newLength = requiredCapacity; |
| 107 } else if (newLength < INITIAL_LENGTH) { | 171 } else if (newLength < INITIAL_LENGTH) { |
| 108 newLength = INITIAL_LENGTH; | 172 newLength = INITIAL_LENGTH; |
| 109 } | 173 } |
| 110 return _createBuffer(newLength); | 174 return _createBuffer(newLength); |
| 111 } | 175 } |
| 112 | 176 |
| 113 void _grow() { | 177 void _grow() { |
| 114 _buffer = _createBiggerBuffer(null)..setRange(0, _length, _buffer); | 178 _buffer = _createBiggerBuffer(null)..setRange(0, _length, _buffer); |
| 115 } | 179 } |
| 116 | 180 |
| 117 void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { | 181 void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { |
| 118 if (end > _length) throw new RangeError.range(end, 0, _length); | 182 if (end > _length) throw new RangeError.range(end, 0, _length); |
| 183 _setRange(start, end, source, skipCount); | |
| 184 } | |
| 185 | |
| 186 /// Like [setRange], but with no bounds checking. | |
| 187 void _setRange(int start, int end, Iterable<E> source, int skipCount) { | |
| 119 if (source is _TypedDataBuffer<E>) { | 188 if (source is _TypedDataBuffer<E>) { |
| 120 _buffer.setRange(start, end, source._buffer, skipCount); | 189 _buffer.setRange(start, end, source._buffer, skipCount); |
| 121 } else { | 190 } else { |
| 122 _buffer.setRange(start, end, source, skipCount); | 191 _buffer.setRange(start, end, source, skipCount); |
| 123 } | 192 } |
| 124 } | 193 } |
| 125 | 194 |
| 126 // TypedData. | 195 // TypedData. |
| 127 | 196 |
| 128 int get elementSizeInBytes => _buffer.elementSizeInBytes; | 197 int get elementSizeInBytes => _buffer.elementSizeInBytes; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 226 Int32x4 get _defaultValue => _zero; | 295 Int32x4 get _defaultValue => _zero; |
| 227 Int32x4List _createBuffer(int size) => new Int32x4List(size); | 296 Int32x4List _createBuffer(int size) => new Int32x4List(size); |
| 228 } | 297 } |
| 229 | 298 |
| 230 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { | 299 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { |
| 231 Float32x4Buffer([int initialLength = 0]) | 300 Float32x4Buffer([int initialLength = 0]) |
| 232 : super(new Float32x4List(initialLength)); | 301 : super(new Float32x4List(initialLength)); |
| 233 Float32x4 get _defaultValue => new Float32x4.zero(); | 302 Float32x4 get _defaultValue => new Float32x4.zero(); |
| 234 Float32x4List _createBuffer(int size) => new Float32x4List(size); | 303 Float32x4List _createBuffer(int size) => new Float32x4List(size); |
| 235 } | 304 } |
| OLD | NEW |