Fixed VariableIndex deep copy

release/4.3a0
Richard Roberts 2012-04-12 19:55:11 +00:00
parent 6fb80c983d
commit 995fda8e21
3 changed files with 58 additions and 1 deletions

View File

@ -23,6 +23,20 @@ namespace gtsam {
using namespace std;
/* ************************************************************************* */
VariableIndex::VariableIndex(const VariableIndex& other) :
index_(indexUnpermuted_) {
*this = other;
}
/* ************************************************************************* */
VariableIndex& VariableIndex::operator=(const VariableIndex& rhs) {
index_ = rhs.index_;
nFactors_ = rhs.nFactors_;
nEntries_ = rhs.nEntries_;
return *this;
}
/* ************************************************************************* */
void VariableIndex::permute(const Permutation& permutation) {
#ifndef NDEBUG

View File

@ -70,6 +70,16 @@ public:
*/
template<class FactorGraph> VariableIndex(const FactorGraph& factorGraph);
/**
* Copy constructor
*/
VariableIndex(const VariableIndex& other);
/**
* Assignment operator
*/
VariableIndex& operator=(const VariableIndex& rhs);
/// @}
/// @name Standard Interface
/// @{
@ -127,7 +137,6 @@ public:
*/
template<typename CONTAINER, class FactorGraph> void remove(const CONTAINER& indices, const FactorGraph& factors);
/**
* Apply a variable permutation. Does not rearrange data, just permutes
* future lookups by variable.

View File

@ -75,7 +75,41 @@ TEST(VariableIndex, remove) {
actual.remove(indices, fg1);
CHECK(assert_equal(expected, actual));
}
/* ************************************************************************* */
TEST(VariableIndex, deep_copy) {
SymbolicFactorGraph fg1, fg2;
fg1.push_factor(0, 1);
fg1.push_factor(0, 2);
fg1.push_factor(5, 9);
fg1.push_factor(2, 3);
fg2.push_factor(1, 3);
fg2.push_factor(2, 4);
fg2.push_factor(3, 5);
fg2.push_factor(5, 6);
// Create original graph and VariableIndex
SymbolicFactorGraph fgOriginal; fgOriginal.push_back(fg1); fgOriginal.push_back(fg2);
VariableIndex original(fgOriginal);
VariableIndex expectedOriginal(fgOriginal);
// Create a factor graph containing only the factors from fg2 and with null
// factors in the place of those of fg1, so that the factor indices are correct.
SymbolicFactorGraph fg2removed(fgOriginal);
fg2removed.remove(0); fg2removed.remove(1); fg2removed.remove(2); fg2removed.remove(3);
VariableIndex expectedRemoved(fg2removed);
// Create a clone and modify the clone - the original should not change
VariableIndex clone(original);
vector<size_t> indices;
indices.push_back(0); indices.push_back(1); indices.push_back(2); indices.push_back(3);
clone.remove(indices, fg1);
// When modifying the clone, the original should have stayed the same
EXPECT(assert_equal(expectedOriginal, original));
EXPECT(assert_equal(expectedRemoved, clone));
}
/* ************************************************************************* */