diff --git a/gtsam/base/treeTraversal-inst.h b/gtsam/base/treeTraversal-inst.h index 4fa114277..4721ba50b 100644 --- a/gtsam/base/treeTraversal-inst.h +++ b/gtsam/base/treeTraversal-inst.h @@ -120,6 +120,18 @@ namespace gtsam { DepthFirstForest( forest, rootData, visitorPre, no_op); } + + /** Traversal function for CloneForest */ + namespace { + template + boost::shared_ptr CloneForestVisitorPre(const NODE& node, const boost::shared_ptr& parentPointer) + { + // Clone the current node and add it to its cloned parent + boost::shared_ptr clone = boost::make_shared(node); + parentPointer->children.push_back(clone); + return clone; + } + } /** Clone a tree, copy-constructing new nodes (calling boost::make_shared) and setting up child * pointers for a clone of the original tree. @@ -129,7 +141,10 @@ namespace gtsam { template std::vector > CloneForest(const FOREST& forest) { - + typedef typename FOREST::Node Node; + boost::shared_ptr rootContainer = boost::make_shared(); + DepthFirstForest(forest, rootContainer, CloneForestVisitorPre); + return std::vector >(rootContainer->children.begin(), rootContainer->children.end()); } }