- Gap buffer
In
computer science , a gap buffer is adynamic array that allows efficient insertion and deletion operations clustered near the same location. Gap buffers are especially common intext editor s, where most changes to the text occur at or near the current location of the cursor. The text is stored in a large buffer in two contiguous segments, with a gap between them for inserting new text. Moving the cursor involves copying text from one side of the gap to the other (sometimes copying is delayed until the next operation that changes the text). Insertion adds new text at the end of the first segment. Deletion increases the size of the gap.The advantage of using a gap buffer over more sophisticated
data structure s (such aslinked list s) is that the text is represented simply as twoliteral string s, which take very little extra space and which can be searched and displayed very quickly.The disadvantage is that operations at different locations in the text and ones that fill the gap (requiring a new gap to be created) require re-copying most of the text, which is especially inefficient for large files. 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.
A gap buffer is used in most
Emacs editors.Example
Below are some examples of operations with buffer gaps. The gap is represented pictorially by the empty space between the square brackets. This representation is a bit misleading: in a typical implementation, the endpoints of the gap are tracked using
pointer s 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. It is a common programming practice to use a semi-open interval for the gap pointers, i.e. the start-of-gap points to the invalid character following the last character in the first buffer, and the end-of-gap points to the first valid character in the second buffer.Initial state:
This is the way [ ] out.
User inserts some new text:
This is the way the world started [ ] out.
User moves the cursor before "started"; system moves "started " from the first buffer to the second buffer.
This is the way the world [ ] started out.
User adds text filling the gap; system creates new g
This is the way the world as we know it [ ] started out.
ee also
*
Dynamic array , the special case of a gap buffer where the gap is always at the end
*Linked list
*Circular buffer External references
* [http://www.codeproject.com/csharp/GenericGapBuffer.asp Overview and implementation in .NET/C#]
* [http://www.lazyhacker.com/gapbuffer/gapbuffer.htm Brief overview and sample C++ code]
* [http://www.codeproject.com/useritems/SplitArrayDictionary.asp Implementation of a cyclic sorted gap buffer in .NET/C#]
* [http://history.dcs.ed.ac.uk/archive/apps/ecce/hmd/e915.imp.html Use of gap buffer in early editor.] (First written somewhere between 1969 and 1971)
Wikimedia Foundation. 2010.