File tree Expand file tree Collapse file tree
src/main/kotlin/be/ugent/topl/mio/ui Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments