1#ifndef KOUTIL_CONTAINER_TEMPLATE_HASH_ARRAY_H
2#define KOUTIL_CONTAINER_TEMPLATE_HASH_ARRAY_H
22template <
typename T,
typename Key,
typename ComptimeData>
24 requires std::is_trivially_constructible_v<T>;
26 { hash.template hash<ComptimeData {}>(key) } -> std::same_as<std::size_t>;
34template <
typename Bucket,
typename KeyID>
35concept is_bucket =
requires(Bucket& bucket, std::size_t v) {
36 { *bucket.begin() } -> std::same_as<std::pair<std::size_t, KeyID>&>;
37 { *bucket.end() } -> std::same_as<std::pair<std::size_t, KeyID>&>;
38 { bucket.size() } -> std::same_as<std::size_t>;
39 { bucket.emplace_back(v, v) };
41 { bucket.erase(bucket.begin()) };
43 typename Bucket::iterator;
45 requires std::forward_iterator<typename Bucket::iterator>;
46 requires std::is_same_v<
typename Bucket::iterator,
decltype(bucket.begin())>
47 && std::is_same_v<
typename Bucket::iterator,
decltype(bucket.end())>;
57template <
typename T,
typename Key,
typename KeyID,
typename ComptimeData>
59 requires std::move_constructible<T> && std::is_trivially_copy_constructible_v<T>;
60 { adapter.template eql<ComptimeData {}>(key, index) } -> std::same_as<bool>;
68template <
typename T,
typename Bucket>
69concept is_allocator =
requires(T alloc, std::size_t n, Bucket* buckets) {
70 requires std::is_constructible_v<T>;
71 { alloc.allocate(n) } -> std::same_as<Bucket*>;
72 { alloc.deallocate(buckets, n) };
78 typename ComptimeData,
103 using ref_t = std::conditional_t<is_const, const bucket_t*, bucket_t*>;
107 using reference = std::conditional_t<is_const, const value_type&, value_type&>;
240 other.m_buckets =
nullptr;
242 other.m_buckets_count = 0;
257 if (&other ==
this) {
281 if (&other ==
this) {
292 other.m_buckets =
nullptr;
294 other.m_buckets_count = 0;
356 return set<Data>(key, new_key_id, adapter);
380 if (it != bucket.end()) {
382 static_cast<std::size_t
>(std::distance(bucket.begin(), it)),
400 if (it != bucket.end()) {
402 static_cast<std::size_t
>(std::distance(bucket.begin(), it)),
459 auto bucket_end = bucket.end();
460 auto bucket_start = bucket.begin();
462 for (
auto start = bucket_start; start != bucket_end; ++start) {
463 if (adapter.template eql<Data>(key, start->second)) {
471 template <comptime_t Data>
473 auto bucket_end = bucket.end();
474 auto bucket_start = bucket.begin();
476 for (
auto start = bucket_start; start != bucket_end; ++start) {
477 if (adapter.template eql<Data>(key, start->second)) {
491 if (it != bucket.end()) {
495 bucket.emplace_back(hash, key_id);
505 if (it != bucket.end()) {
515 if (it != bucket.end()) {
516 it->second = new_key_id;
530 auto new_buckets_mem = alloc.allocate(new_buckets_count);
531 auto new_buckets =
new (new_buckets_mem)
bucket_t[new_buckets_count];
534 for (
auto&& [hash, key_index] :
m_buckets[i]) {
535 new_buckets[hash % new_buckets_count].emplace_back(hash, key_index);
Iterator class template for hash_array.
Definition template_hash_array.h:101
KeyID value_type
Definition template_hash_array.h:106
iterator()
Default constructor.
Definition template_hash_array.h:116
reference operator*()
Definition template_hash_array.h:151
iterator & operator++()
Definition template_hash_array.h:155
const value_type & constant_reference
Definition template_hash_array.h:108
iterator(const iterator &)=default
void increment()
Helper function to increment the iterator.
Definition template_hash_array.h:175
iterator(ref_t bucket, std::size_t bucket_item, ref_t bucket_end)
Constructor with parameters.
Definition template_hash_array.h:126
iterator & operator=(const iterator &)=default
ref_t m_ref
Definition template_hash_array.h:168
bool operator==(const iterator &other) const
Definition template_hash_array.h:149
iterator & operator=(iterator &&)=default
constant_reference operator*() const
Definition template_hash_array.h:153
std::size_t m_item
Definition template_hash_array.h:169
std::conditional_t< is_const, const value_type &, value_type & > reference
Definition template_hash_array.h:107
std::forward_iterator_tag iterator_tag
Definition template_hash_array.h:110
iterator(iterator &&)=default
iterator operator++(int)
Definition template_hash_array.h:160
void find_non_empty()
Definition template_hash_array.h:185
std::ptrdiff_t difference_type
Definition template_hash_array.h:111
ref_t m_bucket_end
Definition template_hash_array.h:170
std::conditional_t< is_const, const bucket_t *, bucket_t * > ref_t
Definition template_hash_array.h:103
Definition template_hash_array.h:83
template_hash_array(std::size_t bucket_count)
Constructor with bucket count.
Definition template_hash_array.h:209
value_t hash_key(const Key &key)
Definition template_hash_array.h:451
bucket_t::const_iterator bucket_const_iter
Definition template_hash_array.h:91
bool empty() const
Check if the hash_array is empty.
Definition template_hash_array.h:303
bool try_set(const key_t &key, const key_id_t &new_key_id, adapter_t adapter)
Try to set new key ID.
Definition template_hash_array.h:355
template_hash_array & operator=(const template_hash_array &other)
Copy assignment operator.
Definition template_hash_array.h:256
bool set(const key_t &key, const key_id_t &new_key_id, adapter_t adapter)
Definition template_hash_array.h:511
Allocator allocator_t
Definition template_hash_array.h:93
bool insert(const key_t &key, const key_id_t &key_id, adapter_t adapter)
Definition template_hash_array.h:485
template_hash_array(template_hash_array &&other)
Move constructor.
Definition template_hash_array.h:234
bucket_const_iter find_bucket_item(const key_t &key, const bucket_t &bucket, adapter_t adapter) const
Definition template_hash_array.h:472
std::size_t m_buckets_count
Definition template_hash_array.h:447
std::size_t value_t
Definition template_hash_array.h:87
ComptimeData comptime_t
Definition template_hash_array.h:94
iterator_t begin()
Get an iterator to the beginning of the hash_array.
Definition template_hash_array.h:413
template_hash_array & operator=(template_hash_array &&other)
Move assignment operator.
Definition template_hash_array.h:280
KeyAdapter adapter_t
Definition template_hash_array.h:92
void destroy()
Definition template_hash_array.h:553
const_iterator_t begin() const
Get a constant iterator to the beginning of the hash_array.
Definition template_hash_array.h:425
float m_max_load_factor
Definition template_hash_array.h:449
~template_hash_array()
Destructor.
Definition template_hash_array.h:248
void rehash_if_needed()
Definition template_hash_array.h:522
std::size_t bucket_count() const
Returns the number of buckets.
Definition template_hash_array.h:315
template_hash_array()
Default constructor.
Definition template_hash_array.h:199
std::size_t m_size
Definition template_hash_array.h:448
void remove(const key_t &key, adapter_t adapter)
Definition template_hash_array.h:501
const_iterator_t end() const
Get a constant iterator to the end of the hash_array.
Definition template_hash_array.h:431
void clear()
Clear all elements from the hash_array.
Definition template_hash_array.h:335
iterator_t find(const key_t &key, adapter_t adapter)
Finds an element in the hash table.
Definition template_hash_array.h:376
std::size_t size() const
Get the number of elements in the hash_array.
Definition template_hash_array.h:309
bucket_t::iterator bucket_iter
Definition template_hash_array.h:90
bucket_t * m_buckets
Definition template_hash_array.h:446
KeyID key_id_t
Definition template_hash_array.h:86
bucket_t & get_bucket(const key_t &key)
Definition template_hash_array.h:456
const_iterator_t cend() const
Get a constant iterator to the end of the hash_array.
Definition template_hash_array.h:443
const_iterator_t find(const key_t &key, adapter_t adapter) const
Finds an element in the hash table.
Definition template_hash_array.h:396
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
void clear_all_buckets()
Definition template_hash_array.h:546
Hash hash_t
Definition template_hash_array.h:89
bucket_iter find_bucket_item(const key_t &key, bucket_t &bucket, adapter_t adapter)
Definition template_hash_array.h:458
template_hash_array(const template_hash_array &other)
Copy constructor.
Definition template_hash_array.h:219
iterator_t end()
Get an iterator to the end of the hash_array.
Definition template_hash_array.h:419
Bucket bucket_t
Definition template_hash_array.h:88
float max_load_factor() const
Returns the maximum load factor.
Definition template_hash_array.h:321
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 template_hash_array.h:344
void erase(const key_t &key, adapter_t adapter)
Erases an element from the hash table.
Definition template_hash_array.h:367
Key key_t
Definition template_hash_array.h:85
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 template_hash_array.h:23
Concept to check if a type is a valid key adapter.
Definition template_hash_array.h:58
Definition comptime_map.h:11