libreda-logic 0.0.3

Logic library for LibrEDA.
Documentation
// SPDX-FileCopyrightText: 2022 Thomas Kramer <code@tkramer.ch>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Traits for truth-tables.
//! Truth-tables are used for expressing complete or partial boolean functions with a small number of inputs.

use crate::traits::{BooleanFunction, PartialBooleanFunction};

/// Abstraction for accessing and evaluating a partially defined truth-table.
pub trait PartialTruthTable: PartialBooleanFunction {
    /// Evaluate the boolean function with the given input bits encoded in an integer. The first bit is in the least significant bit.
    /// Returns `None` if the output is not specified for the given input.
    fn partial_evaluate(&self, input_bits: u64) -> Option<bool>;
}

/// Abstraction for accessing and evaluating a truth-table.
pub trait TruthTable: PartialTruthTable + BooleanFunction {
    /// Get a truth-table bit.
    /// Evaluate the boolean function with the given input bits encoded in an integer. The first bit is in the least significant bit.
    fn get_bit(&self, input_bits: u64) -> bool;

    /// Get the number of entries in this table.
    fn size(&self) -> usize {
        1 << self.num_inputs()
    }
}

/// Abstraction for modifying a truth-table.
pub trait TruthTableEdit {
    /// Set the value of an output bit in the truth-table.
    fn set_bit(&mut self, bit_index: usize, value: bool);
}