Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
# UnificationTable
# disjoint-set

## Introduction
The disjoint-set, also known as union-find, is a data structure that efficiently supports set union and find operations. It is widely used in various fields such as graph algorithms, type inference, and equivalence class management, and plays a key role in compiler implementation (e.g., for type unification and region analysis).

## Features

- Implements the classic union-find algorithm with path compression and union by rank.
- Supports set initialization, union, and finding the representative element (find).
- Supports dynamic elements (no need to pre-allocate the total number of elements).
- Optionally supports persistent (versioned) structures or undo operations.
- Optionally implements a "value merging" feature (e.g., for unifying types).
- Designed with a clean API for easy use in scenarios like compiler implementation, graph algorithms, and region analysis.

## Example
### UnificationTable

> **Note:** Snapshot operations should follow a stack-like (LIFO) order.
> A root snapshot is required for snapshot operations.
Expand All @@ -7,7 +22,7 @@
```moonbit
///|
test "UnificationTable::union_test" {
let table = new(capacity=5)
let table = UnificationTable::new(capacity=5)
for i in 0..<5 {
table.push(i)
}
Expand All @@ -23,12 +38,12 @@ test "UnificationTable::union_test" {
}
```

# Snapshot
### Snapshot

```moonbit
///|
test "UnificationTable::snapshot_unite_test" {
let table = new(capacity=5)
let table = UnificationTable::new(capacity=5)
let _ = table.start_snapshot()
for i in 0..<5 {
table.push(i)
Expand All @@ -47,4 +62,7 @@ test "UnificationTable::snapshot_unite_test" {
assert_false!(table.unioned(index1, index3))
assert_false!(table.unioned(index2, index1))
}
```
```

# Reference
[ena](https://github.com/rust-lang/ena)
Loading