gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Values-inl.h
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 <boost/foreach.hpp>
30 
31 #include <gtsam/base/DerivedValue.h>
32 #include <gtsam/nonlinear/Values.h> // Only so Eclipse finds class definition
33 
34 namespace gtsam {
35 
36  /* ************************************************************************* */
37  template<class ValueType>
39  const Key key;
40  ValueType& value;
41 
42  _ValuesKeyValuePair(Key _key, ValueType& _value) : key(_key), value(_value) {}
43  };
44 
45  /* ************************************************************************* */
46  template<class ValueType>
48  const Key key;
49  const ValueType& value;
50 
51  _ValuesConstKeyValuePair(Key _key, const ValueType& _value) : key(_key), value(_value) {}
53  };
54 
55  /* ************************************************************************* */
56  template<class ValueType>
58  public:
62  typedef KeyValuePair value_type;
63 
64  typedef
65  boost::transform_iterator<
67  boost::filter_iterator<
68  boost::function<bool(const Values::ConstKeyValuePair&)>,
70  iterator;
71 
72  typedef iterator const_iterator;
73 
74  typedef
75  boost::transform_iterator<
77  boost::filter_iterator<
78  boost::function<bool(const Values::ConstKeyValuePair&)>,
80  const_const_iterator;
81 
82  iterator begin() { return begin_; }
83  iterator end() { return end_; }
84  const_iterator begin() const { return begin_; }
85  const_iterator end() const { return end_; }
86  const_const_iterator beginConst() const { return constBegin_; }
87  const_const_iterator endConst() const { return constEnd_; }
88 
90  size_t size() const {
91  size_t i = 0;
92  for (const_const_iterator it = beginConst(); it != endConst(); ++it)
93  ++i;
94  return i;
95  }
96 
97  private:
98  Filtered(const boost::function<bool(const Values::ConstKeyValuePair&)>& filter, Values& values) :
99  begin_(boost::make_transform_iterator(
100  boost::make_filter_iterator(
101  filter, values.begin(), values.end()),
102  &castHelper<ValueType, KeyValuePair, Values::KeyValuePair>)),
103  end_(boost::make_transform_iterator(
104  boost::make_filter_iterator(
105  filter, values.end(), values.end()),
106  &castHelper<ValueType, KeyValuePair, Values::KeyValuePair>)),
107  constBegin_(boost::make_transform_iterator(
108  boost::make_filter_iterator(
109  filter, ((const Values&)values).begin(), ((const Values&)values).end()),
110  &castHelper<const ValueType, ConstKeyValuePair, Values::ConstKeyValuePair>)),
111  constEnd_(boost::make_transform_iterator(
112  boost::make_filter_iterator(
113  filter, ((const Values&)values).end(), ((const Values&)values).end()),
114  &castHelper<const ValueType, ConstKeyValuePair, Values::ConstKeyValuePair>)) {}
115 
116  friend class Values;
117  iterator begin_;
118  iterator end_;
119  const_const_iterator constBegin_;
120  const_const_iterator constEnd_;
121  };
122 
123  /* ************************************************************************* */
124  template<class ValueType>
126  public:
129  typedef KeyValuePair value_type;
130 
131  typedef typename Filtered<ValueType>::const_const_iterator iterator;
132  typedef typename Filtered<ValueType>::const_const_iterator const_iterator;
133 
136  begin_(rhs.beginConst()),
137  end_(rhs.endConst()) {}
138 
139  iterator begin() { return begin_; }
140  iterator end() { return end_; }
141  const_iterator begin() const { return begin_; }
142  const_iterator end() const { return end_; }
143 
145  size_t size() const {
146  size_t i = 0;
147  for (const_iterator it = begin(); it != end(); ++it)
148  ++i;
149  return i;
150  }
151 
152  FastList<Key> keys() const {
153  FastList<Key> result;
154  for(const_iterator it = begin(); it != end(); ++it)
155  result.push_back(it->key);
156  return result;
157  }
158 
159  private:
160  friend class Values;
161  const_iterator begin_;
162  const_iterator end_;
163  ConstFiltered(const boost::function<bool(const Values::ConstKeyValuePair&)>& filter, const Values& values) {
164  // We remove the const from values to create a non-const Filtered
165  // view, then pull the const_iterators out of it.
166  const Filtered<ValueType> filtered(filter, const_cast<Values&>(values));
167  begin_ = filtered.beginConst();
168  end_ = filtered.endConst();
169  }
170  };
171 
172  /* ************************************************************************* */
174  template<class ValueType>
176  BOOST_FOREACH(const typename Filtered<ValueType>::KeyValuePair& key_value, view) {
177  Key key = key_value.key;
178  insert(key, key_value.value);
179  }
180  }
181 
182  /* ************************************************************************* */
183  template<class ValueType>
185  BOOST_FOREACH(const typename ConstFiltered<ValueType>::KeyValuePair& key_value, view) {
186  Key key = key_value.key;
187  insert(key, key_value.value);
188  }
189  }
190 
191  /* ************************************************************************* */
193  inline Values::filter(const boost::function<bool(Key)>& filterFcn) {
194  return filter<Value>(filterFcn);
195  }
196 
197  /* ************************************************************************* */
198  template<class ValueType>
200  Values::filter(const boost::function<bool(Key)>& filterFcn) {
201  return Filtered<ValueType>(boost::bind(&filterHelper<ValueType>, filterFcn, _1), *this);
202  }
203 
204  /* ************************************************************************* */
206  inline Values::filter(const boost::function<bool(Key)>& filterFcn) const {
207  return filter<Value>(filterFcn);
208  }
209 
210  /* ************************************************************************* */
211  template<class ValueType>
213  Values::filter(const boost::function<bool(Key)>& filterFcn) const {
214  return ConstFiltered<ValueType>(boost::bind(&filterHelper<ValueType>, filterFcn, _1), *this);
215  }
216 
217  /* ************************************************************************* */
218  template<typename ValueType>
219  const ValueType& Values::at(Key j) const {
220  // Find the item
221  KeyValueMap::const_iterator item = values_.find(j);
222 
223  // Throw exception if it does not exist
224  if(item == values_.end())
225  throw ValuesKeyDoesNotExist("retrieve", j);
226 
227  // Check the type and throw exception if incorrect
228  if(typeid(*item->second) != typeid(ValueType))
229  throw ValuesIncorrectType(j, typeid(*item->second), typeid(ValueType));
230 
231  // We have already checked the type, so do a "blind" static_cast, not dynamic_cast
232  return static_cast<const ValueType&>(*item->second);
233  }
234 
235  /* ************************************************************************* */
236  template<typename ValueType>
237  boost::optional<const ValueType&> Values::exists(Key j) const {
238  // Find the item
239  KeyValueMap::const_iterator item = values_.find(j);
240 
241  if(item != values_.end()) {
242  // Check the type and throw exception if incorrect
243  if(typeid(*item->second) != typeid(ValueType))
244  throw ValuesIncorrectType(j, typeid(*item->second), typeid(ValueType));
245 
246  // We have already checked the type, so do a "blind" static_cast, not dynamic_cast
247  return static_cast<const ValueType&>(*item->second);
248  } else {
249  return boost::none;
250  }
251  }
252 
253 }
A filtered view of a const Values, returned from Values::filter.
Definition: Values-inl.h:125
Definition: Values-inl.h:47
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:127
Definition: Values.h:421
Definition: Values.h:444
A non-templated config holding any types of Manifold-group elements.
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:75
const ValueType & value
The value.
Definition: Values-inl.h:49
const ValueType & at(Key j) const
Retrieve a variable by key j.
Definition: Values-inl.h:219
size_t Key
Integer nonlinear key type.
Definition: types.h:59
ValueType & value
The value.
Definition: Values-inl.h:40
_ValuesKeyValuePair< ValueType > KeyValuePair
A key-value pair, with the value a specific derived Value type.
Definition: Values-inl.h:60
size_t size() const
Returns the number of values in this view.
Definition: Values-inl.h:145
A filtered view of a Values, returned from Values::filter.
Definition: Values-inl.h:57
const Key key
The key.
Definition: Values-inl.h:39
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:114
const Key key
The key.
Definition: Values-inl.h:48
_ValuesConstKeyValuePair< ValueType > KeyValuePair
A const key-value pair, with the value a specific derived Value type.
Definition: Values-inl.h:128
bool exists(Key j) const
Check if a value exists with key j.
Definition: Values.cpp:75
Values()
Default constructor creates an empty Values class.
Definition: Values.h:149
Definition: FastList.h:38
size_t size() const
Returns the number of values in this view.
Definition: Values-inl.h:90
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:106
Filtered< Value > filter(const boost::function< bool(Key)> &filterFcn)
Return a filtered view of this Values class, without copying any data.
Definition: Values-inl.h:193
boost::transform_iterator< boost::function1< ConstKeyValuePair, const ConstKeyValuePtrPair & >, KeyValueMap::const_iterator > const_iterator
Const forward iterator, with value type ConstKeyValuePair.
Definition: Values.h:128
Definition: Values-inl.h:38
boost::transform_iterator< boost::function1< KeyValuePair, const KeyValuePtrPair & >, KeyValueMap::iterator > iterator
Mutable forward iterator, with value type KeyValuePair.
Definition: Values.h:124
ConstFiltered(const Filtered< ValueType > &rhs)
Conversion from Filtered to ConstFiltered.
Definition: Values-inl.h:135