bpp-phyl3  3.0.0
bpp::Context Class Reference

Context for dataflow node construction. More...

#include <Bpp/Phyl/Likelihood/DataFlow/DataFlow.h>

+ Collaboration diagram for bpp::Context:

Classes

struct  CachedNodeRef
 NodeRef is hashable and comparable as a pointer. CachedNodeRef is hashable and comparable, by comparing the node configuration: More...
 
struct  CachedNodeRefHash
 

Public Member Functions

 Context ()
 
NodeRef cached (NodeRef &&newNode)
 For a newly created node, return its equivalent from the cache. If not already present in the cache, add it and return newNode. If already present in the cache, return the stored one. More...
 
NodeRef cached (NodeRef &newNode)
 
size_t size () const
 
void clear ()
 Clear the context. More...
 
bool erase (const NodeRef &r)
 Remove an element from the map, only if it has no dependent nodes, and likewise down the graph. More...
 
NodeRef getZero ()
 
std::vector< const Node_DF * > getAllNodes () const
 Returns the vector of all pointers to all nodes. More...
 

Private Attributes

std::unordered_set< CachedNodeRef, CachedNodeRefHashnodeCache_
 
NodeRef zero_
 

Detailed Description

Context for dataflow node construction.

A context argument is passed to every function constructing dataflow nodes. This class can thus be used to provide construction-time features.

The only feature for now is merging of nodes representing the same value. Each Context instance stores the set of nodes created with it. When passed to a node creation function (create, derive, recreate), the set is updated and used to prevent duplicate nodes. Internally, creation functions will call Context::cached(newlyCreatedNode), which will return an old node and discard the new if it already exists, or update the set and return the new node. Note that the Context owns shared_ptr to nodes, so they will not be destroyed unless the context is destroyed. Multiple context can be used for independent computations.

Nodes are merged if they represent the same value. As the value is not computed yet, two nodes are merged if they have:

  • the same derived class type (same computation code),
  • the same dependencies (same input value),
  • same additional arguments (constants, etc). Which is equivalent to the value if all additional arguments are compared. (compare|hash)AdditionalArguments implement polymorphic comparison of additional arguments.

Definition at line 526 of file DataFlow.h.

Constructor & Destructor Documentation

◆ Context()

bpp::Context::Context ( )

Definition at line 311 of file DataFlow.cpp.

Member Function Documentation

◆ cached() [1/2]

NodeRef bpp::Context::cached ( NodeRef &&  newNode)

For a newly created node, return its equivalent from the cache. If not already present in the cache, add it and return newNode. If already present in the cache, return the stored one.

The returned node is always of the same derived class than newNode. It represents the same value.

Definition at line 315 of file DataFlow.cpp.

References nodeCache_.

Referenced by bpp::cachedAs().

◆ cached() [2/2]

NodeRef bpp::Context::cached ( NodeRef newNode)

Definition at line 323 of file DataFlow.cpp.

References nodeCache_.

◆ clear()

void bpp::Context::clear ( )

Clear the context.

Definition at line 340 of file DataFlow.cpp.

References erase(), and nodeCache_.

◆ erase()

bool bpp::Context::erase ( const NodeRef r)

Remove an element from the map, only if it has no dependent nodes, and likewise down the graph.

Parameters
rthe upstream node to be removed
Returns
boolean: if the node has been removed from the map.

Definition at line 363 of file DataFlow.cpp.

References nodeCache_.

Referenced by bpp::LikelihoodCalculation::cleanAllLikelihoods(), bpp::AlignedLikelihoodCalculation::cleanAllLikelihoods(), and clear().

◆ getAllNodes()

std::vector< const Node_DF * > bpp::Context::getAllNodes ( ) const

Returns the vector of all pointers to all nodes.

Definition at line 352 of file DataFlow.cpp.

References nodeCache_.

◆ getZero()

NodeRef bpp::Context::getZero ( )
inline

◆ size()

size_t bpp::Context::size ( ) const
inline

Definition at line 543 of file DataFlow.h.

References nodeCache_.

Member Data Documentation

◆ nodeCache_

std::unordered_set<CachedNodeRef, CachedNodeRefHash> bpp::Context::nodeCache_
private

Definition at line 605 of file DataFlow.h.

Referenced by cached(), clear(), erase(), getAllNodes(), and size().

◆ zero_

NodeRef bpp::Context::zero_
private

Definition at line 607 of file DataFlow.h.

Referenced by getZero().


The documentation for this class was generated from the following files: