Gap Buffer is a data structure used for editing and storing text in an efficient manner that is being currently edited. It is also similar to an array but a gap is introduced in the array for handling multiple changes at the cursor. Let’s assume a gap to be another array which contains empty spaces.
Example: Consider an example with initial gap size 10, initially, array or gap are of the same size, as we insert the elements in the array similarly elements will be inserted in the gap buffer, the only difference is gap size reduces on each insert.
This was the basic case to insert the character in the front. Now, whenever there is need to insert a character at certain position we will just move the gap up-to that position using left() and right() then try to insert the character.
Need for Gap Buffer
- Array is a data structure which stores items at contiguous memory location. However, it takes O(1) insertion at end of the array while O(n) time in front because the array will be shifted n places right, n being the length of the array.
- When it comes to text editors we need a faster data structure for insertion, modification as there are multiple changes at the cursor positions.
- In worst case array will take O(n) time for insertion or modification as shown in the example below.
- For inserting ‘GEEKS’ at the front, space is made for inserting each character by shifting the array.
Basic operations in Gap Buffer
Gap Buffer vs Ropes
Now, although its insertion is taking O(1) time but there is another function grow() which which takes approximately O(n) time. So there might a thought that this may take the same time as the rope data structures but the cost of grow is being compensated by amortized cost of other cheaper procedures such as left(), right() and insert(). Therefore this data structure get preferences in text-editors over other such as rope as it is easy to implement.
Implementing Gap Buffer
Initializing the gap buffer with size 10 _ _ _ _ _ _ _ _ _ _ Inserting a string to buffer: GEEKSGEEKS Output: G E E K S G E E K S _ _ _ _ _ _ _ _ _ _ Inserting a string to buffer: FOR Output: G E E K S F O R _ _ _ _ _ _ _ G E E K S
Time complexity of insert: O(1)
Time complexity of grow: O(n)
- Design an efficient data structure for given operations
- Data Structure for Dictionary and Spell Checker?
- Design a data structure that supports insert, delete, search and getRandom in constant time
- Overview of Data Structures | Set 3 (Graph, Trie, Segment Tree and Suffix Tree)
- Persistent data structures
- Disjoint Set Data Structures
- Ropes Data Structure (Fast String Concatenation)
- Burrows - Wheeler Data Transform Algorithm
- Advantages of Trie Data Structure
- Tango Tree Data Structure
- Dynamic Disjoint Set Data Structure for large range values
- Trie Data Structure using smart pointer and OOP in C++
- Gap Buffer | Deletion Operation
- Need for Abstract Data Type and ADT Model
- Design a Queue data structure to get minimum or maximum in O(1) time
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.