Loading...
Searching...
No Matches
Values.h
Go to the documentation of this file.
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
#pragma once
#include <gtsam/base/FastDefaultAllocator.h>
#include <gtsam/base/GenericValue.h>
#include <gtsam/base/VectorSpace.h>
#include <gtsam/inference/Key.h>
#include <boost/ptr_container/serialize_ptr_map.hpp>
#include <boost/shared_ptr.hpp>
#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V42
#include <boost/iterator/transform_iterator.hpp>
#include <boost/iterator/filter_iterator.hpp>
#endif
#include <string>
#include <utility>
namespace gtsam {
// Forward declarations / utilities
class VectorValues;
class ValueAutomaticCasting;
template<typename T> static bool _truePredicate(const T&) { return true; }
/* ************************************************************************* */
class GTSAM_EXPORT ValueCloneAllocator {
public:
static Value* allocate_clone(const Value& a) { return a.clone_(); }
static void deallocate_clone(const Value* a) { a->deallocate_(); }
ValueCloneAllocator() {}
};
class GTSAM_EXPORT Values {
private:
// Internally we store a boost ptr_map, with a ValueCloneAllocator (defined
// below) to clone and deallocate the Value objects, and our compile-flag-
// dependent FastDefaultAllocator to allocate map nodes. In this way, the
// user defines the allocation details (i.e. optimize for memory pool/arenas
// concurrency).
typedef internal::FastDefaultAllocator<typename std::pair<const Key, void*>>::type KeyValuePtrPairAllocator;
typedef boost::ptr_map<
Key,
Value,
std::less<Key>,
ValueCloneAllocator,
KeyValuePtrPairAllocator > KeyValueMap;
// The member to store the values, see just above
KeyValueMap values_;
public:
typedef boost::shared_ptr<Values> shared_ptr;
typedef boost::shared_ptr<const Values> const_shared_ptr;
struct GTSAM_EXPORT KeyValuePair {
const Key key;
Value& value;
KeyValuePair(Key _key, Value& _value) : key(_key), value(_value) {}
};
struct GTSAM_EXPORT ConstKeyValuePair {
const Key key;
const Value& value;
ConstKeyValuePair(Key _key, const Value& _value) : key(_key), value(_value) {}
ConstKeyValuePair(const KeyValuePair& kv) : key(kv.key), value(kv.value) {}
};
typedef KeyValuePair value_type;
Values() = default;
Values(const Values& other);
Values(Values&& other);
Values(std::initializer_list<ConstKeyValuePair> init);
Values(const Values& other, const VectorValues& delta);
void print(const std::string& str = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
bool equals(const Values& other, double tol=1e-9) const;
template <typename ValueType>
const ValueType at(Key j) const;
double atDouble(size_t key) const { return at<double>(key);}
const Value& at(Key j) const;
bool exists(Key j) const;
template<typename ValueType>
boost::optional<const ValueType&> exists(Key j) const;
size_t size() const { return values_.size(); }
bool empty() const { return values_.empty(); }
struct deref_iterator {
using const_iterator_type = typename KeyValueMap::const_iterator;
const_iterator_type it_;
deref_iterator(const_iterator_type it) : it_(it) {}
ConstKeyValuePair operator*() const { return {it_->first, *(it_->second)}; }
boost::shared_ptr<ConstKeyValuePair> operator->() {
return boost::make_shared<ConstKeyValuePair>(it_->first, *(it_->second));
}
bool operator==(const deref_iterator& other) const {
return it_ == other.it_;
}
bool operator!=(const deref_iterator& other) const { return it_ != other.it_; }
deref_iterator& operator++() {
++it_;
return *this;
}
};
deref_iterator begin() const { return deref_iterator(values_.begin()); }
deref_iterator end() const { return deref_iterator(values_.end()); }
deref_iterator find(Key j) const { return deref_iterator(values_.find(j)); }
deref_iterator lower_bound(Key j) const { return deref_iterator(values_.lower_bound(j)); }
deref_iterator upper_bound(Key j) const { return deref_iterator(values_.upper_bound(j)); }
Values retract(const VectorValues& delta) const;
void retractMasked(const VectorValues& delta, const KeySet& mask);
VectorValues localCoordinates(const Values& cp) const;
void insert(Key j, const Value& val);
void insert(const Values& values);
template <typename ValueType>
void insert(Key j, const ValueType& val);
void insertDouble(Key j, double c) { insert<double>(j,c); }
void update(Key j, const Value& val);
template <typename T>
void update(Key j, const T& val);
void update(const Values& values);
void insert_or_assign(Key j, const Value& val);
void insert_or_assign(const Values& values);
template <typename ValueType>
void insert_or_assign(Key j, const ValueType& val);
void erase(Key j);
KeyVector keys() const;
KeySet keySet() const;
Values& operator=(const Values& rhs);
void swap(Values& other) { values_.swap(other.values_); }
void clear() { values_.clear(); }
size_t dim() const;
std::map<Key,size_t> dims() const;
VectorValues zeroVectors() const;
// Count values of given type \c ValueType
template<class ValueType>
size_t count() const {
size_t i = 0;
for (const auto key_value : values_) {
if (dynamic_cast<const GenericValue<ValueType>*>(key_value.second))
++i;
}
return i;
}
template <class ValueType>
std::map<Key, ValueType> // , std::less<Key>, Eigen::aligned_allocator<ValueType>
extract(const std::function<bool(Key)>& filterFcn = &_truePredicate<Key>) const;
#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V42
// Types obtained by iterating
typedef KeyValueMap::const_iterator::value_type ConstKeyValuePtrPair;
typedef KeyValueMap::iterator::value_type KeyValuePtrPair;
typedef boost::transform_iterator<
std::function<KeyValuePair(const KeyValuePtrPair&)>, KeyValueMap::iterator> iterator;
typedef boost::transform_iterator<
std::function<ConstKeyValuePair(const ConstKeyValuePtrPair&)>, KeyValueMap::const_iterator> const_iterator;
typedef boost::transform_iterator<
std::function<KeyValuePair(const KeyValuePtrPair&)>, KeyValueMap::reverse_iterator> reverse_iterator;
typedef boost::transform_iterator<
std::function<ConstKeyValuePair(const ConstKeyValuePtrPair&)>, KeyValueMap::const_reverse_iterator> const_reverse_iterator;
std::pair<iterator, bool> tryInsert(Key j, const Value& value);
static ConstKeyValuePair make_const_deref_pair(const KeyValueMap::const_iterator::value_type& key_value) {
return ConstKeyValuePair(key_value.first, *key_value.second); }
static KeyValuePair make_deref_pair(const KeyValueMap::iterator::value_type& key_value) {
return KeyValuePair(key_value.first, *key_value.second); }
const_iterator _begin() const { return boost::make_transform_iterator(values_.begin(), &make_const_deref_pair); }
const_iterator _end() const { return boost::make_transform_iterator(values_.end(), &make_const_deref_pair); }
iterator begin() { return boost::make_transform_iterator(values_.begin(), &make_deref_pair); }
iterator end() { return boost::make_transform_iterator(values_.end(), &make_deref_pair); }
const_reverse_iterator rbegin() const { return boost::make_transform_iterator(values_.rbegin(), &make_const_deref_pair); }
const_reverse_iterator rend() const { return boost::make_transform_iterator(values_.rend(), &make_const_deref_pair); }
reverse_iterator rbegin() { return boost::make_transform_iterator(values_.rbegin(), &make_deref_pair); }
reverse_iterator rend() { return boost::make_transform_iterator(values_.rend(), &make_deref_pair); }
iterator find(Key j) { return boost::make_transform_iterator(values_.find(j), &make_deref_pair); }
iterator lower_bound(Key j) { return boost::make_transform_iterator(values_.lower_bound(j), &make_deref_pair); }
iterator upper_bound(Key j) { return boost::make_transform_iterator(values_.upper_bound(j), &make_deref_pair); }
template <class ValueType = Value>
class Filtered;
template <class ValueType = Value>
class ConstFiltered;
template <class ValueType>
Values(const Filtered<ValueType>& view);
template <class ValueType>
Values(const ConstFiltered<ValueType>& view);
Filtered<Value> GTSAM_DEPRECATED
filter(const std::function<bool(Key)>& filterFcn);
template <class ValueType>
Filtered<ValueType> GTSAM_DEPRECATED
filter(const std::function<bool(Key)>& filterFcn = &_truePredicate<Key>);
ConstFiltered<Value> GTSAM_DEPRECATED
filter(const std::function<bool(Key)>& filterFcn) const;
template <class ValueType>
ConstFiltered<ValueType> GTSAM_DEPRECATED filter(
const std::function<bool(Key)>& filterFcn = &_truePredicate<Key>) const;
#endif
private:
// Filters based on ValueType (if not Value) and also based on the user-
// supplied \c filter function.
template<class ValueType>
static bool filterHelper(const std::function<bool(Key)> filter, const ConstKeyValuePair& key_value) {
BOOST_STATIC_ASSERT((!boost::is_same<ValueType, Value>::value));
// Filter and check the type
return filter(key_value.key) && (dynamic_cast<const GenericValue<ValueType>*>(&key_value.value));
}
friend class boost::serialization::access;
template<class ARCHIVE>
void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
ar & BOOST_SERIALIZATION_NVP(values_);
}
};
/* ************************************************************************* */
class ValuesKeyAlreadyExists : public std::exception {
protected:
const Key key_;
private:
mutable std::string message_;
public:
ValuesKeyAlreadyExists(Key key) noexcept :
key_(key) {}
~ValuesKeyAlreadyExists() noexcept override {}
Key key() const noexcept { return key_; }
GTSAM_EXPORT const char* what() const noexcept override;
};
/* ************************************************************************* */
class ValuesKeyDoesNotExist : public std::exception {
protected:
const char* operation_;
const Key key_;
private:
mutable std::string message_;
public:
ValuesKeyDoesNotExist(const char* operation, Key key) noexcept :
operation_(operation), key_(key) {}
~ValuesKeyDoesNotExist() noexcept override {}
Key key() const noexcept { return key_; }
GTSAM_EXPORT const char* what() const noexcept override;
};
/* ************************************************************************* */
class ValuesIncorrectType : public std::exception {
protected:
const Key key_;
const std::type_info& storedTypeId_;
const std::type_info& requestedTypeId_;
private:
mutable std::string message_;
public:
ValuesIncorrectType(Key key,
const std::type_info& storedTypeId, const std::type_info& requestedTypeId) noexcept :
key_(key), storedTypeId_(storedTypeId), requestedTypeId_(requestedTypeId) {}
~ValuesIncorrectType() noexcept override {}
Key key() const noexcept { return key_; }
const std::type_info& storedTypeId() const { return storedTypeId_; }
const std::type_info& requestedTypeId() const { return requestedTypeId_; }
GTSAM_EXPORT const char* what() const noexcept override;
};
/* ************************************************************************* */
class DynamicValuesMismatched : public std::exception {
public:
DynamicValuesMismatched() noexcept {}
~DynamicValuesMismatched() noexcept override {}
const char* what() const noexcept override {
return "The Values 'this' and the argument passed to Values::localCoordinates have mismatched keys and values";
}
};
/* ************************************************************************* */
class NoMatchFoundForFixed: public std::exception {
protected:
const size_t M1_, N1_;
const size_t M2_, N2_;
private:
mutable std::string message_;
public:
NoMatchFoundForFixed(size_t M1, size_t N1, size_t M2, size_t N2) noexcept :
M1_(M1), N1_(N1), M2_(M2), N2_(N2) {
}
~NoMatchFoundForFixed() noexcept override {
}
GTSAM_EXPORT const char* what() const noexcept override;
};
/* ************************************************************************* */
template<>
struct traits<Values> : public Testable<Values> {
};
} //\ namespace gtsam
#include <gtsam/nonlinear/Values-inl.h>
FastDefaultAllocator.h
An easy way to control which allocator is used for Fast* collections.
Key.h
gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:86
gtsam::KeyFormatter
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
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:100
gtsam::traits
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition concepts.h:30
gtsam::internal::FastDefaultAllocator
Default allocator for list, map, and set types.
Definition FastDefaultAllocator.h:50
gtsam::GenericValue
Wraps any type T so it can play as a Value.
Definition GenericValue.h:47
gtsam::Testable
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:151
gtsam::Value
This is the base class for any type to be stored in Values.
Definition Value.h:37
gtsam::Value::deallocate_
virtual void deallocate_() const =0
Deallocate a raw pointer of this value.
gtsam::Value::clone_
virtual Value * clone_() const =0
Clone this value in a special memory pool, must be deleted with Value::deallocate_,...
gtsam::VectorValues
VectorValues represents a collection of vector-valued variables associated each with a unique integer...
Definition VectorValues.h:74
gtsam::ValueCloneAllocator
Definition Values.h:49
gtsam::Values
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65
gtsam::Values::update
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...
gtsam::Values::empty
bool empty() const
whether the config is empty
Definition Values.h:184
gtsam::Values::update
void update(Key j, const Value &val)
single element change of existing element
Definition Values.cpp:180
gtsam::Values::equals
bool equals(const Values &other, double tol=1e-9) const
Test whether the sets of keys and values are identical.
Definition Values.cpp:78
gtsam::Values::upper_bound
deref_iterator upper_bound(Key j) const
Find the lowest-ordered element greater than the specified key.
Definition Values.h:219
gtsam::Values::shared_ptr
boost::shared_ptr< Values > shared_ptr
A shared_ptr to this class.
Definition Values.h:87
gtsam::Values::operator=
Values & operator=(const Values &rhs)
Replace all keys and variables.
Definition Values.cpp:247
gtsam::Values::at
const ValueType at(Key j) const
Retrieve a variable by key j.
Definition Values-inl.h:361
gtsam::Values::clear
void clear()
Remove all variables from the config.
Definition Values.h:301
gtsam::Values::swap
void swap(Values &other)
Swap the contents of two Values without copying data.
Definition Values.h:298
gtsam::Values::insertDouble
void insertDouble(Key j, double c)
version for double
Definition Values.h:252
gtsam::Values::insert
void insert(Key j, const Value &val)
Add a variable with the given j, throws KeyAlreadyExists<J> if j is already present.
Definition Values.cpp:157
gtsam::Values::size
size_t size() const
The number of variables in this config.
Definition Values.h:181
gtsam::Values::find
deref_iterator find(Key j) const
Find an element by key, returning an iterator, or end() if the key was not found.
Definition Values.h:213
gtsam::Values::keys
KeyVector keys() const
Returns a vector of keys in the config.
Definition Values.cpp:230
gtsam::Values::const_shared_ptr
boost::shared_ptr< const Values > const_shared_ptr
A const shared_ptr to this class.
Definition Values.h:90
gtsam::Values::keySet
KeySet keySet() const
Returns a set of keys in the config.
Definition Values.cpp:239
gtsam::Values::insert_or_assign
void insert_or_assign(Key j, const Value &val)
If key j exists, update value, else perform an insert.
Definition Values.cpp:203
gtsam::Values::print
void print(const std::string &str="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print method for testing and debugging
Definition Values.cpp:67
gtsam::Values::Values
Values()=default
Default constructor creates an empty Values class.
gtsam::Values::erase
void erase(Key j)
Remove a variable from the config, throws KeyDoesNotExist<J> if j is not present.
Definition Values.cpp:222
gtsam::Values::lower_bound
deref_iterator lower_bound(Key j) const
Find the element greater than or equal to the specified key.
Definition Values.h:216
gtsam::Values::atDouble
double atDouble(size_t key) const
version for double
Definition Values.h:159
gtsam::Values::KeyValuePair
A key-value pair, which you get by dereferencing iterators.
Definition Values.h:93
gtsam::Values::KeyValuePair::value
Value & value
The value.
Definition Values.h:95
gtsam::Values::KeyValuePair::key
const Key key
The key.
Definition Values.h:94
gtsam::Values::ConstKeyValuePair
A key-value pair, which you get by dereferencing iterators.
Definition Values.h:101
gtsam::Values::ConstKeyValuePair::key
const Key key
The key.
Definition Values.h:102
gtsam::Values::ConstKeyValuePair::value
const Value & value
The value.
Definition Values.h:103
gtsam::Values::deref_iterator
Definition Values.h:190
gtsam::ValuesKeyAlreadyExists
Definition Values.h:453
gtsam::ValuesKeyAlreadyExists::key_
const Key key_
The key that already existed.
Definition Values.h:455
gtsam::ValuesKeyAlreadyExists::what
GTSAM_EXPORT const char * what() const noexcept override
The message to be displayed to the user.
Definition Values.cpp:280
gtsam::ValuesKeyAlreadyExists::ValuesKeyAlreadyExists
ValuesKeyAlreadyExists(Key key) noexcept
Construct with the key-value pair attempted to be added.
Definition Values.h:462
gtsam::ValuesKeyAlreadyExists::key
Key key() const noexcept
The duplicate key that was attempted to be added.
Definition Values.h:468
gtsam::ValuesKeyDoesNotExist
Definition Values.h:475
gtsam::ValuesKeyDoesNotExist::ValuesKeyDoesNotExist
ValuesKeyDoesNotExist(const char *operation, Key key) noexcept
Construct with the key that does not exist in the values.
Definition Values.h:485
gtsam::ValuesKeyDoesNotExist::key_
const Key key_
The key that does not exist.
Definition Values.h:478
gtsam::ValuesKeyDoesNotExist::key
Key key() const noexcept
The key that was attempted to be accessed that does not exist.
Definition Values.h:491
gtsam::ValuesKeyDoesNotExist::operation_
const char * operation_
The operation that attempted to access the key.
Definition Values.h:477
gtsam::ValuesIncorrectType
Definition Values.h:498
gtsam::ValuesIncorrectType::storedTypeId
const std::type_info & storedTypeId() const
The typeid of the value stores in the Values.
Definition Values.h:519
gtsam::ValuesIncorrectType::key
Key key() const noexcept
The key that was attempted to be accessed that does not exist.
Definition Values.h:516
gtsam::ValuesIncorrectType::key_
const Key key_
The key requested.
Definition Values.h:500
gtsam::ValuesIncorrectType::ValuesIncorrectType
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:509
gtsam::ValuesIncorrectType::requestedTypeId
const std::type_info & requestedTypeId() const
The requested typeid.
Definition Values.h:522