ActiveLib
Loading...
Searching...
No Matches
Map.h
1
6#ifndef ACTIVE_CONTAINER_MAP
7#define ACTIVE_CONTAINER_MAP
8
9#include "Active/Utility/Cloner.h"
10#include "Active/Utility/Mover.h"
11
12#include <map>
13
14namespace active::container {
15
25 template<class Key, class T> requires utility::Clonable<T>
26 class Map : public std::map<Key, std::unique_ptr<T>> {
27 public:
28
29 // MARK: Types
30
32 using base = std::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 Map() : base() {}
58 Map(const Map& source) : base() { cloneFrom(source); }
63 Map(Map&& source) : base(std::move(source)) {}
67 virtual ~Map() = default;
68
69 // MARK: Operators
70
76 auto operator= (const Map& source) {
77 if (this != &source) {
78 base::clear();
79 cloneFrom(source);
80 }
81 return *this;
82 }
88 auto operator= (Map&& 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, 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(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 Map& source) {
174 for (const auto& item : source)
175 base::operator[](item.first) = (item.second ? clone(*item.second) : mapped_type());
176 }
177 };
178
179}
180
181#endif //ACTIVE_CONTAINER_MAP
Definition Map.h:26
typename base::value_type value_type
Paired key/value type.
Definition Map.h:38
typename base::mapped_type mapped_type
Stored type.
Definition Map.h:36
auto emplace(const Key &key, T &&item)
Definition Map.h:145
Map(const Map &source)
Definition Map.h:58
auto release(iterator &pos)
Definition Map.h:156
virtual ~Map()=default
auto emplace(const Key &key, mapped_type &&item)
Definition Map.h:140
Map()
Definition Map.h:53
typename base::const_iterator const_iterator
Container const iterator type.
Definition Map.h:46
mapped_type & operator[](const Key &key)
Definition Map.h:98
auto find(const Key &key) const
Definition Map.h:106
auto insert(const raw_type &item)
Definition Map.h:115
typename base::iterator iterator
Container iterator type.
Definition Map.h:44
typename base::node_type node_type
Node handle type.
Definition Map.h:40
auto insert(const value_type &item)
Definition Map.h:121
auto release(const_iterator pos)
Definition Map.h:162
std::map< Key, std::unique_ptr< T > > base
Base container type.
Definition Map.h:32
auto operator=(const Map &source)
Definition Map.h:76
Map(Map &&source)
Definition Map.h:63
auto insert(value_type &&item)
Definition Map.h:127
auto insert(node_type &&node)
Definition Map.h:135
typename base::size_type size_type
Container size (index) type.
Definition Map.h:42
std::pair< const Key, T > raw_type
Paired key/raw value type.
Definition Map.h:34
Movable concept for classes/functions dependent on cloning with a move.
Definition Mover.h:33
Definition HashMap.h:14