19 #include <gtsam/base/treeTraversal/parallelTraversalTasks.h>
20 #include <gtsam/base/treeTraversal/statistics.h>
29 #include <boost/shared_ptr.hpp>
30 #include <boost/make_shared.hpp>
31 #include <boost/foreach.hpp>
32 #include <boost/bind.hpp>
37 namespace treeTraversal {
42 template<
typename NODE,
typename DATA>
43 struct TraversalNode {
45 const boost::shared_ptr<NODE>& treeNode;
48 TraversalNode(
const boost::shared_ptr<NODE>& _treeNode, DATA& _parentData) :
49 expanded(
false), treeNode(_treeNode), parentData(_parentData) {}
54 template<
typename NODE,
typename DATA>
55 void operator()(
const boost::shared_ptr<NODE>& node,
const DATA& data) {}
74 template<
class FOREST,
typename DATA,
typename VISITOR_PRE,
typename VISITOR_POST>
75 void DepthFirstForest(FOREST& forest, DATA& rootData, VISITOR_PRE& visitorPre, VISITOR_POST& visitorPost)
78 typedef typename FOREST::Node Node;
79 typedef boost::shared_ptr<Node> sharedNode;
82 typedef TraversalNode<typename FOREST::Node, DATA> TraversalNode;
89 typename Stack::iterator insertLocation = stack.begin();
90 BOOST_FOREACH(
const sharedNode& root, forest.roots())
91 stack.insert(insertLocation, TraversalNode(root, rootData));
98 TraversalNode& node = stack.front();
103 (void) visitorPost(node.treeNode, *node.dataPointer);
104 dataList.erase(node.dataPointer);
109 node.dataPointer = dataList.insert(dataList.end(), visitorPre(node.treeNode, node.parentData));
110 typename Stack::iterator insertLocation = stack.begin();
111 BOOST_FOREACH(
const sharedNode& child, node.treeNode->children)
112 stack.insert(insertLocation, TraversalNode(child, *node.dataPointer));
113 node.expanded =
true;
116 assert(dataList.empty());
130 template<
class FOREST,
typename DATA,
typename VISITOR_PRE>
151 template<
class FOREST,
typename DATA,
typename VISITOR_PRE,
typename VISITOR_POST>
153 int problemSizeThreshold = 10)
157 typedef typename FOREST::Node Node;
158 typedef boost::shared_ptr<Node> sharedNode;
160 tbb::task::spawn_root_and_wait(internal::CreateRootTask<Node>(
161 forest.roots(), rootData, visitorPre, visitorPost, problemSizeThreshold));
171 template<
typename NODE>
172 boost::shared_ptr<NODE>
173 CloneForestVisitorPre(
const boost::shared_ptr<NODE>& node,
const boost::shared_ptr<NODE>& parentPointer)
176 boost::shared_ptr<NODE> clone = boost::make_shared<NODE>(*node);
177 clone->children.clear();
178 parentPointer->children.push_back(clone);
188 template<
class FOREST>
191 typedef typename FOREST::Node Node;
192 boost::shared_ptr<Node> rootContainer = boost::make_shared<Node>();
201 struct PrintForestVisitorPre
204 PrintForestVisitorPre(
const KeyFormatter& formatter) : formatter(formatter) {}
205 template<
typename NODE> std::string operator()(
const boost::shared_ptr<NODE>& node,
const std::string& parentString)
208 node->print(parentString +
"-", formatter);
210 return parentString +
"| ";
217 template<
class FOREST>
219 PrintForestVisitorPre visitor(keyFormatter);
Matrix stack(size_t nrMatrices,...)
create a matrix by stacking other matrices Given a set of matrices: A1, A2, A3... ...
Definition: Matrix.cpp:458
A thin wrapper around std::list that uses boost's fast_pool_allocator.
FastVector< boost::shared_ptr< typename FOREST::Node > > CloneForest(const FOREST &forest)
Clone a tree, copy-constructing new nodes (calling boost::make_shared) and setting up child pointers ...
Definition: treeTraversal-inst.h:189
void DepthFirstForest(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost)
Traverse a forest depth-first with pre-order and post-order visits.
Definition: treeTraversal-inst.h:75
void DepthFirstForestParallel(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost, int problemSizeThreshold=10)
Traverse a forest depth-first with pre-order and post-order visits.
Definition: treeTraversal-inst.h:152
Definition: FastList.h:38
void PrintForest(const FOREST &forest, std::string str, const KeyFormatter &keyFormatter)
Print a tree, prefixing each line with str, and formatting keys using keyFormatter.
Definition: treeTraversal-inst.h:218
Definition: FastVector.h:38
A thin wrapper around std::vector that uses boost's pool_allocator.
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62