Index: lib/typed_buffers.dart |
diff --git a/lib/typed_buffers.dart b/lib/typed_buffers.dart |
index 50ed2414c4cbd58d196a720e9706ecf31338a18e..4839884e210a980d1d3f1139aaeef3ae359e265b 100644 |
--- a/lib/typed_buffers.dart |
+++ b/lib/typed_buffers.dart |
@@ -65,13 +65,67 @@ 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]. |
+ /// |
+ /// Throws a [StateError] if [values] isn't long enough to extract a slice |
+ /// between [start] and [end]. |
+ void addAll(Iterable<E> values, [int start = 0, int end]) { |
+ insertAll(_length, values, start, end); |
Lasse Reichstein Nielsen
2015/10/23 08:00:02
I generally prefer to not define one public API me
nweiz
2015/10/26 23:53:57
Done.
|
+ } |
+ |
+ /// 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]. |
+ /// |
+ /// Throws a [RangeError] if [index] is less than 0 or greater than [length]. |
+ /// Throws a [StateError] if [values] isn't long enough to extract a slice |
Lasse Reichstein Nielsen
2015/10/23 08:00:02
Don't specify the exact errors, just:
The [star
nweiz
2015/10/26 23:53:57
Done, but why? Isn't it good for the user to know
floitsch
2015/10/27 10:26:17
It really matters for exceptions, but not for erro
Lasse Reichstein Nielsen
2015/10/27 10:33:12
What Florian said!
|
+ /// between [start] and [end]. |
+ void insertAll(int index, Iterable<E> values, [int start = 0, int end]) { |
Lasse Reichstein Nielsen
2015/10/23 08:00:02
Maybe split this earlier if [values] is a list:
nweiz
2015/10/26 23:53:57
Done.
|
+ RangeError.checkValidIndex(index, this, "index", _length + 1); |
+ RangeError.checkNotNegative(start, "start"); |
+ if (end != null && start > end) { |
+ throw new RangeError.range(end, start, null, "end"); |
+ } |
+ |
+ // If [values] is an iterable, we can't efficiently get its length. If we |
+ // also don't have [end], we don't know how large to expand [_buffer] so we |
+ // fall back to the default implementation. |
+ if (end == null && values is! List) { |
+ var i = 0; |
+ for (var value in values) { |
+ if (i >= start) insert(index + i - start, value); |
+ i++; |
+ } |
+ if (i < start) throw new StateError("Too few elements"); |
Lasse Reichstein Nielsen
2015/10/23 08:00:02
It's the [values] argument that have too few argum
nweiz
2015/10/26 23:53:57
This is inconsistent with the corresponding error
Lasse Reichstein Nielsen
2015/10/27 10:33:12
So it is.
I'm getting less and less enchanted by t
|
+ return; |
+ } |
+ |
+ if (values is List) { |
+ end ??= values.length; |
+ if (start > values.length || end > values.length) { |
+ throw new StateError("Too few elements"); |
Lasse Reichstein Nielsen
2015/10/23 08:00:02
RangeError.range(end, start, values.length, "end")
|
+ } |
+ } |
+ |
+ 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 +146,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 +180,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 { |