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

Unified Diff: lib/typed_buffers.dart

Issue 1404443005: Add _TypedDataBuffer.addRange. (Closed) Base URL: git@github.com:dart-lang/typed_data@master
Patch Set: Code review changes Created 5 years, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | test/typed_buffers_test.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/typed_buffers.dart
diff --git a/lib/typed_buffers.dart b/lib/typed_buffers.dart
index 50ed2414c4cbd58d196a720e9706ecf31338a18e..ca9d4ab72fed4b465dca35af84fadd0163754860 100644
--- a/lib/typed_buffers.dart
+++ b/lib/typed_buffers.dart
@@ -65,13 +65,29 @@ 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);
+ /// This adds values from [start] (inclusive) to [end] (exclusive) in
+ /// [values]. [start] defaults to `0` and [end] defaults to `values.length`.
+ void addAll(Iterable<E> values, [int start, int end]) {
Lasse Reichstein Nielsen 2015/10/15 10:58:25 Use [int start = 0, int end] like other range para
nweiz 2015/10/15 20:14:10 Done for consistency, although in general default
+ start ??= 0;
+ if (start == 0 && end == null) {
+ // `ListBase.insertAll` bottoms out on `setRange`, which has good handling
+ // of lists, typed buffers, and typed data lists.
+ insertAll(_length, values);
Lasse Reichstein Nielsen 2015/10/15 10:58:25 You could also update insertAll to also take start
nweiz 2015/10/15 20:14:10 Done.
+ return;
+ }
+
+ var list = values is List ? values : values.toList(growable: false);
Lasse Reichstein Nielsen 2015/10/15 10:58:25 I still don't want to convert the iterable to a li
nweiz 2015/10/15 20:14:10 Done.
+ end = RangeError.checkValidRange(start, end, list.length);
Lasse Reichstein Nielsen 2015/10/15 10:58:25 That requires the end to be inside the iterable. W
nweiz 2015/10/15 20:14:10 List.getRange doesn't allow too large a start or e
Lasse Reichstein Nielsen 2015/10/20 08:09:49 Generally we check the limits on lists and not on
nweiz 2015/10/20 21:24:39 Wouldn't this be a breaking change? The error is d
floitsch 2015/10/20 22:30:46 Since one can leave "end" empty I prefer to check,
+
+ var newLength = _length + end - start;
+ _growBuffer(newLength);
Lasse Reichstein Nielsen 2015/10/15 10:58:25 I prefer _ensureCapacity. The function doesn't act
nweiz 2015/10/15 20:14:10 Done.
+ _setRange(_length, newLength, list, start);
+ _length = newLength;
}
void insert(int index, E element) {
@@ -92,6 +108,15 @@ abstract class _TypedDataBuffer<E> extends ListBase<E> {
_buffer = newBuffer;
}
+ /// Grows [_buffer] so that it's at least [requiredLength] long, preserving
+ /// the existing data.
Lasse Reichstein Nielsen 2015/10/15 10:58:25 /// Ensures that [_buffer] is at least [requiredCa
nweiz 2015/10/15 20:14:10 Done.
+ void _growBuffer(int requiredLength) {
+ if (requiredLength < _buffer.length) return;
+ var newBuffer = _createBiggerBuffer(null);
+ newBuffer.setRange(0, _length, _buffer);
+ _buffer = newBuffer;
+ }
+
/**
* Create a bigger buffer.
*
@@ -116,6 +141,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 {
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | test/typed_buffers_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698