insert method

Dot insert(
  1. int i,
  2. T value,
  3. Dot dot
)

Insert value at visible index i with identity dot. Follows Algorithm 1; coalesces into an existing block when dot continues a run.

The new element always lands at visible index i, so the memoised projection is spliced in place rather than rebuilt: an append is O(1) amortised, a middle insert is an O(N) list shift (no re-traversal). Returns the start dot of the block that now holds the new element (the coalesced block's start, or the fresh block's own dot).

Implementation

Dot insert(int i, T value, Dot dot) {
  final vis = _visibleElems(); // === _visibleCache
  final ii = i < 0 ? 0 : (i > vis.length ? vis.length : i);
  final leftOrigin = ii == 0 ? Dot.origin : vis[ii - 1].dot;
  final (start, elem) = _place(leftOrigin, value, dot);
  vis.insert(ii, elem);
  return start;
}