Gap buffer

A gap buffer in computer science is a dynamic array that allows efficient insertion and deletion operations clustered near the same location.

Text in a gap buffer is represented as two strings, which take very little extra space and which can be searched and displayed very quickly, compared to more sophisticated data structures such as linked lists.

The use of gap buffers is based on the assumption that such recopying occurs rarely enough that its cost can be amortized over the more common cheap operations.

This makes the gap buffer a simpler alternative to the rope for use in text editors[1] such as Emacs.

This representation is a bit misleading: in a typical implementation, the endpoints of the gap are tracked using pointers or array indices, and the contents of the gap are ignored; this allows, for example, deletions to be done by adjusting a pointer without changing the text in the buffer.