commonlib
vector_with_keys.h
Go to the documentation of this file.
1 
17 #pragma once
18 
19 #include <vector>
20 #include <map>
21 
23 
24 BEGIN_NAMESPACE_CONTAINERS
25 
26 template <typename KeyType, typename ValueType>
28 {
29 public:
30  using iterator =
32  using const_iterator =
34 
35 public:
40 
45 
50 
55 
60 
65  const KeyType& getKey(std::size_t pos) const;
66 
72  ValueType& operator[](std::size_t pos);
73 
79  const ValueType& operator[](std::size_t pos) const;
80 
85  ValueType& back();
86 
91  const ValueType& back() const;
92 
96  void clear();
97 
101  std::size_t size() const;
102 
106  bool empty() const;
107 
116  bool emplaceBackWithKey(const KeyType& key);
117 
123  iterator erase(iterator position);
124 
130  iterator erase(std::size_t pos);
131 
137  void swap(int32_t posA, int32_t posB);
138 
147  iterator findByKey(const KeyType& key);
148 
157  const_iterator findByKey(const KeyType& key) const;
158 
163  {
164  using namespace VectorWithKeysIterators;
166  }
167 
172  {
173  using namespace VectorWithKeysIterators;
175  }
176 
181  {
182  using namespace VectorWithKeysIterators;
184  }
185 
190  {
191  using namespace VectorWithKeysIterators;
193  }
194 
199  {
200  return begin();
201  }
202 
207  {
208  return end();
209  }
210 
211 private:
212  std::vector<std::pair<KeyType, ValueType>> pairs;
213  std::map<KeyType, typename std::vector<std::pair<KeyType, ValueType>>::iterator> pairsMap;
214 
215  void rebuildPairMap();
216 };
217 
218 template <typename KeyType, typename ValueType>
220 {
221 }
222 
223 template <typename KeyType, typename ValueType>
225 {
226  pairs = other.pairs;
227  rebuildPairMap();
228 }
229 
230 template <typename KeyType, typename ValueType>
232 {
233  if (this != &other)
234  {
235  pairs = other.pairs;
236  rebuildPairMap();
237  }
238  return *this;
239 }
240 
241 template <typename KeyType, typename ValueType>
243 {
244  pairs = std::move(other.pairs);
245  rebuildPairMap();
246 
247  // Clear old map
248  other.pairsMap.clear();
249 }
250 
251 template <typename KeyType, typename ValueType>
253 {
254  if (this != &other)
255  {
256  pairs = std::move(other.pairs);
257  rebuildPairMap();
258 
259  // Clear old map
260  other.pairsMap.clear();
261  }
262  return *this;
263 }
264 
265 template <typename KeyType, typename ValueType>
266 const KeyType& VectorWithKeys<KeyType, ValueType>::getKey(std::size_t pos) const
267 {
268  return pairs[pos].first;
269 }
270 
271 template <typename KeyType, typename ValueType>
273 {
274  return pairs[pos].second;
275 }
276 
277 template <typename KeyType, typename ValueType>
278 const ValueType& VectorWithKeys<KeyType, ValueType>::operator[](std::size_t pos) const
279 {
280  return pairs[pos].second;
281 }
282 
283 template <typename KeyType, typename ValueType>
285 {
286  return pairs.back().second;
287 }
288 
289 template <typename KeyType, typename ValueType>
291 {
292  return pairs.back().second;
293 }
294 
295 template <typename KeyType, typename ValueType>
297 {
298  pairs.clear();
299  pairsMap.clear();
300 }
301 
302 template <typename KeyType, typename ValueType>
304 {
305  return pairs.size();
306 }
307 
308 template <typename KeyType, typename ValueType>
310 {
311  return pairs.empty();
312 }
313 
314 template <typename KeyType, typename ValueType>
316 {
317  if (pairsMap.find(key) == pairsMap.end())
318  {
319  pairs.emplace_back();
320  (pairs.back()).first = key;
321  rebuildPairMap();
322  return true;
323  }
324  return false;
325 }
326 
327 template <typename KeyType, typename ValueType>
329 {
330  auto it = pairs.erase(position.pointee);
331  rebuildPairMap();
332  return iterator(it);
333 }
334 
335 template <typename KeyType, typename ValueType>
337 {
338  auto it = pairs.erase(pairs.begin() + pos);
339  rebuildPairMap();
340  return iterator(it);
341 }
342 
343 template <typename KeyType, typename ValueType>
344 void VectorWithKeys<KeyType, ValueType>::swap(int32_t posA, int32_t posB)
345 {
346  if (posA != posB)
347  {
348  std::swap(pairs[posA], pairs[posB]);
349  rebuildPairMap();
350  }
351 }
352 
353 template <typename KeyType, typename ValueType>
355 {
356  auto pairsMapIter = pairsMap.find(key);
357 
358  if (pairsMapIter != pairsMap.end())
359  {
360  return iterator(pairsMapIter->second);
361  }
362  return end();
363 }
364 
365 template <typename KeyType, typename ValueType>
367 {
368  auto pairsMapIter = pairsMap.find(key);
369 
370  if (pairsMapIter != pairsMap.end())
371  {
372  return iterator(pairsMapIter->second);
373  }
374  return end();
375 }
376 
377 template <typename KeyType, typename ValueType>
379 {
380  pairsMap.clear();
381  for (auto pairIter = pairs.begin(); pairIter != pairs.end(); ++pairIter)
382  {
383  pairsMap.insert(std::make_pair(pairIter->first, pairIter));
384  }
385 }
386 
387 END_NAMESPACE_CONTAINERS
VectorWithKeysIterator provides access to VectorWithKeys elements and it&#39;s keys.
const_iterator end() const
Definition: vector_with_keys.h:189
Definition: vector_with_keys.h:27
iterator erase(iterator position)
Erases an element at an iterator position.
Definition: vector_with_keys.h:328
ValueType & operator[](std::size_t pos)
Mutable access element at position.
Definition: vector_with_keys.h:272
VectorWithKeys< KeyType, ValueType > & operator=(const VectorWithKeys< KeyType, ValueType > &other)
Copy assignment.
Definition: vector_with_keys.h:231
void swap(int32_t posA, int32_t posB)
Swaps elements in container at indexed positions passed.
Definition: vector_with_keys.h:344
Definition: vector_with_keys_iterator.h:27
bool emplaceBackWithKey(const KeyType &key)
Constructs an additional element at the at the back of the container by calling a default constructor...
Definition: vector_with_keys.h:315
const_iterator cend() const
Definition: vector_with_keys.h:206
VectorWithKeys()
Default constructor.
Definition: vector_with_keys.h:219
void clear()
Deletes all objects in the container and sets the size to zero.
Definition: vector_with_keys.h:296
const KeyType & getKey(std::size_t pos) const
Definition: vector_with_keys.h:266
const_iterator begin() const
Definition: vector_with_keys.h:180
std::size_t size() const
Definition: vector_with_keys.h:303
const_iterator cbegin() const
Definition: vector_with_keys.h:198
Definition: vector_with_keys_iterator.h:57
iterator findByKey(const KeyType &key)
Searches for an element with a key value passed.
Definition: vector_with_keys.h:354
ValueType & back()
Mutable access to the last element in the container.
Definition: vector_with_keys.h:284
iterator begin()
Definition: vector_with_keys.h:162
iterator end()
Definition: vector_with_keys.h:171
bool empty() const
Definition: vector_with_keys.h:309