gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VariableIndex.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
18 #pragma once
19 
20 #include <vector>
21 #include <deque>
22 #include <stdexcept>
23 #include <boost/foreach.hpp>
24 
25 #include <gtsam/base/FastList.h>
26 #include <gtsam/base/FastMap.h>
27 #include <gtsam/base/types.h>
28 #include <gtsam/base/timing.h>
29 #include <gtsam/inference/Key.h>
30 
31 namespace gtsam {
32 
42 class GTSAM_EXPORT VariableIndex {
43 public:
44 
45  typedef boost::shared_ptr<VariableIndex> shared_ptr;
46  typedef FastList<size_t> Factors;
47  typedef Factors::iterator Factor_iterator;
48  typedef Factors::const_iterator Factor_const_iterator;
49 
50 protected:
52  KeyMap index_;
53  size_t nFactors_; // Number of factors in the original factor graph.
54  size_t nEntries_; // Sum of involved variable counts of each factor.
55 
56 public:
57  typedef KeyMap::const_iterator const_iterator;
58  typedef KeyMap::const_iterator iterator;
59  typedef KeyMap::value_type value_type;
60 
61 public:
62 
65 
67  VariableIndex() : nFactors_(0), nEntries_(0) {}
68 
73  template<class FG>
74  VariableIndex(const FG& factorGraph) : nFactors_(0), nEntries_(0) { augment(factorGraph); }
75 
79 
84  Key size() const { return index_.size(); }
85 
87  size_t nFactors() const { return nFactors_; }
88 
90  size_t nEntries() const { return nEntries_; }
91 
93  const Factors& operator[](Key variable) const {
94  KeyMap::const_iterator item = index_.find(variable);
95  if(item == index_.end())
96  throw std::invalid_argument("Requested non-existent variable from VariableIndex");
97  else
98  return item->second;
99  }
100 
104 
106  bool equals(const VariableIndex& other, double tol=0.0) const;
107 
109  void print(const std::string& str = "VariableIndex: ",
110  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
111 
116  void outputMetisFormat(std::ostream& os) const;
117 
118 
122 
127  template<class FG>
128  void augment(const FG& factors, boost::optional<const FastVector<size_t>&> newFactorIndices = boost::none);
129 
140  template<typename ITERATOR, class FG>
141  void remove(ITERATOR firstFactor, ITERATOR lastFactor, const FG& factors);
142 
144  template<typename ITERATOR>
145  void removeUnusedVariables(ITERATOR firstKey, ITERATOR lastKey);
146 
148  const_iterator begin() const { return index_.begin(); }
149 
151  const_iterator end() const { return index_.end(); }
152 
154  const_iterator find(Key key) const { return index_.find(key); }
155 
156 protected:
157  Factor_iterator factorsBegin(Key variable) { return internalAt(variable).begin(); }
158  Factor_iterator factorsEnd(Key variable) { return internalAt(variable).end(); }
159 
160  Factor_const_iterator factorsBegin(Key variable) const { return internalAt(variable).begin(); }
161  Factor_const_iterator factorsEnd(Key variable) const { return internalAt(variable).end(); }
162 
164  const Factors& internalAt(Key variable) const {
165  const KeyMap::const_iterator item = index_.find(variable);
166  assert(item != index_.end());
167  return item->second; }
168 
170  Factors& internalAt(Key variable) {
171  const KeyMap::iterator item = index_.find(variable);
172  assert(item != index_.end());
173  return item->second; }
174 
176 };
177 
178 }
179 
Factors & internalAt(Key variable)
Internal version of 'at' that asserts existence.
Definition: VariableIndex.h:170
const_iterator end() const
Iterator to the first variable entry.
Definition: VariableIndex.h:151
A thin wrapper around std::list that uses boost's fast_pool_allocator.
Key size() const
The number of variable entries.
Definition: VariableIndex.h:84
Timing utilities.
const Factors & operator[](Key variable) const
Access a list of factors by variable.
Definition: VariableIndex.h:93
void print(const Matrix &A, const string &s, ostream &stream)
print a matrix
Definition: Matrix.cpp:183
Typedefs for easier changing of types.
A thin wrapper around std::map that uses boost's fast_pool_allocator.
const Factors & internalAt(Key variable) const
Internal version of 'at' that asserts existence.
Definition: VariableIndex.h:164
const_iterator find(Key key) const
Find the iterator for the requested variable entry.
Definition: VariableIndex.h:154
Template to create a binary predicate.
Definition: Testable.h:102
The VariableIndex class computes and stores the block column structure of a factor graph...
Definition: VariableIndex.h:42
size_t Key
Integer nonlinear key type.
Definition: types.h:59
const_iterator begin() const
Iterator to the first variable entry.
Definition: VariableIndex.h:148
VariableIndex()
Default constructor, creates an empty VariableIndex.
Definition: VariableIndex.h:67
size_t nFactors() const
The number of factors in the original factor graph.
Definition: VariableIndex.h:87
size_t nEntries() const
The number of nonzero blocks, i.e.
Definition: VariableIndex.h:90
Definition: FastList.h:38
VariableIndex(const FG &factorGraph)
Create a VariableIndex that computes and stores the block column structure of a factor graph...
Definition: VariableIndex.h:74
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62