pub enum BooleanExpr<Var> {
False,
True,
Var(Var),
Neg(Box<Self>),
And(Box<Self>, Box<Self>),
Or(Box<Self>, Box<Self>),
Xor(Box<Self>, Box<Self>),
}Expand description
Boolean expression.
Variants§
False
Boolean value False.
True
Boolean value True.
Var(Var)
Variable in an expression.
Neg(Box<Self>)
Unary negation of an expression.
And(Box<Self>, Box<Self>)
Binary Boolean AND of two expressions.
Or(Box<Self>, Box<Self>)
Binary Boolean OR of two expressions.
Xor(Box<Self>, Box<Self>)
Binary Boolean XOR of two expressions.
Implementations§
Source§impl<V> BooleanExpr<V>
impl<V> BooleanExpr<V>
Sourcepub fn eval<B>(&self, value_fn: &impl Fn(&V) -> B) -> B
pub fn eval<B>(&self, value_fn: &impl Fn(&V) -> B) -> B
Evaluate the expression.
Variable are passed with the callback function value_fn.
The type B usually represents Boolean logic values. For example bool can be used.
Note that B::default() is required to return the Boolean value false.
§Example
use liberty_io::boolean::*;
// Create a simple boolean expression.
let a = BooleanExpr::Var(0);
let b = BooleanExpr::Var(1);
let expr = a ^ b;
let values = [true, false];
assert_eq!(expr.eval(&|var| values[*var]), true);
let values = [true, true];
assert_eq!(expr.eval(&|var| values[*var]), false);Sourcepub fn substitute_variable_names<V2>(
&self,
substitution: &impl Fn(&V) -> V2,
) -> BooleanExpr<V2>
pub fn substitute_variable_names<V2>( &self, substitution: &impl Fn(&V) -> V2, ) -> BooleanExpr<V2>
Substitute variables in the expression. Can also be used to change the data-type of the variables.
§Example
use liberty_io::boolean::*;
// Create a simple boolean expression.
let a = BooleanExpr::Var(0);
let b = BooleanExpr::Var(1);
let expr = a & b;
// Define the variable mapping used for the substitution.
let substitution = |var: &usize| -> String {
match var {
0 => "a".into(),
1 => "b".into(),
_ => panic!("no such variable")
}
};
// Apply the substitution.
let substituted = expr.substitute_variable_names(&substitution);Sourcepub fn substitute_variables<V2>(
&self,
substitution: &impl Fn(&V) -> BooleanExpr<V2>,
) -> BooleanExpr<V2>
pub fn substitute_variables<V2>( &self, substitution: &impl Fn(&V) -> BooleanExpr<V2>, ) -> BooleanExpr<V2>
Substitute all variables by an expression.
§Example
Replace some variables by a constant:
use liberty_io::boolean::*;
// Create a simple boolean expression.
let a = BooleanExpr::Var("a");
let b = BooleanExpr::Var("b");
let expr = a & b;
// Define the variable mapping used for the substitution.
let substitution = |&var: &&'static str| -> BooleanExpr<&'static str> {
match var {
"a" => BooleanExpr::False, // Replace "a".
var => BooleanExpr::Var(var) // Don't do anything else for all other variables.
}
};
// Apply the substitution.
let substituted = expr.substitute_variables(&substitution);Trait Implementations§
Source§impl<V> Add for BooleanExpr<V>
impl<V> Add for BooleanExpr<V>
Source§impl<V> BitAnd for BooleanExpr<V>
impl<V> BitAnd for BooleanExpr<V>
Source§impl<V> BitOr for BooleanExpr<V>
impl<V> BitOr for BooleanExpr<V>
Source§impl<V> BitXor for BooleanExpr<V>
impl<V> BitXor for BooleanExpr<V>
Source§impl<Var: Clone> Clone for BooleanExpr<Var>
impl<Var: Clone> Clone for BooleanExpr<Var>
Source§impl<Var: Debug> Debug for BooleanExpr<Var>
impl<Var: Debug> Debug for BooleanExpr<Var>
Source§impl<V> Display for BooleanExpr<V>where
V: Display,
impl<V> Display for BooleanExpr<V>where
V: Display,
impl<Var: Eq> Eq for BooleanExpr<Var>
Source§impl<V> From<bool> for BooleanExpr<V>
impl<V> From<bool> for BooleanExpr<V>
Source§impl<Var: Hash> Hash for BooleanExpr<Var>
impl<Var: Hash> Hash for BooleanExpr<Var>
Source§impl<V> Mul for BooleanExpr<V>
impl<V> Mul for BooleanExpr<V>
Source§impl<V> Not for BooleanExpr<V>
impl<V> Not for BooleanExpr<V>
Source§impl<Var: PartialEq> PartialEq for BooleanExpr<Var>
impl<Var: PartialEq> PartialEq for BooleanExpr<Var>
impl<Var: PartialEq> StructuralPartialEq for BooleanExpr<Var>
Auto Trait Implementations§
impl<Var> Freeze for BooleanExpr<Var>
impl<Var> RefUnwindSafe for BooleanExpr<Var>
impl<Var> Send for BooleanExpr<Var>
impl<Var> Sync for BooleanExpr<Var>
impl<Var> Unpin for BooleanExpr<Var>
impl<Var> UnsafeUnpin for BooleanExpr<Var>
impl<Var> UnwindSafe for BooleanExpr<Var>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more