Class TopologicalSort
- java.lang.Object
-
- com.apple.foundationdb.record.query.plan.temp.TopologicalSort
-
@API(EXPERIMENTAL) public class TopologicalSort extends Object
Utility class to provide helpers related to topological sorts. The main purpose on this class is to provide a specific iterable that can efficiently traverse all possible permutations of the input set that do not violate the given dependency constraints. The iterableTopologicalSort.TopologicalOrderPermutationIterable
adheres to the following requirements:- it does not violate the given constraints
- it produces all possible orderings under the given constraints
- it reacts appropriately to give circular dependency constraints between elements (i.e., no infinite loops)
- it iterates all orderings on the fly. That is, it stores only the position of the iteration and does not (pre)create the orderings in memory.
TopologicalSort.TopologicalOrderPermutationIterable
subclassesIterable
in order to provide an additional feature that allows for skipping. Assume we have a set{ a, b, c, d } with constraints { b -> c, b -> d } (c depends on b, d depends on b)
(a, b, c, d) (a, b, d, c) (b, a, c, d) (b, a, d, c) (b, c, a, d) (b, c, d, a) (b, d, a, c) (b, d, c, a)
(b, a, ...)
orderings after the first one was returned ((b, a, c, d
). In this case we would like to instruct the iterator to skip all such orderings and continue iteration at(b, c, a, d)
. Similarly, we want to skip all orderings starting with(b, ...)
once we encountered the first such ordering. This the iterators created by the providedTopologicalSort.TopologicalOrderPermutationIterable
of typeTopologicalSort.TopologicalOrderPermutationIterator
provide a methodTopologicalSort.TopologicalOrderPermutationIterator.skip(int)
to allow skipping to a given prefix.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
TopologicalSort.TopologicalOrderPermutationIterable<T>
Iterable that provides special iterators of typeTopologicalSort.TopologicalOrderPermutationIterator
.static interface
TopologicalSort.TopologicalOrderPermutationIterator<T>
An iterator extendingIterator
providing the ability to skip a certain prefix.
-
Constructor Summary
Constructors Constructor Description TopologicalSort()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Optional<List<T>>
anyTopologicalOrderPermutation(Set<T> set, Function<T,Set<T>> dependsOnFn)
Create a correct topological ordering based on a set and a function describing the depends-on relationships between items in the given set.static <T> TopologicalSort.TopologicalOrderPermutationIterable<T>
topologicalOrderPermutations(Set<T> set, Function<T,Set<T>> dependsOnFn)
Create aTopologicalSort.TopologicalOrderPermutationIterable
based on a set and a function describing the depends-on relationships between items in the given set.
-
-
-
Method Detail
-
topologicalOrderPermutations
public static <T> TopologicalSort.TopologicalOrderPermutationIterable<T> topologicalOrderPermutations(@Nonnull Set<T> set, @Nonnull Function<T,Set<T>> dependsOnFn)
Create aTopologicalSort.TopologicalOrderPermutationIterable
based on a set and a function describing the depends-on relationships between items in the given set.- Type Parameters:
T
- type- Parameters:
set
- the set to create the iterable overdependsOnFn
- a function fromT
toSet<T>
that can be called during the lifecycle of all iterators multiple times repeatedly or not at all for any given element inset
. This method is expected to return instantly and must be stable. Note it is allowed for the set the given function returns to contain elements of typeT
that are not inset
. These items are ignored by the underlying algorithm (that is, they are satisfied by every ordering).- Returns:
- a new
TopologicalSort.TopologicalOrderPermutationIterable
that obeys the constraints as expressed independsOnFn
in a sense that the iterators created by this iterator will not return orderings that violate the given depends-on constraints
-
anyTopologicalOrderPermutation
public static <T> Optional<List<T>> anyTopologicalOrderPermutation(@Nonnull Set<T> set, @Nonnull Function<T,Set<T>> dependsOnFn)
Create a correct topological ordering based on a set and a function describing the depends-on relationships between items in the given set.- Type Parameters:
T
- type- Parameters:
set
- the set to create the iterable overdependsOnFn
- a function fromT
toSet<T>
that can be called during the lifecycle of all iterators multiple times repeatedly or not at all for any given element inset
. This method is expected to return instantly and must be stable. Note it is allowed for the set the given function returns to contain elements of typeT
that are not inset
. These items are ignored by the underlying algorithm (that is, they are satisfied by every ordering).- Returns:
- a permutation of the set that is topologically correctly ordered with respect to
dependsOnFn
-
-