From 6c2f213091f9e7fef90eea567c2a506281c0ffb8 Mon Sep 17 00:00:00 2001 From: Yong-Dian Jian Date: Wed, 5 Sep 2012 15:03:35 +0000 Subject: [PATCH] 1. use DSFVector for spanning tree in SubgraphSolver 2. move DSFVector from unstable to stable 3. create Dummy.cpp to prevent base_unstable degenerate --- {gtsam_unstable => gtsam}/base/DSFVector.cpp | 2 +- {gtsam_unstable => gtsam}/base/DSFVector.h | 0 .../base/tests/testDSFVector.cpp | 2 +- gtsam/linear/SubgraphSolver.cpp | 56 ++----------------- gtsam/linear/SubgraphSolver.h | 25 --------- gtsam_unstable/base/Dummy.cpp | 43 ++++++++++++++ gtsam_unstable/base/Dummy.h | 22 ++------ 7 files changed, 55 insertions(+), 95 deletions(-) rename {gtsam_unstable => gtsam}/base/DSFVector.cpp (98%) rename {gtsam_unstable => gtsam}/base/DSFVector.h (100%) rename {gtsam_unstable => gtsam}/base/tests/testDSFVector.cpp (99%) create mode 100644 gtsam_unstable/base/Dummy.cpp diff --git a/gtsam_unstable/base/DSFVector.cpp b/gtsam/base/DSFVector.cpp similarity index 98% rename from gtsam_unstable/base/DSFVector.cpp rename to gtsam/base/DSFVector.cpp index cdea89f34..6d79dcb53 100644 --- a/gtsam_unstable/base/DSFVector.cpp +++ b/gtsam/base/DSFVector.cpp @@ -18,7 +18,7 @@ #include #include -#include +#include using namespace std; diff --git a/gtsam_unstable/base/DSFVector.h b/gtsam/base/DSFVector.h similarity index 100% rename from gtsam_unstable/base/DSFVector.h rename to gtsam/base/DSFVector.h diff --git a/gtsam_unstable/base/tests/testDSFVector.cpp b/gtsam/base/tests/testDSFVector.cpp similarity index 99% rename from gtsam_unstable/base/tests/testDSFVector.cpp rename to gtsam/base/tests/testDSFVector.cpp index 8997559f5..c0b72f1a0 100644 --- a/gtsam_unstable/base/tests/testDSFVector.cpp +++ b/gtsam/base/tests/testDSFVector.cpp @@ -24,7 +24,7 @@ using namespace boost::assign; #include -#include +#include using namespace std; using namespace gtsam; diff --git a/gtsam/linear/SubgraphSolver.cpp b/gtsam/linear/SubgraphSolver.cpp index 9794ba51e..47d33831b 100644 --- a/gtsam/linear/SubgraphSolver.cpp +++ b/gtsam/linear/SubgraphSolver.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -102,7 +103,7 @@ SubgraphSolver::splitGraph(const GaussianFactorGraph &jfg) { const VariableIndex index(jfg); const size_t n = index.size(); - DisjointSet D(n) ; + DSFVector D(n); GaussianFactorGraph::shared_ptr At(new GaussianFactorGraph()); GaussianFactorGraph::shared_ptr Ac( new GaussianFactorGraph()); @@ -126,10 +127,10 @@ SubgraphSolver::splitGraph(const GaussianFactorGraph &jfg) { if ( jf->keys().size() == 1 ) augment = true; else { const Index u = jf->keys()[0], v = jf->keys()[1], - u_root = D.find(u), v_root = D.find(v); + u_root = D.findSet(u), v_root = D.findSet(v); if ( u_root != v_root ) { t++; augment = true ; - D.makeUnion(u_root, v_root); + D.makeUnionInPlace(u_root, v_root); } } if ( augment ) At->push_back(jf); @@ -139,53 +140,4 @@ SubgraphSolver::splitGraph(const GaussianFactorGraph &jfg) { return boost::tie(At, Ac); } - -SubgraphSolver::DisjointSet::DisjointSet(const size_t n):n_(n),rank_(n,1),parent_(n) { - for ( Index i = 0 ; i < n ; ++i ) parent_[i] = i ; -} - -Index SubgraphSolver::DisjointSet::makeUnion(const Index &u, const Index &v) { - - Index u_root = find(u), v_root = find(v) ; - Index u_rank = rank(u), v_rank = rank(v) ; - - if ( u_root != v_root ) { - if ( v_rank > u_rank ) { - parent_[u_root] = v_root ; - rank_[v_root] += rank_[u_root] ; - return v_root ; - } - else { - parent_[v_root] = u_root ; - rank_[u_root] += rank_[v_root] ; - return u_root ; - } - } - return u_root ; -} - -Index SubgraphSolver::DisjointSet::find(const Index &u) { - vector path ; - Index x = u; - Index x_root = parent_[x] ; - - // find the root, and keep the vertices along the path - while ( x != x_root ) { - path.push_back(x) ; - x = x_root ; - x_root = parent_[x] ; - } - - // path compression - BOOST_FOREACH(const Index &i, path) { - rank_[i] = 1 ; - parent_[i] = x_root ; - } - - return x_root ; -} - - - - } // \namespace gtsam diff --git a/gtsam/linear/SubgraphSolver.h b/gtsam/linear/SubgraphSolver.h index 441fdaeef..7c60c0588 100644 --- a/gtsam/linear/SubgraphSolver.h +++ b/gtsam/linear/SubgraphSolver.h @@ -82,31 +82,6 @@ protected: boost::tuple splitGraph(const GaussianFactorGraph &gfg) ; - -public: - - // a simplfied implementation of disjoint set data structure. - class DisjointSet { - protected: - size_t n_ ; - std::vector rank_ ; - std::vector parent_ ; - - public: - // initialize a disjoint set, point every vertex to itself - DisjointSet(const size_t n) ; - inline size_t size() const { return n_ ; } - - // union the root of u and and the root of v, return the root of u and v - Index makeUnion(const Index &u, const Index &v) ; - - // return the root of u - Index find(const Index &u) ; - - // return the rank of x, which is defined as the cardinality of the set containing x - inline size_t rank(const Index &x) {return rank_[find(x)] ;} - }; - }; } // namespace gtsam diff --git a/gtsam_unstable/base/Dummy.cpp b/gtsam_unstable/base/Dummy.cpp new file mode 100644 index 000000000..90d71d27e --- /dev/null +++ b/gtsam_unstable/base/Dummy.cpp @@ -0,0 +1,43 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @file Dummy.h + * @brief Dummy class for testing MATLAB memory allocation + * @author Andrew Melim + * @author Frank Dellaert + * @date June 14, 2012 + */ + +#include +#include + +namespace gtsam { + +static size_t gDummyCount = 0; + +Dummy::Dummy():id(++gDummyCount) { + std::cout << "Dummy constructor " << id << std::endl; +} + +Dummy::~Dummy() { + std::cout << "Dummy destructor " << id << std::endl; +} + +void Dummy::print(const std::string& s) const { + std::cout << s << "Dummy " << id << std::endl; +} + +unsigned char Dummy::dummyTwoVar(unsigned char a) const { + return a; +} + +} diff --git a/gtsam_unstable/base/Dummy.h b/gtsam_unstable/base/Dummy.h index 995186cd6..0ab344f74 100644 --- a/gtsam_unstable/base/Dummy.h +++ b/gtsam_unstable/base/Dummy.h @@ -17,26 +17,16 @@ * @date June 14, 2012 */ -namespace gtsam { +#include - static size_t gDummyCount; +namespace gtsam { struct Dummy { size_t id; - Dummy():id(++gDummyCount) { - std::cout << "Dummy constructor " << id << std::endl; - } - ~Dummy() { - std::cout << "Dummy destructor " << id << std::endl; - } - void print(const std::string& s="") const { - std::cout << s << "Dummy " << id << std::endl; - } - - unsigned char dummyTwoVar(unsigned char a) const { - return a; - } - + Dummy(); + ~Dummy(); + void print(const std::string& s="") const ; + unsigned char dummyTwoVar(unsigned char a) const ; }; } // namespace gtsam