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 /// The [start] value must be non-negative. The [values] iterable must have at | |
80 /// least [start] elements, and if [end] is specified, it must be greater than | |
81 /// or equal to [start] and [values] must have at least [end] elements. | |
82 void addAll(Iterable<E> values, [int start = 0, int end]) { | |
83 _addAll(values, start, end); | |
84 } | |
85 | |
86 /// Inserts all objects of [values] at position [index] in this list. | |
87 /// | |
88 /// This adds values from [start] (inclusive) to [end] (exclusive) in | |
89 /// [values]. If [end] is omitted, it defaults to adding all elements of | |
90 /// [values] after [start]. | |
91 /// | |
92 /// The [start] value must be non-negative. The [values] iterable must have at | |
93 /// least [start] elements, and if [end] is specified, it must be greater than | |
94 /// or equal to [start] and [values] must have at least [end] elements. | |
95 void insertAll(int index, Iterable<E> values, [int start = 0, int end]) { | |
96 RangeError.checkValidIndex(index, this, "index", _length + 1); | |
Lasse Reichstein Nielsen
2015/10/27 10:10:19
Tricky, this "index" is really not an index (0<=in
| |
97 RangeError.checkNotNegative(start, "start"); | |
98 if (end != null && start > end) { | |
99 throw new RangeError.range(end, start, null, "end"); | |
100 } | |
101 | |
102 // If we're adding to the end of the list anyway, use [_addAll]. This lets | |
103 // us avoid converting [values] into a list even if [end] is null, since we | |
104 // can add values iteratively to the end of the list. We can't do so in the | |
105 // center because copying the trailing elements every time is non-linear. | |
Lasse Reichstein Nielsen
2015/10/27 10:10:19
There are things we can do which is linear, but it
| |
106 if (index == _length) { | |
107 _addAll(values, start, end); | |
108 return; | |
109 } | |
110 | |
111 // If we don't know how much room to make for [values], convert it to a list | |
112 // so we can tell. | |
113 if (end == null && values is! List) { | |
114 values = values.toList(growable: false); | |
Lasse Reichstein Nielsen
2015/10/27 10:10:19
do:
if (end == null) {
if (values is! List) {
nweiz
2015/10/27 21:26:07
Won't calling [skip] avoid throwing an error when
Lasse Reichstein Nielsen
2015/10/28 07:31:56
True.
I might try to optimize this more - it annoy
| |
115 } | |
116 | |
117 _insertKnownLength(index, values, start, end); | |
118 } | |
119 | |
120 /// Does the same thing as [addAll]. | |
121 /// | |
122 /// This allows [addAll] and [insertAll] to share implementation without a | |
123 /// subclass unexpectedly overriding both when it intended to only override | |
124 /// [addAll]. | |
125 void _addAll(Iterable<E> values, [int start = 0, int end]) { | |
126 RangeError.checkNotNegative(start, "start"); | |
Lasse Reichstein Nielsen
2015/10/27 10:10:18
We already checked this for insertAll. Move these
nweiz
2015/10/27 21:26:07
Done.
| |
127 if (end != null && start > end) { | |
128 throw new RangeError.range(end, start, null, "end"); | |
129 } | |
130 | |
131 // If we can efficiently compute the length of the segment to add, do so | |
132 // with [addRange]. This can be more efficient for lists and potentially | |
133 // other well-known types. | |
Lasse Reichstein Nielsen
2015/10/27 10:10:18
How about:
if (values is List) {
end ??= values
nweiz
2015/10/27 21:26:08
Done.
| |
134 if (end != null || values is List) { | |
135 _insertKnownLength(_length, values, start, end); | |
136 return; | |
137 } | |
138 | |
139 // Otherwise, just add values one at a time. | |
140 var i = 0; | |
141 for (var value in values) { | |
142 if (i >= start) add(value); | |
143 i++; | |
144 } | |
145 if (i < start) throw new StateError("Too few elements"); | |
146 } | |
147 | |
148 /// Like [insertAll], but assumes that the length of the slice of [values] is | |
149 /// known (or easy to calculate). | |
150 /// | |
151 /// Specifically, this assumes that if [values] is not a [List], [end] is not | |
152 /// `null`. | |
153 void _insertKnownLength(int index, Iterable<E> values, int start, int end) { | |
154 if (values is List) { | |
155 end ??= values.length; | |
156 if (start > values.length || end > values.length) { | |
157 throw new StateError("Too few elements"); | |
158 } | |
159 } else { | |
160 assert(end != null); | |
161 } | |
162 | |
163 var valuesLength = end - start; | |
164 var newLength = _length + valuesLength; | |
165 _ensureCapacity(newLength); | |
166 | |
167 _buffer.setRange( | |
168 index + valuesLength, _length + valuesLength, _buffer, index); | |
169 _buffer.setRange(index, index + valuesLength, values, start); | |
170 _length = newLength; | |
75 } | 171 } |
76 | 172 |
77 void insert(int index, E element) { | 173 void insert(int index, E element) { |
78 if (index < 0 || index > _length) { | 174 if (index < 0 || index > _length) { |
79 throw new RangeError.range(index, 0, _length); | 175 throw new RangeError.range(index, 0, _length); |
80 } | 176 } |
81 if (_length < _buffer.length) { | 177 if (_length < _buffer.length) { |
82 _buffer.setRange(index + 1, _length + 1, _buffer, index); | 178 _buffer.setRange(index + 1, _length + 1, _buffer, index); |
83 _buffer[index] = element; | 179 _buffer[index] = element; |
84 _length++; | 180 _length++; |
85 return; | 181 return; |
86 } | 182 } |
87 List<E> newBuffer = _createBiggerBuffer(null); | 183 List<E> newBuffer = _createBiggerBuffer(null); |
88 newBuffer.setRange(0, index, _buffer); | 184 newBuffer.setRange(0, index, _buffer); |
89 newBuffer.setRange(index + 1, _length + 1, _buffer, index); | 185 newBuffer.setRange(index + 1, _length + 1, _buffer, index); |
90 newBuffer[index] = element; | 186 newBuffer[index] = element; |
91 _length++; | 187 _length++; |
92 _buffer = newBuffer; | 188 _buffer = newBuffer; |
93 } | 189 } |
94 | 190 |
191 /// Ensures that [_buffer] is at least [requiredCapacity] long, | |
192 /// | |
193 /// Grows the buffer if necessary, preserving existing data. | |
194 void _ensureCapacity(int requiredCapacity) { | |
195 if (requiredCapacity <= _buffer.length) return; | |
196 var newBuffer = _createBiggerBuffer(requiredCapacity); | |
197 newBuffer.setRange(0, _length, _buffer); | |
198 _buffer = newBuffer; | |
199 } | |
200 | |
95 /** | 201 /** |
96 * Create a bigger buffer. | 202 * Create a bigger buffer. |
97 * | 203 * |
98 * This method determines how much bigger a bigger buffer should | 204 * This method determines how much bigger a bigger buffer should |
99 * be. If [requiredLength] is not null, it will be at least that | 205 * 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 | 206 * size. It will always have at least have double the capacity of |
101 * the current buffer. | 207 * the current buffer. |
102 */ | 208 */ |
103 List<E> _createBiggerBuffer(int requiredLength) { | 209 List<E> _createBiggerBuffer(int requiredCapacity) { |
104 int newLength = _buffer.length * 2; | 210 int newLength = _buffer.length * 2; |
105 if (requiredLength != null && newLength < requiredLength) { | 211 if (requiredCapacity != null && newLength < requiredCapacity) { |
106 newLength = requiredLength; | 212 newLength = requiredCapacity; |
107 } else if (newLength < INITIAL_LENGTH) { | 213 } else if (newLength < INITIAL_LENGTH) { |
108 newLength = INITIAL_LENGTH; | 214 newLength = INITIAL_LENGTH; |
109 } | 215 } |
110 return _createBuffer(newLength); | 216 return _createBuffer(newLength); |
111 } | 217 } |
112 | 218 |
113 void _grow() { | 219 void _grow() { |
114 _buffer = _createBiggerBuffer(null)..setRange(0, _length, _buffer); | 220 _buffer = _createBiggerBuffer(null)..setRange(0, _length, _buffer); |
115 } | 221 } |
116 | 222 |
117 void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { | 223 void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { |
118 if (end > _length) throw new RangeError.range(end, 0, _length); | 224 if (end > _length) throw new RangeError.range(end, 0, _length); |
225 _setRange(start, end, source, skipCount); | |
226 } | |
227 | |
228 /// Like [setRange], but with no bounds checking. | |
229 void _setRange(int start, int end, Iterable<E> source, int skipCount) { | |
119 if (source is _TypedDataBuffer<E>) { | 230 if (source is _TypedDataBuffer<E>) { |
120 _buffer.setRange(start, end, source._buffer, skipCount); | 231 _buffer.setRange(start, end, source._buffer, skipCount); |
121 } else { | 232 } else { |
122 _buffer.setRange(start, end, source, skipCount); | 233 _buffer.setRange(start, end, source, skipCount); |
123 } | 234 } |
124 } | 235 } |
125 | 236 |
126 // TypedData. | 237 // TypedData. |
127 | 238 |
128 int get elementSizeInBytes => _buffer.elementSizeInBytes; | 239 int get elementSizeInBytes => _buffer.elementSizeInBytes; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 Int32x4 get _defaultValue => _zero; | 337 Int32x4 get _defaultValue => _zero; |
227 Int32x4List _createBuffer(int size) => new Int32x4List(size); | 338 Int32x4List _createBuffer(int size) => new Int32x4List(size); |
228 } | 339 } |
229 | 340 |
230 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { | 341 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { |
231 Float32x4Buffer([int initialLength = 0]) | 342 Float32x4Buffer([int initialLength = 0]) |
232 : super(new Float32x4List(initialLength)); | 343 : super(new Float32x4List(initialLength)); |
233 Float32x4 get _defaultValue => new Float32x4.zero(); | 344 Float32x4 get _defaultValue => new Float32x4.zero(); |
234 Float32x4List _createBuffer(int size) => new Float32x4List(size); | 345 Float32x4List _createBuffer(int size) => new Float32x4List(size); |
235 } | 346 } |
OLD | NEW |