Trait calc_graph::Calc

source ·
pub trait Calc {
    type Value;

    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

The type of values calculated by the node.

Required Methods

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 - A BitSet that can be used to skip a call to add_dep when this node is reachable from a dependency via multiple routes.
  • dep - The id of the dependent node.

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:

  1. 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 the dirty bitset.
  2. Call eval on each of the precedent nodes and remember the version number and value returned by each precedent.
  3. 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.
  4. Calculate a new value for this node: value = f(prec1_value, prec2_value, ...). Update the cache with the calculated version and the new value.
  5. 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 - A BitSet that indicates the nodes that were marked dirty due to an update to a Node<Source>.

Trait Implementations

Implements Calc for BoxedNode.

The type of values calculated by the node.
When this node is used as a precedent, add_dep is called by dependent nodes when they are created. Read more
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. Read more

Implementations on Foreign Types

Implements Calc for SharedNode.

Implements Calc for BoxedNode.

Implementors