From 098704ac22fc571f239667e5504d33640b3a7772 Mon Sep 17 00:00:00 2001 From: p-zach Date: Tue, 15 Apr 2025 18:19:25 -0400 Subject: [PATCH] Wrap VariableIndex.at(), .empty() --- gtsam/inference/VariableIndex.cpp | 18 ++++++++++++++++++ gtsam/inference/VariableIndex.h | 16 ++++------------ gtsam/inference/inference.i | 3 +++ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/gtsam/inference/VariableIndex.cpp b/gtsam/inference/VariableIndex.cpp index 53d400223..3d22501b3 100644 --- a/gtsam/inference/VariableIndex.cpp +++ b/gtsam/inference/VariableIndex.cpp @@ -23,6 +23,24 @@ namespace gtsam { using namespace std; +const FactorIndices& operator[](Key variable) const { + KeyMap::const_iterator item = index_.find(variable); +} + +const FactorIndices& at(Key variable) const { + KeyMap::const_iterator item = (*this)[variable]; + if(item == index_.end()) + throw std::invalid_argument("Requested non-existent variable '" + + DefaultKeyFormatter(variable) + + "' from VariableIndex"); + else + return item->second; +} + +bool empty(Key variable) const { + return (*this)[variable].empty(); +} + /* ************************************************************************* */ bool VariableIndex::equals(const VariableIndex& other, double tol) const { return this->nEntries_ == other.nEntries_ && this->nFactors_ == other.nFactors_ diff --git a/gtsam/inference/VariableIndex.h b/gtsam/inference/VariableIndex.h index 2f872b25d..9885cb1cb 100644 --- a/gtsam/inference/VariableIndex.h +++ b/gtsam/inference/VariableIndex.h @@ -84,20 +84,12 @@ class GTSAM_EXPORT VariableIndex { size_t nEntries() const { return nEntries_; } /// Access a list of factors by variable - const FactorIndices& operator[](Key variable) const { - KeyMap::const_iterator item = index_.find(variable); - if(item == index_.end()) - throw std::invalid_argument("Requested non-existent variable '" + - DefaultKeyFormatter(variable) + - "' from VariableIndex"); - else - return item->second; - } + const FactorIndices& operator[](Key variable) const; + + const FactorIndices& at(Key variable) const; /// Return true if no factors associated with a variable - bool empty(Key variable) const { - return (*this)[variable].empty(); - } + bool empty(Key variable) const; /// @} /// @name Testable diff --git a/gtsam/inference/inference.i b/gtsam/inference/inference.i index 13c4c9853..084d97a01 100644 --- a/gtsam/inference/inference.i +++ b/gtsam/inference/inference.i @@ -199,6 +199,9 @@ class VariableIndex { VariableIndex(const T& factorGraph); VariableIndex(const gtsam::VariableIndex& other); + const FactorIndices& at(Key variable) const; + bool empty(Key variable) const; + // Testable bool equals(const gtsam::VariableIndex& other, double tol) const; void print(string s = "VariableIndex: ",