treeTraversal-inst.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
17 #pragma once
18 
21 
22 #include <gtsam/base/FastList.h>
23 #include <gtsam/base/FastVector.h>
24 #include <gtsam/inference/Key.h>
25 #include <gtsam/config.h> // for GTSAM_USE_TBB
26 
27 #include <stack>
28 #include <vector>
29 #include <string>
30 #include <memory>
31 #include <cassert>
32 
33 namespace gtsam {
34 
36 namespace treeTraversal {
37 
38 /* ************************************************************************* */
39 namespace {
40 // Internal node used in DFS preorder stack
41 template<typename NODE, typename DATA>
42 struct TraversalNode {
43  bool expanded;
44  const std::shared_ptr<NODE>& treeNode;
45  DATA& parentData;
46  typename FastList<DATA>::iterator dataPointer;
47  TraversalNode(const std::shared_ptr<NODE>& _treeNode, DATA& _parentData) :
48  expanded(false), treeNode(_treeNode), parentData(_parentData) {
49  }
50 };
51 
52 // Do nothing - default argument for post-visitor for tree traversal
53 struct no_op {
54  template<typename NODE, typename DATA>
55  void operator()(const std::shared_ptr<NODE>& node, const DATA& data) {
56  }
57 };
58 
59 }
60 
75 template<class FOREST, typename DATA, typename VISITOR_PRE,
76  typename VISITOR_POST>
77 void DepthFirstForest(FOREST& forest, DATA& rootData, VISITOR_PRE& visitorPre,
78  VISITOR_POST& visitorPost) {
79  // Typedefs
80  typedef typename FOREST::Node Node;
81  typedef std::shared_ptr<Node> sharedNode;
82 
83  // Depth first traversal stack
84  typedef TraversalNode<typename FOREST::Node, DATA> TraversalNode;
85  typedef FastList<TraversalNode> Stack;
86  Stack stack;
87  FastList<DATA> dataList; // List to store node data as it is returned from the pre-order visitor
88 
89  // Add roots to stack (insert such that they are visited and processed in order
90  {
91  typename Stack::iterator insertLocation = stack.begin();
92  for(const sharedNode& root: forest.roots())
93  stack.insert(insertLocation, TraversalNode(root, rootData));
94  }
95 
96  // Traverse
97  while (!stack.empty()) {
98  // Get next node
99  TraversalNode& node = stack.front();
100 
101  if (node.expanded) {
102  // If already expanded, then the data stored in the node is no longer needed, so visit
103  // then delete it.
104  (void) visitorPost(node.treeNode, *node.dataPointer);
105  dataList.erase(node.dataPointer);
106  stack.pop_front();
107  } else {
108  // If not already visited, visit the node and add its children (use reverse iterators so
109  // children are processed in the order they appear)
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;
116  }
117  }
118  assert(dataList.empty());
119 }
120 
132 template<class FOREST, typename DATA, typename VISITOR_PRE>
133 void DepthFirstForest(FOREST& forest, DATA& rootData, VISITOR_PRE& visitorPre) {
134  no_op visitorPost;
135  DepthFirstForest(forest, rootData, visitorPre, visitorPost);
136 }
137 
154 template<class FOREST, typename DATA, typename VISITOR_PRE,
155  typename VISITOR_POST>
156 void DepthFirstForestParallel(FOREST& forest, DATA& rootData,
157  VISITOR_PRE& visitorPre, VISITOR_POST& visitorPost,
158  int problemSizeThreshold = 10) {
159 #ifdef GTSAM_USE_TBB
160  // Typedefs
161  typedef typename FOREST::Node Node;
162 
163  internal::CreateRootTask<Node>(forest.roots(), rootData, visitorPre,
164  visitorPost, problemSizeThreshold);
165 #else
166  DepthFirstForest(forest, rootData, visitorPre, visitorPost);
167 #endif
168 }
169 
170 /* ************************************************************************* */
172 namespace {
173 template<typename NODE>
174 std::shared_ptr<NODE> CloneForestVisitorPre(
175  const std::shared_ptr<NODE>& node,
176  const std::shared_ptr<NODE>& parentPointer) {
177  // Clone the current node and add it to its cloned parent
178  std::shared_ptr<NODE> clone = std::make_shared<NODE>(*node);
179  clone->children.clear();
180  parentPointer->children.push_back(clone);
181  return clone;
182 }
183 }
184 
190 template<class FOREST>
192  const FOREST& forest) {
193  typedef typename FOREST::Node Node;
194  std::shared_ptr<Node> rootContainer = std::make_shared<Node>();
195  DepthFirstForest(forest, rootContainer, CloneForestVisitorPre<Node>);
196  return FastVector<std::shared_ptr<Node> >(rootContainer->children.begin(),
197  rootContainer->children.end());
198 }
199 
200 /* ************************************************************************* */
202 namespace {
203 struct PrintForestVisitorPre {
205  PrintForestVisitorPre(const KeyFormatter& formatter) :
207  }
208  template<typename NODE> std::string operator()(
209  const std::shared_ptr<NODE>& node, const std::string& parentString) {
210  // Print the current node
211  node->print(parentString + "-", formatter);
212  // Increment the indentation
213  return parentString + "| ";
214  }
215 };
216 }
217 
220 template<class FOREST>
221 void PrintForest(const FOREST& forest, std::string str,
222  const KeyFormatter& keyFormatter) {
223  PrintForestVisitorPre visitor(keyFormatter);
224  DepthFirstForest(forest, str, visitor);
225 }
226 } // namespace treeTraversal
227 
228 } // namespace gtsam
FastVector.h
A thin wrapper around std::vector that uses a custom allocator.
gtsam::FastVector
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
expanded
bool expanded
Definition: treeTraversal-inst.h:43
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
statistics.h
Tools for gathering statistics about a forest to aid tuning parallel traversal.
Key.h
data
int data[]
Definition: Map_placement_new.cpp:1
parentData
DATA & parentData
Definition: treeTraversal-inst.h:45
operator()
internal::enable_if< internal::valid_indexed_view_overload< RowIndices, ColIndices >::value &&internal::traits< typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::ReturnAsIndexedView, typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::type operator()(const RowIndices &rowIndices, const ColIndices &colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST
Definition: IndexedViewMethods.h:73
gtsam::KeyFormatter
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
gtsam::stack
Matrix stack(size_t nrMatrices,...)
Definition: Matrix.cpp:396
gtsam::treeTraversal::DepthFirstForest
void DepthFirstForest(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost)
Definition: treeTraversal-inst.h:77
gtsam::FastList
Definition: FastList.h:43
str
Definition: pytypes.h:1558
gtsam::treeTraversal::DepthFirstForestParallel
void DepthFirstForestParallel(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost, int problemSizeThreshold=10)
Definition: treeTraversal-inst.h:156
gtsam::treeTraversal::PrintForest
void PrintForest(const FOREST &forest, std::string str, const KeyFormatter &keyFormatter)
Definition: treeTraversal-inst.h:221
gtsam
traits
Definition: SFMdata.h:40
gtsam::treeTraversal::CloneForest
FastVector< std::shared_ptr< typename FOREST::Node > > CloneForest(const FOREST &forest)
Definition: treeTraversal-inst.h:191
parallelTraversalTasks.h
FastList.h
A thin wrapper around std::list that uses boost's fast_pool_allocator.
dataPointer
FastList< DATA >::iterator dataPointer
Definition: treeTraversal-inst.h:46
treeNode
const std::shared_ptr< NODE > & treeNode
Definition: treeTraversal-inst.h:44
Node
static sharedNode Node(Key key, const SymbolicFactorGraph &factors, const ChildNodes::Result &children)
Definition: testSymbolicEliminationTree.cpp:86
sharedNode
SymbolicEliminationTree::sharedNode sharedNode
Definition: testSymbolicEliminationTree.cpp:31


gtsam
Author(s):
autogenerated on Thu Dec 19 2024 04:08:11