Signed-off-by: Graham <gpe@openrs2.dev>
@ -1,5 +1,10 @@
package dev.openrs2.util.collect
/**
* A data structure containing a set of elements partitioned into a number of
* non-overlapping subsets. New elements belong to singleton subsets. The
* [union] function combines two subsets together into a single larger subset.
*/
interface DisjointSet<T> : Iterable<DisjointSet.Partition<T>> {
interface Partition<T> : Iterable<T>
@ -2,6 +2,11 @@ package dev.openrs2.util.collect
import java.util.ArrayDeque
* A [DisjointSet] implementation backed by a disjoint-set forest, as described
* in chapter 21.3 of the third edition of CLRS. It uses path compression and
* union by rank.
class ForestDisjointSet<T> : DisjointSet<T> {
private class Node<T>(val value: T) : DisjointSet.Partition<T> {
val children = mutableListOf<Node<T>>()