koutil
Loading...
Searching...
No Matches
hash_array.h
Go to the documentation of this file.
1#ifndef KOUTIL_CONTAINER_HASH_ARRAY_H
2#define KOUTIL_CONTAINER_HASH_ARRAY_H
3
5#include <cassert>
6#include <concepts>
7#include <cstddef>
8#include <functional>
9#include <memory>
10#include <type_traits>
11#include <utility>
12#include <vector>
13
14namespace koutil::container {
15
21template <typename T, typename Key>
22concept is_hash = requires(T hash, const Key& key) {
23 requires std::is_trivially_constructible_v<T>;
24
25 { hash(key) } -> std::same_as<std::size_t>;
26};
27
34template <typename T, typename Key, typename KeyID>
35concept is_key_adapter = requires(const Key& key, T adapter, const KeyID& index) {
36 requires std::move_constructible<T> && std::is_trivially_copy_constructible_v<T>;
37 { adapter.eql(key, index) } -> std::same_as<bool>;
38};
39
49template <
50 typename Key,
51 typename KeyID,
53 is_hash<Key> Hash = std::hash<Key>,
54 is_bucket<KeyID> Bucket = std::vector<std::pair<std::size_t, KeyID>>,
55 is_allocator<Bucket> Allocator = std::allocator<Bucket>>
57private:
58 using key_t = Key;
59 using key_id_t = KeyID;
60 using value_t = std::size_t;
61 using bucket_t = Bucket;
62 using hash_t = Hash;
63 using bucket_iter = bucket_t::iterator;
64 using adapter_t = KeyAdapter;
65 using allocator_t = Allocator;
66
68 template <bool> bool eql(const key_t& key, const key_id_t& id) const { return adapter.eql(key, id); }
69
71 };
72
73 struct HashWrapper {
74 template <bool> std::size_t hash(const key_t& key) { return hasher(key); }
75
77 };
78
79 constexpr static bool comptime_value = true;
80
83
84public:
87
91 hash_array() = default;
92
98 hash_array(std::size_t bucket_count)
100
106 hash_array(const hash_array& other)
107 : m_storage(other.m_storage) { }
108
114 hash_array(hash_array&& other)
115 : m_storage(std::move(other.m_storage))
116
117 { }
118
122 ~hash_array() = default;
123
130 hash_array& operator=(const hash_array& other) = default;
137 hash_array& operator=(hash_array&& other) = default;
138
143 [[nodiscard]] bool empty() const { return m_storage.empty(); }
144
149 [[nodiscard]] std::size_t size() const { return m_storage.size(); }
150
155 [[nodiscard]] std::size_t bucket_count() const { return m_storage.bucket_count(); }
156
161 [[nodiscard]] float max_load_factor() const { return m_storage.max_load_factor(); }
162
167 void set_max_load_factor(float factor) {
168 assert(factor > 0);
170 }
171
175 void clear() { m_storage.clear(); }
176
183 bool try_insert(const key_t& key, const key_id_t& key_id, adapter_t adapter) {
184
185 return m_storage.template try_insert<comptime_value>(key, key_id, AdapterWrapper { adapter });
186 }
187
194 bool try_set(const key_t& key, const key_id_t& new_key_id, adapter_t adapter) {
195 return m_storage.template try_set<comptime_value>(key, new_key_id, AdapterWrapper { adapter });
196 }
197
206 void erase(const key_t& key, adapter_t adapter) {
207 m_storage.template erase<comptime_value>(key, AdapterWrapper { adapter });
208 }
209
217 iterator_t find(const key_t& key, adapter_t adapter) {
218 return m_storage.template find<comptime_value>(key, AdapterWrapper { adapter });
219 }
220
228 const_iterator_t find(const key_t& key, adapter_t adapter) const {
229 return m_storage.template find<comptime_value>(key, AdapterWrapper { adapter });
230 }
231
236 iterator_t begin() { return m_storage.begin(); }
237
242 iterator_t end() { return m_storage.end(); }
243
248 const_iterator_t begin() const { return m_storage.begin(); }
249
254 const_iterator_t end() const { return m_storage.end(); }
255
260 const_iterator_t cbegin() const { return m_storage.cbegin(); }
261
266 const_iterator_t cend() const { return m_storage.cend(); }
267
268private:
270};
271}
272
273#endif
A hash array with open addressing and linear probing.
Definition hash_array.h:56
Allocator allocator_t
Definition hash_array.h:65
const_iterator_t cend() const
Get a constant iterator to the end of the hash_array.
Definition hash_array.h:265
iterator_t end()
Get an iterator to the end of the hash_array.
Definition hash_array.h:241
hash_array()=default
Default constructor.
bool try_set(const key_t &key, const key_id_t &new_key_id, adapter_t adapter)
Try to set new key ID.
Definition hash_array.h:193
void erase(const key_t &key, adapter_t adapter)
Erases an element from the hash table.
Definition hash_array.h:205
KeyID key_id_t
Definition hash_array.h:59
std::size_t bucket_count() const
Returns the number of buckets.
Definition hash_array.h:154
template_hash_array_t::iterator_t iterator_t
Definition hash_array.h:84
std::size_t size() const
Get the number of elements in the hash_array.
Definition hash_array.h:148
KeyAdapter adapter_t
Definition hash_array.h:64
template_hash_array_t m_storage
Definition hash_array.h:268
iterator_t begin()
Get an iterator to the beginning of the hash_array.
Definition hash_array.h:235
bool try_insert(const key_t &key, const key_id_t &key_id, adapter_t adapter)
Try to insert a key and key ID into the hash_array.
Definition hash_array.h:182
iterator_t find(const key_t &key, adapter_t adapter)
Finds an element in the hash table.
Definition hash_array.h:216
std::size_t value_t
Definition hash_array.h:60
bool empty() const
Check if the hash_array is empty.
Definition hash_array.h:142
~hash_array()=default
Destructor.
bucket_t::iterator bucket_iter
Definition hash_array.h:63
Key key_t
Definition hash_array.h:58
void clear()
Clear all elements from the hash_array.
Definition hash_array.h:174
static constexpr bool comptime_value
Definition hash_array.h:79
template_hash_array_t::const_iterator_t const_iterator_t
Definition hash_array.h:85
const_iterator_t cbegin() const
Get a constant iterator to the beginning of the hash_array.
Definition hash_array.h:259
float max_load_factor() const
Returns the maximum load factor.
Definition hash_array.h:160
Hash hash_t
Definition hash_array.h:62
void set_max_load_factor(float factor)
Sets a new maximum load factor.
Definition hash_array.h:166
Bucket bucket_t
Definition hash_array.h:61
hash_array & operator=(const hash_array &other)=default
Copy assignment operator.
bool empty() const
Check if the hash_array is empty.
Definition template_hash_array.h:303
iterator_t begin()
Get an iterator to the beginning of the hash_array.
Definition template_hash_array.h:413
std::size_t bucket_count() const
Returns the number of buckets.
Definition template_hash_array.h:315
void clear()
Clear all elements from the hash_array.
Definition template_hash_array.h:335
std::size_t size() const
Get the number of elements in the hash_array.
Definition template_hash_array.h:309
const_iterator_t cend() const
Get a constant iterator to the end of the hash_array.
Definition template_hash_array.h:443
void set_max_load_factor(float factor)
Sets a new maximum load factor.
Definition template_hash_array.h:327
const_iterator_t cbegin() const
Get a constant iterator to the beginning of the hash_array.
Definition template_hash_array.h:437
iterator_t end()
Get an iterator to the end of the hash_array.
Definition template_hash_array.h:419
float max_load_factor() const
Returns the maximum load factor.
Definition template_hash_array.h:321
Concept to check if a type is a valid allocator for a given bucket type.
Definition template_hash_array.h:69
Concept to check if a type is a valid bucket type.
Definition template_hash_array.h:35
Concept to check if a type is a valid hash function for a given Key type.
Definition hash_array.h:22
Concept to check if a type is a valid key adapter.
Definition hash_array.h:35
Definition comptime_map.h:11
bool eql(const key_t &key, const key_id_t &id) const
Definition hash_array.h:68
adapter_t adapter
Definition hash_array.h:70
hash_t hasher
Definition hash_array.h:76
std::size_t hash(const key_t &key)
Definition hash_array.h:74