1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Add optional filter for transitive closure.

This commit is contained in:
Cedric Beust 2017-02-09 10:44:59 -08:00
parent 11980ca940
commit 47f7072d9c
2 changed files with 17 additions and 13 deletions

View file

@ -65,15 +65,18 @@ class DynamicGraph<T> {
}
}
fun <T> transitiveClosureGraph(roots: List<T>, childrenFor: (T) -> List<T>) : List<Node<T>>
= roots.map { transitiveClosureGraph(it, childrenFor) }
fun <T> transitiveClosureGraph(roots: List<T>, childrenFor: (T) -> List<T>,
filter: (T) -> Boolean): List<Node<T>>
= roots.map { transitiveClosureGraph(it, childrenFor, filter) }
fun <T> transitiveClosureGraph(root: T, childrenFor: (T) -> List<T>, seen: HashSet<T> = hashSetOf()) : Node<T> {
fun <T> transitiveClosureGraph(root: T, childrenFor: (T) -> List<T>,
filter: (T) -> Boolean = { t: T -> true },
seen: HashSet<T> = hashSetOf()) : Node<T> {
val children = arrayListOf<Node<T>>()
childrenFor(root).forEach { child ->
childrenFor(root).filter(filter).forEach { child ->
if (! seen.contains(child)) {
seen.add(child)
val c = transitiveClosureGraph(child, childrenFor, seen)
val c = transitiveClosureGraph(child, childrenFor, filter, seen)
children.add(c)
}
}