Values.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 <gtsam/inference/Key.h>
30 #include <gtsam/base/VectorSpace.h>
31 
32 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
33 #include <boost/serialization/unique_ptr.hpp>
34 #endif
35 
36 
37 #include <memory>
38 #include <string>
39 #include <utility>
40 
41 namespace gtsam {
42 
43  // Forward declarations / utilities
44  class VectorValues;
45  class ValueAutomaticCasting;
46  template<typename T> static bool _truePredicate(const T&) { return true; }
47 
48  /* ************************************************************************* */
49  class GTSAM_EXPORT ValueCloneAllocator {
50  public:
51  static Value* allocate_clone(const Value& a) { return a.clone_(); }
52  static void deallocate_clone(const Value* a) { a->deallocate_(); }
54  };
55 
65  class GTSAM_EXPORT Values {
66 
67  private:
68  // Internally we store a boost ptr_map, with a ValueCloneAllocator (defined
69  // below) to clone and deallocate the Value objects, and our compile-flag-
70  // dependent FastDefaultAllocator to allocate map nodes. In this way, the
71  // user defines the allocation details (i.e. optimize for memory pool/arenas
72  // concurrency).
74  using KeyValueMap =
75  std::map<Key, std::unique_ptr<Value>, std::less<Key>,
76  std::allocator<std::pair<const Key, std::unique_ptr<Value>>>>;
77 
78  // The member to store the values, see just above
80 
81  public:
82 
84  typedef std::shared_ptr<Values> shared_ptr;
85 
87  typedef std::shared_ptr<const Values> const_shared_ptr;
88 
90  struct GTSAM_EXPORT KeyValuePair {
91  const Key key;
93 
94  KeyValuePair(Key _key, Value& _value) : key(_key), value(_value) {}
95  };
96 
98  struct GTSAM_EXPORT ConstKeyValuePair {
99  const Key key;
100  const Value& value;
101 
102  ConstKeyValuePair(Key _key, const Value& _value) : key(_key), value(_value) {}
103  ConstKeyValuePair(const KeyValuePair& kv) : key(kv.key), value(kv.value) {}
104  };
105 
107 
110 
112  Values() = default;
113 
115  Values(const Values& other);
116 
118  Values(Values&& other);
119 
125  Values(std::initializer_list<ConstKeyValuePair> init);
126 
128  Values(const Values& other, const VectorValues& delta);
129 
133 
135  void print(const std::string& str = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
136 
138  bool equals(const Values& other, double tol=1e-9) const;
139 
143 
152  template <typename ValueType>
153  const ValueType at(Key j) const;
154 
156  double atDouble(size_t key) const { return at<double>(key);}
157 
163  const Value& at(Key j) const;
164 
168  bool exists(Key j) const;
169 
174  template<typename ValueType>
175  const ValueType * exists(Key j) const;
176 
178  size_t size() const { return values_.size(); }
179 
181  bool empty() const { return values_.empty(); }
182 
186 
187  struct deref_iterator {
188  using const_iterator_type = typename KeyValueMap::const_iterator;
191  ConstKeyValuePair operator*() const { return {it_->first, *(it_->second)}; }
192  std::unique_ptr<ConstKeyValuePair> operator->() {
193  return std::make_unique<ConstKeyValuePair>(it_->first, *(it_->second));
194  }
195  bool operator==(const deref_iterator& other) const {
196  return it_ == other.it_;
197  }
198  bool operator!=(const deref_iterator& other) const { return it_ != other.it_; }
200  ++it_;
201  return *this;
202  }
203  };
204 
205  deref_iterator begin() const { return deref_iterator(values_.begin()); }
206  deref_iterator end() const { return deref_iterator(values_.end()); }
207 
210  deref_iterator find(Key j) const { return deref_iterator(values_.find(j)); }
211 
213  deref_iterator lower_bound(Key j) const { return deref_iterator(values_.lower_bound(j)); }
214 
216  deref_iterator upper_bound(Key j) const { return deref_iterator(values_.upper_bound(j)); }
217 
221 
223  Values retract(const VectorValues& delta) const;
224 
229  void retractMasked(const VectorValues& delta, const KeySet& mask);
230 
232  VectorValues localCoordinates(const Values& cp) const;
233 
235 
237  void insert(Key j, const Value& val);
238 
240  void insert(const Values& values);
241 
245  template <typename ValueType>
246  void insert(Key j, const ValueType& val);
247 
249  void insertDouble(Key j, double c) { insert<double>(j,c); }
250 
252  void update(Key j, const Value& val);
253 
258  template <typename T>
259  void update(Key j, const T& val);
260 
262  void update(const Values& values);
263 
265  void insert_or_assign(Key j, const Value& val);
266 
271  void insert_or_assign(const Values& values);
272 
274  template <typename ValueType>
275  void insert_or_assign(Key j, const ValueType& val);
276 
278  void erase(Key j);
279 
284  KeyVector keys() const;
285 
289  KeySet keySet() const;
290 
292  Values& operator=(const Values& rhs);
293 
295  void swap(Values& other) { values_.swap(other.values_); }
296 
298  void clear() { values_.clear(); }
299 
301  size_t dim() const;
302 
304  std::map<Key,size_t> dims() const;
305 
307  VectorValues zeroVectors() const;
308 
309  // Count values of given type \c ValueType
310  template <class ValueType>
311  size_t count() const;
312 
332  template <class ValueType>
333  std::map<Key, ValueType> // , std::less<Key>, Eigen::aligned_allocator<ValueType>
334  extract(const std::function<bool(Key)>& filterFcn = &_truePredicate<Key>) const;
335 
336  private:
337  // Filters based on ValueType (if not Value) and also based on the user-
338  // supplied \c filter function.
339  template<class ValueType>
340  static bool filterHelper(const std::function<bool(Key)> filter, const ConstKeyValuePair& key_value) {
341  // static_assert if ValueType is type: Value
342  static_assert(!std::is_same<Value, ValueType>::value, "ValueType must not be type: Value to use this filter");
343  // Filter and check the type
344  return filter(key_value.key) && (dynamic_cast<const GenericValue<ValueType>*>(&key_value.value));
345  }
346 
347 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
348 
349  friend class boost::serialization::access;
350  template<class ARCHIVE>
351  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
352  ar & BOOST_SERIALIZATION_NVP(values_);
353  }
354 #endif
355 
356  };
357 
358  /* ************************************************************************* */
359  class ValuesKeyAlreadyExists : public std::exception {
360  protected:
361  const Key key_;
362 
363  private:
364  mutable std::string message_;
365 
366  public:
369  key_(key) {}
370 
371  ~ValuesKeyAlreadyExists() noexcept override {}
372 
374  Key key() const noexcept { return key_; }
375 
377  GTSAM_EXPORT const char* what() const noexcept override;
378  };
379 
380  /* ************************************************************************* */
381  class ValuesKeyDoesNotExist : public std::exception {
382  protected:
383  const char* operation_;
384  const Key key_;
385 
386  private:
387  mutable std::string message_;
388 
389  public:
391  ValuesKeyDoesNotExist(const char* operation, Key key) noexcept :
392  operation_(operation), key_(key) {}
393 
394  ~ValuesKeyDoesNotExist() noexcept override {}
395 
397  Key key() const noexcept { return key_; }
398 
400  GTSAM_EXPORT const char* what() const noexcept override;
401  };
402 
403  /* ************************************************************************* */
404  class ValuesIncorrectType : public std::exception {
405  protected:
406  const Key key_;
407  const std::type_info& storedTypeId_;
408  const std::type_info& requestedTypeId_;
409 
410  private:
411  mutable std::string message_;
412 
413  public:
416  const std::type_info& storedTypeId, const std::type_info& requestedTypeId) noexcept :
417  key_(key), storedTypeId_(storedTypeId), requestedTypeId_(requestedTypeId) {}
418 
419  ~ValuesIncorrectType() noexcept override {}
420 
422  Key key() const noexcept { return key_; }
423 
425  const std::type_info& storedTypeId() const { return storedTypeId_; }
426 
428  const std::type_info& requestedTypeId() const { return requestedTypeId_; }
429 
431  GTSAM_EXPORT const char* what() const noexcept override;
432  };
433 
434  /* ************************************************************************* */
435  class DynamicValuesMismatched : public std::exception {
436 
437  public:
439 
440  ~DynamicValuesMismatched() noexcept override {}
441 
442  const char* what() const noexcept override {
443  return "The Values 'this' and the argument passed to Values::localCoordinates have mismatched keys and values";
444  }
445  };
446 
447  /* ************************************************************************* */
448  class NoMatchFoundForFixed: public std::exception {
449 
450  protected:
451  const size_t M1_, N1_;
452  const size_t M2_, N2_;
453 
454  private:
455  mutable std::string message_;
456 
457  public:
458  NoMatchFoundForFixed(size_t M1, size_t N1, size_t M2, size_t N2) noexcept :
459  M1_(M1), N1_(N1), M2_(M2), N2_(N2) {
460  }
461 
462  ~NoMatchFoundForFixed() noexcept override {
463  }
464 
465  GTSAM_EXPORT const char* what() const noexcept override;
466  };
467 
468  /* ************************************************************************* */
470  template<>
471  struct traits<Values> : public Testable<Values> {
472  };
473 
474 } //\ namespace gtsam
475 
476 
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
const gtsam::Symbol key('X', 0)
const_iterator_type it_
Definition: Values.h:189
static bool _truePredicate(const T &)
Definition: Values.h:46
void clear()
Definition: Values.h:298
deref_iterator(const_iterator_type it)
Definition: Values.h:190
A insert(1, 2)=0
def update(text)
Definition: relicense.py:46
static void deallocate_clone(const Value *a)
Definition: Values.h:52
std::string serialize(const T &input)
serializes to a string
deref_iterator lower_bound(Key j) const
Definition: Values.h:213
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:90
Key key() const noexcept
The key that was attempted to be accessed that does not exist.
Definition: Values.h:422
~ValuesKeyDoesNotExist() noexcept override
Definition: Values.h:394
std::shared_ptr< const Values > const_shared_ptr
A const shared_ptr to this class.
Definition: Values.h:87
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
virtual Value * clone_() const =0
Key key() const noexcept
The duplicate key that was attempted to be added.
Definition: Values.h:374
leaf::MyValues values
deref_iterator find(Key j) const
Definition: Values.h:210
MatrixXf M1
virtual void deallocate_() const =0
Default allocator for list, map, and set types.
ValuesKeyDoesNotExist(const char *operation, Key key) noexcept
Construct with the key that does not exist in the values.
Definition: Values.h:391
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:98
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
const std::type_info & storedTypeId() const
The typeid of the value stores in the Values.
Definition: Values.h:425
internal::FastDefaultAllocator< typename std::pair< const Key, void * > >::type KeyValuePtrPairAllocator
Definition: Values.h:73
const Value & value
The value.
Definition: Values.h:100
const char * operation_
The operation that attempted to access the key.
Definition: Values.h:383
KeyValuePair value_type
Definition: Values.h:106
ConstKeyValuePair operator*() const
Definition: Values.h:191
const std::type_info & requestedTypeId() const
The requested typeid.
Definition: Values.h:428
const Key key
The key.
Definition: Values.h:91
std::unique_ptr< ConstKeyValuePair > operator->()
Definition: Values.h:192
M1<< 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12;Map< MatrixXf > M2(M1.data(), 6, 2)
KeyValueMap values_
Definition: Values.h:79
Definition: pytypes.h:1403
~ValuesIncorrectType() noexcept override
Definition: Values.h:419
~NoMatchFoundForFixed() noexcept override
Definition: Values.h:462
bool operator!=(const deref_iterator &other) const
Definition: Values.h:198
Array< double, 1, 3 > e(1./3., 0.5, 2.)
size_t size() const
Definition: Values.h:178
typename KeyValueMap::const_iterator const_iterator_type
Definition: Values.h:188
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
std::map< Key, std::unique_ptr< Value >, std::less< Key >, std::allocator< std::pair< const Key, std::unique_ptr< Value > >> > KeyValueMap
Definition: Values.h:76
double atDouble(size_t key) const
version for double
Definition: Values.h:156
ValuesKeyAlreadyExists(Key key) noexcept
Construct with the key-value pair attempted to be added.
Definition: Values.h:368
traits
Definition: chartTesting.h:28
KeyValuePair(Key _key, Value &_value)
Definition: Values.h:94
const Key key
The key.
Definition: Values.h:99
A small structure to hold a non zero as a triplet (i,j,value).
Definition: SparseUtil.h:162
void swap(Values &other)
Definition: Values.h:295
const Key key_
The key that already existed.
Definition: Values.h:361
const char * what() const noexcept override
Definition: Values.h:442
std::vector< float > Values
std::string message_
Definition: Values.h:411
~DynamicValuesMismatched() noexcept override
Definition: Values.h:440
NoMatchFoundForFixed(size_t M1, size_t N1, size_t M2, size_t N2) noexcept
Definition: Values.h:458
deref_iterator & operator++()
Definition: Values.h:199
const std::type_info & requestedTypeId_
Definition: Values.h:408
const Key key_
The key requested.
Definition: Values.h:406
const std::type_info & storedTypeId_
Definition: Values.h:407
static Value * allocate_clone(const Value &a)
Definition: Values.h:51
bool operator==(const deref_iterator &other) const
Definition: Values.h:195
ValuesIncorrectType(Key key, const std::type_info &storedTypeId, const std::type_info &requestedTypeId) noexcept
Construct with the key that does not exist in the values.
Definition: Values.h:415
DynamicValuesMismatched() noexcept
Definition: Values.h:438
Value & value
The value.
Definition: Values.h:92
const G double tol
Definition: Group.h:86
deref_iterator upper_bound(Key j) const
Definition: Values.h:216
const KeyVector keys
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
void insertDouble(Key j, double c)
version for double
Definition: Values.h:249
deref_iterator begin() const
Definition: Values.h:205
deref_iterator end() const
Definition: Values.h:206
An easy way to control which allocator is used for Fast* collections.
std::shared_ptr< Values > shared_ptr
A shared_ptr to this class.
Definition: Values.h:84
bool empty() const
Definition: Values.h:181
~ValuesKeyAlreadyExists() noexcept override
Definition: Values.h:371
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
ConstKeyValuePair(const KeyValuePair &kv)
Definition: Values.h:103
std::ptrdiff_t j
Key key() const noexcept
The key that was attempted to be accessed that does not exist.
Definition: Values.h:397
Definition: pytypes.h:1370
static bool filterHelper(const std::function< bool(Key)> filter, const ConstKeyValuePair &key_value)
Definition: Values.h:340
const Key key_
The key that does not exist.
Definition: Values.h:384
ConstKeyValuePair(Key _key, const Value &_value)
Definition: Values.h:102


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:40:43