125using namespace Aleph;
184 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
201 using Hash_Fct = std::function<size_t(
const Key &)>;
216 Bucket() noexcept(
std::is_nothrow_default_constructible_v<Key>)
228 [[nodiscard]]
constexpr bool valid() const noexcept
237 std::string status_str;
240 case EMPTY: status_str =
"EMPTY";
242 case BUSY: status_str =
"BUSY";
244 case DELETED: status_str =
"DELETED";
247 std::string probe_type_str;
250 case NO_PROBED: probe_type_str =
"NO_PROBED";
259 return out <<
"Bucket at " << &bucket <<
'\n'
260 <<
"status = " << status_str <<
'\n'
261 <<
"probe_type = " << probe_type_str <<
'\n'
284 assert(bucket.status !=
BUSY);
289 bucket.status =
BUSY;
290 bucket.probe_type = probe_type;
291 ++bucket.probe_counter;
298 assert(bucket->status ==
BUSY or bucket->status ==
DELETED);
300 --bucket->probe_counter;
301 if (bucket->probe_counter == 0)
304 assert(bucket->status ==
DELETED);
306 bucket->status =
EMPTY;
342 if (
table ==
nullptr)
345 const auto begin =
reinterpret_cast<std::uintptr_t
>(&
table[0]);
346 const auto end =
reinterpret_cast<std::uintptr_t
>(&
table[
len]);
347 const auto addr =
reinterpret_cast<std::uintptr_t
>(bucket);
349 if (addr < begin or addr >=
end)
352 const auto offset_with_base =
353 static_cast<std::ptrdiff_t
>(addr -
begin);
354 return offset_with_base %
sizeof(*bucket) == 0;
360 return bucket - &
table[0];
366 if (
table ==
nullptr)
369 const auto addr =
reinterpret_cast<std::uintptr_t
>(&key);
370 const auto table_begin =
reinterpret_cast<std::uintptr_t
>(&
table[0]);
371 const auto table_end =
reinterpret_cast<std::uintptr_t
>(&
table[
len]);
373 if (addr < table_begin or addr >= table_end)
376 constexpr auto key_offset = offsetof(
Bucket, key);
377 return ((addr - table_begin - key_offset) %
sizeof(
Bucket)) == 0;
389 template <
typename RecordBucket>
391 RecordBucket && record_bucket)
noexcept
394 std::forward<RecordBucket>(record_bucket));
403 template <
typename RecordBucket>
405 RecordBucket && record_bucket)
noexcept
407 assert(bucket->status ==
BUSY);
413 if (&
table[i_fst] == bucket)
420 if (&
table[i_snd] == bucket)
423 record_bucket(&
table[i_fst]);
428 record_bucket(&
table[i_fst]);
430 record_bucket(&
table[i_snd]);
436 record_bucket(&
table[i]);
450 const auto base =
reinterpret_cast<std::uintptr_t
>(rec);
472 std::swap(
table, other.table);
473 std::swap(
hash_fct, other.hash_fct);
476 std::swap(
N, other.N);
477 std::swap(
len, other.len);
487 const float lower,
const float upper,
const bool resize)
529 Hash_Fct_Ptr first_hash_fct = Aleph::dft_hash_ptr_fct<Key>,
555 template <
typename H1,
typename H2>
556 requires (std::is_convertible_v<H1, Hash_Fct_Ptr>
557 and std::is_convertible_v<H2, Hash_Fct_Ptr>
558 and (std::is_class_v<std::remove_cvref_t<H1>>
559 or std::is_class_v<std::remove_cvref_t<H2>>))
571 if (
table !=
nullptr)
590 assert(
table !=
nullptr);
617 other.table =
nullptr;
620 other.deleted_buckets = 0;
657 [[nodiscard]] Key *
search(
const Key & key)
noexcept
659 return const_cast<Key *
>(std::as_const(*this).search(key));
664 [[nodiscard]]
const Key *
search(
const Key & key)
const noexcept
666 if (
len == 0) [[unlikely]]
670 if (
table[i_fst].status ==
EMPTY) [[unlikely]]
676 assert(
table[i_fst].probe_counter > 0);
688 if (i_fst == i_snd) [[unlikely]]
697 assert(
table[i_snd].probe_counter > 0);
713 __builtin_prefetch(&
table[(i + 1 <
len) ? i + 1 : 0], 0, 1);
714 switch (
table[i].status)
717 assert(
table[i].probe_counter == 0);
720 assert(
table[i].probe_counter > 0);
721 if (
cmp(
table[i].key, key)) [[unlikely]]
728 assert(
table[i].probe_counter > 0);
730 default:
AH_ERROR(
"ODhashTable search: inconsistent bucket status");
763 second_hash_fct = std::move(old);
806 Bucket *candidate =
nullptr;
807 size_t candidate_idx = 0;
812 candidate = &
table[i_fst];
817 candidate = &
table[i_snd];
822 for (
size_t c = 0; c <
len; ++c)
826 __builtin_prefetch(&
table[(i + 1 <
len) ? i + 1 : 0], 0, 1);
834 if (candidate_idx == 0)
836 if (candidate_idx == 1)
845 for (
size_t k = 0;
k < candidate_idx - 2; ++
k)
857 for (
size_t k = 0;
k < c; ++
k)
870 candidate = &
table[i];
871 candidate_idx = c + 2;
878 if (candidate_idx == 0)
880 if (candidate_idx == 1)
888 for (
size_t k = 0;
k < candidate_idx - 2; ++
k)
905 Bucket *candidate =
nullptr;
906 size_t candidate_linear_steps = 0;
909 switch (
table[i_fst].status)
914 candidate = &
table[i_fst];
919 return {&
table[i_fst],
true};
924 switch (
table[i_snd].status)
932 if (candidate ==
nullptr)
934 candidate = &
table[i_snd];
940 return {&
table[i_snd],
true};
945 const bool candidate_at_first = (candidate == &
table[i_fst]);
946 const bool candidate_at_second = (candidate == &
table[i_snd]);
949 for (
size_t c = 0; c <
len; ++c)
953 __builtin_prefetch(&
table[(i + 1 <
len) ? i + 1 : 0], 0, 1);
954 switch (
table[i].status)
958 return {&
table[i],
true};
961 if (candidate ==
nullptr)
963 candidate = &
table[i];
964 candidate_linear_steps = c + 1;
972 if (candidate_at_first)
977 if (candidate_at_second)
984 for (
size_t k = 0;
k < candidate_linear_steps - 1; ++
k)
996 for (
size_t k = 0;
k < c; ++
k)
1003 default:
AH_ERROR(
"ODhashTable: Invalid bucket status");
1011 if (candidate_at_first)
1016 if (candidate_at_second)
1022 for (
size_t k = 0;
k < candidate_linear_steps - 1; ++
k)
1030 return {
nullptr,
false};
1055 <<
"Bucket containing key is not BUSY";
1077 <<
"Key not in hash table";
1082 if (i_fst == i_snd) [[unlikely]]
1083 goto linear_probe_remove;
1097 <<
"Key not in hash table";
1099 linear_probe_remove:
1102 for (
size_t c = 0; c <
len; ++c)
1106 __builtin_prefetch(&
table[(i + 1 <
len) ? i + 1 : 0], 0, 1);
1108 switch (
table[i].status)
1115 if (
cmp(
table[i].key, key)) [[unlikely]]
1148 size_t num_busy = 0, num_deleted = 0, num_empty = 0;
1149 size_t max_len = std::numeric_limits<size_t>::min();
1150 for (
size_t i = 0; i <
len; ++i)
1151 switch (
table[i].status)
1162 assert(
table[i_fst].probe_counter > 0);;
1171 assert(
table[i_snd].probe_counter > 0);;
1184 max_len = std::max(max_len,
count);
1198 float avg = 0,
sum = 0;
1199 for (
size_t i = 0; i < lens.
size(); ++i)
1210 for (
size_t i = 0; i < lens.
size(); ++i)
1212 const float s = i - avg;
1213 var += lens(i) * s * s;
1219 stats.num_busy = num_busy;
1220 stats.num_deleted = num_deleted;
1221 stats.num_empty = num_empty;
1222 std::swap(lens,
stats.lens);
1225 stats.max_len = max_len;
1232 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
#define AH_ERROR(...)
Print an error message (always enabled).
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Iterator wrapper for C++ raw arrays and circular buffers.
size_t size_t int32_t * out
size_t size() const noexcept
Return the current dimension of array.
Number Theoretic Transform over Z / MOD Z.
Open addressing hash table with double hashing collision resolution.
Bucket * key_in_table_to_bucket(const Key &key) noexcept
Converts a key reference inside the table to its containing bucket.
static Bucket * key_to_bucket(Key *rec) noexcept
typename OhashCommon< ODhashTable< Key, Cmp >, Key >::Stats Stats
void release_bucket(Bucket *bucket, const Key &key, RecordBucket &&record_bucket) noexcept
Frees bucket, which was reached with the probe sequence of key, and walks that sequence back: every b...
const Key * search(const Key &key) const noexcept
searches the table for the key.
size_t bucket_to_index(Bucket *bucket) const noexcept
void deallocate_bucket_with_record(Bucket *bucket, RecordBucket &&record_bucket) noexcept
Template version of deallocate_bucket that accepts a callback to record modified buckets before decre...
void set_second_hash_fct(Hash_Fct fct)
Sets the hash function of the second probe.
bool is_key_in_table(const Key &key) const noexcept
Returns true if the key reference points to a key inside the table.
size_t(*)(const Key &) Hash_Fct_Ptr
size_t index_backward(size_t &i) const noexcept
void decrease_probe_counter(Bucket *bucket) noexcept
constexpr Hash_Fct get_second_hash_fct() const noexcept
void remove_without_shrinking(const Key &key)
~ODhashTable()
Free the whole hash table.
std::function< size_t(const Key &)> Hash_Fct
void swap(ODhashTable &other) noexcept
Swaps this table with other in constant time.
ODhashTable(const size_t l, Hash_Fct fst_hash_fct, Hash_Fct snd_hash_fct, Cmp cpm_fct, const float lower, const float upper, const bool resize)
void undo_allocate_bucket(Bucket *bucket, const Key &key) noexcept
Undoes what allocate_bucket() or hard_allocate_bucket() did when they reserved bucket for key,...
constexpr Cmp & get_compare() noexcept
ODhashTable(const ODhashTable &other)
Copy constructor.
void remove_bucket(Bucket *bucket)
Delete the record from the table.
bool is_valid_bucket(Bucket *bucket) const noexcept
ODhashTable(ODhashTable &&other) noexcept
Move constructor.
void remove(const Key &key)
Remove a key from the hash table.
Bucket * allocate_bucket(const Key &key) noexcept
ODhashTable(size_t len, H1 &&first_hash_fct, H2 &&second_hash_fct, Cmp cmp, float lower_alpha, float upper_alpha, bool with_resize)
Builds a table when a hash function is a lambda without captures.
size_t index_forward(size_t &i) const noexcept
static void update_stat_len(DynArray< size_t > &lens, size_t i)
void deallocate_bucket(Bucket *bucket) noexcept
constexpr const Cmp & get_compare() const noexcept
ODhashTable(size_t len=Primes::DefaultPrime, Hash_Fct_Ptr first_hash_fct=Aleph::dft_hash_ptr_fct< Key >, Hash_Fct_Ptr second_hash_fct=Aleph::snd_hash_ptr_fct< Key >, Cmp cmp=Cmp(), float lower_alpha=hash_default_lower_alpha, float upper_alpha=hash_default_upper_alpha, bool with_resize=true)
Key * search(const Key &key) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Bucket * allocate_bucket(Bucket &bucket, unsigned char probe_type) noexcept
void set_second_hash_fct(Hash_Fct_Ptr fct)
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::tuple< Bucket *, bool > hard_allocate_bucket(const Key &key) noexcept
ODhashTable & operator=(const ODhashTable &other)
Copy assignment.
Common methods to the Aleph-w ( ) containers.
and
Insert a new element into the container by constructing it from the given args.
Common sequential searching methods on containers.
CRTP mixin providing common operations for open addressing hash tables.
size_t resize(size_t new_size)
Resizes the hash table to a new capacity.
void copy_from_table(const HashTbl &other)
Copies all entries from another hash table.
constexpr bool contains(const Key &key) const noexcept
Alias for has().
void rehash()
Rebuilds the hash table with the same capacity.
void shrink_after_removal() noexcept
Shrinks a table that resizes itself, after a removal took its load factor under the lower threshold.
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
Equivalence relation constraint for equality comparators.
Doubly linked circular list implementation.
void swap_compare(Cmp &a, Cmp &b) noexcept
Swaps the comparators of two hash tables.
Common hash table utilities and base classes.
#define OHASH_COMMON(class_name)
Standard hash functions for Aleph types.
Common operations for open addressing hash tables (CRTP mixin).
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
Main namespace for Aleph-w library functions.
size_t snd_hash_fct(const Key &key) noexcept
Secondary default hash: different distribution from dft_hash_fct.
const float hash_default_upper_alpha
const float hash_default_lower_alpha
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
const unsigned long DefaultPrime
Default prime number used when no specific size is requested.
Prime number utilities for hash tables and mathematical operations.
Bucket() noexcept(std::is_nothrow_default_constructible_v< Key >)
unsigned int probe_counter
constexpr bool valid() const noexcept
friend std::ostream & operator<<(std::ostream &out, const Bucket &bucket)
Generic traversal of the container through its iterator.
Lazy and scalable dynamic array implementation.