Skip to content

Commit 42eb2c9

Browse files
committed
Add WIP LeafCounter which calculates the height of each node without recursion
Currently is very slow though due to dealing with a lot of height 1 nodes. Maybe the default should be 1 and we should track completed nodes instead?
1 parent 77eba89 commit 42eb2c9

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package be.ugent.topl.mio.ui
2+
3+
import be.ugent.topl.mio.debugger.MultiverseGraph
4+
import be.ugent.topl.mio.debugger.MultiverseNode
5+
6+
class LeafCounter(private val graph: MultiverseGraph) {
7+
private val cachedHeights = mutableMapOf<MultiverseNode, Int>()
8+
9+
// TODO: Maybe it only needs to update changed nodes.
10+
fun calculateLeafCounts() {
11+
println("Calculating leaf counts")
12+
val time = System.currentTimeMillis()
13+
cachedHeights.clear()
14+
val stack = mutableListOf(graph.rootNode)
15+
while (stack.isNotEmpty()) {
16+
val currentNode = stack.last()
17+
if (currentNode.children.isEmpty()) {
18+
cachedHeights[currentNode] = 1
19+
}
20+
// We already calculated the height of our children -> calculate our height!
21+
else if (cachedHeights.contains(currentNode.children.first())) {
22+
var heightSum = 0
23+
for (child in currentNode.children) {
24+
heightSum += cachedHeights[child]!!
25+
}
26+
cachedHeights[currentNode] = heightSum
27+
}
28+
if (cachedHeights.containsKey(currentNode)) {
29+
stack.removeLast()
30+
}
31+
stack.addAll(currentNode.children)
32+
}
33+
println("Finished calculating leaf counts ${System.currentTimeMillis() - time} ms elapsed")
34+
}
35+
36+
fun getLeafCount(node: MultiverseNode): Int {
37+
return cachedHeights[node]!!
38+
}
39+
}

0 commit comments

Comments
 (0)