MVault 1.0.0
Simple c++ database
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
31 AllMapIterator() noexcept = default;
32
38 AllMapIterator(const AllMapIterator& other) noexcept
39 {
40 IsMultiMap = other.IsMultiMap;
41 DataMultiMapIt = other.DataMultiMapIt;
42 DataMapIt = other.DataMapIt;
43 }
44
51 {
52 IsMultiMap = other.IsMultiMap;
53 DataMultiMapIt = other.DataMultiMapIt;
54 DataMapIt = other.DataMapIt;
55 }
56
64 AllMapIterator(const MultiContainerIterator& dataMultiMapIt,
65 const ContainerIterator& dataMapIt, const bool& isMultiMap) noexcept
66 {
67 IsMultiMap = isMultiMap;
68 DataMultiMapIt = dataMultiMapIt;
69 DataMapIt = dataMapIt;
70 };
71
79 AllMapIterator& operator=(const AllMapIterator& other) noexcept
80 {
81 if (&other != this)
82 {
83 IsMultiMap = other.IsMultiMap;
84 DataMultiMapIt = other.DataMultiMapIt;
85 DataMapIt = other.DataMapIt;
86 }
87
88 return *this;
89 }
90
99 {
100 if (&other != this)
101 {
102 IsMultiMap = other.IsMultiMap;
103 DataMultiMapIt = other.DataMultiMapIt;
104 DataMapIt = other.DataMapIt;
105 }
106
107 return *this;
108 }
109
117 bool operator!=(const AllMapIterator& other) const noexcept
118 {
119 if (IsMultiMap)
120 return DataMultiMapIt != other.DataMultiMapIt;
121 else
122 return DataMapIt != other.DataMapIt;
123 }
124
132 bool operator==(const AllMapIterator& other) const noexcept
133 {
134 if (IsMultiMap)
135 return DataMultiMapIt == other.DataMultiMapIt;
136 else
137 return DataMapIt == other.DataMapIt;
138 }
139
146 {
147 if (IsMultiMap)
148 ++DataMultiMapIt;
149 else
150 ++DataMapIt;
151 return *this;
152 }
153
160 {
161 if (IsMultiMap)
162 --DataMultiMapIt;
163 else
164 --DataMapIt;
165 return *this;
166 }
167
173 std::pair<KeyType, ValueType> operator*() const noexcept
174 {
175 if (IsMultiMap)
176 return *DataMultiMapIt;
177 else
178 return *DataMapIt;
179 }
180
186 ContainerIterator GetContainerIterator() const noexcept
187 {
188 return DataMapIt;
189 }
190
196 MultiContainerIterator GetMultiContainerIterator() const noexcept
197 {
198 return DataMultiMapIt;
199 }
200
202 ~AllMapIterator() = default;
203 };
204
208 template <class KeyType, class ValueType>
210 typename std::unordered_map<KeyType, ValueType>::iterator, KeyType, ValueType>;
211
215 template <class KeyType, class ValueType>
217 typename std::map<KeyType, ValueType>::iterator, KeyType, ValueType>;
218
222 template <class KeyType, class ValueType>
224 typename std::map<KeyType, ValueType>::reverse_iterator, KeyType, ValueType>;
225
226
239 template <class MultiContainer, class Container, class Iterator, class KeyType, class ValueType>
241 {
242 protected:
244 bool IsMultiMap = false;
245
247 MultiContainer DataMultiMap;
248
250 Container DataMap;
251 public:
252
260 Iterator begin() noexcept
261 {
262 return Iterator(DataMultiMap.begin(), DataMap.begin(), IsMultiMap);
263 }
264
272 Iterator end() noexcept
273 {
274 return Iterator(DataMultiMap.end(), DataMap.end(), IsMultiMap);
275 }
276
282 Iterator Begin() noexcept
283 {
284 return Iterator(DataMultiMap.begin(), DataMap.begin(), IsMultiMap);
285 }
286
292 Iterator End() noexcept
293 {
294 return Iterator(DataMultiMap.end(), DataMap.end(), IsMultiMap);
295 }
296
302 bool IsMultiContainer() const noexcept
303 {
304 return IsMultiMap;
305 }
306
319 template <class EmplaceKeyType, class EmplaceValueType>
320 std::pair<Iterator, bool> Emplace(EmplaceKeyType&& key, EmplaceValueType&& value) noexcept
321 {
322 if (IsMultiMap)
323 {
324 auto dataMultiMapIt = DataMultiMap.emplace(std::forward<EmplaceKeyType>(key), std::forward<EmplaceValueType>(value));
325 return std::pair<Iterator, bool>(Iterator(dataMultiMapIt, DataMap.end(), true), true);
326 }
327 else
328 {
329 auto dataMapIt = DataMap.emplace(std::forward<EmplaceKeyType>(key), std::forward<EmplaceValueType>(value));
330 return std::pair<Iterator, bool>(Iterator(DataMultiMap.end(), dataMapIt.first, false), dataMapIt.second);
331 }
332 }
333
341 std::pair<Iterator, Iterator> EqualRange(const KeyType& data) noexcept
342 {
343 if (IsMultiMap)
344 {
345 auto rangePair = DataMultiMap.equal_range(data);
346 return std::pair<Iterator, Iterator>(Iterator(rangePair.first, DataMap.end(), true), Iterator(rangePair.second, DataMap.end(), true));
347 }
348 else
349 {
350 auto rangePair = DataMap.equal_range(data);
351 return std::pair<Iterator, Iterator>(Iterator(DataMultiMap.end(), rangePair.first, false), Iterator(DataMultiMap.end(), rangePair.second, false));
352 }
353 }
354
362 Iterator Find(const KeyType& data) noexcept
363 {
364 if (IsMultiMap)
365 {
366 auto retIt = DataMultiMap.find(data);
367 return Iterator(retIt, DataMap.end(), true);
368 }
369 else
370 {
371 auto retIt = DataMap.find(data);
372 return Iterator(DataMultiMap.end(), retIt, false);
373 }
374 }
375
383 std::size_t Erase(const KeyType& data) noexcept
384 {
385 std::size_t res;
386 if (IsMultiMap)
387 res = DataMultiMap.erase(data);
388 else
389 res = DataMap.erase(data);
390
391 return res;
392 }
393
401 Iterator Erase(const Iterator& dataIt) noexcept
402 {
403 if (IsMultiMap)
404 {
405 auto retIt = DataMultiMap.erase(dataIt.GetMultiContainerIterator());
406 return Iterator(retIt, DataMap.end(), true);
407 }
408 else
409 {
410 auto retIt = DataMap.erase(dataIt.GetContainerIterator());
411 return Iterator(DataMultiMap.end(), retIt, false);
412 }
413 }
414
416 void Clear() noexcept
417 {
418 if (IsMultiMap)
419 DataMultiMap.clear();
420 else
421 DataMap.clear();
422 }
423
429 std::size_t Size() noexcept
430 {
431 if (IsMultiMap)
432 return DataMultiMap.size();
433 else
434 return DataMap.size();
435 }
436 };
437
441 template <class KeyType, class ValueType>
442 class Map : public ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>,
443 MapIterator<KeyType, ValueType>, KeyType, ValueType>
444 {
445 public:
451 explicit Map(const bool& isMultiMap) noexcept
452 {
453 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::IsMultiMap = isMultiMap;
454 }
455
464 MapIterator<KeyType, ValueType> LowerBound(const KeyType& data) noexcept
465 {
466 return MapIterator<KeyType, ValueType>(ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMultiMap.lower_bound(data),
467 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMap.lower_bound(data),
468 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::IsMultiMap);
469 }
470
480 {
481 return MapIterator<KeyType, ValueType>(ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMultiMap.upper_bound(data),
482 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMap.upper_bound(data),
483 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::IsMultiMap);
484 }
485
492 {
493 return ReverseMapIterator<KeyType, ValueType>(ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMultiMap.rbegin(),
494 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMap.rbegin(),
495 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::IsMultiMap);
496 }
497
504 {
505 return ReverseMapIterator<KeyType, ValueType>(ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMultiMap.rend(),
506 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::DataMap.rend(),
507 ParentMap<std::multimap<KeyType, ValueType>, std::map<KeyType, ValueType>, MapIterator<KeyType, ValueType>, KeyType, ValueType>::IsMultiMap);
508 }
509 };
510
514 template <class KeyType, class ValueType>
515 class UnorderedMap : public ParentMap<std::unordered_multimap<KeyType, ValueType>, std::unordered_map<KeyType, ValueType>,
516 UnorderedMapIterator<KeyType, ValueType>, KeyType, ValueType>
517 {
518 public:
528 };
529}
Iterator class for all library maps.
Definition Map.h:18
ContainerIterator GetContainerIterator() const noexcept
Method for get container iterator.
Definition Map.h:186
AllMapIterator & operator=(AllMapIterator &&other) noexcept
Deleted move assignment operator.
Definition Map.h:98
AllMapIterator & operator=(const AllMapIterator &other) noexcept
Assignment operator.
Definition Map.h:79
AllMapIterator() noexcept=default
Default constructor.
AllMapIterator & operator++() noexcept
Pre increment operator.
Definition Map.h:145
std::pair< KeyType, ValueType > operator*() const noexcept
Operator*.
Definition Map.h:173
~AllMapIterator()=default
Default destructor.
AllMapIterator & operator--() noexcept
Pre decrement operator.
Definition Map.h:159
MultiContainerIterator GetMultiContainerIterator() const noexcept
Method for get multi container iterator.
Definition Map.h:196
bool operator!=(const AllMapIterator &other) const noexcept
Not equal operator.
Definition Map.h:117
bool operator==(const AllMapIterator &other) const noexcept
Compare operator.
Definition Map.h:132
AllMapIterator(AllMapIterator &&other) noexcept
Deleted move constructor.
Definition Map.h:50
AllMapIterator(const MultiContainerIterator &dataMultiMapIt, const ContainerIterator &dataMapIt, const bool &isMultiMap) noexcept
Constructor.
Definition Map.h:64
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:312
Specialization of class DataContainer, used as a type std::multimap<std::string, DataSaver>
Definition DataContainer.h:317
The descendant of the ParentMap class, which stores the map, i.e. the binary tree.
Definition Map.h:444
MapIterator< KeyType, ValueType > LowerBound(const KeyType &data) noexcept
Returns an iterator pointing to the first element that is not less than (i.e. greater or equal to) da...
Definition Map.h:464
ReverseMapIterator< KeyType, ValueType > Rbegin() noexcept
Reverse begin container method.
Definition Map.h:491
ReverseMapIterator< KeyType, ValueType > Rend() noexcept
Reverse end container method.
Definition Map.h:503
Map(const bool &isMultiMap) noexcept
Class constructor.
Definition Map.h:451
MapIterator< KeyType, ValueType > UpperBound(const KeyType &data) noexcept
Returns an iterator pointing to the first element that is greater than key.
Definition Map.h:479
Parent wrapper class for stl containers.
Definition Map.h:241
MultiContainer DataMultiMap
Multi container.
Definition Map.h:247
Iterator Erase(const Iterator &dataIt) noexcept
The method for erase data from Map with iterator.
Definition Map.h:401
std::pair< Iterator, Iterator > EqualRange(const KeyType &data) noexcept
The method for getting range of data.
Definition Map.h:341
std::pair< Iterator, bool > Emplace(EmplaceKeyType &&key, EmplaceValueType &&value) noexcept
The method for inserting data into the Map.
Definition Map.h:320
std::size_t Erase(const KeyType &data) noexcept
The method for erase data from Map with key.
Definition Map.h:383
bool IsMultiMap
Is this multi map.
Definition Map.h:244
void Clear() noexcept
The method for clear Map.
Definition Map.h:416
Iterator Find(const KeyType &data) noexcept
The method for getting iterator with key.
Definition Map.h:362
std::size_t Size() noexcept
The method for get Map size.
Definition Map.h:429
Container DataMap
Container.
Definition Map.h:250
Iterator end() noexcept
End container method.
Definition Map.h:272
Iterator Begin() noexcept
Begin container method.
Definition Map.h:282
Iterator begin() noexcept
Begin container method.
Definition Map.h:260
bool IsMultiContainer() const noexcept
The method to check if it is multi container.
Definition Map.h:302
Iterator End() noexcept
End container method.
Definition Map.h:292
The descendant of the ParentMap class, which stores the unordered_map, i.e. the hash table.
Definition Map.h:517
UnorderedMap(const bool &isMultiMap) noexcept
Class constructor.
Definition Map.h:524