AbstractDisjointSetWithCompression

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

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.

Subclasses that implement path compression should override findAndCompress. The default implementation simply delegates to getDisjointSet and returns this unchanged, so non-compressing subclasses need not override it. The connect method in this class calls findAndCompress for both arguments, so compression is applied automatically during every merge in subclasses that override it.

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
class AbstractDisjointSet[V, W, This]
trait Connected[V]
trait DisjointSet[V, This]
class Object
trait Matchable
class Any
Show all
Known subtypes

Members list

Value members

Abstract methods

def findAndCompress(key: V): (V, This)

Returns the root of the component containing key, together with a (potentially restructured) copy of this disjoint set.

Returns the root of the component containing key, together with a (potentially restructured) copy of this disjoint set.

Making compression explicit here (rather than hiding it inside getDisjointSet) keeps the design honest: a DisjointSet that does not compress still satisfies the trait, and one that does compress makes that fact visible in its API.

Value parameters

key

the element whose root is sought.

Attributes

Returns

a pair (root, updated) where root is the representative of key's component and updated is the (possibly compressed) instance.

Concrete methods

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

Merges the components containing v1 and v2. Short-circuits (returning this) if v1 == v2. Delegates to findAndCompress for both arguments so that subclasses which override it get compression applied during every merge automatically. The asInstanceOf here is safe: this is always a This.

Merges the components containing v1 and v2. Short-circuits (returning this) if v1 == v2. Delegates to findAndCompress for both arguments so that subclasses which override it get compression applied during every merge automatically. The asInstanceOf here is safe: this is always a This.

Attributes

Definition Classes

Inherited methods

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

Inherited from:
AbstractDisjointSet
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

Inherited from:
AbstractDisjointSet
def getDisjointSet(key: V): V

Returns the root of the component containing key.

Returns the root of the component containing key.

Attributes

Inherited from:
AbstractDisjointSet
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

Inherited from:
AbstractDisjointSet
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

Inherited from:
AbstractDisjointSet
def iterator: Iterator[(V, W)]

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

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

Attributes

Inherited from:
AbstractDisjointSet
def maxDepth: Double

The maximum path length to root across all objects.

The maximum path length to root across all objects.

Attributes

Inherited from:
AbstractDisjointSet
def meanDepth: Double

The mean path length to root across all objects.

The mean path length to root across all objects.

Attributes

Inherited from:
AbstractDisjointSet
def parent(v: V): Option[V]

Returns the optional parent of v.

Returns the optional parent of v.

Attributes

Inherited from:
AbstractDisjointSet
def remove(key: V): This

Removes key from this disjoint set.

Removes key from this disjoint set.

Attributes

Inherited from:
AbstractDisjointSet
def size: Int

Returns the number of disjoint sets (roots).

Returns the number of disjoint sets (roots).

Attributes

Inherited from:
AbstractDisjointSet
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 from:
AbstractDisjointSet

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
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

Inherited from:
AbstractDisjointSet
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

Inherited from:
AbstractDisjointSet