Class PartitionSet<A>

java.lang.Object
eu.bandm.tools.util.uni.PartitionSet<A>
Type Parameters:
A - the element type
All Implemented Interfaces:
Nullfree

public class PartitionSet<A> extends Object implements Nullfree
A partitioning of a set of elements into equivalence classes.

The underlying set of elements is not specified explicitly. Rather, an element is added to the domain, i.e., the set of known elements, on the fly if passed as a parameter to one of the methods findRepresentative, equalizes, or mergeClasses.

The identity of an element is considered only up to equals, as if they were stored in a HashSet. Null elements are not supported.

The method equalizes always represents an equivalence relation: as a binary predicate, it is reflexive, transitive and symmetric. More precisely, it represents the smallest equivalence relation that entails all pairwise equivalences that have been added explicitly using the method mergeClasses.

The equivalence classes are not stored explicitly. However, they are represented using a representative element which can be obtained for any member of the equivalence class with the method findRepresentative. Representatives for equivalent elements are guaranteed to be identical according to ==, not just according to equals. It follow that a representative is always represented by itself.

Representatives are not stable over time; a call to mergeClasses(A, A) may change equivalence classes and thus also the assigned representatives.

Since:
1.1
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new trivial partitioning.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns an unmodifiable view on the set of elements known to this partitioning.
    boolean
    equalizes(A elem1, A elem2)
    Checks whether a pair of elements belong to the same equivalence class.
    Returns a unique representative of the equivalence class of the given element.
    boolean
    mergeClasses(A elem1, A elem2)
    Merges the equivalence classes of the given elements.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PartitionSet

      public PartitionSet()
      Creates a new trivial partitioning.

      Initially, every element is only equivalent to itself.

  • Method Details

    • findRepresentative

      public A findRepresentative(A elem)
      Returns a unique representative of the equivalence class of the given element.

      If the element is not yet part of the domain, it is added in a singleton equivalence class of its own, and hence represented by itself.

      The resulting element is chosen such that the result of the expression equalizes(x, y) is the same as the result of the expression findRepresentative(x) == findRepresentative(y), for any elements x and y.

      Parameters:
      elem - an element
      Returns:
      an element that uniquely represents the equivalence class of elem
      Throws:
      NullPointerException - if elem is null
    • equalizes

      public boolean equalizes(A elem1, A elem2)
      Checks whether a pair of elements belong to the same equivalence class.

      If any of the elements is not yet part of the domain, it is added in a singleton equivalence class of its own.

      Parameters:
      elem1 - one element
      elem2 - another element
      Returns:
      true if elem1 and elem2 belong to the same equivalence class; false otherwise
      Throws:
      NullPointerException - if elem1 or elem2 is null
    • domain

      public Set<A> domain()
      Returns an unmodifiable view on the set of elements known to this partitioning.

      Asynchronous changes to this partitioning are reflected by the view.

      An element becomes known when it is first passed as a parameter to one of the methods findRepresentative, equalizes, or mergeClasses. Known elements never become unknown during the lifetime of this partitioning.

      Returns:
      an unmodifiable set that always contains the elements currently known to this partitioning
    • mergeClasses

      public boolean mergeClasses(A elem1, A elem2)
      Merges the equivalence classes of the given elements.

      After this operation, elem1 and elem2 are henceforth equivalent according to this partitioning. Arbitrarily many other pairs of elements may become equivalent by consequence.

      Parameters:
      elem1 - one element
      elem2 - another element
      Returns:
      true if the equivalence relation has changed as the effect of this operation; false if elem1 and elem2 were already equivalent before.
      Throws:
      NullPointerException - if elem1 or elem2 is null