Trait calc_graph::Calc [−][src]
pub trait Calc { type Value; fn add_dep(&mut self, seen: &mut BitSet, dep: usize); fn eval(&mut self, dirty: &mut BitSet) -> (usize, Self::Value); }
Calculates a node's value.
Associated Types
type Value
The type of values calculated by the node.
Required Methods
fn add_dep(&mut self, seen: &mut BitSet, dep: usize)
When this node is used as a precedent, add_dep
is called by dependent nodes when they are created.
Func nodes forward calls to add_dep
to their precedents. Source nodes remember their dependencies so that they
can mark them dirty when the source node changes.
Arguments
seen
- ABitSet
that can be used to skip a call toadd_dep
when this node is reachable from a dependency via multiple routes.dep
- Theusize
id of the dependent node.
fn eval(&mut self, dirty: &mut BitSet) -> (usize, Self::Value)
Returns the value held within the node and the version number of the inputs used to calcuate that value. The value is recalculated if needed.
To calculate a node as a function of some precedent nodes:
- On creation, each func node is assigned a numerical id. If this id is not contained within the
dirty
bitset, immediately return the cached version number and value. Otherwise, remove this id from thedirty
bitset. - Call
eval
on each of the precedent nodes and remember the version number and value returned by each precedent. - Calculate
version = max(prec1_version, prec2_version, ...)
. If this version is lower than or equal to the cached version number, immediately return the cached version number and value. - Calculate a new value for this node:
value = f(prec1_value, prec2_value, ...)
. Update the cache with the calculatedversion
and the newvalue
. - Return
(version, value.clone())
.
Returns a tuple containing:
- A
usize
version number indicating the highest version number of this node's precedents - A
Clone
of the value calculated
Arguments
dirty
- ABitSet
that indicates the nodes that were marked dirty due to an update to aNode<Source>
.
Implementations on Foreign Types
impl<C: Calc> Calc for Arc<Mutex<C>>
[src]
impl<C: Calc> Calc for Arc<Mutex<C>>
Implements Calc
for SharedNode
.
impl<T> Calc for Box<Calc<Value = T> + Send>
[src]
impl<T> Calc for Box<Calc<Value = T> + Send>
Implements Calc
for BoxedNode
.
Implementors
impl<T: Clone> Calc for Source<T> type Value = T;
impl<T: Clone> Calc for Const<T> type Value = T;
impl<T: Clone, F: FnOnce() -> T> Calc for Lazy<T, F> type Value = T;
impl<C1: Calc, T: Clone + PartialEq, F: FnMut(C1::Value) -> T> Calc for Func1<C1, T, F> type Value = T;
impl<C1: Calc, C2: Calc, T: Clone + PartialEq, F: FnMut(C1::Value, C2::Value) -> T> Calc for Func2<C1, C2, T, F> type Value = T;
impl<C1: Calc, C2: Calc, C3: Calc, T: Clone + PartialEq, F: FnMut(C1::Value, C2::Value, C3::Value) -> T> Calc for Func3<C1, C2, C3, T, F> type Value = T;
impl<C1: Calc, C2: Calc, C3: Calc, C4: Calc, T: Clone + PartialEq, F: FnMut(C1::Value, C2::Value, C3::Value, C4::Value) -> T> Calc for Func4<C1, C2, C3, C4, T, F> type Value = T;
impl<C1: Calc, C2: Calc, C3: Calc, C4: Calc, C5: Calc, T: Clone + PartialEq, F: FnMut(C1::Value, C2::Value, C3::Value, C4::Value, C5::Value) -> T> Calc for Func5<C1, C2, C3, C4, C5, T, F> type Value = T;
impl<C1: Calc, C2: Calc, C3: Calc, C4: Calc, C5: Calc, C6: Calc, T: Clone + PartialEq, F: FnMut(C1::Value, C2::Value, C3::Value, C4::Value, C5::Value, C6::Value) -> T> Calc for Func6<C1, C2, C3, C4, C5, C6, T, F> type Value = T;
impl<C1: Calc, C2: Calc, C3: Calc, C4: Calc, C5: Calc, C6: Calc, C7: Calc, T: Clone + PartialEq, F: FnMut(C1::Value, C2::Value, C3::Value, C4::Value, C5::Value, C6::Value, C7::Value) -> T> Calc for Func7<C1, C2, C3, C4, C5, C6, C7, T, F> type Value = T;
impl<C1: Calc, C2: Calc, C3: Calc, C4: Calc, C5: Calc, C6: Calc, C7: Calc, C8: Calc, T: Clone + PartialEq, F: FnMut(C1::Value, C2::Value, C3::Value, C4::Value, C5::Value, C6::Value, C7::Value, C8::Value) -> T> Calc for Func8<C1, C2, C3, C4, C5, C6, C7, C8, T, F> type Value = T;