 Chromium Code Reviews
 Chromium Code Reviews Issue 1404443005:
  Add _TypedDataBuffer.addRange.  (Closed) 
  Base URL: git@github.com:dart-lang/typed_data@master
    
  
    Issue 1404443005:
  Add _TypedDataBuffer.addRange.  (Closed) 
  Base URL: git@github.com:dart-lang/typed_data@master| Index: lib/typed_buffers.dart | 
| diff --git a/lib/typed_buffers.dart b/lib/typed_buffers.dart | 
| index 50ed2414c4cbd58d196a720e9706ecf31338a18e..84bf5fac6871badf7a2fdbb70fa9e994060952c0 100644 | 
| --- a/lib/typed_buffers.dart | 
| +++ b/lib/typed_buffers.dart | 
| @@ -65,13 +65,109 @@ abstract class _TypedDataBuffer<E> extends ListBase<E> { | 
| _buffer[_length++] = value; | 
| } | 
| - // We override the default implementation of `add` and `addAll` because | 
| - // they grow by setting the length in increments of one. We want to grow | 
| - // by doubling capacity in most cases. | 
| + // We override the default implementation of `add` because it grows the list | 
| + // by setting the length in increments of one. We want to grow by doubling | 
| + // capacity in most cases. | 
| void add(E value) { _add(value); } | 
| - void addAll(Iterable<E> values) { | 
| - for (E value in values) _add(value); | 
| + /// Appends all objects of [values] to the end of this buffer. | 
| + /// | 
| + /// This adds values from [start] (inclusive) to [end] (exclusive) in | 
| + /// [values]. If [end] is omitted, it defaults to adding all elements of | 
| + /// [values] after [start]. | 
| + /// | 
| + /// The [start] value must be non-negative. The [values] iterable must have at | 
| + /// least [start] elements, and if [end] is specified, it must be greater than | 
| + /// or equal to [start] and [values] must have at least [end] elements. | 
| + void addAll(Iterable<E> values, [int start = 0, int end]) { | 
| + _addAll(values, start, end); | 
| + } | 
| + | 
| + /// Inserts all objects of [values] at position [index] in this list. | 
| + /// | 
| + /// This adds values from [start] (inclusive) to [end] (exclusive) in | 
| + /// [values]. If [end] is omitted, it defaults to adding all elements of | 
| + /// [values] after [start]. | 
| + /// | 
| + /// The [start] value must be non-negative. The [values] iterable must have at | 
| + /// least [start] elements, and if [end] is specified, it must be greater than | 
| + /// or equal to [start] and [values] must have at least [end] elements. | 
| + void insertAll(int index, Iterable<E> values, [int start = 0, int end]) { | 
| + 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
 | 
| + RangeError.checkNotNegative(start, "start"); | 
| + if (end != null && start > end) { | 
| + throw new RangeError.range(end, start, null, "end"); | 
| + } | 
| + | 
| + // If we're adding to the end of the list anyway, use [_addAll]. This lets | 
| + // us avoid converting [values] into a list even if [end] is null, since we | 
| + // can add values iteratively to the end of the list. We can't do so in the | 
| + // 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
 | 
| + if (index == _length) { | 
| + _addAll(values, start, end); | 
| + return; | 
| + } | 
| + | 
| + // If we don't know how much room to make for [values], convert it to a list | 
| + // so we can tell. | 
| + if (end == null && values is! List) { | 
| + 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
 | 
| + } | 
| + | 
| + _insertKnownLength(index, values, start, end); | 
| + } | 
| + | 
| + /// Does the same thing as [addAll]. | 
| + /// | 
| + /// This allows [addAll] and [insertAll] to share implementation without a | 
| + /// subclass unexpectedly overriding both when it intended to only override | 
| + /// [addAll]. | 
| + void _addAll(Iterable<E> values, [int start = 0, int end]) { | 
| + 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.
 | 
| + if (end != null && start > end) { | 
| + throw new RangeError.range(end, start, null, "end"); | 
| + } | 
| + | 
| + // If we can efficiently compute the length of the segment to add, do so | 
| + // with [addRange]. This can be more efficient for lists and potentially | 
| + // 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.
 | 
| + if (end != null || values is List) { | 
| + _insertKnownLength(_length, values, start, end); | 
| + return; | 
| + } | 
| + | 
| + // Otherwise, just add values one at a time. | 
| + var i = 0; | 
| + for (var value in values) { | 
| + if (i >= start) add(value); | 
| + i++; | 
| + } | 
| + if (i < start) throw new StateError("Too few elements"); | 
| + } | 
| + | 
| + /// Like [insertAll], but assumes that the length of the slice of [values] is | 
| + /// known (or easy to calculate). | 
| + /// | 
| + /// Specifically, this assumes that if [values] is not a [List], [end] is not | 
| + /// `null`. | 
| + void _insertKnownLength(int index, Iterable<E> values, int start, int end) { | 
| + if (values is List) { | 
| + end ??= values.length; | 
| + if (start > values.length || end > values.length) { | 
| + throw new StateError("Too few elements"); | 
| + } | 
| + } else { | 
| + assert(end != null); | 
| + } | 
| + | 
| + var valuesLength = end - start; | 
| + var newLength = _length + valuesLength; | 
| + _ensureCapacity(newLength); | 
| + | 
| + _buffer.setRange( | 
| + index + valuesLength, _length + valuesLength, _buffer, index); | 
| + _buffer.setRange(index, index + valuesLength, values, start); | 
| + _length = newLength; | 
| } | 
| void insert(int index, E element) { | 
| @@ -92,18 +188,28 @@ abstract class _TypedDataBuffer<E> extends ListBase<E> { | 
| _buffer = newBuffer; | 
| } | 
| + /// Ensures that [_buffer] is at least [requiredCapacity] long, | 
| + /// | 
| + /// Grows the buffer if necessary, preserving existing data. | 
| + void _ensureCapacity(int requiredCapacity) { | 
| + if (requiredCapacity <= _buffer.length) return; | 
| + var newBuffer = _createBiggerBuffer(requiredCapacity); | 
| + newBuffer.setRange(0, _length, _buffer); | 
| + _buffer = newBuffer; | 
| + } | 
| + | 
| /** | 
| * Create a bigger buffer. | 
| * | 
| * This method determines how much bigger a bigger buffer should | 
| - * be. If [requiredLength] is not null, it will be at least that | 
| + * be. If [requiredCapacity] is not null, it will be at least that | 
| * size. It will always have at least have double the capacity of | 
| * the current buffer. | 
| */ | 
| - List<E> _createBiggerBuffer(int requiredLength) { | 
| + List<E> _createBiggerBuffer(int requiredCapacity) { | 
| int newLength = _buffer.length * 2; | 
| - if (requiredLength != null && newLength < requiredLength) { | 
| - newLength = requiredLength; | 
| + if (requiredCapacity != null && newLength < requiredCapacity) { | 
| + newLength = requiredCapacity; | 
| } else if (newLength < INITIAL_LENGTH) { | 
| newLength = INITIAL_LENGTH; | 
| } | 
| @@ -116,6 +222,11 @@ abstract class _TypedDataBuffer<E> extends ListBase<E> { | 
| void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) { | 
| if (end > _length) throw new RangeError.range(end, 0, _length); | 
| + _setRange(start, end, source, skipCount); | 
| + } | 
| + | 
| + /// Like [setRange], but with no bounds checking. | 
| + void _setRange(int start, int end, Iterable<E> source, int skipCount) { | 
| if (source is _TypedDataBuffer<E>) { | 
| _buffer.setRange(start, end, source._buffer, skipCount); | 
| } else { |