https://github.com/naotsugu/piecetable
Java implementation of PieceTable data structure.
https://github.com/naotsugu/piecetable
algorithms java
Last synced: 5 months ago
JSON representation
Java implementation of PieceTable data structure.
- Host: GitHub
- URL: https://github.com/naotsugu/piecetable
- Owner: naotsugu
- License: apache-2.0
- Created: 2022-09-12T11:46:59.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2026-01-25T06:24:01.000Z (5 months ago)
- Last Synced: 2026-01-25T21:15:36.690Z (5 months ago)
- Topics: algorithms, java
- Language: Java
- Homepage:
- Size: 5.06 MB
- Stars: 18
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# piecetable
[](https://github.com/naotsugu/jpa-fluent-query/actions/workflows/gradle-build.yml)
Piece table data structure in Java.
Piece table is a data structure typically used to represent a text document editing.
## Operations of piece table
### Initial state
A piece table consists of three columns:
* Which buffer
* Start index in the buffer
* Length in the buffer
In addition to the table, two buffers are used to handle edits:
* `read-only` : A reference to the original text document
* `append-only` : A buffer to a temporary file

### Insert
Inserting characters to the text consists of:
* Appending characters to the `read-only` buffer, and
* Updating the entry in piece table (breaking an entry into two or three)

### Delete
Single character deletion can be one of two possible conditions:
* The deletion is at the start or end of a piece entry, in which case the appropriate entry in piece table is modified.
* The deletion is in the middle of a piece entry, in which case the entry is split then one of the successor entries is modified as above.

## Usage
Add dependencies.
```kotlin
dependencies {
implementation("com.mammb:piecetable:0.6.3")
}
```
There are three classes.
- PieceTable - Primitive piece table implantation by bytes
- Document - Text manipulation with row-column number and charset infer
- TextEdit - Advanced text editing abstraction, with undo/redo, and more
Use `TextEdit` in most cases.
### PieceTable
```java
var pt = PieceTable.of();
pt.insert(0, "a large text".getBytes());
pt.insert(8, "span of ".getBytes());
pt.delete(1, 6);
var bytes = pt.get(0, (int) pt.length());
assertEquals("a span of text", new String(bytes));
```
### Document
```java
var doc = Document.of();
doc.insert(0, 0, "a large text"); // row:0 col:0
doc.insert(0, 8, "span of "); // row:0 col:8
doc.delete(0, 1, 6); // row:0 col:1
var text = doc.getText(0); // get the text of row:0
assertEquals("a span of text", text);
```
To load an existing file, use the following
```java
var path = Path.of("sample.txt");
var doc = Document.of(path);
```
The charset is inferred from the loaded file, but can also be specified explicitly.
```java
var doc = Document.of(path, Charset.forName("xxx"));
```
`CharsetMatch` can also be used to customize charset infer.
```java
CharsetMatch customCharsetMatch = // ...
var doc = Document.of(path, customCharsetMatch);
```
### TextEdit
```java
var edit = TextEdit.of();
edit.insert(0, 0, "a large text");
edit.insert(0, 8, "span of ");
edit.delete(0, 1, 6);
assertEquals("a span of text", edit.getText(0));
edit.undo();
assertEquals("a large span of text", edit.getText(0));
edit.redo();
assertEquals("a span of text", edit.getText(0));
```
## How to run the example application
```shell
./gradlew build
cd examples/fx-editor
./gradlew run
```

This example is merely a simple demonstration. For more practical applications, please refer to the following.
## Sample applications using this library
* [min-editor](https://github.com/naotsugu/min-editor)