HashSet
A Topological sort is used when you have a partial ordering expressed as
dependencies between elements (also often represented as edges in a directed
acyclic graph). A Topological sort should not be used when you have a total
ordering expressed as a Comparator over the items. The algorithm has
the additional characteristic that dependency sets are sorted by the original
list order so that order is preserved when possible.
The sort algorithm works by recursively visiting every item, once and only once. On each visit, the items dependencies are first visited and then the item is added to the sorted list. Thus the algorithm ensures that dependency items are always added before dependent items.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private static class | TopologicalSort.
A comparator that is used to sort dependencies in the order they were in the original list. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | addBeforeAfter(T
The item will be sorted before the after before, T The item will be sorted after the before after)An alternative to |
| public void | addDependency(T
The dependent item will be sorted after all its dependencies dependent, T... The dependency item, will be sorted before its dependent item dependency)Add a dependency to be considered in the sort. |
| public void | sort(T[]
The array to be sorted. array)Sort the passed array according to dependencies previously set with
|
| public void | sort(Collection<T>
The list to be sorted. list)Sort the passed list according to dependencies previously set with
|
| public String | |
| private void | visit(T
The item to be visited item, Set<T> The Set of items already visited visited, List<T> The list to sort items into sorted, Comparator<T> A comparator used to sort dependencies. comparator)Visit an item to be sorted. |
| _dependencies | back to summary |
|---|---|
| private final Map<T, Set<T>> _dependencies | |
| TopologicalSort | back to summary |
|---|---|
| public TopologicalSort() | |
| addBeforeAfter | back to summary |
|---|---|
| public void addBeforeAfter(T before, T after) An alternative to
| |
| addDependency | back to summary |
|---|---|
| public void addDependency(T dependent, T... dependency) Add a dependency to be considered in the sort.
| |
| sort | back to summary |
|---|---|
| public void sort(T[] array) Sort the passed array according to dependencies previously set with
| |
| sort | back to summary |
|---|---|
| public void sort(Collection<T> list) Sort the passed list according to dependencies previously set with
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |
| visit | back to summary |
|---|---|
| private void visit(T item, Set<T> visited, List<T> sorted, Comparator<T> comparator) Visit an item to be sorted.
| |
| Access | Constructor and Description |
|---|---|
| pack-priv | |
| pack-priv |
| CyclicException | back to summary |
|---|---|
| pack-priv CyclicException(Object item) | |
| CyclicException | back to summary |
|---|---|
| pack-priv CyclicException(Object item, TopologicalSort. | |
| Access | Constructor and Description |
|---|---|
| pack-priv | |
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public int | compare(T
the first object to be compared. o1, T the second object to be compared. o2)Implements java. Compares its two arguments for order. |
| _indexes | back to summary |
|---|---|
| private final Map<T, Integer> _indexes | |
| InitialOrderComparator | back to summary |
|---|---|
| pack-priv InitialOrderComparator(T[] initial) | |
| InitialOrderComparator | back to summary |
|---|---|
| pack-priv InitialOrderComparator(Collection<T> initial) | |
| compare | back to summary |
|---|---|
| public int compare(T o1, T o2) Implements java. Doc from java. Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
The implementor must ensure that
The implementor must also ensure that the relation is transitive:
Finally, the implementor must ensure that
| |