pub trait Calc {
type Value;
// Required methods
fn add_dep(&mut self, seen: &mut BitSet, dep: NonZeroUsize);
fn eval(&mut self, dirty: &mut BitSet) -> (NonZeroUsize, Self::Value);
}
Expand description
Calculates a node’s value.
Required Associated Types§
Required Methods§
Sourcefn add_dep(&mut self, seen: &mut BitSet, dep: NonZeroUsize)
fn add_dep(&mut self, seen: &mut BitSet, dep: NonZeroUsize)
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
- The id of the dependent node.
Sourcefn eval(&mut self, dirty: &mut BitSet) -> (NonZeroUsize, Self::Value)
fn eval(&mut self, dirty: &mut BitSet) -> (NonZeroUsize, 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
NonZeroUsize
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>
.