81# define LINBUCKET_BODY(BUCKETNAME) \
87 BUCKETNAME(const BUCKETNAME & bucket) \
88 : Dnode<Key>(bucket) {} \
92 BUCKETNAME(const Key & key) \
93 : Dnode<Key>(key) {} \
95 Key & get_key() noexcept { return this->get_data(); } \
97 Dlink * get_link() noexcept { return &link; } \
99 DLINK_TO_BASE(BUCKETNAME, link);
109 template <
typename Key>
122 template <
typename Key>
156 template <
typename Key,
template <
class>
class BucketType,
158 requires EqualityComparator<Cmp, Key>
160 :
public HashStats<GenLinearHashTable<Key, BucketType, Cmp>>
168 using Hash_Fct = std::function<size_t(
const Key &)>;
229 const auto i = hash %
M;
230 return i <
p ? hash %
MM : i;
243 if (src_list_ptr !=
nullptr)
249 for (
BucketItor it(*src_list_ptr); it.has_curr(); )
255 const Key & key = bucket->get_key();
260 if (tgt_list_ptr ==
nullptr)
261 tgt_list_ptr = &
table.touch(
MP);
266 tgt_list_ptr->
append(bucket);
272 if (tgt_list_ptr !=
nullptr)
294 for (
float alpha = (1.0*
N)/
MP; alpha <= lower_alpha and MP >
len;
302 if (src_list_ptr !=
nullptr and not src_list_ptr->
is_empty())
303 tgt_list_ptr = &
table.touch(new_p);
317 if (src_list_ptr !=
nullptr)
319 if (tgt_list_ptr !=
nullptr)
321 const bool tgt_was_busy = not tgt_list_ptr->
is_empty();
344 <<
"LinearHashTable::set_hash_fct(): the table is not empty";
352 <<
"LinearHashTable::set_hash_fct(): the table is not empty";
368 float __lower_alpha,
float __upper_alpha,
369 bool __remove_all_buckets,
bool __with_resize)
381 <<
"upper alpha must be greater than lower alpha";
435 table.swap(other.table);
437 std::swap(
hash_fct, other.hash_fct);
439 std::swap(
M, other.M);
440 std::swap(
N, other.N);
446 std::swap(
p, other.p);
447 std::swap(
l, other.l);
448 std::swap(
MP, other.MP);
449 std::swap(
MM, other.MM);
450 std::swap(
len, other.len);
468 bucket->get_link()->del();
525 template <
typename SearchKey>
529 for (
BucketItor it(*list); it.has_curr(); it.next_ne())
532 if (
cmp (key, bucket->get_key()))
576 template <
typename HashFunc,
typename SearchKey>
578 const SearchKey & search_key)
const noexcept
581 if (list ==
nullptr or list->
is_empty())
590 const size_t &
size() const noexcept {
return N; }
656 assert(bucket !=
nullptr);
657 assert(
search(bucket->get_key()) == bucket);
663 if (
next->is_empty())
694 bucket->get_link()->del();
700 for (
size_t i = 0; i <
MP; ++i)
702 std::cout <<
"table[" << i <<
"] = [ ";
709 for (
BucketItor it(list); it.has_curr(); it.next_ne())
712 const Key & key = bucket->get_key();
713 std::cout << key <<
",";
716 std::cout <<
"]" <<
'\n';
804template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
826 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_length_error_if(C)
Throws std::length_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
void next_ne() noexcept
Move the iterator one position backward guaranteeing no exception.
Dlink * get_curr() const
Return the current node of iterator.
void next()
Move the iterator one position forward.
Dlink * del()
Remove from the list the current node and move the iterator one position forward.
void prev()
Move the iterator one position backward.
Dlink * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Doubly linked circular list node.
constexpr bool is_empty() const noexcept
Return true if this (as header node) is empty.
void append(Dlink *node) noexcept
Insert node before this.
void swap(Dlink *link) noexcept
Swap this with list whose header is link.
Dlink * del() noexcept
Remove this from the list. this must not be a header node.
void concat_list(Dlink *head) noexcept
Concatenate list head to list this
Dlink * remove_first_ne() noexcept
See remove_next().
Iterator on a list of Dnode objects.
Node belonging to a double circular linked list with header node.
GenLinearHashTable * hash_table
Iterator() noexcept
Instantiate an empty iterator.
Bucket * get_curr()
Return the current bucket.
Bucket * get_curr_ne() noexcept
Bucket * Item_Type
The element type returned by get_curr().
Iterator(const GenLinearHashTable &table) noexcept
Instantiate an iterator over the hash table.
long get_pos() const noexcept
Generic linear hash table.
Bucket * search_with_custom_hash(const HashFunc &hash_func, const SearchKey &search_key) const noexcept
Searches for a key of another type, without building a Key.
const size_t & capacity() const noexcept
Returns the table capacity.
void swap(GenLinearHashTable &other) noexcept
Swaps this table with other in constant time.
std::function< size_t(const Key &)> Hash_Fct
The hash function type.
DynArray< BucketList > table
void set_hash_fct(Hash_Fct_Ptr fct)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void clear() noexcept
Empties the container.
static size_t divide_by_two(size_t n) noexcept
Bucket * remove_bucket(Bucket *bucket) noexcept
Cmp & get_compare() noexcept
const size_t & size() const noexcept
Returns the number of elements in the table.
bool is_empty() const noexcept
return true is table is empty
size_t busy_slots_counter
GenLinearHashTable(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=hash_default_upper_alpha, bool remove_all_buckets=true, bool with_resize=true)
Instantiate a generic linear hash table.
Bucket * search_in_bucket_list(BucketList *list, const SearchKey &key) const
void empty() noexcept
Empties the table: frees every bucket and gives the table its initial size.
typename Dnode< Key >::Iterator BucketItor
static size_t multiply_by_two(size_t n) noexcept
const size_t & expansions() const noexcept
Returns the expansion level performed on the table.
size_t list_index(const size_t hash) const noexcept
size_t call_hash_fct(const Key &key) const
float current_alpha() const noexcept
return the current table load
Bucket * remove(Bucket *bucket) noexcept
Removes bucket from the table, without freeing it.
BucketType< Key > Bucket
The bucket type.
GenLinearHashTable(const GenLinearHashTable &)=delete
Not copyable: the copy that the compiler would write copies the links of the lists,...
const size_t & busy_slots() const noexcept
Returns the number of busy slots in the table.
bool contains(const Key &key) const noexcept
Checks if a key exists in the table.
Bucket * insert(Bucket *bucket)
Insert bucket in the table.
Hash_Fct get_hash_fct() const noexcept
size_t(*)(const Key &) Hash_Fct_Ptr
Bucket * search_or_insert(Bucket *bucket)
GenLinearHashTable & operator=(const GenLinearHashTable &)=delete
Not copyable, for the same reason as the copy constructor.
GenLinearHashTable(size_t __len, Hash_Fct __hash_fct, Cmp __cmp, float __lower_alpha, float __upper_alpha, bool __remove_all_buckets, bool __with_resize)
const Cmp & get_compare() const noexcept
void set_hash_fct(Hash_Fct fct)
Sets the hash function of an empty table.
Bucket * search(const Key &key) const noexcept
Search for key in the linear hash table.
size_t resize(size_t) noexcept
Provided for generic programming compatibility.
Bucket with virtual destructor for a hash table with collision resolution by separate chaining.
virtual ~LinHashBucketVtl()
virtual destructor.
Bucket without virtual destructor for a hash table with collision resolution by separate chaining.
Number Theoretic Transform over Z / MOD Z.
CRTP mixin providing statistics and configuration for chained hash tables.
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.
Standard hash functions for Aleph types.
Common operations for open addressing hash tables (CRTP mixin).
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
const float hash_default_upper_alpha
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
const float hash_default_lower_alpha
const unsigned long DefaultPrime
Default prime number used when no specific size is requested.
Prime number utilities for hash tables and mathematical operations.
Doubly linked list node with typed data.
Lazy and scalable dynamic array implementation.
#define LINBUCKET_BODY(BUCKETNAME)