From b3bb823bdf3700efad1fba44b392b9ec64f60815 Mon Sep 17 00:00:00 2001 From: Fedor Isakov Date: Wed, 25 Sep 2024 15:39:32 +0200 Subject: [PATCH] Minor optimization of term trie collection --- .../reactor/util/ClassicIndexedTermTrie.kt | 51 ++++++++++--------- .../mps/logic/reactor/util/Collections.kt | 6 ++- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/util/ClassicIndexedTermTrie.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/util/ClassicIndexedTermTrie.kt index bae4c3cf..2f5ef88e 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/util/ClassicIndexedTermTrie.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/util/ClassicIndexedTermTrie.kt @@ -110,9 +110,9 @@ class ClassicIndexedTermTrie : IndexedTermTrie { private fun putValue(matchTerm: Term, index: Int, value: T) { val seen = IdentityHashMap() - val nodeStack = arrayListOf(root) - val termStack = arrayListOf(matchTerm) - val argPool = arrayListOf() + val nodeStack = arrayDequeOf(root) + val termStack = arrayDequeOf(matchTerm) + val argPool = ArrayList(16) while (!termStack.isEmpty()) { val node = nodeStack.peek() val term = termStack.pop() @@ -165,7 +165,7 @@ class ClassicIndexedTermTrie : IndexedTermTrie { if (head.removeValue(value, index)) { while (nodeStack.isNotEmpty()) { val base = nodeStack.pop() - base.removeIndex(index) +// base.removeIndex(index) if (head.isLeaf()) { base.dropNext(head) } @@ -291,29 +291,29 @@ class ClassicIndexedTermTrie : IndexedTermTrie { private class PathNode(val symbol: Any, val arity: Int) { - private val indexCardinalities = TIntIntHashMap() // map of index cardinalities + private val indexBits = BitSet() // map of index cardinalities private val next = HashMap>(8) - private val indexedValues = TIntObjectHashMap>() + private var indexedValues: TIntObjectHashMap>? = null - fun allIndexMask(): IndexMask = emptyIndexMask().also { it.addAll(indexCardinalities.keySet()) } + fun allIndexMask(): IndexMask = emptyIndexMask().also { it.addAll(indexBits) } fun addIndex(index: Int) { assert(index >= 0) - val card = if (indexCardinalities.contains(index)) indexCardinalities.get(index) else 0 - indexCardinalities.put(index, card + 1) - } - - fun removeIndex(index: Int) { - assert(index >= 0) - val card = if (indexCardinalities.contains(index)) indexCardinalities.get(index) else 0 - assert(card > 0) - if (card > 1) indexCardinalities.put(index, card - 1) else indexCardinalities.remove(index) +// val card = if (indexBits.contains(index)) indexBits.get(index) else 0 + indexBits.set(index) } +// +// fun removeIndex(index: Int) { +// assert(index >= 0) +// val card = if (indexBits.contains(index)) indexBits.get(index) else 0 +// assert(card > 0) +// if (card > 1) indexBits.set(index) else indexBits.remove(index) +// } fun forEachValue(callback: (T, Int) -> Unit ) { - indexedValues.forEachEntry { index, values -> + indexedValues?.forEachEntry { index, values -> values.forEach { callback(it, index) } true } @@ -323,12 +323,12 @@ class ClassicIndexedTermTrie : IndexedTermTrie { val iter = indexMask.iterator() while (iter.hasNext()) { val index = iter.next() - if (indexedValues.containsKey(index)) { indexedValues.get(index).forEach { callback(it, index) } } + if (indexedValues?.containsKey(index) ?: false) { indexedValues?.get(index)?.forEach { callback(it, index) } } } } fun forEachValueWithIndex(index: Int, callback: (T, Int) -> Unit ) { - if (indexedValues.containsKey(index)) { indexedValues.get(index).forEach { callback(it, index) } } + if (indexedValues?.containsKey(index) ?: false) { indexedValues?.get(index)?.forEach { callback(it, index) } } } fun isLeaf(): Boolean = next.isEmpty() @@ -343,7 +343,7 @@ class ClassicIndexedTermTrie : IndexedTermTrie { next.values.forEach { n -> // containsAny ~~ NOT( all( NOT(contains)) if (indexMask != null) { - if (!n.indexCardinalities.forEach { idx -> !indexMask.contains(idx) }) { + if (!n.indexBits.stream().allMatch { idx -> !indexMask.contains(idx) }) { res.add(n) } } else { @@ -403,16 +403,17 @@ class ClassicIndexedTermTrie : IndexedTermTrie { fun addValue(value: T, index: Int) { assert (index >= 0) addIndex(index) - (indexedValues.get(index) ?: hashSetOf().also { indexedValues.put(index, it) }).add(value) + if (indexedValues == null) indexedValues = TIntObjectHashMap>() + (indexedValues?.get(index) ?: hashSetOf().also { indexedValues?.put(index, it) }).add(value) } fun removeValue(value: T, index: Int): Boolean { assert (index >= 0) - if (indexedValues.get(index)?.remove(value) ?: false) { - if (indexedValues.get(index)?.isEmpty() ?: false) { - indexedValues.remove(index) + if (indexedValues?.get(index)?.remove(value) ?: false) { + if (indexedValues?.get(index)?.isEmpty() ?: false) { + indexedValues?.remove(index) } - removeIndex(index) +// removeIndex(index) return true } return false diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/util/Collections.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/util/Collections.kt index aa0e133f..6f90e4b0 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/util/Collections.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/util/Collections.kt @@ -16,6 +16,7 @@ package jetbrains.mps.logic.reactor.util +import java.util.* import com.github.andrewoma.dexx.collection.ConsList as DexxCollectionConsList import com.github.andrewoma.dexx.collection.Map as DexxCollectionMap import com.github.andrewoma.dexx.collection.Maps as DexxCollectionMaps @@ -56,4 +57,7 @@ interface Sets { } } -fun PersList.without(t: T) = remove(t) \ No newline at end of file +fun PersList.without(t: T) = remove(t) + +fun arrayDequeOf(vararg elements: T): ArrayDeque = + if (elements.size == 0) ArrayDeque() else ArrayDeque(Arrays.asList(* elements))