insertAll method

  1. @override
void insertAll(
  1. int index,
  2. Iterable<int> values, [
  3. int start = 0,
  4. int? end,
])
inherited

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.

Implementation

@override
void insertAll(int index, Iterable<E> values, [int start = 0, int? end]) {
  RangeError.checkValidIndex(index, this, 'index', _length + 1);
  RangeError.checkNotNegative(start, 'start');
  if (end != null) {
    if (start > end) {
      throw RangeError.range(end, start, null, 'end');
    }
    if (start == end) return;
  }

  // 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.
  if (index == _length) {
    _addAll(values, start, end);
    return;
  }

  if (end == null && values is List) {
    end = values.length;
  }
  if (end != null) {
    _insertKnownLength(index, values, start, end);
    return;
  }

  // Add elements at end, growing as appropriate, then put them back at
  // position [index] using flip-by-double-reverse.
  var writeIndex = _length;
  var skipCount = start;
  for (var value in values) {
    if (skipCount > 0) {
      skipCount--;
      continue;
    }
    if (writeIndex == _buffer.length) {
      _grow(writeIndex);
    }
    _buffer[writeIndex++] = value;
  }

  if (skipCount > 0) {
    throw StateError('Too few elements');
  }
  if (end != null && writeIndex < end) {
    throw RangeError.range(end, start, writeIndex, 'end');
  }

  // Swap [index.._length) and [_length..writeIndex) by double-reversing.
  _reverse(_buffer, index, _length);
  _reverse(_buffer, _length, writeIndex);
  _reverse(_buffer, index, writeIndex);
  _length = writeIndex;
  return;
}