open-cpp-utils 0.0.1
Loading...
Searching...
No Matches
map.h
1// =====================================================================================================================
2// open-cpp-utils, an open-source cpp library with data structures that extend the STL.
3// Copyright (C) 2024 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
19#ifndef OPEN_CPP_UTILS_MAP_H
20#define OPEN_CPP_UTILS_MAP_H
21
22#include <utility>
23
24#include "set.h"
25
26namespace open_cpp_utils
27{
28
29template<typename Key, typename Value, class Alloc = std::allocator<struct map_pair>>
30class map
31{
32// Typedefs ============================================================================================================
33
34public:
35 using key_type = Key;
36 using value_type = Value;
37 using pair_type = struct map_pair;
38
39 using key_pointer = key_type*;
40 using const_key_pointer = const key_type*;
41 using key_reference = key_type&;
42 using const_key_reference = const key_type&;
43
44 using value_pointer = value_type*;
45 using const_value_pointer = const value_type*;
46 using value_reference = value_type&;
47 using const_value_reference = const value_type&;
48
49
50 struct hash { size_t operator()(const pair_type& pair) const { return std::hash<key_type>{}(pair.key); } };
51
53 using iterator = typename table_type::iterator;
54 using const_iterator = typename table_type::const_iterator;
55
56 struct map_pair
57 {
58 key_type key;
59 value_type value;
60
61 bool operator==(const map_pair& other) const { return key == other.key; }
62
63 map_pair() : key(), value() { }
64 map_pair(const_key_reference k) : key(k), value() { }
65 map_pair(const_key_reference k, const_value_reference v) : key(k), value(v) { }
66 map_pair(const map_pair& v) : key(v.key), value(v.value) { }
67 map_pair(map_pair&& v) noexcept : key(std::move(v.key)), value(std::move(v.value)) { }
68 ~map_pair() = default;
69
70 map_pair& operator=(const map_pair& v)
71 {
72 key = v.key;
73 value = v.value;
74 return *this;
75 }
76
77 map_pair& operator=(map_pair&& v) noexcept
78 {
79 if(&v == this) return *this;
80
81 key = std::move(v.key);
82 value = std::move(v.value);
83 return *this;
84 }
85 };
86
87
88// Functions ===========================================================================================================
89
90// Constructors & Destructor -------------------------------------------------------------------------------------------
91
92public:
93 map() = default;
94 map(std::initializer_list<pair_type> data) : table_(data) { }
95 map(const map&) = default;
96 map(map&&) = default;
97 ~map() = default;
98
99 void reserve(size_t size) { table_.reserve(size); }
100
101 void insert(const_key_reference key, const_value_reference value);
102 void erase(const_key_reference key);
103
104 value_reference& operator[](const_key_reference key);
105
106 const_value_reference& get(const_key_reference key, const_value_reference& def = value_type()) const;
107
108 iterator find(const_key_reference key) { return table_.find({ key, value_type() }); }
109 const_iterator find(const_key_reference key) const { return table_.find({ key, value_type() }); }
110
111 bool contains(const_key_reference key) { return table_.contains({ key, value_type() }); }
112
113 iterator begin() { return table_.begin(); }
114 iterator end() { return table_.end(); }
115
116 const_iterator begin() const { return table_.begin(); }
117 const_iterator end() const { return table_.end(); }
118
119 size_t size() const { return table_.size(); }
120
121// Variables ===========================================================================================================
122
123private:
124 table_type table_;
125};
126
127template<typename Key, typename Value, class Alloc>
128void map<Key, Value, Alloc>::insert(const_key_reference key, const_value_reference value)
129{
130 iterator it = find({ key, value });
131 if(it != end()) table_.insert({ key, value });
132}
133
134template<typename Key, typename Value, class Alloc>
135void map<Key, Value, Alloc>::erase(const_key_reference key)
136{
137 table_.erase({ key, value_type() });
138}
139
140template<typename Key, typename Value, class Alloc>
141typename map<Key, Value, Alloc>::value_reference map<Key, Value, Alloc>::operator[](const_key_reference key)
142{
143 iterator it = table_.find({ key, value_type() });
144 if(it == table_.end())
145 {
146 table_.insert(key);
147 it = table_.find({ key, value_type() });
148 }
149 return it->value;
150}
151
152template<typename Key, typename Value, class Alloc>
153typename map<Key, Value, Alloc>::const_value_reference map<Key, Value, Alloc>::get(const_key_reference key,
154 const_value_reference def) const
155{
156 auto it = find(key);
157 if(it == end()) return def;
158 return it->value;
159}
160
161}
162
163#endif // OPEN_CPP_UTILS_MAP_H
Definition hash_table.h:86
Definition hash_table.h:35
Definition map.h:31
Definition map.h:50