Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Gap buffer
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Data structure used in text editors}} {{refimprove|date=April 2012}} A '''gap buffer''' in [[computer science]] is a [[dynamic array]] that allows efficient insertion and deletion operations clustered near the same location. Gap buffers are especially common in [[text editor]]s, where most changes to the text occur at or near the current location of the [[cursor (computers)|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 deletes it. Text in a gap buffer is represented as two [[string (computer science)|strings]], which take very little extra space and which can be searched and displayed very quickly, compared to more sophisticated [[data structure]]s such as [[linked list]]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 analysis|amortized]] over the more common cheap operations. This makes the gap buffer a simpler alternative to the [[rope (data structure)|rope]] for use in text editors<ref name="chucarroll">Mark C. Chu-Carroll. "[http://scienceblogs.com/goodmath/2009/02/18/gap-buffers-or-why-bother-with-1/ Gap Buffers, or, Donβt Get Tied Up With Ropes?]" ''ScienceBlogs'', 2009-02-18. Accessed 2013-01-30.</ref> such as [[Emacs]].<ref name="elisp">[https://www.gnu.org/software/emacs/manual/html_node/elisp/Buffer-Gap.html emacs gap buffer info] Accessed 2013-01-30.</ref> ==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 (computer programming)|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. 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 gap: This is the way the world as we know it [ ]started out. ==See also== * [[Dynamic array]], 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]] * [[Circular buffer]] * [[Rope (computer science)]] * [[Piece table]] - data structure used by Bravo and Microsoft Word == References == {{reflist}} ==External links== * [https://github.com/mkohlhaas/Pointers-C-Programming/blob/main/09_Dynamic_Memory_Management/9_7_gapbuf.c Implementation in C] * [http://www.codeproject.com/KB/recipes/GenericGapBuffer.aspx Overview and implementation in .NET/C#] * [https://github.com/lazyhacker/gapbuffer Brief overview and sample C++ code] * [http://www.codeproject.com/KB/recipes/SplitArrayDictionary.aspx 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) * [https://www.gnu.org/software/emacs/manual/html_node/elisp/Buffer-Gap.html emacs gap buffer info] (Emacs gap buffer reference) * [http://www.common-lisp.net/project/flexichain/download/StrandhVilleneuveMoore.pdf Flexichain: An editable sequence and its gap-buffer implementation] * [https://coredumped.dev/2023/08/09/text-showdown-gap-buffers-vs-ropes/ Text showdown: Gap Buffers vs Ropes] [[Category:Arrays]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Refimprove
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)