19#include <gtsam/base/treeTraversal/parallelTraversalTasks.h>
20#include <gtsam/base/treeTraversal/statistics.h>
25#include <gtsam/config.h>
42template<
typename NODE,
typename DATA>
45 const std::shared_ptr<NODE>& treeNode;
47 typename FastList<DATA>::iterator dataPointer;
48 TraversalNode(
const std::shared_ptr<NODE>& _treeNode, DATA& _parentData) :
49 expanded(
false), treeNode(_treeNode), parentData(_parentData) {
55 template<
typename NODE,
typename DATA>
56 void operator()(
const std::shared_ptr<NODE>& node,
const DATA& data) {
76template<
class FOREST,
typename DATA,
typename VISITOR_PRE,
77 typename VISITOR_POST>
79 VISITOR_POST& visitorPost) {
81 typedef typename FOREST::Node
Node;
85 typedef TraversalNode<typename FOREST::Node, DATA> TraversalNode;
92 typename Stack::iterator insertLocation = stack.begin();
94 stack.insert(insertLocation, TraversalNode(root, rootData));
98 while (!stack.empty()) {
100 TraversalNode& node = stack.front();
105 (void) visitorPost(node.treeNode, *node.dataPointer);
106 dataList.erase(node.dataPointer);
111 node.dataPointer = dataList.insert(dataList.end(),
112 visitorPre(node.treeNode, node.parentData));
113 typename Stack::iterator insertLocation = stack.begin();
114 for(
const sharedNode& child: node.treeNode->children)
115 stack.insert(insertLocation, TraversalNode(child, *node.dataPointer));
116 node.expanded =
true;
119 assert(dataList.empty());
129template<
class FOREST,
typename VISITOR_POST>
131 typedef typename FOREST::Node
Node;
134 std::function<void(
const sharedNode&)> visit =
137 for (
const sharedNode& child : node->children) visit(child);
138 (void)visitorPost(node);
157template<
class FOREST,
typename DATA,
typename VISITOR_PRE>
179template<
class FOREST,
typename DATA,
typename VISITOR_PRE,
180 typename VISITOR_POST>
182 VISITOR_PRE& visitorPre, VISITOR_POST& visitorPost,
183 int problemSizeThreshold = 10) {
185#if defined(GTSAM_USE_TBB) && !defined(GTSAM_TBB_BOUNDED_MEMORY_GROWTH_FLAG)
189 typedef typename FOREST::Node
Node;
191 internal::CreateRootTask<Node>(forest.roots(), rootData, visitorPre,
192 visitorPost, problemSizeThreshold);
208template<
class FOREST,
typename VISITOR_POST>
210 int problemSizeThreshold = 10,
211 size_t leafAggregationProblemSize = 0) {
212#if defined(GTSAM_USE_TBB) && !defined(GTSAM_TBB_BOUNDED_MEMORY_GROWTH_FLAG)
215 typedef typename FOREST::Node
Node;
216 internal::CreateRootPostOrderTask<Node>(forest.roots(), visitorPost,
217 problemSizeThreshold,
218 leafAggregationProblemSize);
220 (void)leafAggregationProblemSize;
228template<
typename NODE>
229std::shared_ptr<NODE> CloneForestVisitorPre(
230 const std::shared_ptr<NODE>& node,
231 const std::shared_ptr<NODE>& parentPointer) {
233 std::shared_ptr<NODE> clone = std::make_shared<NODE>(*node);
234 clone->children.clear();
235 parentPointer->children.push_back(clone);
245template<
class FOREST>
247 const FOREST& forest) {
248 typedef typename FOREST::Node
Node;
249 std::shared_ptr<Node> rootContainer = std::make_shared<Node>();
252 rootContainer->children.end());
258struct PrintForestVisitorPre {
261 formatter(formatter) {
263 template<
typename NODE> std::string operator()(
264 const std::shared_ptr<NODE>& node,
const std::string& parentString) {
266 node->print(parentString +
"-", formatter);
268 return parentString +
"| ";
275template<
class FOREST>
278 PrintForestVisitorPre visitor(keyFormatter);
A thin wrapper around std::vector that uses a custom allocator.
A thin wrapper around std::list that uses boost's fast_pool_allocator.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
Internal functions used for traversing trees.
Definition treeTraversal-inst.h:37
void PostOrderForest(FOREST &forest, VISITOR_POST &visitorPost)
Traverse a forest depth-first with post-order visits only.
Definition treeTraversal-inst.h:130
FastVector< std::shared_ptr< typename FOREST::Node > > CloneForest(const FOREST &forest)
Clone a tree, copy-constructing new nodes (calling std::make_shared) and setting up child pointers fo...
Definition treeTraversal-inst.h:246
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:78
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:276
void PostOrderForestParallel(FOREST &forest, VISITOR_POST &visitorPost, int problemSizeThreshold=10, size_t leafAggregationProblemSize=0)
Traverse a forest depth-first with post-order visits only (parallel if TBB).
Definition treeTraversal-inst.h:209
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:181
FastList is a thin wrapper around std::list that uses the boost fast_pool_allocator instead of the de...
Definition FastList.h:43
const FastVector< sharedNode > & roots() const
Return the set of roots (one for a tree, multiple for a forest).
Definition ClusterTree.h:178