AbstractDisjointSet

com.phasmidsoftware.gryphon.adjunct.AbstractDisjointSet
abstract class AbstractDisjointSet[V, W, This <: AbstractDisjointSet[V, W, This]](map: Map[V, W])(f: W => Option[V]) extends DisjointSet[V, This], Connected[V]

Abstract base class for disjoint-set implementations that relies on an immutable Map to associate objects with their parent disjoint set.

All operations are purely functional: every "mutation" returns a new instance constructed via unit, leaving the original unchanged. This makes instances persistent data structures — an old reference remains valid after any connect or put.

Each object V in map is associated with a value of type W. The function f extracts an optional parent V from a W value. If f(w) == None the object is the root of its own component.

The F-bound This &lt;: AbstractDisjointSet[V, W, This] ensures that connect, put, and remove all return the concrete subtype without casting at the call site. The only remaining asInstanceOf casts are in the short-circuit cases where this is returned unchanged — these are safe but unavoidable, since the compiler cannot prove this: This without them.

This abstract class does not perform path compression. For path compression, you must extend the subclass AbstractDisjointSetWithCompression.

Type parameters

This

the concrete subtype (F-bounded polymorphism).

V

the underlying object type.

W

the map value type (may equal Option[V] or compound it with extra data).

Value parameters

f

extracts the optional parent from a W value.

map

the component map: each V maps to its associated W.

Attributes

Graph
Supertypes
trait Connected[V]
trait DisjointSet[V, This]
class Object
trait Matchable
class Any
Known subtypes

Members list

Value members

Abstract methods

protected def union(v1: V, v2: V): Map[V, W]

Subclass-specific map update that unions the components rooted at v1 and v2. Both v1 and v2 are guaranteed to be distinct roots when this is called.

Subclass-specific map update that unions the components rooted at v1 and v2. Both v1 and v2 are guaranteed to be distinct roots when this is called.

Attributes

def unit(map: Map[V, W]): This

Constructs a new instance of the concrete subtype from the given map. Subclasses must implement this to return This.

Constructs a new instance of the concrete subtype from the given map. Subclasses must implement this to return This.

Attributes

Concrete methods

def connect(v1: V, v2: V): This

Merges the components containing v1 and v2. Short-circuits (returning this) if they are already in the same component or if v1 == v2. The asInstanceOf here is safe: this is always a This.

Merges the components containing v1 and v2. Short-circuits (returning this) if they are already in the same component or if v1 == v2. The asInstanceOf here is safe: this is always a This.

Attributes

def depth(v: V): Int

Returns the depth of v in the component tree (root has depth 1).

Returns the depth of v in the component tree (root has depth 1).

Attributes

def get(key: V): Option[W]

Returns the W value associated with key, if present.

Returns the W value associated with key, if present.

Attributes

def getDisjointSet(key: V): V

Returns the root of the component containing key.

Returns the root of the component containing key.

Attributes

def isConnected(v1: V, v2: V): Boolean

True if v1 and v2 belong to the same disjoint set.

True if v1 and v2 belong to the same disjoint set.

Attributes

def isRoot(v: V): Boolean

True if v is the root of its own component (i.e. has no parent).

True if v is the root of its own component (i.e. has no parent).

Attributes

def iterator: Iterator[(V, W)]

Returns an iterator over all (V, W) entries.

Returns an iterator over all (V, W) entries.

Attributes

def maxDepth: Double

The maximum path length to root across all objects.

The maximum path length to root across all objects.

Attributes

def meanDepth: Double

The mean path length to root across all objects.

The mean path length to root across all objects.

Attributes

def parent(v: V): Option[V]

Returns the optional parent of v.

Returns the optional parent of v.

Attributes

def remove(key: V): This

Removes key from this disjoint set.

Removes key from this disjoint set.

Attributes

def size: Int

Returns the number of disjoint sets (roots).

Returns the number of disjoint sets (roots).

Attributes

def updated[W1 >: W](key: V, value: W1): Map[V, W1]

Returns an updated map with key -> value. Note: Map.updated is a purely functional operation on an immutable Map — it returns a new Map and does not mutate the receiver.

Returns an updated map with key -> value. Note: Map.updated is a purely functional operation on an immutable Map — it returns a new Map and does not mutate the receiver.

Attributes

Inherited and Abstract methods

def put(key: V): This

Adds the specified key to the disjoint set data structure and initializes it as a new singleton set if it does not already exist.

Adds the specified key to the disjoint set data structure and initializes it as a new singleton set if it does not already exist.

Value parameters

key

the element to be added to the disjoint set.

Attributes

Returns

the updated disjoint set containing the newly added key.

Inherited from:
DisjointSet