Add DisjointSet and ForestDisjointSet documentation

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 5 years ago
parent de467837f0
commit 1c539f22fe
  1. 5
      util/src/main/java/dev/openrs2/util/collect/DisjointSet.kt
  2. 5
      util/src/main/java/dev/openrs2/util/collect/ForestDisjointSet.kt

@ -1,5 +1,10 @@
package dev.openrs2.util.collect 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 DisjointSet<T> : Iterable<DisjointSet.Partition<T>> {
interface Partition<T> : Iterable<T> interface Partition<T> : Iterable<T>

@ -2,6 +2,11 @@ package dev.openrs2.util.collect
import java.util.ArrayDeque 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> { class ForestDisjointSet<T> : DisjointSet<T> {
private class Node<T>(val value: T) : DisjointSet.Partition<T> { private class Node<T>(val value: T) : DisjointSet.Partition<T> {
val children = mutableListOf<Node<T>>() val children = mutableListOf<Node<T>>()

Loading…
Cancel
Save