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 <gtsam/inference/Factor.h>
21 #include <gtsam/inference/Key.h>
22 #include <gtsam/base/FastMap.h>
23 #include <gtsam/base/FastVector.h>
24 #include <gtsam/dllexport.h>
25 
26 #include <boost/optional/optional.hpp>
27 #include <boost/smart_ptr/shared_ptr.hpp>
28 
29 #include <cassert>
30 #include <stdexcept>
31 
32 namespace gtsam {
33 
43 class GTSAM_EXPORT VariableIndex {
44  public:
45  typedef boost::shared_ptr<VariableIndex> shared_ptr;
46  typedef FactorIndices::iterator Factor_iterator;
47  typedef FactorIndices::const_iterator Factor_const_iterator;
48 
49  protected:
51  KeyMap index_;
52  size_t nFactors_; // Number of factors in the original factor graph.
53  size_t nEntries_; // Sum of involved variable counts of each factor.
54 
55  public:
56  typedef KeyMap::const_iterator const_iterator;
57  typedef KeyMap::const_iterator iterator;
58  typedef KeyMap::value_type value_type;
59 
62 
64  VariableIndex() : nFactors_(0), nEntries_(0) {}
65 
70  template <class FG>
71  explicit VariableIndex(const FG& factorGraph) : nFactors_(0), nEntries_(0) {
72  augment(factorGraph);
73  }
74 
78 
80  size_t size() const { return index_.size(); }
81 
83  size_t nFactors() const { return nFactors_; }
84 
86  size_t nEntries() const { return nEntries_; }
87 
89  const FactorIndices& operator[](Key variable) const {
90  KeyMap::const_iterator item = index_.find(variable);
91  if(item == index_.end())
92  throw std::invalid_argument("Requested non-existent variable from VariableIndex");
93  else
94  return item->second;
95  }
96 
98  bool empty(Key variable) const {
99  return (*this)[variable].empty();
100  }
101 
105 
107  bool equals(const VariableIndex& other, double tol=0.0) const;
108 
110  void print(const std::string& str = "VariableIndex: ",
111  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
112 
117  void outputMetisFormat(std::ostream& os) const;
118 
119 
123 
128  template<class FG>
129  void augment(const FG& factors, boost::optional<const FactorIndices&> newFactorIndices = boost::none);
130 
136  void augmentExistingFactor(const FactorIndex factorIndex, const KeySet & newKeys);
137 
148  template<typename ITERATOR, class FG>
149  void remove(ITERATOR firstFactor, ITERATOR lastFactor, const FG& factors);
150 
152  template<typename ITERATOR>
153  void removeUnusedVariables(ITERATOR firstKey, ITERATOR lastKey);
154 
156  const_iterator begin() const { return index_.begin(); }
157 
159  const_iterator end() const { return index_.end(); }
160 
162  const_iterator find(Key key) const { return index_.find(key); }
163 
164 protected:
165  Factor_iterator factorsBegin(Key variable) { return internalAt(variable).begin(); }
166  Factor_iterator factorsEnd(Key variable) { return internalAt(variable).end(); }
167 
168  Factor_const_iterator factorsBegin(Key variable) const { return internalAt(variable).begin(); }
169  Factor_const_iterator factorsEnd(Key variable) const { return internalAt(variable).end(); }
170 
172  const FactorIndices& internalAt(Key variable) const {
173  const KeyMap::const_iterator item = index_.find(variable);
174  assert(item != index_.end());
175  return item->second;
176  }
177 
180  const KeyMap::iterator item = index_.find(variable);
181  assert(item != index_.end());
182  return item->second;
183  }
184 
186 };
187 
189 template<>
190 struct traits<VariableIndex> : public Testable<VariableIndex> {
191 };
192 
193 } //\ namespace gtsam
194 
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
boost::shared_ptr< VariableIndex > shared_ptr
Definition: VariableIndex.h:45
bool empty(Key variable) const
Return true if no factors associated with a variable.
Definition: VariableIndex.h:98
const_iterator begin() const
Iterator to the first variable entry.
KeyMap::const_iterator iterator
Definition: VariableIndex.h:57
Factor_iterator factorsBegin(Key variable)
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition: Factor.h:32
const_iterator find(Key key) const
Find the iterator for the requested variable entry.
GaussianFactorGraph factors(list_of(factor1)(factor2)(factor3))
const FactorIndices & operator[](Key variable) const
Access a list of factors by variable.
Definition: VariableIndex.h:89
KeyMap::value_type value_type
Definition: VariableIndex.h:58
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
size_t size() const
The number of variable entries. This is equal to the number of unique variable Keys.
Definition: VariableIndex.h:80
const FactorIndices & internalAt(Key variable) const
Internal version of &#39;at&#39; that asserts existence.
Factor_iterator factorsEnd(Key variable)
Definition: pytypes.h:928
size_t nEntries() const
The number of nonzero blocks, i.e. the number of variable-factor entries.
Definition: VariableIndex.h:86
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
VariableIndex()
Default constructor, creates an empty VariableIndex.
Definition: VariableIndex.h:64
FastMap< Key, FactorIndices > KeyMap
Definition: VariableIndex.h:50
const_iterator end() const
Iterator to the first variable entry.
A thin wrapper around std::vector that uses a custom allocator.
traits
Definition: chartTesting.h:28
A thin wrapper around std::map that uses boost&#39;s fast_pool_allocator.
KeyMap::const_iterator const_iterator
Definition: VariableIndex.h:56
VariableIndex(const FG &factorGraph)
Definition: VariableIndex.h:71
FactorIndices & internalAt(Key variable)
Internal version of &#39;at&#39; that asserts existence.
ofstream os("timeSchurFactors.csv")
Factor_const_iterator factorsBegin(Key variable) const
size_t nFactors() const
The number of factors in the original factor graph.
Definition: VariableIndex.h:83
const G double tol
Definition: Group.h:83
FactorIndices::iterator Factor_iterator
Definition: VariableIndex.h:46
std::uint64_t FactorIndex
Integer nonlinear factor index type.
Definition: types.h:64
Factor_const_iterator factorsEnd(Key variable) const
FactorIndices::const_iterator Factor_const_iterator
Definition: VariableIndex.h:47
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
The base class for all factors.


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:51:23