diff --git a/gtsam/inference/VariableIndex.cpp b/gtsam/inference/VariableIndex.cpp index 32780c220..c7ef90e9f 100644 --- a/gtsam/inference/VariableIndex.cpp +++ b/gtsam/inference/VariableIndex.cpp @@ -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 diff --git a/gtsam/inference/VariableIndex.h b/gtsam/inference/VariableIndex.h index 878f6a85a..c9efc6b22 100644 --- a/gtsam/inference/VariableIndex.h +++ b/gtsam/inference/VariableIndex.h @@ -70,6 +70,16 @@ public: */ template 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 void remove(const CONTAINER& indices, const FactorGraph& factors); - /** * Apply a variable permutation. Does not rearrange data, just permutes * future lookups by variable. diff --git a/gtsam/inference/tests/testVariableIndex.cpp b/gtsam/inference/tests/testVariableIndex.cpp index 2ffc03a97..a990a5a87 100644 --- a/gtsam/inference/tests/testVariableIndex.cpp +++ b/gtsam/inference/tests/testVariableIndex.cpp @@ -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 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)); } /* ************************************************************************* */