commonMain.it.unibo.tuprolog.collections.rete.generic.AbstractLeafReteNode.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of theory-jvm Show documentation
Show all versions of theory-jvm Show documentation
In-memory storage and indexing facilities for ordered and unordered knowledge bases composed by logic clauses
package it.unibo.tuprolog.collections.rete.generic
import it.unibo.tuprolog.core.Clause
import kotlin.math.min
/** A leaf Rete Node */
internal abstract class AbstractLeafReteNode(
override val children: MutableMap> = mutableMapOf(),
) : AbstractReteNode(children) {
/** Internal data structure to store leaf elements */
protected abstract val leafElements: MutableList
override val indexedElements: Sequence
get() = leafElements.asSequence()
override fun put(
element: E,
beforeOthers: Boolean,
) {
if (beforeOthers) {
leafElements.add(0, element)
} else {
leafElements.add(element)
}
}
override fun removeWithLimit(
element: E,
limit: Int,
): Sequence =
get(element)
.take(if (limit > 0) min(limit, leafElements.count()) else leafElements.count())
.toList()
.asSequence()
.also { leafElements.removeAll(it) }
override fun removeAll(element: E): Sequence =
get(element)
.toList()
.asSequence()
.also { leafElements.removeAll(it) }
override fun toString(treefy: Boolean): String =
if (treefy) {
"$header {${leafElements.joinToString(".\n\t", "\n\t", ".\n")}}"
} else {
toString()
}
}