Class DisjointSet<E>

  • Type Parameters:
    E - The type of elements in this set.
    All Implemented Interfaces:
    Serializable, Iterable<E>, Collection<E>, Set<E>

    public class DisjointSet<E>
    extends AbstractSet<E>
    implements Serializable
    A set which is disjoint from others DisjointSets. Two sets are disjoint (or mutually exclusive) if their intersection is the empty set. Adding an element to a DisjointSet remove it from any other mutually exclusive DisjointSet. Optionnaly, DisjointSets may also have a trash set receiving removed elements. The example below creates 3 mutually exclusive sets with a trash:
     DisjointSet set0 = new DisjointSet(true); // Used as the trash set.
     DisjointSet set1 = new DisjointSet(set0);
     DisjointSet set2 = new DisjointSet(set0);
     
    Disjoint sets are thread-safe.
    Since:
    2.0
    Author:
    Martin Desruisseaux (IRD)
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      DisjointSet()
      Construct a initially empty set.
      DisjointSet​(boolean hasTrash)
      Construct a initially empty set with an optional trash set.
      DisjointSet​(DisjointSet<E> disjointSet)
      Construct a new set mutually exclusive with the specified set.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(E element)
      Ensures that this collection contains the specified element.
      boolean addAll​(Collection<? extends E> c)
      Adds all of the elements in the specified collection to this set.
      void clear()
      Removes all of the elements from this set.
      boolean contains​(Object element)
      Returns true if this set contains the specified element.
      boolean containsAll​(Collection<?> c)
      Returns true if this set contains all of the elements in the specified collection.
      boolean equals​(Object set)
      Compare this set with the specified object for equality.
      Set<E> getTrash()
      Returns the trash set, or null if there is none.
      int hashCode()
      Returns an hash value for this set.
      Iterator<E> iterator()
      Returns an iterator over the elements in this collection.
      boolean remove​(Object element)
      Removes a single instance of the specified element from this set, if it is present.
      boolean removeAll​(Collection<?> c)
      Removes from this set all of its elements that are contained in the specified collection.
      boolean retainAll​(Collection<?> c)
      Retains only the elements in this set that are contained in the specified collection.
      int size()
      Returns the number of elements in this set.
      Object[] toArray()
      Returns an array containing all of the elements in this collection.
      <T> T[] toArray​(T[] a)
      Returns an array containing all of the elements in this collection.
      String toString()
      Returns a string representation of this set.
      • Methods inherited from class AbstractCollection

        isEmpty
      • Methods inherited from class Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface Iterable

        forEach
      • Methods inherited from interface Set

        isEmpty, spliterator
    • Constructor Detail

      • DisjointSet

        public DisjointSet()
        Construct a initially empty set. There is initially no other set mutually exclusive with this one. Mutually exclusive sets must be created using the DisjointSet(DisjointSet) constructor with this newly created set as argument.

        DisjointSets constructed using this constructor has no trash. All remove operations on this set really remove all references to the removed element, like a usual Set. This is opposed to moving the element to a "trash" set, which is allowed by the DisjointSet(true) constructor.

      • DisjointSet

        public DisjointSet​(boolean hasTrash)
        Construct a initially empty set with an optional trash set. There is initially no other set mutually exclusive with this one. Mutually exclusive sets must be created using the DisjointSet(DisjointSet) constructor with this newly created set as argument.
        Parameters:
        hasTrash - If true, all remove operations will add removed elements to a trash set (thus, really just moving the element to the trash). If false, there is no trash and this constructor behave like the no-argument constructor.
        See Also:
        getTrash()
      • DisjointSet

        public DisjointSet​(DisjointSet<E> disjointSet)
        Construct a new set mutually exclusive with the specified set. All sets mutually exclusive with disjointSet will also be mutually exclusive with the newly created set. If disjointSet has a trash set, the newly created set will use the same trash (i.e. all remove operations will really move the element to the trash set). Otherwise, the new DisjointSet have no trash.
        Parameters:
        disjointSet - The set to be disjoint from.
    • Method Detail

      • getTrash

        public Set<E> getTrash()
        Returns the trash set, or null if there is none. The trash set receive all elements removed from this set.
        Returns:
        The trash set, or null if none.
      • size

        public int size()
        Returns the number of elements in this set. The size of this set may change as a result of adding elements to a mutually exclusive set.
        Specified by:
        size in interface Collection<E>
        Specified by:
        size in interface Set<E>
        Specified by:
        size in class AbstractCollection<E>
      • contains

        public boolean contains​(Object element)
        Returns true if this set contains the specified element.
        Specified by:
        contains in interface Collection<E>
        Specified by:
        contains in interface Set<E>
        Overrides:
        contains in class AbstractCollection<E>
        Parameters:
        element - Object to be checked for containment in this set.
        Returns:
        true if this set contains the specified element.
      • add

        public boolean add​(E element)
        Ensures that this collection contains the specified element. Adding an element to this set will remove it from any mutually exclusive set.
        Specified by:
        add in interface Collection<E>
        Specified by:
        add in interface Set<E>
        Overrides:
        add in class AbstractCollection<E>
        Parameters:
        element - Element whose presence in this set is to be ensured.
        Returns:
        true if the set changed as a result of the call.
      • remove

        public boolean remove​(Object element)
        Removes a single instance of the specified element from this set, if it is present. If this DisjointSet has a trash set, the removed element will be added to the trash set.
        Specified by:
        remove in interface Collection<E>
        Specified by:
        remove in interface Set<E>
        Overrides:
        remove in class AbstractCollection<E>
        Parameters:
        element - Element to be removed from this set.
        Returns:
        true if the set changed as a result of the call.
      • containsAll

        public boolean containsAll​(Collection<?> c)
        Returns true if this set contains all of the elements in the specified collection.
        Specified by:
        containsAll in interface Collection<E>
        Specified by:
        containsAll in interface Set<E>
        Overrides:
        containsAll in class AbstractCollection<E>
        Parameters:
        c - collection to be checked for containment in this collection.
        Returns:
        true if this set contains all of the elements in the specified collection.
      • addAll

        public boolean addAll​(Collection<? extends E> c)
        Adds all of the elements in the specified collection to this set. All of the elements will be removed from mutually exclusive sets.
        Specified by:
        addAll in interface Collection<E>
        Specified by:
        addAll in interface Set<E>
        Overrides:
        addAll in class AbstractCollection<E>
        Parameters:
        c - collection whose elements are to be added to this set.
        Returns:
        true if this set changed as a result of the call.
      • removeAll

        public boolean removeAll​(Collection<?> c)
        Removes from this set all of its elements that are contained in the specified collection. If this DisjointSet has a trash set, all removed elements will be added to the trash set.
        Specified by:
        removeAll in interface Collection<E>
        Specified by:
        removeAll in interface Set<E>
        Overrides:
        removeAll in class AbstractSet<E>
        Parameters:
        c - elements to be removed from this set.
        Returns:
        true if this set changed as a result of the call.
      • retainAll

        public boolean retainAll​(Collection<?> c)
        Retains only the elements in this set that are contained in the specified collection. If this DisjointSet has a trash set, all removed elements will be added to the trash set.
        Specified by:
        retainAll in interface Collection<E>
        Specified by:
        retainAll in interface Set<E>
        Overrides:
        retainAll in class AbstractCollection<E>
        Parameters:
        c - elements to be retained in this collection.
        Returns:
        true if this collection changed as a result of the call.
      • clear

        public void clear()
        Removes all of the elements from this set. If this DisjointSet has a trash set, all removed elements will be added to the trash set.
        Specified by:
        clear in interface Collection<E>
        Specified by:
        clear in interface Set<E>
        Overrides:
        clear in class AbstractCollection<E>
      • iterator

        public Iterator<E> iterator()
        Returns an iterator over the elements in this collection.
        Specified by:
        iterator in interface Collection<E>
        Specified by:
        iterator in interface Iterable<E>
        Specified by:
        iterator in interface Set<E>
        Specified by:
        iterator in class AbstractCollection<E>
      • toArray

        public Object[] toArray()
        Returns an array containing all of the elements in this collection.
        Specified by:
        toArray in interface Collection<E>
        Specified by:
        toArray in interface Set<E>
        Overrides:
        toArray in class AbstractCollection<E>
        Returns:
        an array containing all of the elements in this set.
      • toArray

        public <T> T[] toArray​(T[] a)
        Returns an array containing all of the elements in this collection.
        Specified by:
        toArray in interface Collection<E>
        Specified by:
        toArray in interface Set<E>
        Overrides:
        toArray in class AbstractCollection<E>
        Type Parameters:
        T - The type of elements in the array.
        Parameters:
        a - The array into which the elements of the set are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
        Returns:
        an array containing the elements of the set.
      • toString

        public String toString()
        Returns a string representation of this set.
        Overrides:
        toString in class AbstractCollection<E>
      • hashCode

        public int hashCode()
        Returns an hash value for this set.
        Specified by:
        hashCode in interface Collection<E>
        Specified by:
        hashCode in interface Set<E>
        Overrides:
        hashCode in class AbstractSet<E>
      • equals

        public boolean equals​(Object set)
        Compare this set with the specified object for equality.
        Specified by:
        equals in interface Collection<E>
        Specified by:
        equals in interface Set<E>
        Overrides:
        equals in class AbstractSet<E>
        Parameters:
        set - The object to compare with this set for equality.