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 |
Lasse Reichstein Nielsen
2015/10/20 08:09:49
Start with single sentence.
/// Add elements of
nweiz
2015/10/20 22:54:58
This is the usual rule, but AFAIK the goal of that
Lasse Reichstein Nielsen
2015/10/21 10:11:18
It does WHAT? That's horribly broken, and none of
nweiz
2015/10/21 21:22:30
Doing an experiment, it looks like I'm wrong. Mayb
| |
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 = 0, int end]) => | |
Lasse Reichstein Nielsen
2015/10/20 08:09:49
Don't use => for void functions. Make it a proper
nweiz
2015/10/20 22:54:58
Done.
| |
76 insertAll(_length, values, start, end); | |
77 | |
78 /// This adds values from [start] (inclusive) to [end] (exclusive) in | |
79 /// [values]. [start] defaults to `0` and [end] defaults to `values.length`. | |
80 void insertAll(int index, Iterable<E> values, [int start = 0, int end]) { | |
81 RangeError.checkValidIndex(index, this, "index", _length + 1); | |
82 RangeError.checkNotNegative(start); | |
Lasse Reichstein Nielsen
2015/10/20 08:09:49
...checkNotNegative(start, "start");
nweiz
2015/10/20 22:54:58
Done.
| |
83 if (end != null && start > end) { | |
84 throw new RangeError.range(start, 0, end, "start"); | |
Lasse Reichstein Nielsen
2015/10/20 08:09:49
I'd make this an error of "end":
throw new Range
nweiz
2015/10/20 22:54:58
Done.
| |
85 } | |
86 | |
87 // If [values] is an iterable, we can't efficiently get its length. If we | |
88 // also don't have [end], we don't know how large to expand [_buffer] so we | |
89 // fall back to the default implementation. | |
90 if (end == null && values is! List) { | |
Lasse Reichstein Nielsen
2015/10/20 08:09:49
Just do this if values is not a list.
You don't ac
floitsch
2015/10/20 22:30:46
I don't understand the comment.
| |
91 var i = 0; | |
92 for (var value in values) { | |
93 if (i >= start) { | |
94 if (i == end) break; | |
floitsch
2015/10/20 22:30:47
This is in the branch where "end" == null. So this
nweiz
2015/10/20 22:54:58
Done.
| |
95 insert(index + i - start, value); | |
Lasse Reichstein Nielsen
2015/10/20 08:09:49
This is really horrible - maybe I should do someth
floitsch
2015/10/20 22:30:46
I'm not sure what you mean, but we can't just inse
nweiz
2015/10/20 22:54:58
It seems troublesome to me as well, but Lasse does
| |
96 } | |
97 i++; | |
98 } | |
99 if (i < start) throw new StateError("Too few elements"); | |
floitsch
2015/10/20 22:30:47
This looks like we are checking that the iterable
nweiz
2015/10/20 22:54:58
In this block, we know [end] is null. Below, we re
| |
100 return; | |
101 } | |
102 | |
103 if (values is List) { | |
104 end ??= values.length; | |
105 if (start > values.length || end > values.length) { | |
106 throw new StateError("Too few elements"); | |
Lasse Reichstein Nielsen
2015/10/20 08:09:49
Don't throw here, the input is an iterable.
If sta
floitsch
2015/10/20 22:30:47
As stated above: I would actually prefer if we che
| |
107 } | |
108 } | |
109 | |
110 var valuesLength = end - start; | |
111 var newLength = _length + valuesLength; | |
112 _ensureCapacity(newLength); | |
113 | |
114 _buffer.setRange( | |
115 index + valuesLength, _length + valuesLength, _buffer, index); | |
116 _buffer.setRange(index, index + valuesLength, values, start); | |
117 _length = newLength; | |
75 } | 118 } |
76 | 119 |
77 void insert(int index, E element) { | 120 void insert(int index, E element) { |
78 if (index < 0 || index > _length) { | 121 if (index < 0 || index > _length) { |
79 throw new RangeError.range(index, 0, _length); | 122 throw new RangeError.range(index, 0, _length); |
80 } | 123 } |
81 if (_length < _buffer.length) { | 124 if (_length < _buffer.length) { |
82 _buffer.setRange(index + 1, _length + 1, _buffer, index); | 125 _buffer.setRange(index + 1, _length + 1, _buffer, index); |
83 _buffer[index] = element; | 126 _buffer[index] = element; |
84 _length++; | 127 _length++; |
85 return; | 128 return; |
86 } | 129 } |
87 List<E> newBuffer = _createBiggerBuffer(null); | 130 List<E> newBuffer = _createBiggerBuffer(null); |
88 newBuffer.setRange(0, index, _buffer); | 131 newBuffer.setRange(0, index, _buffer); |
89 newBuffer.setRange(index + 1, _length + 1, _buffer, index); | 132 newBuffer.setRange(index + 1, _length + 1, _buffer, index); |
90 newBuffer[index] = element; | 133 newBuffer[index] = element; |
91 _length++; | 134 _length++; |
92 _buffer = newBuffer; | 135 _buffer = newBuffer; |
93 } | 136 } |
94 | 137 |
138 /// Ensures that [_buffer] is at least [requiredCapacity] long, | |
139 /// | |
140 /// Grows the buffer if necessary, preserving existing data. | |
141 void _ensureCapacity(int requiredCapacity) { | |
142 if (requiredCapacity < _buffer.length) return; | |
Lasse Reichstein Nielsen
2015/10/20 08:09:49
<=
nweiz
2015/10/20 22:54:58
Done.
| |
143 var newBuffer = _createBiggerBuffer(null); | |
Lasse Reichstein Nielsen
2015/10/20 08:09:49
pass requiredCapacity as parameter?
nweiz
2015/10/20 22:54:58
Done.
| |
144 newBuffer.setRange(0, _length, _buffer); | |
145 _buffer = newBuffer; | |
146 } | |
147 | |
95 /** | 148 /** |
96 * Create a bigger buffer. | 149 * Create a bigger buffer. |
97 * | 150 * |
98 * This method determines how much bigger a bigger buffer should | 151 * This method determines how much bigger a bigger buffer should |
99 * be. If [requiredLength] is not null, it will be at least that | 152 * 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 | 153 * size. It will always have at least have double the capacity of |
101 * the current buffer. | 154 * the current buffer. |
102 */ | 155 */ |
103 List<E> _createBiggerBuffer(int requiredLength) { | 156 List<E> _createBiggerBuffer(int requiredCapacity) { |
104 int newLength = _buffer.length * 2; | 157 int newLength = _buffer.length * 2; |
105 if (requiredLength != null && newLength < requiredLength) { | 158 if (requiredCapacity != null && newLength < requiredCapacity) { |
106 newLength = requiredLength; | 159 newLength = requiredCapacity; |
107 } else if (newLength < INITIAL_LENGTH) { | 160 } else if (newLength < INITIAL_LENGTH) { |
108 newLength = INITIAL_LENGTH; | 161 newLength = INITIAL_LENGTH; |
109 } | 162 } |
110 return _createBuffer(newLength); | 163 return _createBuffer(newLength); |
111 } | 164 } |
112 | 165 |
113 void _grow() { | 166 void _grow() { |
114 _buffer = _createBiggerBuffer(null)..setRange(0, _length, _buffer); | 167 _buffer = _createBiggerBuffer(null)..setRange(0, _length, _buffer); |
115 } | 168 } |
116 | 169 |
117 void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { | 170 void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { |
118 if (end > _length) throw new RangeError.range(end, 0, _length); | 171 if (end > _length) throw new RangeError.range(end, 0, _length); |
172 _setRange(start, end, source, skipCount); | |
173 } | |
174 | |
175 /// Like [setRange], but with no bounds checking. | |
176 void _setRange(int start, int end, Iterable<E> source, int skipCount) { | |
119 if (source is _TypedDataBuffer<E>) { | 177 if (source is _TypedDataBuffer<E>) { |
120 _buffer.setRange(start, end, source._buffer, skipCount); | 178 _buffer.setRange(start, end, source._buffer, skipCount); |
121 } else { | 179 } else { |
122 _buffer.setRange(start, end, source, skipCount); | 180 _buffer.setRange(start, end, source, skipCount); |
123 } | 181 } |
124 } | 182 } |
125 | 183 |
126 // TypedData. | 184 // TypedData. |
127 | 185 |
128 int get elementSizeInBytes => _buffer.elementSizeInBytes; | 186 int get elementSizeInBytes => _buffer.elementSizeInBytes; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 Int32x4 get _defaultValue => _zero; | 284 Int32x4 get _defaultValue => _zero; |
227 Int32x4List _createBuffer(int size) => new Int32x4List(size); | 285 Int32x4List _createBuffer(int size) => new Int32x4List(size); |
228 } | 286 } |
229 | 287 |
230 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { | 288 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { |
231 Float32x4Buffer([int initialLength = 0]) | 289 Float32x4Buffer([int initialLength = 0]) |
232 : super(new Float32x4List(initialLength)); | 290 : super(new Float32x4List(initialLength)); |
233 Float32x4 get _defaultValue => new Float32x4.zero(); | 291 Float32x4 get _defaultValue => new Float32x4.zero(); |
234 Float32x4List _createBuffer(int size) => new Float32x4List(size); | 292 Float32x4List _createBuffer(int size) => new Float32x4List(size); |
235 } | 293 } |
OLD | NEW |