From 52bedcad3a11d0f7f93c0c4c804ffd937315d2f0 Mon Sep 17 00:00:00 2001 From: Chris Beall Date: Thu, 22 Oct 2009 21:33:00 +0000 Subject: [PATCH] order 1 factors by using map --- cpp/FactorGraph-inl.h | 23 +++++++++++++++++++++++ cpp/FactorGraph.h | 8 +++++--- cpp/LinearFactorGraph.cpp | 16 ++++++---------- cpp/LinearFactorGraph.h | 2 +- cpp/smallExample.cpp | 1 + cpp/testLinearFactorGraph.cpp | 8 ++++---- 6 files changed, 40 insertions(+), 18 deletions(-) diff --git a/cpp/FactorGraph-inl.h b/cpp/FactorGraph-inl.h index 82e2843cc..c00ed062b 100644 --- a/cpp/FactorGraph-inl.h +++ b/cpp/FactorGraph-inl.h @@ -16,12 +16,35 @@ #include "Ordering.h" #include "FactorGraph.h" + #include +using namespace std; + namespace gtsam { /* ************************************************************************* */ +template +void FactorGraph::push_back(shared_factor factor) { + factors_.push_back(factor); // add the actual factor + int i = factors_.size() - 1; // index of factor + list keys = factor->keys(); // get keys for factor + BOOST_FOREACH(string key, keys){ // for each key push i onto list + Indices::iterator it = indices_.find(key); // old list for that key (if exists) + if (it==indices_.end()){ // there's no list yet, so make one + list indices(1, i); + indices_.insert(pair >(key, indices)); // insert new indices into factorMap + } + else{ + list *indices_ptr; + indices_ptr = &(it->second); + indices_ptr->push_back(i); + } + } +} + +/* ************************************************************************* */ /** * Call colamd given a column-major symbolic matrix A * @param n_col colamd arg 1: number of rows in A diff --git a/cpp/FactorGraph.h b/cpp/FactorGraph.h index 0055c83ac..7cf78fd68 100644 --- a/cpp/FactorGraph.h +++ b/cpp/FactorGraph.h @@ -37,6 +37,10 @@ namespace gtsam { /** Collection of factors */ std::vector factors_; + /** For each variable a list of factor indices connected to it */ + typedef std::map > Indices; + Indices indices_; + public: /** STL like, return the iterator pointing to the first factor */ @@ -65,9 +69,7 @@ namespace gtsam { } /** Add a factor */ - void push_back(shared_factor ptr_f) { - factors_.push_back(ptr_f); - } + void push_back(shared_factor factor); /** unnormalized error */ double error(const Config& c) const { diff --git a/cpp/LinearFactorGraph.cpp b/cpp/LinearFactorGraph.cpp index 0d04ba5c5..e5d471748 100644 --- a/cpp/LinearFactorGraph.cpp +++ b/cpp/LinearFactorGraph.cpp @@ -48,18 +48,14 @@ set LinearFactorGraph::find_separator(const string& key) const } /* ************************************************************************* */ -/** O(n) */ -/* ************************************************************************* */ -std::vector LinearFactorGraph::factors(const std::string& key) { - vector found; - - for(int i=0;iinvolves(key)) - found.push_back(i); - - return found; +/** O(1) */ +/* ************************************************************************* */ +list LinearFactorGraph::factors(const string& key) const { + Indices::const_iterator it = indices_.find(key); + return it->second; } + /* ************************************************************************* */ /** O(n) */ /* ************************************************************************* */ diff --git a/cpp/LinearFactorGraph.h b/cpp/LinearFactorGraph.h index 24bf09347..586681d3c 100644 --- a/cpp/LinearFactorGraph.h +++ b/cpp/LinearFactorGraph.h @@ -56,7 +56,7 @@ namespace gtsam { * Return indices for all factors that involve the given node * @param key the key for the given node */ - std::vector factors(const std::string& key); + std::list factors(const std::string& key) const; /** * find all the factors that involve the given node and remove them diff --git a/cpp/smallExample.cpp b/cpp/smallExample.cpp index 0a35299ba..8e0f7af0a 100644 --- a/cpp/smallExample.cpp +++ b/cpp/smallExample.cpp @@ -23,6 +23,7 @@ using namespace std; #include "Simulated2DOdometry.h" #include "Simulated2DMeasurement.h" #include "simulated2D.h" +#include "FactorGraph-inl.h" namespace gtsam { diff --git a/cpp/testLinearFactorGraph.cpp b/cpp/testLinearFactorGraph.cpp index 56ee7a428..3eef30000 100644 --- a/cpp/testLinearFactorGraph.cpp +++ b/cpp/testLinearFactorGraph.cpp @@ -459,15 +459,15 @@ TEST( LinearFactorGraph, factor_lookup) LinearFactorGraph fg = createLinearFactorGraph(); // ask for all factor indices connected to x1 - vector x1_factors = fg.factors("x1"); + list x1_factors = fg.factors("x1"); int x1_indices[] = { 0, 1, 2 }; - vector x1_expected(x1_indices, x1_indices + 3); + list x1_expected(x1_indices, x1_indices + 3); CHECK(x1_factors==x1_expected); // ask for all factor indices connected to x2 - vector x2_factors = fg.factors("x2"); + list x2_factors = fg.factors("x2"); int x2_indices[] = { 1, 3 }; - vector x2_expected(x2_indices, x2_indices + 2); + list x2_expected(x2_indices, x2_indices + 2); CHECK(x2_factors==x2_expected); }