Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: lib/typed_buffers.dart

Issue 1728943003: Fix TypedDataBuffer.insertAll() with an Iterable. (Closed) Base URL: git@github.com:dart-lang/typed_data@master
Patch Set: Code review changes Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /// Growable typed-data lists. 5 /// Growable typed-data lists.
6 /// 6 ///
7 /// These lists works just as a typed-data list, except that they are growable. 7 /// These lists works just as a typed-data list, except that they are growable.
8 /// They use an underlying buffer, and when that buffer becomes too small, it 8 /// They use an underlying buffer, and when that buffer becomes too small, it
9 /// is replaced by a new buffer. 9 /// is replaced by a new buffer.
10 /// 10 ///
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 } else { 52 } else {
53 newBuffer = _createBiggerBuffer(newLength); 53 newBuffer = _createBiggerBuffer(newLength);
54 } 54 }
55 newBuffer.setRange(0, _length, _buffer); 55 newBuffer.setRange(0, _length, _buffer);
56 _buffer = newBuffer; 56 _buffer = newBuffer;
57 } 57 }
58 _length = newLength; 58 _length = newLength;
59 } 59 }
60 60
61 void _add(E value) { 61 void _add(E value) {
62 if (_length == _buffer.length) _grow(); 62 if (_length == _buffer.length) _grow(_length);
63 _buffer[_length++] = value; 63 _buffer[_length++] = value;
64 } 64 }
65 65
66 // We override the default implementation of `add` because it grows the list 66 // We override the default implementation of `add` because it grows the list
67 // by setting the length in increments of one. We want to grow by doubling 67 // by setting the length in increments of one. We want to grow by doubling
68 // capacity in most cases. 68 // capacity in most cases.
69 void add(E value) { _add(value); } 69 void add(E value) { _add(value); }
70 70
71 /// Appends all objects of [values] to the end of this buffer. 71 /// Appends all objects of [values] to the end of this buffer.
72 /// 72 ///
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 if (end == null && values is List) { 118 if (end == null && values is List) {
119 end = values.length; 119 end = values.length;
120 } 120 }
121 if (end != null) { 121 if (end != null) {
122 _insertKnownLength(index, values, start, end); 122 _insertKnownLength(index, values, start, end);
123 return; 123 return;
124 } 124 }
125 125
126 // Add elements at end, growing as appropriate, then put them back at 126 // Add elements at end, growing as appropriate, then put them back at
127 // position [index] using flip-by-double-reverse. 127 // position [index] using flip-by-double-reverse.
128 if (end != null) values = values.take(end); 128 var writeIndex = _length;
129 int writeIndex = _length; 129 var skipCount = start;
130 int skipCount = start;
131 for (var value in values) { 130 for (var value in values) {
132 if (skipCount > 0) { 131 if (skipCount > 0) {
133 skipCount--; 132 skipCount--;
134 continue; 133 continue;
135 } 134 }
136 if (writeIndex == _buffer.length) { 135 if (writeIndex == _buffer.length) {
137 _grow(); 136 _grow(writeIndex);
138 } 137 }
139 _buffer[writeIndex++] = value; 138 _buffer[writeIndex++] = value;
140 } 139 }
140
141 if (skipCount > 0) { 141 if (skipCount > 0) {
142 throw new StateError("Too few elements"); 142 throw new StateError("Too few elements");
143 } 143 }
144 if (end != null && writeIndex < end) { 144 if (end != null && writeIndex < end) {
145 throw new RangeError.range(end, start, writeIndex, "end"); 145 throw new RangeError.range(end, start, writeIndex, "end");
146 } 146 }
147
147 // Swap [index.._length) and [_length..writeIndex) by double-reversing. 148 // Swap [index.._length) and [_length..writeIndex) by double-reversing.
148 _reverse(_buffer, index, _length); 149 _reverse(_buffer, index, _length);
149 _reverse(_buffer, _length, writeIndex); 150 _reverse(_buffer, _length, writeIndex);
150 _reverse(_buffer, index, writeIndex); 151 _reverse(_buffer, index, writeIndex);
151 _length = writeIndex; 152 _length = writeIndex;
152 return; 153 return;
153 } 154 }
154 155
155 // Reverses the range [start..end) of buffer. 156 // Reverses the range [start..end) of buffer.
156 static void _reverse(List<int> buffer, int start, int end) { 157 static void _reverse(List<int> buffer, int start, int end) {
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 List<E> _createBiggerBuffer(int requiredCapacity) { 249 List<E> _createBiggerBuffer(int requiredCapacity) {
249 int newLength = _buffer.length * 2; 250 int newLength = _buffer.length * 2;
250 if (requiredCapacity != null && newLength < requiredCapacity) { 251 if (requiredCapacity != null && newLength < requiredCapacity) {
251 newLength = requiredCapacity; 252 newLength = requiredCapacity;
252 } else if (newLength < INITIAL_LENGTH) { 253 } else if (newLength < INITIAL_LENGTH) {
253 newLength = INITIAL_LENGTH; 254 newLength = INITIAL_LENGTH;
254 } 255 }
255 return _createBuffer(newLength); 256 return _createBuffer(newLength);
256 } 257 }
257 258
258 void _grow() { 259 /// Grows the buffer.
259 _buffer = _createBiggerBuffer(null)..setRange(0, _length, _buffer); 260 ///
261 /// This copies the first [length] elements into the new buffer.
262 void _grow(int length) {
263 _buffer = _createBiggerBuffer(null)..setRange(0, length, _buffer);
260 } 264 }
261 265
262 void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { 266 void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) {
263 if (end > _length) throw new RangeError.range(end, 0, _length); 267 if (end > _length) throw new RangeError.range(end, 0, _length);
264 _setRange(start, end, source, skipCount); 268 _setRange(start, end, source, skipCount);
265 } 269 }
266 270
267 /// Like [setRange], but with no bounds checking. 271 /// Like [setRange], but with no bounds checking.
268 void _setRange(int start, int end, Iterable<E> source, int skipCount) { 272 void _setRange(int start, int end, Iterable<E> source, int skipCount) {
269 if (source is _TypedDataBuffer<E>) { 273 if (source is _TypedDataBuffer<E>) {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 Int32x4 get _defaultValue => _zero; 378 Int32x4 get _defaultValue => _zero;
375 Int32x4List _createBuffer(int size) => new Int32x4List(size); 379 Int32x4List _createBuffer(int size) => new Int32x4List(size);
376 } 380 }
377 381
378 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { 382 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> {
379 Float32x4Buffer([int initialLength = 0]) 383 Float32x4Buffer([int initialLength = 0])
380 : super(new Float32x4List(initialLength)); 384 : super(new Float32x4List(initialLength));
381 Float32x4 get _defaultValue => new Float32x4.zero(); 385 Float32x4 get _defaultValue => new Float32x4.zero();
382 Float32x4List _createBuffer(int size) => new Float32x4List(size); 386 Float32x4List _createBuffer(int size) => new Float32x4List(size);
383 } 387 }
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698