updated find_factors_and_remove to use map of factor indices to gather factors connected to a variable. factors are set to null instead of being erased. also updated size() to count non-NULL factors, and print() to only print non-NULL factors. added new unit test which tries to remove the same variable twice.
parent
52bedcad3a
commit
baef89ccf0
|
@ -65,7 +65,11 @@ namespace gtsam {
|
||||||
|
|
||||||
/** return the numbers of the factors_ in the factor graph */
|
/** return the numbers of the factors_ in the factor graph */
|
||||||
inline size_t size() const {
|
inline size_t size() const {
|
||||||
return factors_.size();
|
int size_=0;
|
||||||
|
for (const_iterator factor = factors_.begin(); factor != factors_.end(); factor++)
|
||||||
|
if(*factor != NULL)
|
||||||
|
size_++;
|
||||||
|
return size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Add a factor */
|
/** Add a factor */
|
||||||
|
@ -91,7 +95,7 @@ namespace gtsam {
|
||||||
std::cout << s << std::endl;
|
std::cout << s << std::endl;
|
||||||
printf("size: %d\n", (int) size());
|
printf("size: %d\n", (int) size());
|
||||||
for (const_iterator factor = factors_.begin(); factor != factors_.end(); factor++)
|
for (const_iterator factor = factors_.begin(); factor != factors_.end(); factor++)
|
||||||
(*factor)->print();
|
if(*factor != NULL) (*factor)->print();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Check equality */
|
/** Check equality */
|
||||||
|
|
|
@ -57,21 +57,22 @@ list<int> LinearFactorGraph::factors(const string& key) const {
|
||||||
|
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
/** O(n) */
|
/** find all non-NULL factors for a variable, then set factors to NULL */
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
LinearFactorSet
|
LinearFactorSet LinearFactorGraph::find_factors_and_remove(const string& key) {
|
||||||
LinearFactorGraph::find_factors_and_remove(const string& key)
|
|
||||||
{
|
|
||||||
LinearFactorSet found;
|
LinearFactorSet found;
|
||||||
|
|
||||||
for(iterator factor=factors_.begin(); factor!=factors_.end(); )
|
Indices::iterator it = indices_.find(key);
|
||||||
if ((*factor)->involves(key)) {
|
list<int> *indices_ptr; // pointer to indices list in indices_ map
|
||||||
found.push_back(*factor);
|
indices_ptr = &(it->second);
|
||||||
factor = factors_.erase(factor);
|
|
||||||
} else {
|
|
||||||
factor++; // important, erase will have effect of ++
|
|
||||||
}
|
|
||||||
|
|
||||||
|
for (list<int>::iterator it = indices_ptr->begin(); it != indices_ptr->end(); it++) {
|
||||||
|
if(factors_[*it] == NULL){ // skip NULL factors
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
found.push_back(factors_[*it]);
|
||||||
|
factors_[*it].reset(); // set factor to NULL.
|
||||||
|
}
|
||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -494,6 +494,32 @@ TEST( LinearFactorGraph, find_factors_and_remove )
|
||||||
LONGS_EQUAL(1,fg.size());
|
LONGS_EQUAL(1,fg.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************* */
|
||||||
|
TEST( LinearFactorGraph, find_factors_and_remove__twice )
|
||||||
|
{
|
||||||
|
// create the graph
|
||||||
|
LinearFactorGraph fg = createLinearFactorGraph();
|
||||||
|
|
||||||
|
// We expect to remove these three factors: 0, 1, 2
|
||||||
|
LinearFactor::shared_ptr f0 = fg[0];
|
||||||
|
LinearFactor::shared_ptr f1 = fg[1];
|
||||||
|
LinearFactor::shared_ptr f2 = fg[2];
|
||||||
|
|
||||||
|
// call the function
|
||||||
|
LinearFactorSet factors = fg.find_factors_and_remove("x1");
|
||||||
|
|
||||||
|
// Check the factors
|
||||||
|
CHECK(f0==factors[0]);
|
||||||
|
CHECK(f1==factors[1]);
|
||||||
|
CHECK(f2==factors[2]);
|
||||||
|
|
||||||
|
factors = fg.find_factors_and_remove("x1");
|
||||||
|
CHECK(factors.size() == 0);
|
||||||
|
|
||||||
|
// CHECK if the factors are deleted from the factor graph
|
||||||
|
LONGS_EQUAL(1,fg.size());
|
||||||
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
int main() { TestResult tr; return TestRegistry::runAllTests(tr);}
|
int main() { TestResult tr; return TestRegistry::runAllTests(tr);}
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
|
|
Loading…
Reference in New Issue