mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 00:17:11 -07:00
Add optional filter for transitive closure.
This commit is contained in:
parent
11980ca940
commit
47f7072d9c
2 changed files with 17 additions and 13 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue