in reactor/Core/src/jetbrains/mps/logic/reactor/util/ClassicIndexedTermTrie.kt [316:343]
fun terms2bases(): Pair<List<PathNode<T>>, List<PathNode<T>>> {
// for every current node there is a number
// initially 0
// counting down with every call to allNext()
// increased by current node's arity
val terms = ArrayList<PathNode<T>>()
val bases = ArrayList<PathNode<T>>()
val stack = arrayListOf(allNext() to 0)
while (stack.isNotEmpty()) {
val (nn, count) = stack.pop()
for (n in nn) {
val newCount = count + n.arity
if (newCount == 0) {
bases.add(n)
} else {
terms.add(n)
stack.push(n.allNext() to (newCount - 1))
}
}
}
return terms to bases
}