insertAllKeys method

void insertAllKeys(
  1. int index,
  2. List<TKey> keys
)

Inserts the nids of keys at visible position index, preserving their relative order. Each key must be registered.

Implementation

void insertAllKeys(int index, List<TKey> keys) {
  final n = keys.length;
  if (n == 0) {
    return;
  }
  _ensureOrderCapacity(_len + n);
  // memmove: shift [index, _len) to [index + n, _len + n).
  _orderNids.setRange(index + n, _len + n, _orderNids, index);
  for (int i = 0; i < n; i++) {
    final nid = _nids[keys[i]]!;
    _orderNids[index + i] = nid;
  }
  _len += n;
  if (!_suppress) {
    // L25.7: a run of siblings shares its whole ancestor chain, so bump
    // each inserted slot by one and the chain above the common parent
    // ONCE by n, instead of walking the chain n times. A run with no
    // shared parent (roots) or of mixed depth (an expand re-inserting a
    // nested visible subtree) keeps the per-nid walk, which the
    // subtree-size invariant needs there.
    final firstParent = _parentByNidLookup(_orderNids[index]);
    bool sameParent = firstParent != kNoParentNid;
    for (int i = 1; sameParent && i < n; i++) {
      if (_parentByNidLookup(_orderNids[index + i]) != firstParent) {
        sameParent = false;
      }
    }
    if (sameParent) {
      debugInsertSharedChainCount++;
      for (int i = 0; i < n; i++) {
        _subtreeSizeByNid[_orderNids[index + i]] += 1;
      }
      bumpFromSelf(firstParent, n);
    } else {
      debugInsertPerNidCount++;
      for (int i = 0; i < n; i++) {
        bumpFromSelf(_orderNids[index + i], 1);
      }
    }
  }
  _onMutated();
}