ActiveLib
Loading...
Searching...
No Matches
HashMap.h
1
6#ifndef ACTIVE_CONTAINER_HASH_MAP
7#define ACTIVE_CONTAINER_HASH_MAP
8
9#include "Active/Utility/Cloner.h"
10#include "Active/Utility/Mover.h"
11
12#include <unordered_map>
13
15
25 template<class Key, class T> requires utility::Clonable<T>
26 class HashMap : public std::unordered_map<Key, std::unique_ptr<T>> {
27 public:
28
29 // MARK: Types
30
32 using base = std::unordered_map<Key, std::unique_ptr<T>>;
34 using raw_type = std::pair<const Key, T>;
36 using mapped_type = typename base::mapped_type;
38 using value_type = typename base::value_type;
40 using node_type = typename base::node_type;
42 using size_type = typename base::size_type;
44 using iterator = typename base::iterator;
46 using const_iterator = typename base::const_iterator;
47
48 // MARK: Constructors
49
53 HashMap() : base() {}
58 HashMap(const HashMap& source) : base() { cloneFrom(source); }
63 HashMap(HashMap&& source) : base(std::move(source)) {}
67 virtual ~HashMap() = default;
68
69 // MARK: Operators
70
76 auto operator= (const HashMap& source) {
77 if (this != &source) {
78 base::clear();
79 cloneFrom(source);
80 }
81 return *this;
82 }
88 auto operator= (HashMap&& source) {
89 if (this != &source)
90 base::operator=(std::move(source));
91 return *this;
92 }
98 mapped_type& operator[] (const Key& key) { return base::operator[](key); }
99
100 // MARK: Functions (const)
101
106 auto find(const Key& key) const { return base::find(key); }
107
108 // MARK: Functions (mutating)
109
115 auto insert(const raw_type& item) { return emplace(item.first, std::move(clone(item.second))); }
121 auto insert(const value_type& item) { return emplace(item.first, clone(*item.second)); }
127 auto insert(value_type&& item) {
128 emplace(item.first, std::move(item.second));
129 }
135 auto insert(node_type&& node) { return base::insert(std::move(node)); }
140 auto emplace(const Key& key, mapped_type&& item) { return base::emplace(std::make_pair(key, std::move(item))); }
145 auto emplace(const Key& key, T&& item) {
146 if constexpr(utility::Movable<T>)
147 base::emplace(key, cloneMove(std::move(item)));
148 else
149 base::emplace(key, clone(item));
150 }
156 auto release(iterator& pos) { return release(const_iterator(pos)); }
163 auto item = std::move(pos.second);
164 erase(pos);
165 return item;
166 }
167
168 private:
173 void cloneFrom(const HashMap& source) {
174 for (const auto& item : source)
175 base::emplace(item.first, std::unique_ptr<T>{item.second ? clone(*item.second) : nullptr});
176 }
177 };
178
179}
180
181#endif //ACTIVE_CONTAINER_HASH_MAP
Definition HashMap.h:26
auto release(const_iterator pos)
Definition HashMap.h:162
auto find(const Key &key) const
Definition HashMap.h:106
auto insert(const value_type &item)
Definition HashMap.h:121
typename base::mapped_type mapped_type
Stored type.
Definition HashMap.h:36
typename base::iterator iterator
Container iterator type.
Definition HashMap.h:44
HashMap()
Definition HashMap.h:53
auto insert(value_type &&item)
Definition HashMap.h:127
virtual ~HashMap()=default
typename base::value_type value_type
Paired key/value type.
Definition HashMap.h:38
typename base::const_iterator const_iterator
Container const iterator type.
Definition HashMap.h:46
typename base::size_type size_type
Container size (index) type.
Definition HashMap.h:42
auto operator=(const HashMap &source)
Definition HashMap.h:76
HashMap(const HashMap &source)
Definition HashMap.h:58
HashMap(HashMap &&source)
Definition HashMap.h:63
auto emplace(const Key &key, mapped_type &&item)
Definition HashMap.h:140
auto insert(const raw_type &item)
Definition HashMap.h:115
std::unordered_map< Key, std::unique_ptr< T > > base
Base container type.
Definition HashMap.h:32
auto insert(node_type &&node)
Definition HashMap.h:135
auto release(iterator &pos)
Definition HashMap.h:156
typename base::node_type node_type
Node handle type.
Definition HashMap.h:40
auto emplace(const Key &key, T &&item)
Definition HashMap.h:145
std::pair< const Key, T > raw_type
Paired key/raw value type.
Definition HashMap.h:34
mapped_type & operator[](const Key &key)
Definition HashMap.h:98
Clonable concept for classes/functions dependent on cloning.
Definition Cloner.h:33
Movable concept for classes/functions dependent on cloning with a move.
Definition Mover.h:33
Definition HashMap.h:14