Values-inl.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 
25 #pragma once
26 
27 #include <utility>
28 
29 #include <gtsam/nonlinear/Values.h> // Only so Eclipse finds class definition
30 
31 namespace gtsam {
32 
33 
34  /* ************************************************************************* */
35  template<class ValueType>
37  const Key key;
38  ValueType& value;
39 
40  _ValuesKeyValuePair(Key _key, ValueType& _value) : key(_key), value(_value) {}
41  };
42 
43  /* ************************************************************************* */
44  template<class ValueType>
46  const Key key;
47  const ValueType& value;
48 
49  _ValuesConstKeyValuePair(Key _key, const ValueType& _value) :
50  key(_key), value(_value) {
51  }
53  key(rhs.key), value(rhs.value) {
54  }
55  };
56 
57  /* ************************************************************************* */
58 
59  // Cast helpers for making _Values[Const]KeyValuePair's from Values::[Const]KeyValuePair
60  // need to use a struct here for later partial specialization
61  template<class ValueType, class CastedKeyValuePairType, class KeyValuePairType>
63  static CastedKeyValuePairType cast(KeyValuePairType key_value) {
64  // Static cast because we already checked the type during filtering
65  return CastedKeyValuePairType(key_value.key,
66  const_cast<GenericValue<ValueType>&>(static_cast<const GenericValue<
67  ValueType>&>(key_value.value)).value());
68  }
69  };
70  // partial specialized version for ValueType == Value
71  template<class CastedKeyValuePairType, class KeyValuePairType>
72  struct ValuesCastHelper<Value, CastedKeyValuePairType, KeyValuePairType> {
73  static CastedKeyValuePairType cast(KeyValuePairType key_value) {
74  // Static cast because we already checked the type during filtering
75  // in this case the casted and keyvalue pair are essentially the same type
76  // (key, Value&) so perhaps this could be done with just a cast of the key_value?
77  return CastedKeyValuePairType(key_value.key, key_value.value);
78  }
79  };
80  // partial specialized version for ValueType == Value
81  template<class CastedKeyValuePairType, class KeyValuePairType>
82  struct ValuesCastHelper<const Value, CastedKeyValuePairType, KeyValuePairType> {
83  static CastedKeyValuePairType cast(KeyValuePairType key_value) {
84  // Static cast because we already checked the type during filtering
85  // in this case the casted and keyvalue pair are essentially the same type
86  // (key, Value&) so perhaps this could be done with just a cast of the key_value?
87  return CastedKeyValuePairType(key_value.key, key_value.value);
88  }
89  };
90 
91  /* ************************************************************************* */
92  template<class ValueType>
93  class Values::Filtered {
94  public:
98  typedef KeyValuePair value_type;
99 
100  typedef
101  boost::transform_iterator<
102  KeyValuePair(*)(Values::KeyValuePair),
103  boost::filter_iterator<
104  boost::function<bool(const Values::ConstKeyValuePair&)>,
107 
109 
110  typedef
111  boost::transform_iterator<
112  ConstKeyValuePair(*)(Values::ConstKeyValuePair),
113  boost::filter_iterator<
114  boost::function<bool(const Values::ConstKeyValuePair&)>,
117 
118  iterator begin() { return begin_; }
119  iterator end() { return end_; }
120  const_iterator begin() const { return begin_; }
121  const_iterator end() const { return end_; }
122  const_const_iterator beginConst() const { return constBegin_; }
123  const_const_iterator endConst() const { return constEnd_; }
124 
126  size_t size() const {
127  size_t i = 0;
128  for (const_const_iterator it = beginConst(); it != endConst(); ++it)
129  ++i;
130  return i;
131  }
132 
133  private:
135  const boost::function<bool(const Values::ConstKeyValuePair&)>& filter,
136  Values& values) :
137  begin_(
138  boost::make_transform_iterator(
139  boost::make_filter_iterator(filter, values.begin(), values.end()),
140  &ValuesCastHelper<ValueType, KeyValuePair, Values::KeyValuePair>::cast)), end_(
141  boost::make_transform_iterator(
142  boost::make_filter_iterator(filter, values.end(), values.end()),
143  &ValuesCastHelper<ValueType, KeyValuePair, Values::KeyValuePair>::cast)), constBegin_(
144  boost::make_transform_iterator(
145  boost::make_filter_iterator(filter,
146  ((const Values&) values).begin(),
147  ((const Values&) values).end()),
148  &ValuesCastHelper<ValueType, ConstKeyValuePair,
149  Values::ConstKeyValuePair>::cast)), constEnd_(
150  boost::make_transform_iterator(
151  boost::make_filter_iterator(filter,
152  ((const Values&) values).end(),
153  ((const Values&) values).end()),
154  &ValuesCastHelper<ValueType, ConstKeyValuePair,
155  Values::ConstKeyValuePair>::cast)) {
156  }
157 
158  friend class Values;
163  };
164 
165  /* ************************************************************************* */
166  template<class ValueType>
167  class Values::ConstFiltered {
168  public:
171  typedef KeyValuePair value_type;
172 
175 
178  begin_(rhs.beginConst()),
179  end_(rhs.endConst()) {}
180 
181  iterator begin() { return begin_; }
182  iterator end() { return end_; }
183  const_iterator begin() const { return begin_; }
184  const_iterator end() const { return end_; }
185 
187  size_t size() const {
188  size_t i = 0;
189  for (const_iterator it = begin(); it != end(); ++it)
190  ++i;
191  return i;
192  }
193 
194  FastList<Key> keys() const {
196  for(const_iterator it = begin(); it != end(); ++it)
197  result.push_back(it->key);
198  return result;
199  }
200 
201  private:
202  friend class Values;
203  const_iterator begin_;
204  const_iterator end_;
206  const boost::function<bool(const Values::ConstKeyValuePair&)>& filter,
207  const Values& values) {
208  // We remove the const from values to create a non-const Filtered
209  // view, then pull the const_iterators out of it.
210  const Filtered<ValueType> filtered(filter, const_cast<Values&>(values));
211  begin_ = filtered.beginConst();
212  end_ = filtered.endConst();
213  }
214  };
215 
216  /* ************************************************************************* */
218  template<class ValueType>
219  Values::Values(const Values::Filtered<ValueType>& view) {
220  for(const auto key_value: view) {
221  Key key = key_value.key;
222  insert(key, static_cast<const ValueType&>(key_value.value));
223  }
224  }
225 
226  /* ************************************************************************* */
227  template<class ValueType>
228  Values::Values(const Values::ConstFiltered<ValueType>& view) {
229  for(const auto key_value: view) {
230  Key key = key_value.key;
231  insert(key, static_cast<const ValueType&>(key_value.value));
232  }
233  }
234 
235  /* ************************************************************************* */
236  Values::Filtered<Value>
237  inline Values::filter(const boost::function<bool(Key)>& filterFcn) {
238  return filter<Value>(filterFcn);
239  }
240 
241  /* ************************************************************************* */
242  template<class ValueType>
243  Values::Filtered<ValueType>
244  Values::filter(const boost::function<bool(Key)>& filterFcn) {
245  return Filtered<ValueType>(boost::bind(&filterHelper<ValueType>, filterFcn, _1), *this);
246  }
247 
248  /* ************************************************************************* */
249  Values::ConstFiltered<Value>
250  inline Values::filter(const boost::function<bool(Key)>& filterFcn) const {
251  return filter<Value>(filterFcn);
252  }
253 
254  /* ************************************************************************* */
255  template<class ValueType>
256  Values::ConstFiltered<ValueType>
257  Values::filter(const boost::function<bool(Key)>& filterFcn) const {
258  return ConstFiltered<ValueType>(boost::bind(&filterHelper<ValueType>, filterFcn, _1), *this);
259  }
260 
261  /* ************************************************************************* */
262  template<>
263  inline bool Values::filterHelper<Value>(const boost::function<bool(Key)> filter,
264  const ConstKeyValuePair& key_value) {
265  // Filter and check the type
266  return filter(key_value.key);
267  }
268 
269  /* ************************************************************************* */
270 
271  namespace internal {
272 
273  // Check the type and throw exception if incorrect
274  // Generic version, partially specialized below for various Eigen Matrix types
275  template <typename ValueType>
276  struct handle {
277  ValueType operator()(Key j, const Value* const pointer) {
278  try {
279  // value returns a const ValueType&, and the return makes a copy !!!!!
280  return dynamic_cast<const GenericValue<ValueType>&>(*pointer).value();
281  } catch (std::bad_cast&) {
282  throw ValuesIncorrectType(j, typeid(*pointer), typeid(ValueType));
283  }
284  }
285  };
286 
287  template <typename MatrixType, bool isDynamic>
289 
290  // Handle dynamic matrices
291  template <int M, int N>
292  struct handle_matrix<Eigen::Matrix<double, M, N>, true> {
294  try {
295  // value returns a const Matrix&, and the return makes a copy !!!!!
296  return dynamic_cast<const GenericValue<Eigen::Matrix<double, M, N>>&>(*pointer).value();
297  } catch (std::bad_cast&) {
298  // If a fixed matrix was stored, we end up here as well.
299  throw ValuesIncorrectType(j, typeid(*pointer), typeid(Eigen::Matrix<double, M, N>));
300  }
301  }
302  };
303 
304  // Handle fixed matrices
305  template <int M, int N>
306  struct handle_matrix<Eigen::Matrix<double, M, N>, false> {
308  try {
309  // value returns a const MatrixMN&, and the return makes a copy !!!!!
310  return dynamic_cast<const GenericValue<Eigen::Matrix<double, M, N>>&>(*pointer).value();
311  } catch (std::bad_cast&) {
312  Matrix A;
313  try {
314  // Check if a dynamic matrix was stored
315  A = handle_matrix<Eigen::MatrixXd, true>()(j, pointer); // will throw if not....
316  } catch (const ValuesIncorrectType&) {
317  // Or a dynamic vector
318  A = handle_matrix<Eigen::VectorXd, true>()(j, pointer); // will throw if not....
319  }
320  // Yes: check size, and throw if not a match
321  if (A.rows() != M || A.cols() != N)
322  throw NoMatchFoundForFixed(M, N, A.rows(), A.cols());
323  else
324  return A; // copy but not malloc
325  }
326  }
327  };
328 
329  // Handle matrices
330  template <int M, int N>
331  struct handle<Eigen::Matrix<double, M, N>> {
334  (M == Eigen::Dynamic || N == Eigen::Dynamic)>()(j, pointer);
335  }
336  };
337 
338  } // internal
339 
340  /* ************************************************************************* */
341  template <typename ValueType>
342  const ValueType Values::at(Key j) const {
343  // Find the item
344  KeyValueMap::const_iterator item = values_.find(j);
345 
346  // Throw exception if it does not exist
347  if (item == values_.end()) throw ValuesKeyDoesNotExist("at", j);
348 
349  // Check the type and throw exception if incorrect
350  // h() split in two lines to avoid internal compiler error (MSVC2017)
352  return h(j, item->second);
353  }
354 
355  /* ************************************************************************* */
356  template<typename ValueType>
357  boost::optional<const ValueType&> Values::exists(Key j) const {
358  // Find the item
359  KeyValueMap::const_iterator item = values_.find(j);
360 
361  if(item != values_.end()) {
362  // dynamic cast the type and throw exception if incorrect
363  const Value& value = *item->second;
364  try {
365  return dynamic_cast<const GenericValue<ValueType>&>(value).value();
366  } catch (std::bad_cast &) {
367  // NOTE(abe): clang warns about potential side effects if done in typeid
368  const Value* value = item->second;
369  throw ValuesIncorrectType(j, typeid(*value), typeid(ValueType));
370  }
371  } else {
372  return boost::none;
373  }
374  }
375 
376  /* ************************************************************************* */
377 
378  // insert a templated value
379  template<typename ValueType>
380  void Values::insert(Key j, const ValueType& val) {
381  insert(j, static_cast<const Value&>(GenericValue<ValueType>(val)));
382  }
383 
384  // update with templated value
385  template <typename ValueType>
386  void Values::update(Key j, const ValueType& val) {
387  update(j, static_cast<const Value&>(GenericValue<ValueType>(val)));
388  }
389 
390 }
Eigen::Matrix< double, M, N > operator()(Key j, const Value *const pointer)
Definition: Values-inl.h:332
const_const_iterator beginConst() const
Definition: Values-inl.h:122
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:38
A insert(1, 2)=0
bool exists(Key j) const
Definition: Values.cpp:104
_ValuesConstKeyValuePair(const _ValuesKeyValuePair< ValueType > &rhs)
Definition: Values-inl.h:52
def update(text)
Definition: relicense.py:46
A non-templated config holding any types of Manifold-group elements.
size_t size() const
Definition: Values-inl.h:126
void insert(Key j, const Value &val)
Definition: Values.cpp:140
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:43
const_iterator end() const
Definition: Values-inl.h:184
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set view
leaf::MyValues values
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
_ValuesKeyValuePair< ValueType > KeyValuePair
Definition: Values-inl.h:96
Eigen::Matrix< double, M, N > operator()(Key j, const Value *const pointer)
Definition: Values-inl.h:293
#define N
Definition: gksort.c:12
const T & value() const
Return a constant value.
Definition: GenericValue.h:67
const_const_iterator constBegin_
Definition: Values-inl.h:161
const_const_iterator constEnd_
Definition: Values-inl.h:162
const Key key
The key.
Definition: Values-inl.h:46
ValueType operator()(Key j, const Value *const pointer)
Definition: Values-inl.h:277
static CastedKeyValuePairType cast(KeyValuePairType key_value)
Definition: Values-inl.h:63
boost::transform_iterator< boost::function1< KeyValuePair, const KeyValuePtrPair & >, KeyValueMap::iterator > iterator
Mutable forward iterator, with value type KeyValuePair.
Definition: Values.h:120
ConstFiltered(const Filtered< ValueType > &rhs)
Definition: Values-inl.h:177
Values result
Filtered< ValueType >::const_const_iterator iterator
Definition: Values-inl.h:173
const ValueType at(Key j) const
Definition: Values-inl.h:342
const Key key
The key.
Definition: Values-inl.h:37
Eigen::Matrix< double, M, N > operator()(Key j, const Value *const pointer)
Definition: Values-inl.h:307
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
Filtered< ValueType >::const_const_iterator const_iterator
Definition: Values-inl.h:174
boost::transform_iterator< boost::function1< ConstKeyValuePair, const ConstKeyValuePtrPair & >, KeyValueMap::const_iterator > const_iterator
Const forward iterator, with value type ConstKeyValuePair.
Definition: Values.h:124
static CastedKeyValuePairType cast(KeyValuePairType key_value)
Definition: Values-inl.h:83
traits
Definition: chartTesting.h:28
const double h
const_iterator begin() const
Definition: Values-inl.h:183
const_const_iterator endConst() const
Definition: Values-inl.h:123
_ValuesConstKeyValuePair< ValueType > KeyValuePair
Definition: Values-inl.h:170
boost::transform_iterator< KeyValuePair(*)(Values::KeyValuePair), boost::filter_iterator< boost::function< bool(const Values::ConstKeyValuePair &)>, Values::iterator > > iterator
Definition: Values-inl.h:106
ValueType & value
The value.
Definition: Values-inl.h:38
_ValuesConstKeyValuePair< ValueType > ConstKeyValuePair
Definition: Values-inl.h:97
static CastedKeyValuePairType cast(KeyValuePairType key_value)
Definition: Values-inl.h:73
Filtered(const boost::function< bool(const Values::ConstKeyValuePair &)> &filter, Values &values)
Definition: Values-inl.h:134
void update(Key j, const Value &val)
Definition: Values.cpp:161
const ValueType & value
The value.
Definition: Values-inl.h:47
const int Dynamic
Definition: Constants.h:21
boost::transform_iterator< ConstKeyValuePair(*)(Values::ConstKeyValuePair), boost::filter_iterator< boost::function< bool(const Values::ConstKeyValuePair &)>, Values::const_iterator > > const_const_iterator
Definition: Values-inl.h:116
The matrix class, also used for vectors and row-vectors.
KeyValuePair value_type
Definition: Values-inl.h:98
const_iterator end() const
Definition: Values-inl.h:121
_ValuesConstKeyValuePair(Key _key, const ValueType &_value)
Definition: Values-inl.h:49
ConstFiltered(const boost::function< bool(const Values::ConstKeyValuePair &)> &filter, const Values &values)
Definition: Values-inl.h:205
_ValuesKeyValuePair(Key _key, ValueType &_value)
Definition: Values-inl.h:40
const_iterator begin() const
Definition: Values-inl.h:120
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
std::ptrdiff_t j
Filtered< Value > filter(const boost::function< bool(Key)> &filterFcn)
Definition: Values-inl.h:237
FastList< Key > keys() const
Definition: Values-inl.h:194


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