KeyValueVector.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotAPI::RobotUnit
17  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18  * @date 2017
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 #pragma once
23 
25 #include <vector>
26 #include <map>
27 
28 namespace armarx
29 {
30  /**
31  * @brief This class is pretty much similar to a map.
32  *
33  * This class stores keys and values in two vectors and uses a map to map the keys to the index of the corresponding value
34  * This enables map acces and index access.
35  * The index of an inserted element never changes (this is different to a map).
36  * Index access may be required in a high frequency case (e.g. an rt control loop)
37  */
38  template < class KeyT, class ValT,
39  class KeyContT = std::vector<KeyT>,
40  class ValContT = std::vector<ValT>,
41  class IdxT = typename ValContT::size_type,
42  class MapT = std::map<KeyT, IdxT >>
44  {
45  public:
46  void add(KeyT key, ValT value);
47  IdxT index(const KeyT& k) const;
48  IdxT size() const;
49 
50  ValT& at(IdxT i);
51  ValT const& at(IdxT i) const;
52  ValT& at(const KeyT& k);
53  ValT const& at(const KeyT& k) const;
54  ValT& at(const KeyT& k, ValT& defaultVal);
55  ValT const& at(const KeyT& k, const ValT& defaultVal) const;
56 
57  ValContT const& values() const;
58  MapT const& indices() const;
59  KeyContT const& keys() const;
60  IdxT count(const KeyT& k) const;
61  bool has(const KeyT& k) const;
62  void clear();
63 
64  using iterator = typename ValContT::iterator;
65  using const_iterator = typename ValContT::const_iterator;
66 
67  iterator begin();
68  iterator end();
69 
70  const_iterator begin() const;
71  const_iterator end() const;
72 
73  const_iterator cbegin() const;
74  const_iterator cend() const;
75 
76 
77  private:
79  static std::string DescribeKey(const T* key);
80  static std::string DescribeKey(...);
81 
82  MapT indices_;
83  KeyContT keys_;
84  ValContT values_;
85  };
86 }
87 
88 // implementation
89 namespace armarx
90 {
91  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
93  {
94  if (indices_.find(key) != indices_.end())
95  {
96  throw std::invalid_argument {"Already added a value for this key" + DescribeKey(&key)};
97  }
98  indices_.emplace(key, size());
99  values_.emplace_back(std::move(value));
100  keys_.emplace_back(std::move(key));
101  }
102 
103  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
105  {
106  return indices_.at(k);
107  }
108 
109  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
111  {
112  return values_.at(i);
113  }
114 
115  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
117  {
118  return values_.at(i);
119  }
120 
121  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
123  {
124  return at(index(k));
125  }
126 
127  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
129  {
130  return at(index(k));
131  }
132 
133  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
135  {
136  return has(k) ? at(index(k)) : defaultVal;
137  }
138 
139  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
140  const ValT& KeyValueVector<KeyT, ValT, KeyContT, ValContT, IdxT, MapT>::at(const KeyT& k, const ValT& defaultVal) const
141  {
142  return has(k) ? at(index(k)) : defaultVal;
143  }
144 
145  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
147  {
148  return values_.size();
149  }
150 
151  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
153  {
154  return values_;
155  }
156 
157  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
159  {
160  return indices_;
161  }
162 
163  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
165  {
166  return keys_;
167  }
168 
169  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
171  {
172  return indices_.count(k);
173  }
174 
175  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
177  {
178  return indices_.count(k);
179  }
180 
181  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
183  {
184  indices_.clear();
185  keys_.clear();
186  values_.clear();
187  }
188 
189  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
192  {
193  return values_.begin();
194  }
195 
196  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
199  {
200  return values_.end();
201  }
202 
203  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
206  {
207  return values_.cbegin();
208  }
209 
210  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
213  {
214  return values_.cend();
215  }
216 
217  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
220  {
221  return begin();
222  }
223 
224  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
227  {
228  return end();
229  }
230 
231  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
233  {
234  return "";
235  }
236 
237  template < class KeyT, class ValT, class KeyContT, class ValContT, class IdxT, class MapT>
238  template<class T, class>
239  std::string KeyValueVector<KeyT, ValT, KeyContT, ValContT, IdxT, MapT>::DescribeKey(const T* key)
240  {
241  return ": " + to_string(*key);
242  }
243 }
armarx::KeyValueVector::keys
KeyContT const & keys() const
Definition: KeyValueVector.h:164
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::KeyValueVector::at
ValT & at(IdxT i)
Definition: KeyValueVector.h:110
armarx::KeyValueVector::begin
iterator begin()
Definition: KeyValueVector.h:191
armarx::KeyValueVector::values
ValContT const & values() const
Definition: KeyValueVector.h:152
armarx::KeyValueVector::size
IdxT size() const
Definition: KeyValueVector.h:146
armarx::KeyValueVector::has
bool has(const KeyT &k) const
Definition: KeyValueVector.h:176
TemplateMetaProgramming.h
armarx::KeyValueVector::index
IdxT index(const KeyT &k) const
Definition: KeyValueVector.h:104
armarx::KeyValueVector::cbegin
const_iterator cbegin() const
Definition: KeyValueVector.h:219
armarx::KeyValueVector< std::string, armarx::JointController * >::const_iterator
typename std::vector< armarx::JointController * > ::const_iterator const_iterator
Definition: KeyValueVector.h:65
armarx::KeyValueVector::count
IdxT count(const KeyT &k) const
Definition: KeyValueVector.h:170
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::KeyValueVector::add
void add(KeyT key, ValT value)
Definition: KeyValueVector.h:92
armarx::KeyValueVector::clear
void clear()
Definition: KeyValueVector.h:182
armarx::KeyValueVector< std::string, armarx::JointController * >::iterator
typename std::vector< armarx::JointController * > ::iterator iterator
Definition: KeyValueVector.h:64
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::KeyValueVector
This class is pretty much similar to a map.
Definition: KeyValueVector.h:43
armarx::KeyValueVector::end
iterator end()
Definition: KeyValueVector.h:198
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::KeyValueVector::cend
const_iterator cend() const
Definition: KeyValueVector.h:226
armarx::KeyValueVector::indices
MapT const & indices() const
Definition: KeyValueVector.h:158
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28