10#include <unordered_map>
11#include <initializer_list>
36template <
typename K,
typename V>
44 using Data = std::unordered_map<K, V>;
84 d = std::move(
other.d);
151 auto it = d.find(key);
152 if(
it != d.end())
return it->second;
157 bool contains(
const K &key)
const {
return d.find(key) != d.end(); }
177 d.insert_or_assign(key,
val);
187 d.insert_or_assign(key, std::move(
val));
197 return d.erase(key) > 0;
247 template <
typename Func>
249 for(
const auto &[
k, v] : d) func(
k, v);
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