All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.it.unibo.tuprolog.bdd.impl.builder.ReducedBinaryDecisionDiagramBuilder.kt Maven / Gradle / Ivy

Go to download

Multi-platform library for representing and manipulating Binary Decision Diagrams

There is a newer version: 1.0.4
Show newest version
package it.unibo.tuprolog.bdd.impl.builder

import it.unibo.tuprolog.bdd.BinaryDecisionDiagram
import it.unibo.tuprolog.bdd.BinaryDecisionDiagramBuilder
import it.unibo.tuprolog.bdd.BinaryDecisionDiagramBuilder.Companion.defaultOf

/**
 * Returns a [BinaryDecisionDiagramBuilder] instance that applies
 * reduction optimizations through the `reduce` algorithm, and
 * delegates the construction of each node to [delegate]. By default,
 * [delegate] is set as [defaultOf]. The following reductions are
 * performed:
 * - Removal of duplicate variable nodes
 * - Removal of duplicate terminal nodes
 * - Removal of redundant variable nodes, which are
 * [BinaryDecisionDiagram.Variable] nodes where low and high point
 * to the same node
 * */
internal class ReducedBinaryDecisionDiagramBuilder>(
    private val delegate: BinaryDecisionDiagramBuilder
) : BinaryDecisionDiagramBuilder {
    private val table: MutableMap> = mutableMapOf()

    override fun buildVariable(
        value: T,
        low: BinaryDecisionDiagram,
        high: BinaryDecisionDiagram
    ): BinaryDecisionDiagram {
        if (low == high) {
            return low
        }

        val newNode = delegate.buildVariable(value, low, high)
        val key = newNode.hashCode()
        val cached = table[key]
        return if (cached != null) {
            cached
        } else {
            table[key] = newNode
            newNode
        }
    }

    override fun buildTerminal(truth: Boolean): BinaryDecisionDiagram {
        return delegate.buildTerminal(truth)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy