| 1 | /* |
| 2 | * Copyright 2008 The Closure Compiler Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.google.javascript.jscomp.graph; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.Set; |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Union-Find is a classical algorithm used to find connected components in |
| 24 | * graph theory. |
| 25 | * |
| 26 | * <p>Each equivalence class has a representative element that is chosen |
| 27 | * arbitrarily and is used to determine if two elements are members of the same |
| 28 | * class. |
| 29 | * |
| 30 | * <p>See <a |
| 31 | * href="http://www.algorithmist.com/index.php?title=Union_Find&oldid=7575"> |
| 32 | * algorithmist</a> for more detail. |
| 33 | * |
| 34 | * @param <E> element type |
| 35 | */ |
| 36 | public interface UnionFind<E> { |
| 37 | |
| 38 | /** |
| 39 | * Adds the given element to a new set if it is not already in a set. |
| 40 | * |
| 41 | * @throws UnsupportedOperationException if the add operation is not |
| 42 | * supported by this union-find. |
| 43 | */ |
| 44 | public void add(E e); |
| 45 | |
| 46 | /** |
| 47 | * Unions the equivalence classes of {@code a} and {@code b} and returns the |
| 48 | * representative of the resulting equivalence class. The elements will be |
| 49 | * added if they are not already present. |
| 50 | * |
| 51 | * @throws UnsupportedOperationException if the add operation is not |
| 52 | * supported by this union-find. |
| 53 | */ |
| 54 | public E union(E a, E b); |
| 55 | |
| 56 | /** Returns the representative of the equivalence class of {@code e}. */ |
| 57 | public E find(E e); |
| 58 | |
| 59 | /** |
| 60 | * Returns true if {@code a} and {@code b} belong to the same equivalence |
| 61 | * class. |
| 62 | * |
| 63 | * @throws IllegalArgumentException if any argument is not an element of this |
| 64 | * structure. |
| 65 | */ |
| 66 | public boolean areEquivalent(E a, E b); |
| 67 | |
| 68 | /** Returns an unmodifiable set of all elements added to the UnionFind. */ |
| 69 | public Set<E> elements(); |
| 70 | |
| 71 | /** |
| 72 | * Returns an immutable collection containing all equivalence classes. The |
| 73 | * returned collection represents a snapshot of the current state and will not |
| 74 | * reflect changes made to the data structure. |
| 75 | */ |
| 76 | public Collection<Set<E>> allEquivalenceClasses(); |
| 77 | |
| 78 | /** |
| 79 | * Returns the elements in the same equivalence class as {@code value}. |
| 80 | * |
| 81 | * @return an unmodifiable view. As equivalence classes are merged, this set |
| 82 | * will reflect those changes. |
| 83 | * @throws IllegalArgumentException if a requested element does not belong |
| 84 | * to the structure. |
| 85 | */ |
| 86 | public Set<E> findAll(final E value); |
| 87 | |
| 88 | } |