Apex › Storage & databases

The gap buffer — how Emacs edits text

The whole document lives in one array with a run of empty slots — the gap — parked at the cursor. Typing drops a character into the gap in O(1), no shifting. Backspace and delete-forward just grow the gap. The only costly move is relocating the cursor, which slides the gap there by copying the characters it passes over (O(distance)). It bets that edits cluster at one point — and usually wins. Type, delete, and drag the cursor to watch the gap move.

More in Storage & databases