libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
map.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <map>
11#include <initializer_list>
14#include <promeki/core/list.h>
15
17
37template <typename K, typename V>
38class Map {
40 public:
43
45 using Data = std::map<K, V>;
46
48 using Iterator = typename Data::iterator;
49
51 using ConstIterator = typename Data::const_iterator;
52
54 using RevIterator = typename Data::reverse_iterator;
55
57 using ConstRevIterator = typename Data::const_reverse_iterator;
58
60 Map() = default;
61
63 Map(const Map &other) : d(other.d) {}
64
66 Map(Map &&other) noexcept : d(std::move(other.d)) {}
67
72 Map(std::initializer_list<std::pair<const K, V>> initList) : d(initList) {}
73
75 ~Map() = default;
76
79 d = other.d;
80 return *this;
81 }
82
84 Map &operator=(Map &&other) noexcept {
85 d = std::move(other.d);
86 return *this;
87 }
88
89 // -- Iterators --
90
92 Iterator begin() noexcept { return d.begin(); }
93
95 ConstIterator begin() const noexcept { return d.cbegin(); }
96
98 ConstIterator cbegin() const noexcept { return d.cbegin(); }
99
101 ConstIterator constBegin() const noexcept { return d.cbegin(); }
102
104 Iterator end() noexcept { return d.end(); }
105
107 ConstIterator end() const noexcept { return d.cend(); }
108
110 ConstIterator cend() const noexcept { return d.cend(); }
111
113 ConstIterator constEnd() const noexcept { return d.cend(); }
114
116 RevIterator rbegin() noexcept { return d.rbegin(); }
117
119 RevIterator revBegin() noexcept { return d.rbegin(); }
120
122 ConstRevIterator crbegin() const noexcept { return d.crbegin(); }
123
126
128 RevIterator rend() noexcept { return d.rend(); }
129
131 RevIterator revEnd() noexcept { return d.rend(); }
132
134 ConstRevIterator crend() const noexcept { return d.crend(); }
135
138
139 // -- Capacity --
140
142 bool isEmpty() const noexcept { return d.empty(); }
143
145 size_t size() const noexcept { return d.size(); }
146
147 // -- Lookup --
148
155 V &operator[](const K &key) { return d[key]; }
156
166 const V &operator[](const K &key) const { return d.at(key); }
167
175 V value(const K &key, const V &defaultValue = V{}) const {
176 auto it = d.find(key);
177 if(it != d.end()) return it->second;
178 return defaultValue;
179 }
180
182 bool contains(const K &key) const { return d.find(key) != d.end(); }
183
189 Iterator find(const K &key) { return d.find(key); }
190
192 ConstIterator find(const K &key) const { return d.find(key); }
193
194 // -- Modifiers --
195
201 void insert(const K &key, const V &val) {
202 d.insert_or_assign(key, val);
203 return;
204 }
205
211 void insert(const K &key, V &&val) {
212 d.insert_or_assign(key, std::move(val));
213 return;
214 }
215
221 bool remove(const K &key) {
222 return d.erase(key) > 0;
223 }
224
231 return d.erase(pos);
232 }
233
236 d.clear();
237 return;
238 }
239
244 void swap(Map &other) noexcept {
245 d.swap(other.d);
246 return;
247 }
248
249 // -- Convenience --
250
252 List<K> keys() const {
253 List<K> ret;
254 ret.reserve(d.size());
255 for(const auto &[k, v] : d) ret.pushToBack(k);
256 return ret;
257 }
258
260 List<V> values() const {
261 List<V> ret;
262 ret.reserve(d.size());
263 for(const auto &[k, v] : d) ret.pushToBack(v);
264 return ret;
265 }
266
272 template <typename Func>
273 void forEach(Func &&func) const {
274 for(const auto &[k, v] : d) func(k, v);
275 return;
276 }
277
278 // -- Comparison --
279
281 friend bool operator==(const Map &lhs, const Map &rhs) { return lhs.d == rhs.d; }
282
284 friend bool operator!=(const Map &lhs, const Map &rhs) { return lhs.d != rhs.d; }
285
286 private:
287 Data d;
288};
289
Dynamic array container wrapping std::vector.
Definition list.h:40
void reserve(size_t newCapacity)
Pre-allocates storage for at least newCapacity elements.
Definition list.h:314
void pushToBack(const T &value)
Pushes an item onto the back of the list.
Definition list.h:455
Ordered associative container wrapping std::map.
Definition map.h:38
ConstRevIterator crend() const noexcept
Returns a const reverse iterator to one before the first entry.
Definition map.h:134
Iterator find(const K &key)
Finds the entry for key.
Definition map.h:189
ConstIterator constEnd() const noexcept
Returns a const iterator to one past the last entry.
Definition map.h:113
bool contains(const K &key) const
Returns true if key exists in the map.
Definition map.h:182
List< V > values() const
Returns a list of all values.
Definition map.h:260
Map & operator=(Map &&other) noexcept
Move assignment operator.
Definition map.h:84
friend bool operator==(const Map &lhs, const Map &rhs)
Returns true if both maps have identical contents.
Definition map.h:281
Map(std::initializer_list< std::pair< const K, V > > initList)
Constructs a map from an initializer list of key-value pairs.
Definition map.h:72
void insert(const K &key, V &&val)
Inserts or assigns a key-value pair (move overload).
Definition map.h:211
std::map< K, V > Data
Underlying std::map storage type.
Definition map.h:45
ConstIterator end() const noexcept
Returns a const iterator to one past the last entry.
Definition map.h:107
ConstIterator constBegin() const noexcept
Returns a const iterator to the first entry.
Definition map.h:101
ConstIterator cbegin() const noexcept
Returns a const iterator to the first entry.
Definition map.h:98
Map(const Map &other)
Copy constructor.
Definition map.h:63
void insert(const K &key, const V &val)
Inserts or assigns a key-value pair.
Definition map.h:201
typename Data::iterator Iterator
Mutable forward iterator.
Definition map.h:48
V & operator[](const K &key)
Returns a reference to the value for key, inserting a default-constructed value if it does not exist.
Definition map.h:155
Map(Map &&other) noexcept
Move constructor.
Definition map.h:66
ConstRevIterator constRevEnd() const noexcept
Returns a const reverse iterator to one before the first entry.
Definition map.h:137
RevIterator rend() noexcept
Returns a mutable reverse iterator to one before the first entry.
Definition map.h:128
V value(const K &key, const V &defaultValue=V{}) const
Returns the value for key, or defaultValue if the key is not present.
Definition map.h:175
RevIterator rbegin() noexcept
Returns a mutable reverse iterator to the last entry.
Definition map.h:116
Iterator begin() noexcept
Returns a mutable iterator to the first entry.
Definition map.h:92
size_t size() const noexcept
Returns the number of key-value pairs.
Definition map.h:145
List< K > keys() const
Returns a list of all keys.
Definition map.h:252
bool isEmpty() const noexcept
Returns true if the map has no entries.
Definition map.h:142
typename Data::const_iterator ConstIterator
Const forward iterator.
Definition map.h:51
void clear() noexcept
Removes all entries.
Definition map.h:235
Map & operator=(const Map &other)
Copy assignment operator.
Definition map.h:78
typename Data::const_reverse_iterator ConstRevIterator
Const reverse iterator.
Definition map.h:57
ConstRevIterator crbegin() const noexcept
Returns a const reverse iterator to the last entry.
Definition map.h:122
RevIterator revEnd() noexcept
Returns a mutable reverse iterator to one before the first entry.
Definition map.h:131
Iterator remove(Iterator pos)
Removes the entry at pos.
Definition map.h:230
ConstRevIterator constRevBegin() const noexcept
Returns a const reverse iterator to the last entry.
Definition map.h:125
ConstIterator cend() const noexcept
Returns a const iterator to one past the last entry.
Definition map.h:110
ConstIterator find(const K &key) const
Const overload of find().
Definition map.h:192
void swap(Map &other) noexcept
Swaps contents with another map.
Definition map.h:244
RevIterator revBegin() noexcept
Returns a mutable reverse iterator to the last entry.
Definition map.h:119
Map()=default
Default constructor. Creates an empty map.
bool remove(const K &key)
Removes the entry for key.
Definition map.h:221
~Map()=default
Destructor.
void forEach(Func &&func) const
Calls func for every key-value pair.
Definition map.h:273
friend bool operator!=(const Map &lhs, const Map &rhs)
Returns true if the maps differ.
Definition map.h:284
ConstIterator begin() const noexcept
Returns a const iterator to the first entry.
Definition map.h:95
const V & operator[](const K &key) const
Returns a const reference to the value for key.
Definition map.h:166
Iterator end() noexcept
Returns a mutable iterator to one past the last entry.
Definition map.h:104
typename Data::reverse_iterator RevIterator
Mutable reverse iterator.
Definition map.h:54
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
#define PROMEKI_SHARED_FINAL(TYPE)
Macro for non-polymorphic native shared objects.
Definition sharedptr.h:88