libreda_logic/truth_table/traits.rs
1// SPDX-FileCopyrightText: 2022 Thomas Kramer <code@tkramer.ch>
2//
3// SPDX-License-Identifier: AGPL-3.0-or-later
4
5//! Traits for truth-tables.
6//! Truth-tables are used for expressing complete or partial boolean functions with a small number of inputs.
7
8use crate::traits::{BooleanFunction, PartialBooleanFunction};
9
10/// Abstraction for accessing and evaluating a partially defined truth-table.
11pub trait PartialTruthTable: PartialBooleanFunction {
12 /// Evaluate the boolean function with the given input bits encoded in an integer. The first bit is in the least significant bit.
13 /// Returns `None` if the output is not specified for the given input.
14 fn partial_evaluate(&self, input_bits: u64) -> Option<bool>;
15}
16
17/// Abstraction for accessing and evaluating a truth-table.
18pub trait TruthTable: PartialTruthTable + BooleanFunction {
19 /// Get a truth-table bit.
20 /// Evaluate the boolean function with the given input bits encoded in an integer. The first bit is in the least significant bit.
21 fn get_bit(&self, input_bits: u64) -> bool;
22
23 /// Get the number of entries in this table.
24 fn size(&self) -> usize {
25 1 << self.num_inputs()
26 }
27}
28
29/// Abstraction for modifying a truth-table.
30pub trait TruthTableEdit {
31 /// Set the value of an output bit in the truth-table.
32 fn set_bit(&mut self, bit_index: usize, value: bool);
33}