libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
hashmap.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <unordered_map>
11#include <initializer_list>
14#include <promeki/core/list.h>
15
17
36template <typename K, typename V>
37class HashMap {
39 public:
42
44 using Data = std::unordered_map<K, V>;
45
47 using Key = K;
48
50 using Value = V;
51
53 using Iterator = typename Data::iterator;
54
56 using ConstIterator = typename Data::const_iterator;
57
59 HashMap() = default;
60
62 HashMap(const HashMap &other) : d(other.d) {}
63
65 HashMap(HashMap &&other) noexcept : d(std::move(other.d)) {}
66
71 HashMap(std::initializer_list<std::pair<const K, V>> initList) : d(initList) {}
72
74 ~HashMap() = default;
75
78 d = other.d;
79 return *this;
80 }
81
84 d = std::move(other.d);
85 return *this;
86 }
87
88 // -- Iterators --
89
91 Iterator begin() noexcept { return d.begin(); }
92
94 ConstIterator begin() const noexcept { return d.cbegin(); }
95
97 ConstIterator cbegin() const noexcept { return d.cbegin(); }
98
100 ConstIterator constBegin() const noexcept { return d.cbegin(); }
101
103 Iterator end() noexcept { return d.end(); }
104
106 ConstIterator end() const noexcept { return d.cend(); }
107
109 ConstIterator cend() const noexcept { return d.cend(); }
110
112 ConstIterator constEnd() const noexcept { return d.cend(); }
113
114 // -- Capacity --
115
117 bool isEmpty() const noexcept { return d.empty(); }
118
120 size_t size() const noexcept { return d.size(); }
121
122 // -- Lookup --
123
130 V &operator[](const K &key) { return d[key]; }
131
141 const V &operator[](const K &key) const { return d.at(key); }
142
150 V value(const K &key, const V &defaultValue = V{}) const {
151 auto it = d.find(key);
152 if(it != d.end()) return it->second;
153 return defaultValue;
154 }
155
157 bool contains(const K &key) const { return d.find(key) != d.end(); }
158
164 Iterator find(const K &key) { return d.find(key); }
165
167 ConstIterator find(const K &key) const { return d.find(key); }
168
169 // -- Modifiers --
170
176 void insert(const K &key, const V &val) {
177 d.insert_or_assign(key, val);
178 return;
179 }
180
186 void insert(const K &key, V &&val) {
187 d.insert_or_assign(key, std::move(val));
188 return;
189 }
190
196 bool remove(const K &key) {
197 return d.erase(key) > 0;
198 }
199
206 return d.erase(pos);
207 }
208
211 d.clear();
212 return;
213 }
214
219 void swap(HashMap &other) noexcept {
220 d.swap(other.d);
221 return;
222 }
223
224 // -- Convenience --
225
227 List<K> keys() const {
228 List<K> ret;
229 ret.reserve(d.size());
230 for(const auto &[k, v] : d) ret.pushToBack(k);
231 return ret;
232 }
233
235 List<V> values() const {
236 List<V> ret;
237 ret.reserve(d.size());
238 for(const auto &[k, v] : d) ret.pushToBack(v);
239 return ret;
240 }
241
247 template <typename Func>
248 void forEach(Func &&func) const {
249 for(const auto &[k, v] : d) func(k, v);
250 return;
251 }
252
253 // -- Comparison --
254
256 friend bool operator==(const HashMap &lhs, const HashMap &rhs) { return lhs.d == rhs.d; }
257
259 friend bool operator!=(const HashMap &lhs, const HashMap &rhs) { return lhs.d != rhs.d; }
260
261 private:
262 Data d;
263};
264
Unordered associative container wrapping std::unordered_map.
Definition hashmap.h:37
~HashMap()=default
Destructor.
bool contains(const K &key) const
Returns true if key exists in the hash map.
Definition hashmap.h:157
ConstIterator constEnd() const noexcept
Returns a const iterator to one past the last entry.
Definition hashmap.h:112
const V & operator[](const K &key) const
Returns a const reference to the value for key.
Definition hashmap.h:141
HashMap & operator=(HashMap &&other) noexcept
Move assignment operator.
Definition hashmap.h:83
bool isEmpty() const noexcept
Returns true if the hash map has no entries.
Definition hashmap.h:117
ConstIterator end() const noexcept
Returns a const iterator to one past the last entry.
Definition hashmap.h:106
void forEach(Func &&func) const
Calls func for every key-value pair.
Definition hashmap.h:248
void insert(const K &key, const V &val)
Inserts or assigns a key-value pair.
Definition hashmap.h:176
void clear() noexcept
Removes all entries.
Definition hashmap.h:210
void swap(HashMap &other) noexcept
Swaps contents with another hash map.
Definition hashmap.h:219
ConstIterator cend() const noexcept
Returns a const iterator to one past the last entry.
Definition hashmap.h:109
ConstIterator cbegin() const noexcept
Returns a const iterator to the first entry.
Definition hashmap.h:97
size_t size() const noexcept
Returns the number of key-value pairs.
Definition hashmap.h:120
typename Data::iterator Iterator
Mutable forward iterator.
Definition hashmap.h:53
HashMap(std::initializer_list< std::pair< const K, V > > initList)
Constructs a hash map from an initializer list of key-value pairs.
Definition hashmap.h:71
ConstIterator begin() const noexcept
Returns a const iterator to the first entry.
Definition hashmap.h:94
friend bool operator!=(const HashMap &lhs, const HashMap &rhs)
Returns true if the hash maps differ.
Definition hashmap.h:259
HashMap(HashMap &&other) noexcept
Move constructor.
Definition hashmap.h:65
V value(const K &key, const V &defaultValue=V{}) const
Returns the value for key, or defaultValue if the key is not present.
Definition hashmap.h:150
bool remove(const K &key)
Removes the entry for key.
Definition hashmap.h:196
friend bool operator==(const HashMap &lhs, const HashMap &rhs)
Returns true if both hash maps have identical contents.
Definition hashmap.h:256
ConstIterator constBegin() const noexcept
Returns a const iterator to the first entry.
Definition hashmap.h:100
Iterator find(const K &key)
Finds the entry for key.
Definition hashmap.h:164
List< V > values() const
Returns a list of all values.
Definition hashmap.h:235
HashMap()=default
Default constructor. Creates an empty hash map.
ConstIterator find(const K &key) const
Const overload of find().
Definition hashmap.h:167
Iterator begin() noexcept
Returns a mutable iterator to the first entry.
Definition hashmap.h:91
typename Data::const_iterator ConstIterator
Const forward iterator.
Definition hashmap.h:56
List< K > keys() const
Returns a list of all keys.
Definition hashmap.h:227
Iterator remove(Iterator pos)
Removes the entry at pos.
Definition hashmap.h:205
void insert(const K &key, V &&val)
Inserts or assigns a key-value pair (move overload).
Definition hashmap.h:186
Iterator end() noexcept
Returns a mutable iterator to one past the last entry.
Definition hashmap.h:103
std::unordered_map< K, V > Data
Underlying std::unordered_map storage type.
Definition hashmap.h:44
HashMap(const HashMap &other)
Copy constructor.
Definition hashmap.h:62
V & operator[](const K &key)
Returns a reference to the value for key, inserting a default-constructed value if it does not exist.
Definition hashmap.h:130
HashMap & operator=(const HashMap &other)
Copy assignment operator.
Definition hashmap.h:77
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
#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