19 #include <gtsam/base/treeTraversal/parallelTraversalTasks.h> 20 #include <gtsam/base/treeTraversal/statistics.h> 25 #include <gtsam/config.h> 30 #include <boost/shared_ptr.hpp> 31 #include <boost/make_shared.hpp> 36 namespace treeTraversal {
41 template<
typename NODE,
typename DATA>
42 struct TraversalNode {
44 const boost::shared_ptr<NODE>& treeNode;
47 TraversalNode(
const boost::shared_ptr<NODE>& _treeNode, DATA& _parentData) :
48 expanded(
false), treeNode(_treeNode), parentData(_parentData) {
54 template<
typename NODE,
typename DATA>
55 void operator()(
const boost::shared_ptr<NODE>& node,
const DATA& data) {
75 template<
class FOREST,
typename DATA,
typename VISITOR_PRE,
76 typename VISITOR_POST>
78 VISITOR_POST& visitorPost) {
80 typedef typename FOREST::Node Node;
81 typedef boost::shared_ptr<Node> sharedNode;
84 typedef TraversalNode<typename FOREST::Node, DATA> TraversalNode;
91 typename Stack::iterator insertLocation =
stack.begin();
92 for(
const sharedNode& root: forest.roots())
93 stack.insert(insertLocation, TraversalNode(root, rootData));
97 while (!
stack.empty()) {
99 TraversalNode& node =
stack.front();
104 (void) visitorPost(node.treeNode, *node.dataPointer);
105 dataList.erase(node.dataPointer);
110 node.dataPointer = dataList.insert(dataList.end(),
111 visitorPre(node.treeNode, node.parentData));
112 typename Stack::iterator insertLocation =
stack.begin();
113 for(
const sharedNode& child: node.treeNode->children)
114 stack.insert(insertLocation, TraversalNode(child, *node.dataPointer));
115 node.expanded =
true;
118 assert(dataList.empty());
132 template<
class FOREST,
typename DATA,
typename VISITOR_PRE>
152 template<
class FOREST,
typename DATA,
typename VISITOR_PRE,
153 typename VISITOR_POST>
155 VISITOR_PRE& visitorPre, VISITOR_POST& visitorPost,
156 int problemSizeThreshold = 10) {
159 typedef typename FOREST::Node Node;
161 tbb::task::spawn_root_and_wait(
162 internal::CreateRootTask<Node>(forest.roots(), rootData, visitorPre,
163 visitorPost, problemSizeThreshold));
172 template<
typename NODE>
173 boost::shared_ptr<NODE> CloneForestVisitorPre(
174 const boost::shared_ptr<NODE>& node,
175 const boost::shared_ptr<NODE>& parentPointer) {
177 boost::shared_ptr<NODE> clone = boost::make_shared<NODE>(*node);
178 clone->children.clear();
179 parentPointer->children.push_back(clone);
189 template<
class FOREST>
190 FastVector<boost::shared_ptr<typename FOREST::Node> >
CloneForest(
191 const FOREST& forest) {
192 typedef typename FOREST::Node Node;
193 boost::shared_ptr<Node> rootContainer = boost::make_shared<Node>();
195 return FastVector<boost::shared_ptr<Node> >(rootContainer->children.begin(),
196 rootContainer->children.end());
202 struct PrintForestVisitorPre {
205 formatter(formatter) {
207 template<
typename NODE> std::string operator()(
208 const boost::shared_ptr<NODE>& node,
const std::string& parentString) {
210 node->print(parentString +
"-", formatter);
212 return parentString +
"| ";
219 template<
class FOREST>
222 PrintForestVisitorPre visitor(keyFormatter);
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:77
A thin wrapper around std::vector that uses a custom allocator.
A thin wrapper around std::list that uses boost's fast_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: Key.h:33
Definition: FastList.h:38
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:154
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:220
Matrix stack(size_t nrMatrices,...)
create a matrix by stacking other matrices Given a set of matrices: A1, A2, A3...
Definition: Matrix.cpp:392
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
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:190