Class EquivalenceSet<E extends java.lang.Comparable<E>>

  • Type Parameters:
    E - Element type

    public class EquivalenceSet<E extends java.lang.Comparable<E>>
    extends java.lang.Object
    Set of elements organized into equivalence classes.

    Elements are equivalent by the rules of a mathematical equivalence relation:

    Reflexive
    Every element e is equivalent to itself
    Symmetric
    If e is equivalent to f, then f is equivalent to e
    Transitive
    If e is equivalent to f, and f is equivalent to g, then e is equivalent to g

    For any given pair of elements, answers in O(log N) (two hash-table lookups) whether they are equivalent to each other.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<E,​E> parents  
    • Constructor Summary

      Constructors 
      Constructor Description
      EquivalenceSet()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      E add​(E e)
      Adds an element, and returns the element (which is its own parent).
      boolean areEquivalent​(E e, E f)
      Returns whether two elements are in the same equivalence class.
      int classCount()
      Returns the number of equivalence classes in this equivalence set.
      void clear()
      Removes all elements in this equivalence set.
      E equiv​(E e, E f)
      Marks two elements as equivalent.
      java.util.SortedMap<E,​java.util.SortedSet<E>> map()
      Returns a map of the canonical element in each equivalence class to the set of elements in that class.
      int size()
      Returns the number of elements in this equivalence set.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • parents

        private final java.util.Map<E extends java.lang.Comparable<E>,​E extends java.lang.Comparable<E>> parents
    • Constructor Detail

      • EquivalenceSet

        public EquivalenceSet()
    • Method Detail

      • add

        public E add​(E e)
        Adds an element, and returns the element (which is its own parent). If already present, returns the element's parent.
      • equiv

        public E equiv​(E e,
                       E f)
        Marks two elements as equivalent. They may or may not be registered, and they may or may not be equal.
      • areEquivalent

        public boolean areEquivalent​(E e,
                                     E f)
        Returns whether two elements are in the same equivalence class. Returns false if either or both of the elements are not registered.
      • map

        public java.util.SortedMap<E,​java.util.SortedSet<E>> map()
        Returns a map of the canonical element in each equivalence class to the set of elements in that class. The keys are sorted in natural order, as are the elements within each key.
      • clear

        public void clear()
        Removes all elements in this equivalence set.
      • size

        public int size()
        Returns the number of elements in this equivalence set.
      • classCount

        public int classCount()
        Returns the number of equivalence classes in this equivalence set.