diff --git a/inference/Conditional.h b/inference/Conditional.h index 0a86de0d9..1ddd29907 100644 --- a/inference/Conditional.h +++ b/inference/Conditional.h @@ -61,18 +61,27 @@ public: /** Constructor from a frontal variable and a vector of parents */ Conditional(Index key, const std::vector& parents) : nrFrontals_(1) { - keys_.resize(1 + parents.size()); - *(beginFrontals()) = key; - std::copy(parents.begin(), parents.end(), beginParents()); - } + keys_.resize(1 + parents.size()); + *(beginFrontals()) = key; + std::copy(parents.begin(), parents.end(), beginParents()); + } + + /** Constructor from a frontal variable and an iterator range of parents */ + template + static Conditional::shared_ptr FromRange(Index key, Iterator firstParent, Iterator lastParent) : nrFrontals_(1) { + Conditional::shared_ptr conditional(new Conditional); + conditional->keys_.push_back(key); + std::copy(firstKey, lastKey, back_inserter(conditional->keys_)); + return ret; + } /** Named constructor from any number of frontal variables and parents */ template - static shared_ptr fromRange(Iterator firstKey, Iterator lastKey, size_t nrFrontals) { - shared_ptr conditional(new Conditional); - conditional->nrFrontals_ = nrFrontals; - std::copy(firstKey, lastKey, back_inserter(conditional->keys_)); - return conditional; + static Conditional::shared_ptr FromRange(Iterator firstKey, Iterator lastKey, size_t nrFrontals) { + Conditional::shared_ptr conditional(new Conditional); + conditional->nrFrontals_ = nrFrontals; + std::copy(firstKey, lastKey, back_inserter(conditional->keys_)); + return conditional; } /** check equality */ diff --git a/inference/Factor.cpp b/inference/Factor.cpp index 553d4d3c0..9c1469761 100644 --- a/inference/Factor.cpp +++ b/inference/Factor.cpp @@ -53,7 +53,7 @@ boost::shared_ptr > Factor::eliminate(size_t nrFrontals) { BayesNet::shared_ptr fragment(new BayesNet()); const_iterator nextFrontal = this->begin(); for(Index n = 0; n < nrFrontals; ++n, ++nextFrontal) - fragment->push_back(Conditional::fromRange(nextFrontal, const_iterator(this->end()), 1)); + fragment->push_back(Conditional::FromRange(nextFrontal, const_iterator(this->end()), 1)); if(nrFrontals > 0) keys_.assign(fragment->back()->beginParents(), fragment->back()->endParents()); return fragment; diff --git a/inference/tests/testSymbolicFactor.cpp b/inference/tests/testSymbolicFactor.cpp index 307155f5b..efb659b91 100644 --- a/inference/tests/testSymbolicFactor.cpp +++ b/inference/tests/testSymbolicFactor.cpp @@ -22,9 +22,9 @@ TEST(SymbolicFactor, eliminate) { BayesNet fragment = *actual.eliminate(3); Factor expected(keys.begin()+3, keys.end()); - Conditional::shared_ptr expected0 = Conditional::fromRange(keys.begin(), keys.end(), 1); - Conditional::shared_ptr expected1 = Conditional::fromRange(keys.begin()+1, keys.end(), 1); - Conditional::shared_ptr expected2 = Conditional::fromRange(keys.begin()+2, keys.end(), 1); + Conditional::shared_ptr expected0 = Conditional::FromRange(keys.begin(), keys.end(), 1); + Conditional::shared_ptr expected1 = Conditional::FromRange(keys.begin()+1, keys.end(), 1); + Conditional::shared_ptr expected2 = Conditional::FromRange(keys.begin()+2, keys.end(), 1); CHECK(assert_equal(fragment.size(), size_t(3))); CHECK(assert_equal(expected, actual));