Struct mostinefficientsha::term::Term
[−]
[src]
pub struct Term { // some fields omitted }
A Term
is either constant, symbolic or the result of a logical operation
of other terms.
A Term
can be evaluated to a f64 fuzzy bit that is between 0 and 1.
During later use, many Term
s will span some large trees of logic operations.
The root term
of each tree should then be lazily evaluated if needed. The f64
evaluation result depends on the type of operation the tree consists of and
the leaves, that are either symbolic (0.0 to 1.0) or constant (0.0 or 1.0).
- The
Term
can beConstant
. Its value is eitherfalse=0.0
ortrue=1.0
. Constants are shortcutted during Term creation. For example theand
-operation between afalse
constant and another arbitraryTerm
will result in a constantfalse
becausefalse & a == false
. - The
Term
can beSymbolic
. Its value is a number between 0.0 and 1.0. In contrast to constants, symbols can not be short cut. During later use, you will set all symbols to choosen values and then evaluate the root term. - The
Term
can be a logical operation. Currently implemented are:Xor ^
,And &
,Or |
andNot !
. Except forNot
, all Terms take two operands.
The implementation uses reference counted terms RTerm = Rc<Term>
to prevent copying large parts of trees and allow caching during evaluations. This makes the Term
network structure not a tree, but an acyclic directed graph with multiple roots, I guess.
During those lazy evaluations, every Term caches its value. This caching is not done for successive calls to the root term. It is instead important since some sub trees may be used by thousands of other terms. In other words, the flattened out structure may be very large. (Gigabytes if the logic structure of SHA256 is printed into an ascii file.)
Use reset()
to clear the cache.
Methods
impl Term
[src]
fn symbol() -> RTerm
Create a Symbol
type term with unassigned value. Assign value with set()
.
fn c1() -> RTerm
Shortcut for creating a true
constant.
fn c0() -> RTerm
Shortcut for creating a false
constant.
fn constant(c: bool) -> RTerm
Create a Constant
type term with given value.
fn is_const(&self) -> bool
Checks if this term is of type constant.
fn const_val(&self) -> bool
Returns the constant value of this Term
.
Panics if type is not Constant
.
fn xor(a: &RTerm, b: &RTerm) -> RTerm
Creates a new RTerm
that lazily evaluates to the xor operation a ^ b
of the two input terms.
fn or(a: &RTerm, b: &RTerm) -> RTerm
Creates a new RTerm
that lazily evaluates to the or operation a | b
of the two input terms.
fn and(a: &RTerm, b: &RTerm) -> RTerm
Creates a new RTerm
that lazily evaluates to the and operation a & b
of the two input terms.
fn not(a: &RTerm) -> RTerm
Creates a new RTerm
that lazily evaluates to the not operation !a
of the input term.
fn half_add(a: &RTerm, b: &RTerm) -> (RTerm, RTerm)
Shortcut for ( ab , a&b ). The first term is the sum bit of a logic half adder, the second term is the carry bit.
fn full_add(a: &RTerm, b: &RTerm, carry: &RTerm) -> (RTerm, RTerm)
Shortcut for ( abc , (a&b)c&(ab) ). The first term is the sum bit of a logic full adder, the second term is the carry bit.
fn set(&self, n: f64)
Sets the value of a Symbol
type term.
Panics if the type is not Symbol
.
fn reset(&self)
Reset the cached value of this term and all terms this term depends on. Does not reset anything if this term has already been reset.
fn evaluate(&self) -> f64
Evaluate this term by recursively evaluating all sub terms.
Cache the evaluated value.
Xor: result = abs(a-b)
And: result = a*b
Or: result = min[ sqrt(a)+sqrt(b) , 1 ]
Not: result = 1-a
fn max_logic_depth_and_max_stack_size(&self, stack_cnt: usize) -> (usize, usize)
Stack size needed when evaluating this Term
recursively.
The stack_cnt is the current stack size. (Use 0 at the root term).
It also returns the maximum logic depth. This value is created by
utilizing the eval cache. So you must call reset()
before and after
using this function.
Returns: (max_logic_depth, max_stacksize)
fn nr_of_terms(&self) -> usize
Returns the number of RTerms that contribute to the evaluation of this RTerm.
This function uses the eval cache, so you must call reset()
before and
after using this function.
To prevent double counting, this function returns the actual number on
the first call and zero on all following calls.
Returns: number_of_uncounted_rterms_including_this_rterm
fn nr_of_terms_flattened(&self) -> usize
Returns the number of Terms that contribute to the evaluation if the tree would have been flattened.
Trait Implementations
impl Clone for Term
[src]
fn clone(&self) -> Term
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more