115using namespace Aleph;
166 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
184 using Hash_Fct = std::function<size_t(
const Key &)>;
205 const auto base =
reinterpret_cast<std::uintptr_t
>(rec);
228 if (
table ==
nullptr)
231 const auto begin =
reinterpret_cast<std::uintptr_t
>(&
table[0]);
232 const auto end =
reinterpret_cast<std::uintptr_t
>(&
table[
len]);
233 const auto addr =
reinterpret_cast<std::uintptr_t
>(bucket);
235 if (addr < begin or addr >=
end)
238 const auto offset_with_base =
239 static_cast<std::ptrdiff_t
>(addr -
begin);
241 return offset_with_base %
sizeof(*bucket) == 0;
256 const float l_alpha,
const float u_alpha,
const bool resize)
310 template <
typename H>
332 template <
typename H1,
typename H2>
333 requires (std::is_constructible_v<Hash_Fct, H1>
334 and std::is_constructible_v<Hash_Fct, H2>
347 if (
table !=
nullptr)
364 std::swap(
table, other.table);
365 std::swap(
N, other.N);
366 std::swap(
len, other.len);
368 std::swap(
hash_fct, other.hash_fct);
409 :
table(other.table),
N(other.N),
len(other.len),
414 other.table =
nullptr;
417 other.deleted_buckets = 0;
449 [[nodiscard]] Key *
search(
const Key & key)
noexcept
451 return const_cast<Key *
>(std::as_const(*this).search(key));
456 [[nodiscard]]
const Key *
search(
const Key &key)
const noexcept
458 if (
len == 0) [[unlikely]]
462 for (
size_t c = 0; c <
len; ++c)
465 __builtin_prefetch(&
table[(i + 1 <
len) ? i + 1 : 0], 0, 1);
471 return &
table[i].key;
494 Bucket * first_deleted =
nullptr;
497 for (
size_t c = 0; c <
len; ++c)
500 __builtin_prefetch(&
table[(i + 1 <
len) ? i + 1 : 0], 0, 1);
503 if (b.status ==
EMPTY) [[likely]]
504 return occupy(first_deleted !=
nullptr ? first_deleted : &b);
505 if (b.status ==
BUSY)
507 if (
cmp(key, b.key)) [[unlikely]]
512 if (first_deleted ==
nullptr)
520 if (first_deleted !=
nullptr) [[unlikely]]
521 return occupy(first_deleted);
532 Bucket * first_deleted =
nullptr;
535 for (
size_t c = 0; c <
len; ++c)
538 __builtin_prefetch(&
table[(i + 1 <
len) ? i + 1 : 0], 0, 1);
541 if (b.status ==
EMPTY) [[likely]]
542 return {
occupy(first_deleted !=
nullptr ? first_deleted : &b),
false};
543 else if (b.status ==
BUSY)
550 if (first_deleted ==
nullptr)
558 if (first_deleted !=
nullptr) [[unlikely]]
559 return {
occupy(first_deleted),
false};
561 return {
nullptr,
false};
565 [[nodiscard]]
size_t prev_index(
const size_t i)
const noexcept
567 return (i == 0) ?
len - 1 : i - 1;
571 [[nodiscard]]
size_t next_index(
const size_t i)
const noexcept
573 return (i + 1 ==
len) ? 0 : i + 1;
612 const auto idx =
static_cast<size_t>(bucket - &
table[0]);
632 const auto idx =
static_cast<size_t>(bucket - &
table[0]);
646 if (displacement >=
steps)
654 bucket->status =
EMPTY;
677 for (
size_t c = 0; c <
len; ++c)
680 __builtin_prefetch(&
table[(i + 1 <
len) ? i + 1 : 0], 0, 1);
710 size_t num_deleted = 0;
711 size_t num_empty = 0;
712 size_t max_len = std::numeric_limits<size_t>::min();
713 for (
size_t i = 0; i <
len; ++i)
714 switch (
table[i].status)
732 max_len = std::max(max_len,
count);
748 for (
size_t i = 0; i < lens.
size(); ++i)
759 for (
size_t i = 0; i < lens.
size(); ++i)
761 const float s = i - avg;
762 var += lens(i) * s * s;
768 stats.num_busy = num_busy;
769 stats.num_deleted = num_deleted;
770 stats.num_empty = num_empty;
771 std::swap(lens,
stats.lens);
774 stats.max_len = max_len;
780 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.
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
size_t size() const noexcept
Return the current dimension of array.
Number Theoretic Transform over Z / MOD Z.
Open addressing hash table with linear probing collision resolution.
OLhashTable(size_t len, Hash_Fct hash_fct, Hash_Fct, Cmp cmp, float lower_alpha, float upper_alpha, bool with_resize)
~OLhashTable()
Release all occupied memory.
OLhashTable(size_t len, Hash_Fct_Ptr hash_fct, Hash_Fct_Ptr, Cmp cmp, float lower_alpha, float upper_alpha, bool with_resize)
Bucket * occupy(Bucket *bucket) noexcept
Gives bucket, empty or the one of a removed key, to a new key.
std::tuple< Bucket *, bool > hard_allocate_bucket(const Key &key) noexcept
size_t next_index(const size_t i) const noexcept
Index of next bucket (handles wrap-around)
std::function< size_t(const Key &)> Hash_Fct
OLhashTable(OLhashTable &&other) noexcept
Move constructor.
OLhashTable(size_t len, Hash_Fct hash_fct, Hash_Fct_Ptr, Cmp cmp, float lower_alpha, float upper_alpha, bool with_resize)
Constructor with two hash functions for metaprogramming compatibility with ODhashTable type.
void cleanup_deleted_chain(size_t idx) noexcept
Cleanup DELETED entries that are at the end of collision chains.
static void update_stat_len(DynArray< size_t > &lens, size_t i)
OLhashTable(const OLhashTable &other)
Copy constructor.
bool is_valid_bucket(Bucket *bucket) const noexcept
OLhashTable(size_t len=Primes::DefaultPrime, Hash_Fct_Ptr hash_fct=Aleph::dft_hash_ptr_fct< Key >, Cmp cmp=Cmp(), float lower_alpha=hash_default_lower_alpha, float upper_alpha=0.70f, bool with_resize=true)
size_t(*)(const Key &) Hash_Fct_Ptr
constexpr const Cmp & get_compare() const noexcept
OLhashTable(size_t len, H1 &&hash_fct, H2 &&, Cmp cmp, float lower_alpha, float upper_alpha, bool with_resize)
Builds a table from two hash functions when one of them is a lambda without captures.
Bucket * allocate_bucket(const Key &key) noexcept
void undo_allocate_bucket(Bucket *bucket, const Key &) noexcept
Undoes what allocate_bucket() or hard_allocate_bucket() did when they reserved bucket for a key that ...
void deallocate_bucket(Bucket *bucket) noexcept
Removes the key of bucket, a busy bucket of the table.
constexpr Cmp & get_compare() noexcept
OLhashTable(size_t len, H &&hash_fct, Cmp cmp, float lower_alpha, float upper_alpha, bool with_resize)
Builds a table whose hash function is a lambda without captures.
Key * search(const Key &key) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
void swap(OLhashTable &other) noexcept
Swaps this table with other in constant time.
OLhashTable(const size_t l, Hash_Fct hash_f, Cmp cmp_f, const float l_alpha, const float u_alpha, const bool resize)
Instantiate a hash table with hash function __hash_fct and dimension len.
OLhashTable & operator=(OLhashTable &&other) noexcept
const Key * search(const Key &key) const noexcept
Finds the key and returns the associated record if key is find inside the table; otherwise,...
size_t prev_index(const size_t i) const noexcept
Index of previous bucket (handles wrap-around)
static Bucket * key_to_bucket(Key *rec) noexcept
OLhashTable & operator=(const OLhashTable &other)
Copy assignment.
typename OhashCommon< OLhashTable< Key, Cmp >, Key >::Stats Stats
void remove(const Key &key)
Remove the key referenced by key.
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 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.
A class type that converts to the function pointer type Ptr.
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.
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 >)
Generic traversal of the container through its iterator.