MVault 0.0.1
Loading...
Searching...
No Matches
Map.h
1#pragma once
2
3#include <map>
4#include <unordered_map>
5
6namespace mvlt
7{
16 template <class MultiContainerIterator, class ContainerIterator, class KeyType, class ValueType>
18 {
19 private:
20 // Is this iterator for multi container
21 bool IsMultiMap;
22
23 // Multi container iterator
24 MultiContainerIterator DataMultiMapIt;
25
26 // Container iterator
27 ContainerIterator DataMapIt;
28 public:
29
32
40 AllMapIterator(MultiContainerIterator dataMultiMapIt,
41 ContainerIterator dataMapIt, bool isMultiMap)
42 {
43 IsMultiMap = isMultiMap;
44 DataMultiMapIt = dataMultiMapIt;
45 DataMapIt = dataMapIt;
46 };
47
54 {
55 IsMultiMap = other.IsMultiMap;
56 DataMultiMapIt = other.DataMultiMapIt;
57 DataMapIt = other.DataMapIt;
58 }
59
68 {
69 if (&other != this)
70 {
71 IsMultiMap = other.IsMultiMap;
72 DataMultiMapIt = other.DataMultiMapIt;
73 DataMapIt = other.DataMapIt;
74 }
75
76 return *this;
77 }
78
86 bool operator!=(AllMapIterator const& other) const
87 {
88 if (IsMultiMap)
89 return DataMultiMapIt != other.DataMultiMapIt;
90 else
91 return DataMapIt != other.DataMapIt;
92 }
93
101 bool operator==(AllMapIterator const& other) const
102 {
103 if (IsMultiMap)
104 return DataMultiMapIt == other.DataMultiMapIt;
105 else
106 return DataMapIt == other.DataMapIt;
107 }
108
115 {
116 if (IsMultiMap)
117 ++DataMultiMapIt;
118 else
119 ++DataMapIt;
120 return *this;
121 }
122
129 {
130 if (IsMultiMap)
131 --DataMultiMapIt;
132 else
133 --DataMapIt;
134 return *this;
135 }
136
142 std::pair<KeyType, ValueType> operator*() const
143 {
144 if (IsMultiMap)
145 return *DataMultiMapIt;
146 else
147 return *DataMapIt;
148 }
149
155 ContainerIterator GetContainerIterator() const
156 {
157 return DataMapIt;
158 }
159
165 MultiContainerIterator GetMultiContainerIterator() const
166 {
167 return DataMultiMapIt;
168 }
169 };
170
174 template <class KeyType, class ValueType>
175 using UnorderedMapIterator = AllMapIterator<typename std::unordered_multimap<KeyType, ValueType>::iterator,
176 typename std::unordered_map<KeyType, ValueType>::iterator, KeyType, ValueType>;
177
181 template <class KeyType, class ValueType>
182 using MapIterator = AllMapIterator<typename std::multimap<KeyType, ValueType>::iterator,
183 typename std::map<KeyType, ValueType>::iterator, KeyType, ValueType>;
184
188 template <class KeyType, class ValueType>
189 using ReverseMapIterator = AllMapIterator<typename std::multimap<KeyType, ValueType>::reverse_iterator,
190 typename std::map<KeyType, ValueType>::reverse_iterator, KeyType, ValueType>;
191
192
205 template <class MultiContainer, class Container, class Iterator, class KeyType, class ValueType>
207 {
208 protected:
210 bool IsMultiMap = false;
211
212 // Multi container
214
215 // Container
216 Container DataMap;
217 public:
218
230
239 {
241 }
242
252
259 {
261 }
262
275 template <class EmplaceKeyType, class EmplaceValueType>
276 std::pair<Iterator, bool> Emplace(EmplaceKeyType&& key, EmplaceValueType&& value)
277 {
278 if (IsMultiMap)
279 {
280 auto dataMultiMapIt = DataMultiMap.emplace(std::forward<EmplaceKeyType>(key), std::forward<EmplaceValueType>(value));
281 return std::pair<Iterator, bool>(Iterator(dataMultiMapIt, DataMap.end(), true), true);
282 }
283 else
284 {
285 auto dataMapIt = DataMap.emplace(std::forward<EmplaceKeyType>(key), std::forward<EmplaceValueType>(value));
286 return std::pair<Iterator, bool>(Iterator(DataMultiMap.end(), dataMapIt.first, false), dataMapIt.second);
287 }
288 }
289
297 std::pair<Iterator, Iterator> EqualRange(const KeyType& data)
298 {
299 if (IsMultiMap)
300 {
301 auto rangePair = DataMultiMap.equal_range(data);
302 return std::pair<Iterator, Iterator>(Iterator(rangePair.first, DataMap.end(), true), Iterator(rangePair.second, DataMap.end(), true));
303 }
304 else
305 {
306 auto rangePair = DataMap.equal_range(data);
307 return std::pair<Iterator, Iterator>(Iterator(DataMultiMap.end(), rangePair.first, false), Iterator(DataMultiMap.end(), rangePair.second, false));
308 }
309 }
310
319 {
320 if (IsMultiMap)
321 {
322 auto retIt = DataMultiMap.find(data);
323 return Iterator(retIt, DataMap.end(), true);
324 }
325 else
326 {
327 auto retIt = DataMap.find(data);
328 return Iterator(DataMultiMap.end(), retIt, false);
329 }
330 }
331
339 std::size_t Erase(const KeyType& data)
340 {
341 std::size_t res;
342 if (IsMultiMap)
343 res = DataMultiMap.erase(data);
344 else
345 res = DataMap.erase(data);
346
347 return res;
348 }
349
358 {
359 if (IsMultiMap)
360 {
362 return Iterator(retIt, DataMap.end(), true);
363 }
364 else
365 {
367 return Iterator(DataMultiMap.end(), retIt, false);
368 }
369 }
370
372 void Clear()
373 {
374 if (IsMultiMap)
375 DataMultiMap.clear();
376 else
377 DataMap.clear();
378 }
379
385 std::size_t Size()
386 {
387 if (IsMultiMap)
388 return DataMultiMap.size();
389 else
390 return DataMap.size();
391 }
392 };
393
397 template <class KeyType, class ValueType>
398 class Map : public ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>,
399 MapIterator<KeyType, ValueType>, KeyType, ValueType>
400 {
401 public:
411
421 {
422 return MapIterator<KeyType, ValueType>(ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMultiMap.lower_bound(data),
423 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMap.lower_bound(data),
424 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::IsMultiMap);
425 }
426
436 {
437 return MapIterator<KeyType, ValueType>(ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMultiMap.upper_bound(data),
438 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMap.upper_bound(data),
439 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::IsMultiMap);
440 }
441
448 {
449 return ReverseMapIterator<KeyType, ValueType>(ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMultiMap.rbegin(),
450 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMap.rbegin(),
451 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::IsMultiMap);
452 }
453
460 {
461 return ReverseMapIterator<KeyType, ValueType>(ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMultiMap.rend(),
462 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMap.rend(),
463 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::IsMultiMap);
464 }
465 };
466
470 template <class KeyType, class ValueType>
471 class UnorderedMap : public ParentMap<std::unordered_multimap<KeyType, ValueType>, std::unordered_map<KeyType, ValueType>,
472 UnorderedMapIterator<KeyType, ValueType>, KeyType, ValueType>
473 {
474 public:
484 };
485}
Iterator class for all library maps.
Definition Map.h:18
AllMapIterator & operator++()
Pre increment operator.
Definition Map.h:114
AllMapIterator()
Default constructor.
Definition Map.h:31
bool operator==(AllMapIterator const &other) const
Compare operator.
Definition Map.h:101
AllMapIterator(MultiContainerIterator dataMultiMapIt, ContainerIterator dataMapIt, bool isMultiMap)
Constructor.
Definition Map.h:40
ContainerIterator GetContainerIterator() const
Method for get container iterator.
Definition Map.h:155
AllMapIterator & operator=(const AllMapIterator &other)
Assignment operator.
Definition Map.h:67
bool operator!=(AllMapIterator const &other) const
Not equal operator.
Definition Map.h:86
MultiContainerIterator GetMultiContainerIterator() const
Method for get multi container iterator.
Definition Map.h:165
std::pair< KeyType, ValueType > operator*() const
Operator*.
Definition Map.h:142
AllMapIterator & operator--()
Pre decrement operator.
Definition Map.h:128
AllMapIterator(const AllMapIterator &other)
Copy constructor.
Definition Map.h:53
iterator begin() noexcept
Begin provides access to the Data iterator.
Definition DataContainer.h:42
iterator end() noexcept
End provides access to the Data iterator.
Definition DataContainer.h:47
Specialization of class DataContainer, used as a type std::map<std::string, DataSaver>
Definition DataContainer.h:309
Specialization of class DataContainer, used as a type std::multimap<std::string, DataSaver>
Definition DataContainer.h:313
The descendant of the ParentMap class, which stores the map, i.e. the binary tree.
Definition Map.h:400
ReverseMapIterator< KeyType, ValueType > Rbegin()
Reverse begin container method.
Definition Map.h:447
Map(bool isMultiMap)
Class constructor.
Definition Map.h:407
MapIterator< KeyType, ValueType > LowerBound(const KeyType &data)
Returns an iterator pointing to the first element that is not less than (i.e. greater or equal to) da...
Definition Map.h:420
ReverseMapIterator< KeyType, ValueType > Rend()
Reverse end container method.
Definition Map.h:459
MapIterator< KeyType, ValueType > UpperBound(const KeyType &data)
Returns an iterator pointing to the first element that is greater than key.
Definition Map.h:435
Parent wrapper class for stl containers.
Definition Map.h:207
Iterator End()
End container method.
Definition Map.h:258
Iterator begin()
Begin container method.
Definition Map.h:226
std::size_t Size()
The method for get Map size.
Definition Map.h:385
Iterator Find(const KeyType &data)
The method for getting iterator with key.
Definition Map.h:318
Iterator Begin()
Begin container method.
Definition Map.h:248
std::size_t Erase(const KeyType &data)
The method for erase data from Map with key.
Definition Map.h:339
Iterator Erase(const Iterator &dataIt)
The method for erase data from Map with iterator.
Definition Map.h:357
std::pair< Iterator, Iterator > EqualRange(const KeyType &data)
The method for getting range of data.
Definition Map.h:297
bool IsMultiMap
Is this multi map.
Definition Map.h:210
std::pair< Iterator, bool > Emplace(EmplaceKeyType &&key, EmplaceValueType &&value)
The method for inserting data into the Map.
Definition Map.h:276
void Clear()
The method for clear Map.
Definition Map.h:372
Iterator end()
End container method.
Definition Map.h:238
The descendant of the ParentMap class, which stores the unordered_map, i.e. the hash table.
Definition Map.h:473
UnorderedMap(bool isMultiMap)
Class constructor.
Definition Map.h:480