ent
parent
1d06f1e766
commit
e6f2cd4989
|
|
@ -27,6 +27,8 @@
|
||||||
#include <gtsam/base/FastVector.h>
|
#include <gtsam/base/FastVector.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <queue>
|
||||||
|
#include <deque>
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
|
|
@ -111,6 +113,25 @@ namespace gtsam {
|
||||||
/** Copy constructor */
|
/** Copy constructor */
|
||||||
BayesTree(const This& other);
|
BayesTree(const This& other);
|
||||||
|
|
||||||
|
~BayesTree() {
|
||||||
|
for (auto&& root: roots_) {
|
||||||
|
std::queue<Clique*> bfs_queue;
|
||||||
|
std::deque<Clique*> topological_order;
|
||||||
|
bfs_queue.push(root.get());
|
||||||
|
while (!bfs_queue.empty()) {
|
||||||
|
Clique* current = bfs_queue.front();
|
||||||
|
bfs_queue.pop();
|
||||||
|
topological_order.push_front(current);
|
||||||
|
for (auto&& child: current->children) {
|
||||||
|
bfs_queue.push(child.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (auto&& clique: topological_order) {
|
||||||
|
clique->children.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
/** Assignment operator */
|
/** Assignment operator */
|
||||||
|
|
|
||||||
|
|
@ -25,8 +25,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <queue>
|
|
||||||
#include <deque>
|
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
|
|
@ -99,28 +98,7 @@ namespace gtsam {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Virtual destructor.
|
// Virtual destructor.
|
||||||
virtual ~BayesTreeCliqueBase() {
|
virtual ~BayesTreeCliqueBase() {}
|
||||||
// default destructor will cause stack overflow for deletion of large trees
|
|
||||||
// so we delete the tree explicitly by first BFSing the tree to determine the topological order
|
|
||||||
// and then clearing the children of each node in topological order
|
|
||||||
// Only work in single threaded mode
|
|
||||||
std::queue<This*> bfs_queue;
|
|
||||||
std::deque<This*> topological_order;
|
|
||||||
bfs_queue.push(this);
|
|
||||||
while (!bfs_queue.empty()) {
|
|
||||||
auto node = bfs_queue.front();
|
|
||||||
bfs_queue.pop();
|
|
||||||
topological_order.push_front(node);
|
|
||||||
for (auto& child : node->children) {
|
|
||||||
if (child.use_count() == 1) {
|
|
||||||
bfs_queue.push(child.get());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (auto node : topological_order) {
|
|
||||||
node->children.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -171,18 +171,18 @@ class ClusterTree {
|
||||||
// use default destructor which recursively deletes all nodes with shared_ptr causes stack overflow.
|
// use default destructor which recursively deletes all nodes with shared_ptr causes stack overflow.
|
||||||
// so for each tree, we do a BFS to get sequence of nodes to delete, and clear their children first.
|
// so for each tree, we do a BFS to get sequence of nodes to delete, and clear their children first.
|
||||||
for (auto&& root : roots_) {
|
for (auto&& root : roots_) {
|
||||||
std::queue<Cluster*> q;
|
std::queue<Cluster*> bfs_queue;
|
||||||
std::deque<Cluster*> nodes;
|
std::deque<Cluster*> topological_order;
|
||||||
q.push(root.get());
|
bfs_queue.push(root.get());
|
||||||
while (!q.empty()) {
|
while (!bfs_queue.empty()) {
|
||||||
auto node = q.front();
|
auto node = bfs_queue.front();
|
||||||
nodes.push_front(node);
|
topological_order.push_front(node);
|
||||||
q.pop();
|
bfs_queue.pop();
|
||||||
for (auto&& child : node->children) {
|
for (auto&& child : node->children) {
|
||||||
q.push(child.get());
|
bfs_queue.push(child.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (auto&& node : nodes) {
|
for (auto&& node : topological_order) {
|
||||||
node->children.clear();
|
node->children.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue