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