A gap buffer in
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includin ...
is a
dynamic array
In computer science, a dynamic array, growable array, resizable array, dynamic table, mutable array, or array list is a random access, variable-size list data structure that allows elements to be added or removed. It is supplied with standard lib ...
that allows efficient insertion and deletion operations clustered near the same location. Gap buffers are especially common in
text editor
A text editor is a type of computer program that edits plain text. Such programs are sometimes known as "notepad" software (e.g. Windows Notepad). Text editors are provided with operating systems and software development packages, and can be u ...
s, where most changes to the text occur at or near the current location of the
cursor
Cursor may refer to:
* Cursor (user interface), an indicator used to show the current position for user interaction on a computer monitor or other display device
* Cursor (databases), a control structure that enables traversal over the records i ...
. 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 deletes it.
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 structure
In computer science, a data structure is a data organization, management, and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the rel ...
s such as
linked list
In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes whi ...
s. However, operations at different locations in the text and ones that fill the gap (requiring a new gap to be created) may require 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
In computer science, amortized analysis is a method for Analysis of algorithms, analyzing a given algorithm's Computational complexity, complexity, or how much of a resource, especially time or memory, it takes to Execution (computing), execute. ...
over the more common cheap operations. This makes the gap buffer a simpler alternative to the
rope
A rope is a group of yarns, plies, fibres, or strands that are twisted or braided together into a larger and stronger form. Ropes have tensile strength and so can be used for dragging and lifting. Rope is thicker and stronger than similarly ...
for use in text editors
[Mark C. Chu-Carroll.]
Gap Buffers, or, Don’t Get Tied Up With Ropes?
''ScienceBlogs'', 2009-02-18. Accessed 2013-01-30. such as
Emacs
Emacs , originally named EMACS (an acronym for "Editor MACroS"), is a family of text editors that are characterized by their extensibility. The manual for the most widely used variant, GNU Emacs, describes it as "the extensible, customizable, ...
.
[emacs gap buffer info](_blank)
Accessed 2013-01-30.
Example
Below are some examples of operations with buffer gaps. The gap is represented 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
Pointer may refer to:
Places
* Pointer, Kentucky
* Pointers, New Jersey
* Pointers Airport, Wasco County, Oregon, United States
* The Pointers, a pair of rocks off Antarctica
People with the name
* Pointer (surname), a surname (including a list ...
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 (or equivalently, the pointers are considered to point "between" characters).
Initial state:
This is the way
ut.
User inserts some new text:
This is the way the world started
ut.
User moves the cursor before "started"; system moves "started " from the first buffer to the second buffer.
This is the way the world
tarted out.
User adds text filling the gap; system creates new gap:
This is the way the world as we know it
tarted out.
See also
*
Dynamic array
In computer science, a dynamic array, growable array, resizable array, dynamic table, mutable array, or array list is a random access, variable-size list data structure that allows elements to be added or removed. It is supplied with standard lib ...
, the special case of a gap buffer where the gap is always at the end
*
Zipper (data structure), conceptually a generalization of the gap buffer.
*
Linked list
In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes whi ...
*
Circular buffer
In computer science, a circular buffer, circular queue, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams. Ther ...
*
Rope (computer science)
*
Piece table In computing, a piece table is a data structure typically used to represent a text document while it is edited in a text editor. Initially a reference (or 'span') to the whole of the original file is created, which represents the as yet unchanged fi ...
- data structure used by Bravo and Microsoft Word
References
{{reflist
External links
Implementation in COverview and implementation in .NET/C#Brief overview and sample C++ codeImplementation of a cyclic sorted gap buffer in .NET/C#(First written somewhere between 1969 and 1971)
Emacs gap buffer reference)
Flexichain: An editable sequence and its gap-buffer implementation
Arrays