Piece table


A Piece Table is a data structure typically used to represent a series of edits on a text document. An initial reference to the whole of the original file is created, with subsequent inserts and deletes being created as combinations of one, two, or three references to sections of either the original document or of the spans associated with earlier inserts.
Typically the text of the original document is held in one immutable block, and the text of each subsequent insert is stored in new immutable blocks. Because even deleted text is still included in the piece table, this makes multi-level or unlimited undo easier to implement with a piece table than with alternative data structures such as a gap buffer.
This data structure was invented by J Strother Moore.

Description

For this description, we use buffer as the immutable block to hold the contents.
A piece table consists of three columns:
In addition to the table, two buffers are used to handle edits:

Index

Definition: Index: return the character at position i
To retrieve the i-th character, the appropriate entry in a piece table is read.
Example
Given the following buffers and piece table:
BufferContent
Original fileipsum sit amet
Add fileLorem deletedtext dolor

WhichStart IndexLength
Add06
Original06
Add185
Original69

To access the i-th character, the appropriate entry in the piece table is looked up.
For instance, to get the value of Index, the 3rd entry of piece table is retrieved. This is because the 3rd entry describes the characters from index 12 to 17. The piece table entry instructs the program to look for the characters in the "add file" buffer, starting at index 18 in that buffer. The relative index in that entry is 3, making the value of Index the character "o".
For the buffers and piece table given above, the following text is shown:
Lorem ipsum dolor sit amet
The buffer implies such a sequence of action:
start: ipsum sit amet
insert: Lorem ipsum sit amet
insertion of " deletedtext " somewhere
"deletedtext" is removed
insert: Lorem ipsum dolor sit amet

Insert

Inserting characters to the text consists of:
Deletion involves only modifying the appropriate entry in piece table.

Time complexity

Although a piece table itself is a data structure, it does not state how it should be implemented. The time complexity of operations depends on how the table is being implemented. One possible implementation of a piece table is in a splay tree, because it provides quick access to recently-accessed elements.

Usage

Several text editors use an in-RAM piece table internally, including Bravo,Abiword, Atom and Visual Studio Code.
The "fast save" feature in some versions of Microsoft Word uses a piece table for the on-disk file format.
The on-disk representation of text files in the Oberon System uses a piece chain technique that allows pieces of one document to point to text stored in some other document, similar to transclusion.