Adapt to the new persistent collections API.

This commit is contained in:
Fedor Isakov 2019-09-28 16:43:07 +02:00
parent 8ce96ec41e
commit c7b07f3b1a
6 changed files with 44 additions and 20 deletions

View File

@ -24,6 +24,7 @@ import jetbrains.mps.logic.reactor.logical.Logical
import jetbrains.mps.logic.reactor.logical.LogicalOwner
import jetbrains.mps.logic.reactor.logical.MetaLogical
import jetbrains.mps.logic.reactor.program.Constraint
import jetbrains.mps.logic.reactor.util.assoc
import jetbrains.mps.unification.Term
internal class OccurrenceMatcherImpl(val contextSubst: Subst? = null) : OccurrenceMatcher {
@ -70,7 +71,7 @@ internal class OccurrenceMatcherImpl(val contextSubst: Subst? = null) : Occurren
if (matchSubst!!.containsKey(ptn))
matchSubst!![ptn].let { matchAny(it, trg) }
else
matchSubst!!.put(ptn, trg!!).also { matchSubst = it }.run { true }
matchSubst!!.assoc(ptn, trg!!).also { matchSubst = it }.run { true }
}
is Term ->
when {

View File

@ -68,14 +68,14 @@ internal class RuleMatcherImpl(private val ruleLookup: RuleLookup,
leafNodes,
leafSignatures,
seenOccurrences,
consumedSignatures.add(ruleMatch.signatureArray().toSignature()))
consumedSignatures.put(ruleMatch.signatureArray().toSignature()))
override fun forget(ruleMatch: RuleMatchEx): RuleMatchingProbe =
RuleMatchFront(trunkNodes,
leafNodes,
leafSignatures,
seenOccurrences,
consumedSignatures.remove(ruleMatch.signatureArray().toSignature()))
consumedSignatures.without(ruleMatch.signatureArray().toSignature()))
override fun expand(occ: Occurrence): RuleMatchingProbe =
expand(occ, bitSetOfOnes(head.size))
@ -86,7 +86,7 @@ internal class RuleMatcherImpl(private val ruleLookup: RuleLookup,
*/
override fun expand(occ: Occurrence, mask: BitSet, profiler: Profiler?): RuleMatchFront {
val reactivated = seenOccurrences.contains(occ.identity)
val newSeen = if (reactivated) seenOccurrences else seenOccurrences.add(occ.identity)
val newSeen = if (reactivated) seenOccurrences else seenOccurrences.put(occ.identity)
val newTrunkNodes = ArrayList<MatchNode>(trunkNodes.size).apply { addAll(trunkNodes) }
val newLeafNodes = arrayListOf<MatchNode>()
var newSignatures = leafSignatures
@ -123,7 +123,7 @@ internal class RuleMatcherImpl(private val ruleLookup: RuleLookup,
// ...unless propagation (to avoid cycles)
if (reactivated && propagation && consumedSignatures.contains(signature)) break
newLeafNodes.add(ex)
newSignatures = newSignatures.add(signature)
newSignatures = newSignatures.put(signature)
}
newTrunkNodes.add(ex)
@ -141,7 +141,7 @@ internal class RuleMatcherImpl(private val ruleLookup: RuleLookup,
if (!node.hasOccurrence(occ)) {
newTrunkNodes.add(node)
if (node.leaf) {
newSignatures = newSignatures.add(node.signature())
newSignatures = newSignatures.without(node.signature())
}
}
}
@ -155,7 +155,7 @@ internal class RuleMatcherImpl(private val ruleLookup: RuleLookup,
}
override fun forgetSeen(occ: Occurrence): RuleMatchFront {
val newSeen = seenOccurrences.remove(occ.identity)
val newSeen = seenOccurrences.without(occ.identity)
return RuleMatchFront(trunkNodes, leafNodes, leafSignatures, newSeen, consumedSignatures) // NB: not modifying genId
}
@ -186,7 +186,7 @@ internal class RuleMatcherImpl(private val ruleLookup: RuleLookup,
val leaf = vacant.cardinality() == 0
val trail: PersSet<Int> =
if (parent is MatchNode) parent.trail.add(occurrence.identity) else Sets.of(occurrence.identity)
if (parent is MatchNode) parent.trail.put(occurrence.identity) else Sets.of(occurrence.identity)
fun constraint(): Constraint = head[headIndex]

View File

@ -16,7 +16,7 @@
package jetbrains.mps.logic.reactor.core.internal
import jetbrains.mps.logic.reactor.core.*
import jetbrains.mps.logic.reactor.core.LogicalObserver
import jetbrains.mps.logic.reactor.logical.Logical
import jetbrains.mps.logic.reactor.util.*
@ -36,6 +36,7 @@ import jetbrains.mps.logic.reactor.util.*
internal class StateFrame constructor() : LogicalObserver
{
// FIXME use Vector instead of ConsList
private var observers: PersMap<Id<Logical<*>>, PersList<LogicalObserver>> = Maps.of()
constructor(prototype: StateFrame) : this() {
@ -49,14 +50,14 @@ internal class StateFrame constructor() : LogicalObserver
fun addForwardingObserver(logical: Logical<*>, observer: LogicalObserver) {
val logicalId = Id(logical)
this.observers = observers.put(logicalId,
this.observers = observers.assoc(logicalId,
observers[logicalId]?.prepend(observer) ?: Lists.of(observer))
}
fun removeForwardingObserver(logical: Logical<*>, observer: LogicalObserver) {
val logicalId = Id(logical)
observers[logicalId]?.let {
this.observers = observers.put(logicalId, it.remove(observer))
this.observers = observers.assoc(logicalId, it.remove(observer)!!)
}
}

View File

@ -55,3 +55,5 @@ interface Sets {
fun <T> copyOf(it: Iterable<T>) = DexxCollectionSets.copyOf<T>(it)
}
}
fun <T> PersList<T>.without(t: T) = remove(t)

View File

@ -17,6 +17,9 @@
package jetbrains.mps.logic.reactor.util
import com.github.andrewoma.dexx.collection.ConsList
import com.github.andrewoma.dexx.collection.Map
import com.github.andrewoma.dexx.collection.Pair
import com.github.andrewoma.dexx.collection.Set
/**
* @author Fedor Isakov
@ -37,4 +40,14 @@ fun <E> ConsList<E>?.remove(e: E): ConsList<E>? {
val idx = indexOf(e)
if (idx >= 0) removeAt(idx) else this
}
}
}
fun <T> Set<T>.put(t: T) = add(t)
fun <T> Set<T>.without(t: T) = remove(t)
fun <K,V> Map<K, V>.assoc(k: K, v: V) = put(k, v)
fun <K,V> Map<K, V>.without(k: K) = remove(k)
fun <K,V> Pair<K,V>.getValue(): V = component2()

View File

@ -167,7 +167,7 @@ class PersistentTermTrie<T>() : TermTrie<T> {
} else {
// wildcard consumes the rest of the trie
node.allNext().forEach { visitAll(it, visitor) }
node.forEachNext { visitAll(it, visitor) }
}
} else {
@ -202,7 +202,7 @@ class PersistentTermTrie<T>() : TermTrie<T> {
*/
private fun visitAll(node: PathNode<T>, visitor: (T) -> Unit) {
node.values().forEach(visitor)
node.allNext().forEach { visitAll(it, visitor) }
node.forEachNext { visitAll(it, visitor) }
}
private fun deref(term: Term): Term {
@ -247,7 +247,14 @@ class PersistentTermTrie<T>() : TermTrie<T> {
/**
* Returns all trie nodes that are direct successors of this one.
*/
fun allNext(): Iterable<PathNode<T>> = next.values()
// fun allNext(): Iterable<PathNode<T>> = next.values()
inline fun forEachNext(code: (PathNode<T>) -> Unit) {
val it = next.iterator()
while (it.hasNext()) {
code(it.next().getValue())
}
}
/**
* Returns a pair of iterables:
@ -275,8 +282,8 @@ class PersistentTermTrie<T>() : TermTrie<T> {
val edge = ArrayList<PathNode<T>>()
val stack = ArrayList<Pair<PathNode<T>, Int>>()
for (n in allNext()) {
stack.push(n to 0)
forEachNext {
stack.push(it to 0)
}
while (stack.isNotEmpty()) {
@ -287,16 +294,16 @@ class PersistentTermTrie<T>() : TermTrie<T> {
} else {
term.add(n)
n.allNext().forEach { stack.push(it to (newCount - 1)) }
n.forEachNext { stack.push(it to (newCount - 1)) }
}
}
return term to edge
}
fun putNext(node: PathNode<T>): PathNode<T> = PathNode(this, next.put(node.symbol, node))
fun putNext(node: PathNode<T>): PathNode<T> = PathNode(this, next.assoc(node.symbol, node))
fun removeNext(node: PathNode<T>): PathNode<T> = PathNode(this, next.remove(node.symbol))
fun removeNext(node: PathNode<T>): PathNode<T> = PathNode(this, next.without(node.symbol))
fun addValue(value: T): PathNode<T> = PathNode(this, values.add(value))