Class PartitionSet<A>
- Type Parameters:
A- the element type
- All Implemented Interfaces:
Nullfree
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 -
Method Summary
Modifier and TypeMethodDescriptiondomain()Returns an unmodifiable view on the set of elements known to this partitioning.booleanChecks whether a pair of elements belong to the same equivalence class.findRepresentative(A elem) Returns a unique representative of the equivalence class of the given element.booleanmergeClasses(A elem1, A elem2) Merges the equivalence classes of the given elements.
-
Constructor Details
-
PartitionSet
public PartitionSet()Creates a new trivial partitioning.Initially, every element is only equivalent to itself.
-
-
Method Details
-
findRepresentative
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 expressionfindRepresentative(x) == findRepresentative(y), for any elementsxandy.- Parameters:
elem- an element- Returns:
- an element that uniquely represents the equivalence class of
elem - Throws:
NullPointerException- ifelemis null
-
equalizes
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 elementelem2- another element- Returns:
trueifelem1andelem2belong to the same equivalence class;falseotherwise- Throws:
NullPointerException- ifelem1orelem2is null
-
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, ormergeClasses. 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
Merges the equivalence classes of the given elements.After this operation,
elem1andelem2are henceforth equivalent according to this partitioning. Arbitrarily many other pairs of elements may become equivalent by consequence.- Parameters:
elem1- one elementelem2- another element- Returns:
trueif the equivalence relation has changed as the effect of this operation;falseifelem1andelem2were already equivalent before.- Throws:
NullPointerException- ifelem1orelem2is null
-