| Crates.io | indices_union_find |
| lib.rs | indices_union_find |
| version | 0.1.2 |
| created_at | 2025-07-18 07:57:17.180494+00 |
| updated_at | 2025-11-28 08:17:26.309742+00 |
| description | An efficient and minimal union-find (disjoint-set) implementation over generic unsigned index types. |
| homepage | |
| repository | https://github.com/enjuichang123/indices_union_find.git |
| max_upload_size | |
| id | 1758715 |
| size | 91,971 |
indices_union_findAn efficient and minimal union-find (disjoint-set) implementation over generic unsigned index types.
This crate provides:
UnionFind
Partition
Both structures operate over any unsigned integer type implementing the UnsignedIndex trait (u8, u16, u32, …).
The UnionFind
Internally, the structure stores:
parent: Vec
size: Vec
Two standard optimizations ensure efficiency:
Path compression — flattens trees during find, reducing lookup cost.
Union by size — keeps trees shallow by attaching the smaller subtree to the larger.
Together, these yield effectively constant-time find and union, even for large index sets.
The companion type, Partition
/// An ordered set of boundary nodes defining alternating interval membership.
///
/// Each interval [aᵢ, aᵢ₊₁) is considered filled if i is even,
/// and empty if i is odd.
///
/// The boundaries must be strictly ordered and deduplicated; any
/// overlapping or unordered input must be resolved during construction.
pub boundaries: BTreeSet
This representation enables:
efficient membership tests,
fast iteration over contiguous ranges,
compact storage for sparse or clustered sets,
linear-time set algebra (union, intersection, difference, symmetric difference).
The structure can be constructed from common unordered index collections such as:
BTreeSet
HashSet
key sets of BTreeMap<T, _> or HashMap<T, _>
The constructor automatically merges consecutive indices into minimal contiguous intervals. Even in worst-case alternating patterns, the number of stored boundaries is at most one greater than the number of filled indices.
This interval-based design is well-suited for applications where many indices share the same value or state. Grouping indices into contiguous classes prevents redundant work and lets algorithms operate once per interval instead of once per element.
This project is licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you shall be licensed as above, without any additional terms or conditions.