Use functor structs instead of pointer-to-template-function

release/4.3a0
Richard Roberts 2013-08-08 16:30:10 +00:00
parent 2925995d25
commit b572ad8131
3 changed files with 69 additions and 56 deletions

View File

@ -312,15 +312,18 @@ namespace gtsam {
/* ************************************************************************* */
/** Traversal function for PrintForest */
namespace {
template<typename NODE>
std::string
PrintForestVisitorPre(const boost::shared_ptr<NODE>& node, const std::string& parentString, const KeyFormatter& formatter)
struct PrintForestVisitorPre
{
const KeyFormatter& formatter;
PrintForestVisitorPre(const KeyFormatter& formatter) : formatter(formatter) {}
template<typename NODE> std::string operator()(const boost::shared_ptr<NODE>& node, const std::string& parentString)
{
// Print the current node
node->print(parentString + "-", formatter);
// Increment the indentation
return parentString + "| ";
}
};
}
/** Print a tree, prefixing each line with \c str, and formatting keys using \c keyFormatter.
@ -328,7 +331,8 @@ namespace gtsam {
template<class FOREST>
void PrintForest(const FOREST& forest, std::string str, const KeyFormatter& keyFormatter) {
typedef typename FOREST::Node Node;
DepthFirstForest(forest, str, boost::bind(PrintForestVisitorPre<Node>, _1, _2, keyFormatter));
PrintForestVisitorPre visitor(keyFormatter);
DepthFirstForest(forest, str, visitor);
}
}

View File

@ -161,12 +161,13 @@ namespace gtsam {
/* ************************************************************************* */
// Elimination post-order visitor - combine the child factors with our own factors, add the
// resulting conditional to the BayesTree, and add the remaining factor to the parent. The
// extra argument 'eliminationFunction' is passed from JunctionTree::eliminate using
// boost::bind.
// resulting conditional to the BayesTree, and add the remaining factor to the parent.
template<class JUNCTIONTREE>
void eliminationPostOrderVisitor(const typename JUNCTIONTREE::sharedNode& node, EliminationData<JUNCTIONTREE>& myData,
const typename JUNCTIONTREE::Eliminate& eliminationFunction)
struct EliminationPostOrderVisitor
{
const typename JUNCTIONTREE::Eliminate& eliminationFunction;
EliminationPostOrderVisitor(const typename JUNCTIONTREE::Eliminate& eliminationFunction) : eliminationFunction(eliminationFunction) {}
void operator()(const typename JUNCTIONTREE::sharedNode& node, EliminationData<JUNCTIONTREE>& myData)
{
// Typedefs
typedef typename JUNCTIONTREE::sharedFactor sharedFactor;
@ -202,6 +203,7 @@ namespace gtsam {
if(!eliminationResult.second->empty())
myData.parentData->childFactors[myData.myIndexInParent] = eliminationResult.second;
}
};
}
/* ************************************************************************* */
@ -278,9 +280,10 @@ namespace gtsam {
// that contains all of the roots as its children. rootsContainer also stores the remaining
// uneliminated factors passed up from the roots.
EliminationData<This> rootsContainer(0, roots_.size());
EliminationPostOrderVisitor<This> visitorPost(function);
//tbb::task_scheduler_init init(1);
treeTraversal::DepthFirstForest/*Parallel*/(*this, rootsContainer, eliminationPreOrderVisitor<This>,
boost::bind(eliminationPostOrderVisitor<This>, _1, _2, function)/*, 10*/);
treeTraversal::DepthFirstForest/*Parallel*/(*this, rootsContainer,
eliminationPreOrderVisitor<This>, visitorPost/*, 10*/);
// Create BayesTree from roots stored in the dummy BayesTree node.
boost::shared_ptr<BayesTreeType> result = boost::make_shared<BayesTreeType>();

View File

@ -50,14 +50,20 @@ namespace gtsam {
/* ************************************************************************* */
template<class TREE, class RESULT>
void eliminationPostOrderVisitor(const typename TREE::sharedNode& node, EliminationData<TREE>& myData,
RESULT& result, const typename TREE::Eliminate& eliminationFunction)
struct EliminationPostOrderVisitor
{
RESULT& result;
const typename TREE::Eliminate& eliminationFunction;
EliminationPostOrderVisitor(RESULT& result, const typename TREE::Eliminate& eliminationFunction) :
result(result), eliminationFunction(eliminationFunction) {}
void operator()(const typename TREE::sharedNode& node, EliminationData<TREE>& myData)
{
// Call eliminate on the node and add the result to the parent's gathered factors
typename TREE::sharedFactor childFactor = node->eliminate(result, eliminationFunction, myData.childFactors);
if(!childFactor->empty())
myData.parentData->childFactors.push_back(childFactor);
}
};
}
/* ************************************************************************* */
@ -79,8 +85,8 @@ namespace gtsam {
// elimination (using the gathered child factors) and store the result in the parent's
// gathered factors.
EliminationData<TREE> rootData(0, tree.roots().size());
treeTraversal::DepthFirstForest(tree, rootData, eliminationPreOrderVisitor<TREE>,
boost::bind(eliminationPostOrderVisitor<TREE,RESULT>, _1, _2, result, function));
EliminationPostOrderVisitor<TREE,RESULT> visitorPost(result, function);
treeTraversal::DepthFirstForest(tree, rootData, eliminationPreOrderVisitor<TREE>, visitorPost);
// Return remaining factors
return rootData.childFactors;