25 #include <gtsam/config.h>
36 namespace treeTraversal {
41 template<
typename NODE,
typename DATA>
42 struct TraversalNode {
47 TraversalNode(
const std::shared_ptr<NODE>& _treeNode, DATA& _parentData) :
54 template<
typename NODE,
typename DATA>
55 void operator()(
const std::shared_ptr<NODE>& node,
const DATA&
data) {
75 template<
class FOREST,
typename DATA,
typename VISITOR_PRE,
76 typename VISITOR_POST>
78 VISITOR_POST& visitorPost) {
84 typedef TraversalNode<typename FOREST::Node, DATA> TraversalNode;
91 typename Stack::iterator insertLocation =
stack.begin();
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>
154 template<
class FOREST,
typename DATA,
typename VISITOR_PRE,
155 typename VISITOR_POST>
157 VISITOR_PRE& visitorPre, VISITOR_POST& visitorPost,
158 int problemSizeThreshold = 10) {
163 internal::CreateRootTask<Node>(forest.roots(), rootData, visitorPre,
164 visitorPost, problemSizeThreshold);
173 template<
typename NODE>
174 std::shared_ptr<NODE> CloneForestVisitorPre(
175 const std::shared_ptr<NODE>& node,
176 const std::shared_ptr<NODE>& parentPointer) {
178 std::shared_ptr<NODE> clone = std::make_shared<NODE>(*node);
179 clone->children.clear();
180 parentPointer->children.push_back(clone);
190 template<
class FOREST>
192 const FOREST& forest) {
194 std::shared_ptr<Node> rootContainer = std::make_shared<Node>();
197 rootContainer->children.end());
203 struct PrintForestVisitorPre {
208 template<
typename NODE> std::string
operator()(
209 const std::shared_ptr<NODE>& node,
const std::string& parentString) {
211 node->print(parentString +
"-",
formatter);
213 return parentString +
"| ";
220 template<
class FOREST>
223 PrintForestVisitorPre visitor(keyFormatter);