removeN method

  1. @override
void removeN(
  1. int idx,
  2. int count
)
override

Removes count elements starting at idx.

Implementation

@override
void removeN(int idx, int count) {
  if (count > 0) {
    _requireBounds(idx);

    final n = length;
    final removals = min(n - idx, count);
    final finalLength = n - removals;
    final suffixStart = idx + removals;

    // If we know we can resize after removing, do it right away using arrayCopy
    // Else, choose the shorter: either move the prefix (0 until idx) right OR the suffix (idx+removals until n) left
    if (_shouldShrink(finalLength)) {
      final array2 = ArrayDeque.alloc<A>(finalLength);
      _copySliceToArray(0, array2, 0, idx);
      _copySliceToArray(suffixStart, array2, idx, n);
      _reset(array2, 0, finalLength);
    } else if (2 * idx <= finalLength) {
      // Cheaper to move the prefix right
      var i = suffixStart - 1;

      while (i >= removals) {
        _set(i, _get(i - removals));
        i -= 1;
      }

      while (i >= 0) {
        _set(i, null);
        i -= 1;
      }

      start = _startPlus(removals);
    } else {
      // Cheaper to move the suffix left
      var i = idx;

      while (i < finalLength) {
        _set(i, _get(i + removals));
        i += 1;
      }

      while (i < n) {
        _set(i, null);
        i += 1;
      }

      end = _endMinus(removals);
    }
  } else if (count < 0) {
    throw ArgumentError("removing negative number of elements: $count");
  }
}