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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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` and `addAll` because |
| 69 // they grow by setting the length in increments of one. We want to grow | 69 // they grow by setting the length in increments of one. We want to grow |
| 70 // by doubling capacity in most cases. | 70 // by doubling 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 void addAll(Iterable<E> values) { |
|
Lasse Reichstein Nielsen
2015/10/14 07:09:59
Maybe addAll is a special case of insertAll, and w
Lasse Reichstein Nielsen
2015/10/14 07:10:00
I'd rather have specialized list code and general
nweiz
2015/10/14 20:20:20
Really? This is contrary to what ListBase does in
nweiz
2015/10/14 20:20:20
Done. ListBase.insertAll bottoms out on setRange a
| |
| 74 for (E value in values) _add(value); | 74 var list = values is List ? values : values.toList(); |
| 75 var newLength = _length + list.length; | |
| 76 _createBiggerBuffer(newLength); | |
|
Lasse Reichstein Nielsen
2015/10/14 07:10:00
Only grow the backing store if necessary.
The _cre
nweiz
2015/10/14 20:20:20
Done.
| |
| 77 _setRange(_length, newLength, list); | |
| 78 _length = newLength; | |
| 79 } | |
| 80 | |
| 81 /// Adds a range of values in [source] to the end of the buffer. | |
|
Lasse Reichstein Nielsen
2015/10/14 07:09:59
Comment format is inconsistent with the rest of th
nweiz
2015/10/14 20:20:20
The existing style is already inconsistent—see [_b
| |
| 82 /// | |
| 83 /// This adds values from [sourceStart] (inclusive) to [sourceEnd] | |
| 84 /// (exclusive). If [sourceEnd] is omitted, adds all values after | |
| 85 /// [sourceStart]. | |
| 86 void addRange(Iterable<E> source, int sourceStart, [int sourceEnd]) { | |
|
Lasse Reichstein Nielsen
2015/10/14 07:09:59
Validate sourceStart and sourceEnd:
0 <= sourceS
Lasse Reichstein Nielsen
2015/10/14 07:09:59
Just add the parameters to addAll;
void addAll(I
Lasse Reichstein Nielsen
2015/10/14 07:10:00
Naming-wise, functions named "somethingRange" usua
nweiz
2015/10/14 20:20:20
Acknowledged.
nweiz
2015/10/14 20:20:20
Done.
Do you really prefer "start" and "end" to "
nweiz
2015/10/14 20:20:20
Done.
| |
| 87 var list; | |
| 88 if (source is List) { | |
| 89 list = source; | |
| 90 } else { | |
| 91 if (sourceEnd != null) source = source.take(sourceEnd); | |
| 92 list = source.skip(sourceStart).toList(); | |
|
Lasse Reichstein Nielsen
2015/10/14 07:10:00
This looks wrong - you create a list of only the e
nweiz
2015/10/14 20:20:20
Done.
| |
| 93 } | |
| 94 | |
| 95 sourceEnd ??= list.length; | |
| 96 var newLength = _length + sourceEnd - sourceStart; | |
| 97 _createBiggerBuffer(newLength); | |
| 98 _setRange(_length, newLength, list, sourceStart); | |
| 99 _length = newLength; | |
| 75 } | 100 } |
| 76 | 101 |
| 77 void insert(int index, E element) { | 102 void insert(int index, E element) { |
| 78 if (index < 0 || index > _length) { | 103 if (index < 0 || index > _length) { |
| 79 throw new RangeError.range(index, 0, _length); | 104 throw new RangeError.range(index, 0, _length); |
| 80 } | 105 } |
| 81 if (_length < _buffer.length) { | 106 if (_length < _buffer.length) { |
| 82 _buffer.setRange(index + 1, _length + 1, _buffer, index); | 107 _buffer.setRange(index + 1, _length + 1, _buffer, index); |
| 83 _buffer[index] = element; | 108 _buffer[index] = element; |
| 84 _length++; | 109 _length++; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 109 } | 134 } |
| 110 return _createBuffer(newLength); | 135 return _createBuffer(newLength); |
| 111 } | 136 } |
| 112 | 137 |
| 113 void _grow() { | 138 void _grow() { |
| 114 _buffer = _createBiggerBuffer(null)..setRange(0, _length, _buffer); | 139 _buffer = _createBiggerBuffer(null)..setRange(0, _length, _buffer); |
| 115 } | 140 } |
| 116 | 141 |
| 117 void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { | 142 void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { |
| 118 if (end > _length) throw new RangeError.range(end, 0, _length); | 143 if (end > _length) throw new RangeError.range(end, 0, _length); |
| 144 _setRange(start, end, source._buffer, skipCount); | |
| 145 } | |
| 146 | |
| 147 /// Like [setRange], but with no bounds checking. | |
| 148 void _setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { | |
|
Lasse Reichstein Nielsen
2015/10/14 07:10:00
Make skipCount not optional here.
I generally don
nweiz
2015/10/14 20:20:20
Done.
| |
| 119 if (source is _TypedDataBuffer<E>) { | 149 if (source is _TypedDataBuffer<E>) { |
| 120 _buffer.setRange(start, end, source._buffer, skipCount); | 150 _buffer.setRange(start, end, source._buffer, skipCount); |
| 121 } else { | 151 } else { |
| 122 _buffer.setRange(start, end, source, skipCount); | 152 _buffer.setRange(start, end, source, skipCount); |
| 123 } | 153 } |
| 124 } | 154 } |
| 125 | 155 |
| 126 // TypedData. | 156 // TypedData. |
| 127 | 157 |
| 128 int get elementSizeInBytes => _buffer.elementSizeInBytes; | 158 int get elementSizeInBytes => _buffer.elementSizeInBytes; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 226 Int32x4 get _defaultValue => _zero; | 256 Int32x4 get _defaultValue => _zero; |
| 227 Int32x4List _createBuffer(int size) => new Int32x4List(size); | 257 Int32x4List _createBuffer(int size) => new Int32x4List(size); |
| 228 } | 258 } |
| 229 | 259 |
| 230 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { | 260 class Float32x4Buffer extends _TypedDataBuffer<Float32x4> { |
| 231 Float32x4Buffer([int initialLength = 0]) | 261 Float32x4Buffer([int initialLength = 0]) |
| 232 : super(new Float32x4List(initialLength)); | 262 : super(new Float32x4List(initialLength)); |
| 233 Float32x4 get _defaultValue => new Float32x4.zero(); | 263 Float32x4 get _defaultValue => new Float32x4.zero(); |
| 234 Float32x4List _createBuffer(int size) => new Float32x4List(size); | 264 Float32x4List _createBuffer(int size) => new Float32x4List(size); |
| 235 } | 265 } |
| OLD | NEW |