gtsam 4.1.1
gtsam
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
28#include <gtsam/base/GenericValue.h>
29#include <gtsam/base/VectorSpace.h>
30#include <gtsam/inference/Key.h>
31#include <boost/iterator/transform_iterator.hpp>
32#include <boost/iterator/filter_iterator.hpp>
33#include <boost/ptr_container/serialize_ptr_map.hpp>
34#include <boost/shared_ptr.hpp>
35
36#include <string>
37#include <utility>
38
39namespace gtsam {
40
41 // Forward declarations / utilities
42 class VectorValues;
43 class ValueAutomaticCasting;
44 template<typename T> static bool _truePredicate(const T&) { return true; }
45
46 /* ************************************************************************* */
47 class GTSAM_EXPORT ValueCloneAllocator {
48 public:
49 static Value* allocate_clone(const Value& a) { return a.clone_(); }
50 static void deallocate_clone(const Value* a) { a->deallocate_(); }
52 };
53
63 class GTSAM_EXPORT Values {
64
65 private:
66 // Internally we store a boost ptr_map, with a ValueCloneAllocator (defined
67 // below) to clone and deallocate the Value objects, and our compile-flag-
68 // dependent FastDefaultAllocator to allocate map nodes. In this way, the
69 // user defines the allocation details (i.e. optimize for memory pool/arenas
70 // concurrency).
71 typedef internal::FastDefaultAllocator<typename std::pair<const Key, void*>>::type KeyValuePtrPairAllocator;
72 typedef boost::ptr_map<
73 Key,
74 Value,
75 std::less<Key>,
77 KeyValuePtrPairAllocator > KeyValueMap;
78
79 // The member to store the values, see just above
80 KeyValueMap values_;
81
82 // Types obtained by iterating
83 typedef KeyValueMap::const_iterator::value_type ConstKeyValuePtrPair;
84 typedef KeyValueMap::iterator::value_type KeyValuePtrPair;
85
86 public:
87
89 typedef boost::shared_ptr<Values> shared_ptr;
90
92 typedef boost::shared_ptr<const Values> const_shared_ptr;
93
95 struct GTSAM_EXPORT KeyValuePair {
96 const Key key;
98
99 KeyValuePair(Key _key, Value& _value) : key(_key), value(_value) {}
100 };
101
103 struct GTSAM_EXPORT ConstKeyValuePair {
104 const Key key;
105 const Value& value;
106
107 ConstKeyValuePair(Key _key, const Value& _value) : key(_key), value(_value) {}
108 ConstKeyValuePair(const KeyValuePair& kv) : key(kv.key), value(kv.value) {}
109 };
110
112 typedef boost::transform_iterator<
113 std::function<KeyValuePair(const KeyValuePtrPair&)>, KeyValueMap::iterator> iterator;
114
116 typedef boost::transform_iterator<
117 std::function<ConstKeyValuePair(const ConstKeyValuePtrPair&)>, KeyValueMap::const_iterator> const_iterator;
118
120 typedef boost::transform_iterator<
121 std::function<KeyValuePair(const KeyValuePtrPair&)>, KeyValueMap::reverse_iterator> reverse_iterator;
122
124 typedef boost::transform_iterator<
125 std::function<ConstKeyValuePair(const ConstKeyValuePtrPair&)>, KeyValueMap::const_reverse_iterator> const_reverse_iterator;
126
127 typedef KeyValuePair value_type;
128
130 template<class ValueType = Value>
131 class Filtered;
132
134 template<class ValueType = Value>
135 class ConstFiltered;
136
139
141 Values(const Values& other);
142
144 Values(Values&& other);
145
151 Values(std::initializer_list<ConstKeyValuePair> init);
152
154 Values(const Values& other, const VectorValues& delta);
155
157 template<class ValueType>
158 Values(const Filtered<ValueType>& view);
159
161 template<class ValueType>
162 Values(const ConstFiltered<ValueType>& view);
163
166
168 void print(const std::string& str = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
169
171 bool equals(const Values& other, double tol=1e-9) const;
172
174
183 template <typename ValueType>
184 const ValueType at(Key j) const;
185
187 double atDouble(size_t key) const { return at<double>(key);}
188
194 const Value& at(Key j) const;
195
199 bool exists(Key j) const;
200
205 template<typename ValueType>
206 boost::optional<const ValueType&> exists(Key j) const;
207
210 iterator find(Key j) { return boost::make_transform_iterator(values_.find(j), &make_deref_pair); }
211
214 const_iterator find(Key j) const { return boost::make_transform_iterator(values_.find(j), &make_const_deref_pair); }
215
217 iterator lower_bound(Key j) { return boost::make_transform_iterator(values_.lower_bound(j), &make_deref_pair); }
218
220 const_iterator lower_bound(Key j) const { return boost::make_transform_iterator(values_.lower_bound(j), &make_const_deref_pair); }
221
223 iterator upper_bound(Key j) { return boost::make_transform_iterator(values_.upper_bound(j), &make_deref_pair); }
224
226 const_iterator upper_bound(Key j) const { return boost::make_transform_iterator(values_.upper_bound(j), &make_const_deref_pair); }
227
229 size_t size() const { return values_.size(); }
230
232 bool empty() const { return values_.empty(); }
233
234 const_iterator begin() const { return boost::make_transform_iterator(values_.begin(), &make_const_deref_pair); }
235 const_iterator end() const { return boost::make_transform_iterator(values_.end(), &make_const_deref_pair); }
236 iterator begin() { return boost::make_transform_iterator(values_.begin(), &make_deref_pair); }
237 iterator end() { return boost::make_transform_iterator(values_.end(), &make_deref_pair); }
238 const_reverse_iterator rbegin() const { return boost::make_transform_iterator(values_.rbegin(), &make_const_deref_pair); }
239 const_reverse_iterator rend() const { return boost::make_transform_iterator(values_.rend(), &make_const_deref_pair); }
240 reverse_iterator rbegin() { return boost::make_transform_iterator(values_.rbegin(), &make_deref_pair); }
241 reverse_iterator rend() { return boost::make_transform_iterator(values_.rend(), &make_deref_pair); }
242
245
247 Values retract(const VectorValues& delta) const;
248
250 VectorValues localCoordinates(const Values& cp) const;
251
253
255 void insert(Key j, const Value& val);
256
258 void insert(const Values& values);
259
263 template <typename ValueType>
264 void insert(Key j, const ValueType& val);
265
267 void insertDouble(Key j, double c) { insert<double>(j,c); }
268
273 std::pair<iterator, bool> tryInsert(Key j, const Value& value);
274
276 void update(Key j, const Value& val);
277
282 template <typename T>
283 void update(Key j, const T& val);
284
286 void update(const Values& values);
287
289 void erase(Key j);
290
295 KeyVector keys() const;
296
298 Values& operator=(const Values& rhs);
299
301 void swap(Values& other) { values_.swap(other.values_); }
302
304 void clear() { values_.clear(); }
305
307 size_t dim() const;
308
310 VectorValues zeroVectors() const;
311
325 Filtered<Value>
326 filter(const std::function<bool(Key)>& filterFcn);
327
347 template<class ValueType>
348 Filtered<ValueType>
349 filter(const std::function<bool(Key)>& filterFcn = &_truePredicate<Key>);
350
364 ConstFiltered<Value>
365 filter(const std::function<bool(Key)>& filterFcn) const;
366
385 template<class ValueType>
386 ConstFiltered<ValueType>
387 filter(const std::function<bool(Key)>& filterFcn = &_truePredicate<Key>) const;
388
389 // Count values of given type \c ValueType
390 template<class ValueType>
391 size_t count() const {
392 size_t i = 0;
393 for (const auto key_value : *this) {
394 if (dynamic_cast<const GenericValue<ValueType>*>(&key_value.value))
395 ++i;
396 }
397 return i;
398 }
399
400 private:
401 // Filters based on ValueType (if not Value) and also based on the user-
402 // supplied \c filter function.
403 template<class ValueType>
404 static bool filterHelper(const std::function<bool(Key)> filter, const ConstKeyValuePair& key_value) {
405 BOOST_STATIC_ASSERT((!boost::is_same<ValueType, Value>::value));
406 // Filter and check the type
407 return filter(key_value.key) && (dynamic_cast<const GenericValue<ValueType>*>(&key_value.value));
408 }
409
411 friend class boost::serialization::access;
412 template<class ARCHIVE>
413 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
414 ar & BOOST_SERIALIZATION_NVP(values_);
415 }
416
417 static ConstKeyValuePair make_const_deref_pair(const KeyValueMap::const_iterator::value_type& key_value) {
418 return ConstKeyValuePair(key_value.first, *key_value.second); }
419
420 static KeyValuePair make_deref_pair(const KeyValueMap::iterator::value_type& key_value) {
421 return KeyValuePair(key_value.first, *key_value.second); }
422
423 };
424
425 /* ************************************************************************* */
426 class ValuesKeyAlreadyExists : public std::exception {
427 protected:
428 const Key key_;
429
430 private:
431 mutable std::string message_;
432
433 public:
436 key_(key) {}
437
438 ~ValuesKeyAlreadyExists() noexcept override {}
439
441 Key key() const noexcept { return key_; }
442
444 GTSAM_EXPORT const char* what() const noexcept override;
445 };
446
447 /* ************************************************************************* */
448 class ValuesKeyDoesNotExist : public std::exception {
449 protected:
450 const char* operation_;
451 const Key key_;
452
453 private:
454 mutable std::string message_;
455
456 public:
458 ValuesKeyDoesNotExist(const char* operation, Key key) noexcept :
459 operation_(operation), key_(key) {}
460
461 ~ValuesKeyDoesNotExist() noexcept override {}
462
464 Key key() const noexcept { return key_; }
465
467 GTSAM_EXPORT const char* what() const noexcept override;
468 };
469
470 /* ************************************************************************* */
471 class ValuesIncorrectType : public std::exception {
472 protected:
473 const Key key_;
474 const std::type_info& storedTypeId_;
475 const std::type_info& requestedTypeId_;
476
477 private:
478 mutable std::string message_;
479
480 public:
483 const std::type_info& storedTypeId, const std::type_info& requestedTypeId) noexcept :
484 key_(key), storedTypeId_(storedTypeId), requestedTypeId_(requestedTypeId) {}
485
486 ~ValuesIncorrectType() noexcept override {}
487
489 Key key() const noexcept { return key_; }
490
492 const std::type_info& storedTypeId() const { return storedTypeId_; }
493
495 const std::type_info& requestedTypeId() const { return requestedTypeId_; }
496
498 GTSAM_EXPORT const char* what() const noexcept override;
499 };
500
501 /* ************************************************************************* */
502 class DynamicValuesMismatched : public std::exception {
503
504 public:
505 DynamicValuesMismatched() noexcept {}
506
507 ~DynamicValuesMismatched() noexcept override {}
508
509 const char* what() const noexcept override {
510 return "The Values 'this' and the argument passed to Values::localCoordinates have mismatched keys and values";
511 }
512 };
513
514 /* ************************************************************************* */
515 class NoMatchFoundForFixed: public std::exception {
516
517 protected:
518 const size_t M1_, N1_;
519 const size_t M2_, N2_;
520
521 private:
522 mutable std::string message_;
523
524 public:
525 NoMatchFoundForFixed(size_t M1, size_t N1, size_t M2, size_t N2) noexcept :
526 M1_(M1), N1_(N1), M2_(M2), N2_(N2) {
527 }
528
529 ~NoMatchFoundForFixed() noexcept override {
530 }
531
532 GTSAM_EXPORT const char* what() const noexcept override;
533 };
534
535 /* ************************************************************************* */
537 template<>
538 struct traits<Values> : public Testable<Values> {
539 };
540
541} //\ namespace gtsam
542
543
544#include <gtsam/nonlinear/Values-inl.h>
An easy way to control which allocator is used for Fast* collections.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:112
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:69
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
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
Default allocator for list, map, and set types.
Definition: FastDefaultAllocator.h:50
Wraps any type T so it can play as a Value.
Definition: GenericValue.h:47
Template to create a binary predicate.
Definition: Testable.h:111
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:151
This is the base class for any type to be stored in Values.
Definition: Value.h:36
virtual void deallocate_() const =0
Deallocate a raw pointer of this value.
virtual Value * clone_() const =0
Clone this value in a special memory pool, must be deleted with Value::deallocate_,...
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:74
A filtered view of a Values, returned from Values::filter.
Definition: Values-inl.h:95
A filtered view of a const Values, returned from Values::filter.
Definition: Values-inl.h:169
Definition: Values.h:47
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:63
void update(Key j, const T &val)
Templated version to update a variable with the given j, throws KeyDoesNotExist<J> if j is not presen...
bool empty() const
whether the config is empty
Definition: Values.h:232
iterator upper_bound(Key j)
Find the lowest-ordered element greater than the specified key.
Definition: Values.h:223
const_iterator upper_bound(Key j) const
Find the lowest-ordered element greater than the specified key.
Definition: Values.h:226
boost::transform_iterator< std::function< KeyValuePair(const KeyValuePtrPair &)>, KeyValueMap::reverse_iterator > reverse_iterator
Mutable reverse iterator, with value type KeyValuePair.
Definition: Values.h:121
boost::transform_iterator< std::function< ConstKeyValuePair(const ConstKeyValuePtrPair &)>, KeyValueMap::const_iterator > const_iterator
Const forward iterator, with value type ConstKeyValuePair.
Definition: Values.h:117
boost::shared_ptr< Values > shared_ptr
A shared_ptr to this class.
Definition: Values.h:89
void clear()
Remove all variables from the config.
Definition: Values.h:304
const_iterator find(Key j) const
Find an element by key, returning an iterator, or end() if the key was not found.
Definition: Values.h:214
const_iterator lower_bound(Key j) const
Find the element greater than or equal to the specified key.
Definition: Values.h:220
boost::transform_iterator< std::function< ConstKeyValuePair(const ConstKeyValuePtrPair &)>, KeyValueMap::const_reverse_iterator > const_reverse_iterator
Const reverse iterator, with value type ConstKeyValuePair.
Definition: Values.h:125
void swap(Values &other)
Swap the contents of two Values without copying data.
Definition: Values.h:301
void insertDouble(Key j, double c)
version for double
Definition: Values.h:267
size_t size() const
The number of variables in this config.
Definition: Values.h:229
iterator find(Key j)
Find an element by key, returning an iterator, or end() if the key was not found.
Definition: Values.h:210
boost::shared_ptr< const Values > const_shared_ptr
A const shared_ptr to this class.
Definition: Values.h:92
Values()
Default constructor creates an empty Values class.
Definition: Values.h:138
iterator lower_bound(Key j)
Find the element greater than or equal to the specified key.
Definition: Values.h:217
boost::transform_iterator< std::function< KeyValuePair(const KeyValuePtrPair &)>, KeyValueMap::iterator > iterator
Mutable forward iterator, with value type KeyValuePair.
Definition: Values.h:113
double atDouble(size_t key) const
version for double
Definition: Values.h:187
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:95
Value & value
The value.
Definition: Values.h:97
const Key key
The key.
Definition: Values.h:96
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:103
const Key key
The key.
Definition: Values.h:104
const Value & value
The value.
Definition: Values.h:105
Definition: Values.h:426
const Key key_
The key that already existed.
Definition: Values.h:428
GTSAM_EXPORT const char * what() const noexcept override
The message to be displayed to the user.
Definition: Values.cpp:216
ValuesKeyAlreadyExists(Key key) noexcept
Construct with the key-value pair attempted to be added.
Definition: Values.h:435
Key key() const noexcept
The duplicate key that was attempted to be added.
Definition: Values.h:441
Definition: Values.h:448
ValuesKeyDoesNotExist(const char *operation, Key key) noexcept
Construct with the key that does not exist in the values.
Definition: Values.h:458
const Key key_
The key that does not exist.
Definition: Values.h:451
Key key() const noexcept
The key that was attempted to be accessed that does not exist.
Definition: Values.h:464
const char * operation_
The operation that attempted to access the key.
Definition: Values.h:450
Definition: Values.h:471
const std::type_info & storedTypeId() const
The typeid of the value stores in the Values.
Definition: Values.h:492
Key key() const noexcept
The key that was attempted to be accessed that does not exist.
Definition: Values.h:489
const Key key_
The key requested.
Definition: Values.h:473
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:482
const std::type_info & requestedTypeId() const
The requested typeid.
Definition: Values.h:495
Definition: Values.h:502
Definition: Values.h:515