Class StandardUnionFind<E>

java.lang.Object
com.google.javascript.jscomp.graph.StandardUnionFind<E>
Type Parameters:
E - element type
All Implemented Interfaces:
UnionFind<E>, Serializable

public class StandardUnionFind<E> extends Object implements Serializable, UnionFind<E>
A Union-Find implementation.

This class implements Union-Find algorithm with rank and path compression.

See algorithmist for more detail.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty UnionFind structure.
    Creates an UnionFind structure being a copy of other structure.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(@Nullable E e)
    Adds the given element to a new set if it is not already in a set.
    void
     
    com.google.common.collect.ImmutableList<com.google.common.collect.ImmutableSet<E>>
    Returns an immutable collection containing all equivalence classes.
    com.google.common.collect.ImmutableSet<E>
    Return the reprsentative elements of all the equivalence classes.
    boolean
    areEquivalent(@Nullable E a, @Nullable E b)
    Returns true if a and b belong to the same equivalence class.
    Returns an unmodifiable set of all elements added to the UnionFind.
    find(@Nullable E e)
    Returns the representative of the equivalence class of e.
    findAll(@Nullable E value)
    Returns the elements in the same equivalence class as value.
    union(@Nullable E a, @Nullable E b)
    Unions the equivalence classes of a and b and returns the representative of the resulting equivalence class.

    Methods inherited from class java.lang.Object

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

    • StandardUnionFind

      public StandardUnionFind()
      Creates an empty UnionFind structure.
    • StandardUnionFind

      public StandardUnionFind(UnionFind<E> other)
      Creates an UnionFind structure being a copy of other structure. The created structure is optimal in a sense that the paths to the root from any element will have a length of at most 1.
      Parameters:
      other - structure to be copied
  • Method Details

    • add

      public void add(@Nullable E e)
      Description copied from interface: UnionFind
      Adds the given element to a new set if it is not already in a set.
      Specified by:
      add in interface UnionFind<E>
    • addAll

      public void addAll(Iterable<E> es)
    • union

      @CanIgnoreReturnValue public E union(@Nullable E a, @Nullable E b)
      Description copied from interface: UnionFind
      Unions the equivalence classes of a and b and returns the representative of the resulting equivalence class. The elements will be added if they are not already present.
      Specified by:
      union in interface UnionFind<E>
    • find

      public E find(@Nullable E e)
      Description copied from interface: UnionFind
      Returns the representative of the equivalence class of e.
      Specified by:
      find in interface UnionFind<E>
    • areEquivalent

      public boolean areEquivalent(@Nullable E a, @Nullable E b)
      Description copied from interface: UnionFind
      Returns true if a and b belong to the same equivalence class.
      Specified by:
      areEquivalent in interface UnionFind<E>
    • elements

      public Set<E> elements()
      Description copied from interface: UnionFind
      Returns an unmodifiable set of all elements added to the UnionFind.
      Specified by:
      elements in interface UnionFind<E>
    • allEquivalenceClasses

      public com.google.common.collect.ImmutableList<com.google.common.collect.ImmutableSet<E>> allEquivalenceClasses()
      Description copied from interface: UnionFind
      Returns an immutable collection containing all equivalence classes. The returned collection represents a snapshot of the current state and will not reflect changes made to the data structure.
      Specified by:
      allEquivalenceClasses in interface UnionFind<E>
    • allRepresentatives

      public com.google.common.collect.ImmutableSet<E> allRepresentatives()
      Return the reprsentative elements of all the equivalence classes.

      This is a "snapshot" view of the representatives at the time the method was called.

    • findAll

      public Set<E> findAll(@Nullable E value)
      Description copied from interface: UnionFind
      Returns the elements in the same equivalence class as value.
      Specified by:
      findAll in interface UnionFind<E>
      Returns:
      an unmodifiable view. As equivalence classes are merged, this set will reflect those changes.