diff --git a/cpp/BayesTree-inl.h b/cpp/BayesTree-inl.h index 8af6db53b..75081b8b6 100644 --- a/cpp/BayesTree-inl.h +++ b/cpp/BayesTree-inl.h @@ -44,13 +44,22 @@ namespace gtsam { cout << endl; } + /* ************************************************************************* */ + template + size_t BayesTree::Clique::treeSize() const { + size_t size = 1; + BOOST_FOREACH(shared_ptr child, children_) + size += child->treeSize(); + return size; + } + /* ************************************************************************* */ template void BayesTree::Clique::printTree(const string& indent) const { - print(indent); - BOOST_FOREACH(shared_ptr child, children_) - child->printTree(indent+" "); - } + print(indent); + BOOST_FOREACH(shared_ptr child, children_) + child->printTree(indent+" "); + } /* ************************************************************************* */ // The shortcut density is a conditional P(S|R) of the separator of this @@ -178,7 +187,7 @@ namespace gtsam { /* ************************************************************************* */ template void BayesTree::print(const string& s) const { - cout << s << ": size == " << nodes_.size() << endl; + cout << s << ": size == " << size() << endl; if (nodes_.empty()) return; root_->printTree(""); } diff --git a/cpp/BayesTree.h b/cpp/BayesTree.h index 0b094d68b..4db78290e 100644 --- a/cpp/BayesTree.h +++ b/cpp/BayesTree.h @@ -61,6 +61,9 @@ namespace gtsam { /** is this the root of a Bayes tree ? */ inline bool isRoot() const { return parent_==NULL;} + /** The size of subtree rooted at this clique, i.e., nr of Cliques */ + size_t treeSize() const; + /** print this node and entire subtree below it */ void printTree(const std::string& indent) const; @@ -124,7 +127,7 @@ namespace gtsam { /** number of cliques */ inline size_t size() const { - return nodes_.size(); + return root_->treeSize(); } /** return root clique */ diff --git a/cpp/testBayesTree.cpp b/cpp/testBayesTree.cpp index bfc54e7b9..07f31fdcf 100644 --- a/cpp/testBayesTree.cpp +++ b/cpp/testBayesTree.cpp @@ -53,7 +53,7 @@ TEST( BayesTree, constructor ) bayesTree.insert(X); // Check Size - LONGS_EQUAL(6,bayesTree.size()); + LONGS_EQUAL(4,bayesTree.size()); // Check root BayesNet expected_root; @@ -106,7 +106,7 @@ TEST( BayesTree, linear_smoother_shortcuts ) // Create the Bayes tree GaussianBayesTree bayesTree(chordalBayesNet); - LONGS_EQUAL(7,bayesTree.size()); + LONGS_EQUAL(6,bayesTree.size()); // Check the conditional P(Root|Root) GaussianBayesNet empty; @@ -175,7 +175,7 @@ TEST( BayesTree, balanced_smoother_marginals ) // Create the Bayes tree GaussianBayesTree bayesTree(chordalBayesNet); - LONGS_EQUAL(7,bayesTree.size()); + LONGS_EQUAL(4,bayesTree.size()); // Check marginal on x1 GaussianBayesNet expected1 = simpleGaussian("x1", zero(2), sigmax1);