Formatting

release/4.3a0
dellaert 2015-06-21 13:56:48 -07:00
parent e2d49922d2
commit 22b7d8276a
1 changed files with 200 additions and 195 deletions

View File

@ -46,13 +46,15 @@ namespace gtsam {
DATA& parentData; DATA& parentData;
typename FastList<DATA>::iterator dataPointer; typename FastList<DATA>::iterator dataPointer;
TraversalNode(const boost::shared_ptr<NODE>& _treeNode, DATA& _parentData) : TraversalNode(const boost::shared_ptr<NODE>& _treeNode, DATA& _parentData) :
expanded(false), treeNode(_treeNode), parentData(_parentData) {} expanded(false), treeNode(_treeNode), parentData(_parentData) {
}
}; };
// Do nothing - default argument for post-visitor for tree traversal // Do nothing - default argument for post-visitor for tree traversal
struct no_op { struct no_op {
template<typename NODE, typename DATA> template<typename NODE, typename DATA>
void operator()(const boost::shared_ptr<NODE>& node, const DATA& data) {} void operator()(const boost::shared_ptr<NODE>& node, const DATA& data) {
}
}; };
} }
@ -71,9 +73,10 @@ namespace gtsam {
* call to \c visitorPre (the \c DATA object may be modified by visiting the children). * call to \c visitorPre (the \c DATA object may be modified by visiting the children).
* @param rootData The data to pass by reference to \c visitorPre when it is called on each * @param rootData The data to pass by reference to \c visitorPre when it is called on each
* root node. */ * root node. */
template<class FOREST, typename DATA, typename VISITOR_PRE, typename VISITOR_POST> template<class FOREST, typename DATA, typename VISITOR_PRE,
void DepthFirstForest(FOREST& forest, DATA& rootData, VISITOR_PRE& visitorPre, VISITOR_POST& visitorPost) typename VISITOR_POST>
{ void DepthFirstForest(FOREST& forest, DATA& rootData, VISITOR_PRE& visitorPre,
VISITOR_POST& visitorPost) {
// Typedefs // Typedefs
typedef typename FOREST::Node Node; typedef typename FOREST::Node Node;
typedef boost::shared_ptr<Node> sharedNode; typedef boost::shared_ptr<Node> sharedNode;
@ -92,8 +95,7 @@ namespace gtsam {
} }
// Traverse // Traverse
while(!stack.empty()) while (!stack.empty()) {
{
// Get next node // Get next node
TraversalNode& node = stack.front(); TraversalNode& node = stack.front();
@ -106,7 +108,8 @@ namespace gtsam {
} else { } else {
// If not already visited, visit the node and add its children (use reverse iterators so // If not already visited, visit the node and add its children (use reverse iterators so
// children are processed in the order they appear) // children are processed in the order they appear)
node.dataPointer = dataList.insert(dataList.end(), visitorPre(node.treeNode, node.parentData)); node.dataPointer = dataList.insert(dataList.end(),
visitorPre(node.treeNode, node.parentData));
typename Stack::iterator insertLocation = stack.begin(); typename Stack::iterator insertLocation = stack.begin();
BOOST_FOREACH(const sharedNode& child, node.treeNode->children) BOOST_FOREACH(const sharedNode& child, node.treeNode->children)
stack.insert(insertLocation, TraversalNode(child, *node.dataPointer)); stack.insert(insertLocation, TraversalNode(child, *node.dataPointer));
@ -128,8 +131,7 @@ namespace gtsam {
* @param rootData The data to pass by reference to \c visitorPre when it is called on each * @param rootData The data to pass by reference to \c visitorPre when it is called on each
* root node. */ * root node. */
template<class FOREST, typename DATA, typename VISITOR_PRE> template<class FOREST, typename DATA, typename VISITOR_PRE>
void DepthFirstForest(FOREST& forest, DATA& rootData, VISITOR_PRE& visitorPre) void DepthFirstForest(FOREST& forest, DATA& rootData, VISITOR_PRE& visitorPre) {
{
no_op visitorPost; no_op visitorPost;
DepthFirstForest(forest, rootData, visitorPre, visitorPost); DepthFirstForest(forest, rootData, visitorPre, visitorPost);
} }
@ -148,30 +150,31 @@ namespace gtsam {
* call to \c visitorPre (the \c DATA object may be modified by visiting the children). * call to \c visitorPre (the \c DATA object may be modified by visiting the children).
* @param rootData The data to pass by reference to \c visitorPre when it is called on each * @param rootData The data to pass by reference to \c visitorPre when it is called on each
* root node. */ * root node. */
template<class FOREST, typename DATA, typename VISITOR_PRE, typename VISITOR_POST> template<class FOREST, typename DATA, typename VISITOR_PRE,
void DepthFirstForestParallel(FOREST& forest, DATA& rootData, VISITOR_PRE& visitorPre, VISITOR_POST& visitorPost, typename VISITOR_POST>
int problemSizeThreshold = 10) void DepthFirstForestParallel(FOREST& forest, DATA& rootData,
{ VISITOR_PRE& visitorPre, VISITOR_POST& visitorPost,
int problemSizeThreshold = 10) {
#ifdef GTSAM_USE_TBB #ifdef GTSAM_USE_TBB
// Typedefs // Typedefs
typedef typename FOREST::Node Node; typedef typename FOREST::Node Node;
typedef boost::shared_ptr<Node> sharedNode; typedef boost::shared_ptr<Node> sharedNode;
tbb::task::spawn_root_and_wait(internal::CreateRootTask<Node>( tbb::task::spawn_root_and_wait(
forest.roots(), rootData, visitorPre, visitorPost, problemSizeThreshold)); internal::CreateRootTask<Node>(forest.roots(), rootData, visitorPre,
visitorPost, problemSizeThreshold));
#else #else
DepthFirstForest(forest, rootData, visitorPre, visitorPost); DepthFirstForest(forest, rootData, visitorPre, visitorPost);
#endif #endif
} }
/* ************************************************************************* */ /* ************************************************************************* */
/** Traversal function for CloneForest */ /** Traversal function for CloneForest */
namespace { namespace {
template<typename NODE> template<typename NODE>
boost::shared_ptr<NODE> boost::shared_ptr<NODE> CloneForestVisitorPre(
CloneForestVisitorPre(const boost::shared_ptr<NODE>& node, const boost::shared_ptr<NODE>& parentPointer) const boost::shared_ptr<NODE>& node,
{ const boost::shared_ptr<NODE>& parentPointer) {
// Clone the current node and add it to its cloned parent // Clone the current node and add it to its cloned parent
boost::shared_ptr<NODE> clone = boost::make_shared<NODE>(*node); boost::shared_ptr<NODE> clone = boost::make_shared<NODE>(*node);
clone->children.clear(); clone->children.clear();
@ -186,24 +189,25 @@ namespace gtsam {
* return a collection of shared pointers to \c FOREST::Node. * return a collection of shared pointers to \c FOREST::Node.
* @return The new collection of roots. */ * @return The new collection of roots. */
template<class FOREST> template<class FOREST>
FastVector<boost::shared_ptr<typename FOREST::Node> > CloneForest(const FOREST& forest) FastVector<boost::shared_ptr<typename FOREST::Node> > CloneForest(
{ const FOREST& forest) {
typedef typename FOREST::Node Node; typedef typename FOREST::Node Node;
boost::shared_ptr<Node> rootContainer = boost::make_shared<Node>(); boost::shared_ptr<Node> rootContainer = boost::make_shared<Node>();
DepthFirstForest(forest, rootContainer, CloneForestVisitorPre<Node>); DepthFirstForest(forest, rootContainer, CloneForestVisitorPre<Node>);
return FastVector<boost::shared_ptr<Node> >(rootContainer->children.begin(), rootContainer->children.end()); return FastVector<boost::shared_ptr<Node> >(rootContainer->children.begin(),
rootContainer->children.end());
} }
/* ************************************************************************* */ /* ************************************************************************* */
/** Traversal function for PrintForest */ /** Traversal function for PrintForest */
namespace { namespace {
struct PrintForestVisitorPre struct PrintForestVisitorPre {
{
const KeyFormatter& formatter; const KeyFormatter& formatter;
PrintForestVisitorPre(const KeyFormatter& formatter) : formatter(formatter) {} PrintForestVisitorPre(const KeyFormatter& formatter) :
template<typename NODE> std::string operator()(const boost::shared_ptr<NODE>& node, const std::string& parentString) formatter(formatter) {
{ }
template<typename NODE> std::string operator()(
const boost::shared_ptr<NODE>& node, const std::string& parentString) {
// Print the current node // Print the current node
node->print(parentString + "-", formatter); node->print(parentString + "-", formatter);
// Increment the indentation // Increment the indentation
@ -215,7 +219,8 @@ namespace gtsam {
/** Print a tree, prefixing each line with \c str, and formatting keys using \c keyFormatter. /** Print a tree, prefixing each line with \c str, and formatting keys using \c keyFormatter.
* To print each node, this function calls the \c print function of the tree nodes. */ * To print each node, this function calls the \c print function of the tree nodes. */
template<class FOREST> template<class FOREST>
void PrintForest(const FOREST& forest, std::string str, const KeyFormatter& keyFormatter) { void PrintForest(const FOREST& forest, std::string str,
const KeyFormatter& keyFormatter) {
PrintForestVisitorPre visitor(keyFormatter); PrintForestVisitorPre visitor(keyFormatter);
DepthFirstForest(forest, str, visitor); DepthFirstForest(forest, str, visitor);
} }