Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::GenLhashTable< Key, BucketType, Cmp > Class Template Reference

Generic hash table with collision resolution by separate chaining. More...

#include <tpl_lhash.H>

Inheritance diagram for Aleph::GenLhashTable< Key, BucketType, Cmp >:
[legend]
Collaboration diagram for Aleph::GenLhashTable< Key, BucketType, Cmp >:
[legend]

Classes

class  Iterator
 Iterator over a GenLhashTable hash table. More...
 

Public Types

using Bucket = BucketType
 
using Hash_Fct = std::function< size_t(const Key &)>
 
using Hash_Fct_Ptr = size_t(*)(const Key &)
 
using Key_Type = Key
 
using Item_Type = Key
 

Public Member Functions

Cmp & get_compare ()
 
const Cmp & get_compare () const
 
 GenLhashTable (size_t table_size=Primes::DefaultPrime, Hash_Fct_Ptr hash_fct=Aleph::dft_hash_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 hash table.
 
void swap (GenLhashTable &other) noexcept
 
 GenLhashTable (const GenLhashTable &)=delete
 
GenLhashTableoperator= (const GenLhashTable &)=delete
 
 GenLhashTable (GenLhashTable &&other) noexcept
 
GenLhashTableoperator= (GenLhashTable &&other) noexcept
 
void empty () noexcept
 Empties the hash table; frees memory of all buckets.
 
Hash_Fct get_hash_fct () const noexcept
 
void set_hash_fct (Hash_Fct fct) noexcept
 Set the internal hash function.
 
void set_hash_fct (Hash_Fct_Ptr fct) noexcept
 
float current_alpha () const noexcept
 return the current table load
 
Bucketinsert (Bucket *bucket)
 Inserts bucket into the table and returns its address if the key is not already in the table; otherwise returns nullptr.
 
Bucketsearch_or_insert (Bucket *bucket)
 
Bucketsearch (const Key &key) const noexcept
 Search in the table for a bucket with key.
 
Bucketremove (Bucket *bucket) noexcept
 Removes bucket from the table.
 
size_t resize (const size_t new_size)
 Resizes the hash table to new_size and re-locates keys.
 
virtual ~GenLhashTable ()
 Frees the table memory and, if remove_all_buckets is true (specified in the constructor), also frees memory of all buckets.
 
Bucketsearch_next (Bucket *bucket) const
 Returns the next bucket that collides with bucket.
 
const size_tcapacity () const noexcept
 Returns the table capacity.
 
const size_tsize () const noexcept
 Returns the number of elements contained in the table.
 
const size_tget_num_busy_slots () const noexcept
 Returns the number of occupied entries in the array.
 
constexpr bool is_empty () const noexcept
 
- Public Member Functions inherited from HashStats< GenLhashTable< Key, BucketType, Cmp > >
Stats stats () const
 Computes statistics about chain length distribution.
 
void print_stats (const Stats &stats) const
 Prints statistics to standard output.
 
void set_upper_alpha (float __upper_alpha)
 Sets the upper load factor threshold.
 
void set_lower_alpha (float __lower_alpha)
 Sets the lower load factor threshold.
 
constexpr float get_lower_alpha () const noexcept
 Returns the lower load factor threshold.
 
constexpr float get_upper_alpha () const noexcept
 Returns the upper load factor threshold.
 

Protected Member Functions

 GenLhashTable (const size_t table_size, Hash_Fct hash_f, Cmp cpm_fct, const float lower, const float upper, const bool remove_all, const bool resize)
 
template<typename HashFunc , typename SearchKey >
Bucketsearch_with_custom_hash (HashFunc hash_func, const SearchKey &search_key) const noexcept
 Helper method for derived classes to perform custom heterogeneous searches.
 

Protected Attributes

Hash_Fct hash_fct
 
size_t len
 
Cmp cmp
 
float lower_alpha
 
float upper_alpha
 

Private Types

using BucketList = Dnode< Key >
 
using BucketItor = typename Dnode< Key >::Iterator
 
using Node = Dnode< Key >
 

Private Member Functions

Bucketsearch_in_bucket_list (BucketList &list, const Key &key) const
 
Bucketremove_bucket (Bucket *bucket) noexcept
 

Private Attributes

BucketListtable
 
size_t N
 
size_t busy_slots_counter
 
bool remove_all_buckets
 
bool with_resize
 

Friends

class HashStats< GenLhashTable< Key, BucketType, Cmp > >
 

Detailed Description

template<typename Key, class BucketType, class Cmp>
class Aleph::GenLhashTable< Key, BucketType, Cmp >

Generic hash table with collision resolution by separate chaining.

The GenLhashTable type implements a generic hash table with collision resolution by separate chaining, where the bucket type is a type parameter.

Normally, this is intended as the back-end of the LhashTable and LhashTableVtl types, which are fundamentally the same, except that they already define their bucket classes without or with a virtual destructor.

Parameters
Keyindexing key type of the table.
BucketTypebucket type.
Cmpkey comparison class.
See also
LhashTable LhashTableVtl

Definition at line 150 of file tpl_lhash.H.

Member Typedef Documentation

◆ Bucket

Definition at line 155 of file tpl_lhash.H.

◆ BucketItor

template<typename Key , class BucketType , class Cmp >
using Aleph::GenLhashTable< Key, BucketType, Cmp >::BucketItor = typename Dnode<Key>::Iterator
private

Definition at line 170 of file tpl_lhash.H.

◆ BucketList

template<typename Key , class BucketType , class Cmp >
using Aleph::GenLhashTable< Key, BucketType, Cmp >::BucketList = Dnode<Key>
private

Definition at line 169 of file tpl_lhash.H.

◆ Hash_Fct

template<typename Key , class BucketType , class Cmp >
using Aleph::GenLhashTable< Key, BucketType, Cmp >::Hash_Fct = std::function<size_t(const Key &)>

Definition at line 157 of file tpl_lhash.H.

◆ Hash_Fct_Ptr

template<typename Key , class BucketType , class Cmp >
using Aleph::GenLhashTable< Key, BucketType, Cmp >::Hash_Fct_Ptr = size_t (*)(const Key &)

Definition at line 159 of file tpl_lhash.H.

◆ Item_Type

template<typename Key , class BucketType , class Cmp >
using Aleph::GenLhashTable< Key, BucketType, Cmp >::Item_Type = Key

Definition at line 163 of file tpl_lhash.H.

◆ Key_Type

template<typename Key , class BucketType , class Cmp >
using Aleph::GenLhashTable< Key, BucketType, Cmp >::Key_Type = Key

Definition at line 161 of file tpl_lhash.H.

◆ Node

template<typename Key , class BucketType , class Cmp >
using Aleph::GenLhashTable< Key, BucketType, Cmp >::Node = Dnode<Key>
private

Definition at line 171 of file tpl_lhash.H.

Constructor & Destructor Documentation

◆ GenLhashTable() [1/4]

template<typename Key , class BucketType , class Cmp >
Aleph::GenLhashTable< Key, BucketType, Cmp >::GenLhashTable ( const size_t  table_size,
Hash_Fct  hash_f,
Cmp  cpm_fct,
const float  lower,
const float  upper,
const bool  remove_all,
const bool  resize 
)
inlineprotected

◆ GenLhashTable() [2/4]

template<typename Key , class BucketType , class Cmp >
Aleph::GenLhashTable< Key, BucketType, Cmp >::GenLhashTable ( size_t  table_size = Primes::DefaultPrime,
Hash_Fct_Ptr  hash_fct = Aleph::dft_hash_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 
)
inline

Instantiate a generic hash table.

Parameters
[in]hash_fcthash function.
cmpkey comparison functor.
[in]lower_alphalower load factor.
[in]upper_alphaupper load factor.
[in]table_sizetable size.
[in]remove_all_bucketsif true, then all buckets are freed when the destructor is invoked.
with_resizeif true, the table is resized when the load factor exceeds upper_alpha.
Exceptions
bad_allocif there is not enough memory to allocate the table.

Definition at line 219 of file tpl_lhash.H.

◆ GenLhashTable() [3/4]

template<typename Key , class BucketType , class Cmp >
Aleph::GenLhashTable< Key, BucketType, Cmp >::GenLhashTable ( const GenLhashTable< Key, BucketType, Cmp > &  )
delete

◆ GenLhashTable() [4/4]

template<typename Key , class BucketType , class Cmp >
Aleph::GenLhashTable< Key, BucketType, Cmp >::GenLhashTable ( GenLhashTable< Key, BucketType, Cmp > &&  other)
inlinenoexcept

Definition at line 246 of file tpl_lhash.H.

References Aleph::maps().

◆ ~GenLhashTable()

template<typename Key , class BucketType , class Cmp >
virtual Aleph::GenLhashTable< Key, BucketType, Cmp >::~GenLhashTable ( )
inlinevirtual

Frees the table memory and, if remove_all_buckets is true (specified in the constructor), also frees memory of all buckets.

Definition at line 475 of file tpl_lhash.H.

References Aleph::GenLhashTable< Key, BucketType, Cmp >::empty(), Aleph::GenLhashTable< Key, BucketType, Cmp >::remove_all_buckets, and Aleph::GenLhashTable< Key, BucketType, Cmp >::table.

Member Function Documentation

◆ capacity()

template<typename Key , class BucketType , class Cmp >
const size_t & Aleph::GenLhashTable< Key, BucketType, Cmp >::capacity ( ) const
inlinenoexcept

Returns the table capacity.

Definition at line 507 of file tpl_lhash.H.

References Aleph::GenLhashTable< Key, BucketType, Cmp >::len.

Referenced by TEST().

◆ current_alpha()

◆ empty()

◆ get_compare() [1/2]

template<typename Key , class BucketType , class Cmp >
Cmp & Aleph::GenLhashTable< Key, BucketType, Cmp >::get_compare ( )
inline

Definition at line 188 of file tpl_lhash.H.

References Aleph::GenLhashTable< Key, BucketType, Cmp >::cmp.

◆ get_compare() [2/2]

template<typename Key , class BucketType , class Cmp >
const Cmp & Aleph::GenLhashTable< Key, BucketType, Cmp >::get_compare ( ) const
inline

Definition at line 190 of file tpl_lhash.H.

References Aleph::GenLhashTable< Key, BucketType, Cmp >::cmp.

◆ get_hash_fct()

template<typename Key , class BucketType , class Cmp >
Hash_Fct Aleph::GenLhashTable< Key, BucketType, Cmp >::get_hash_fct ( ) const
inlinenoexcept

◆ get_num_busy_slots()

template<typename Key , class BucketType , class Cmp >
const size_t & Aleph::GenLhashTable< Key, BucketType, Cmp >::get_num_busy_slots ( ) const
inlinenoexcept

Returns the number of occupied entries in the array.

Definition at line 514 of file tpl_lhash.H.

References Aleph::GenLhashTable< Key, BucketType, Cmp >::busy_slots_counter.

◆ insert()

◆ is_empty()

◆ operator=() [1/2]

◆ operator=() [2/2]

◆ remove()

◆ remove_bucket()

◆ resize()

◆ search()

template<typename Key , class BucketType , class Cmp >
Bucket * Aleph::GenLhashTable< Key, BucketType, Cmp >::search ( const Key &  key) const
inlinenoexcept

◆ search_in_bucket_list()

◆ search_next()

template<typename Key , class BucketType , class Cmp >
Bucket * Aleph::GenLhashTable< Key, BucketType, Cmp >::search_next ( Bucket bucket) const
inline

Returns the next bucket that collides with bucket.

If it does not exist, then nullptr is returned.

Definition at line 485 of file tpl_lhash.H.

References Aleph::GenLhashTable< Key, BucketType, Cmp >::cmp, Aleph::GenLhashTable< Key, BucketType, Cmp >::hash_fct, Aleph::GenLhashTable< Key, BucketType, Cmp >::len, Aleph::maps(), and Aleph::GenLhashTable< Key, BucketType, Cmp >::table.

◆ search_or_insert()

◆ search_with_custom_hash()

template<typename Key , class BucketType , class Cmp >
Bucket * Aleph::GenLhashTable< Key, BucketType, Cmp >::search_with_custom_hash ( HashFunc  hash_func,
const SearchKey search_key 
) const
inlineprotectednoexcept

Helper method for derived classes to perform custom heterogeneous searches.

This method allows derived classes (such as DynMapHashTable) to search for elements using a different type than the stored key type, without needing to construct the full key object. This is particularly useful for map-like wrappers that store pairs but want to search by key only.

Parameters
hash_funcFunction to compute the hash of the search key
search_keyThe key to search for (can be different type than Key)
Returns
Pointer to the bucket if found, nullptr otherwise
Note
The comparator (cmp) must support comparison between the stored Key type and the SearchKey type for this to work. For example, Dft_Pair_Cmp supports comparing Pair with Key.

Example usage in DynMapHashTable:

Pair * search(const Key & key) const {
return this->search_with_custom_hash(original_hash_fct, key);
}
Bucket * search(const Key &key) const noexcept
Search in the table for a bucket with key.
Definition tpl_lhash.H:412
Bucket * search_with_custom_hash(HashFunc hash_func, const SearchKey &search_key) const noexcept
Helper method for derived classes to perform custom heterogeneous searches.
Definition tpl_lhash.H:335

Definition at line 335 of file tpl_lhash.H.

References Aleph::GenLhashTable< Key, BucketType, Cmp >::cmp, Aleph::GenLhashTable< Key, BucketType, Cmp >::len, Aleph::maps(), and Aleph::GenLhashTable< Key, BucketType, Cmp >::table.

◆ set_hash_fct() [1/2]

template<typename Key , class BucketType , class Cmp >
void Aleph::GenLhashTable< Key, BucketType, Cmp >::set_hash_fct ( Hash_Fct  fct)
inlinenoexcept

Set the internal hash function.

Definition at line 352 of file tpl_lhash.H.

References Aleph::GenLhashTable< Key, BucketType, Cmp >::hash_fct, and Aleph::maps().

◆ set_hash_fct() [2/2]

template<typename Key , class BucketType , class Cmp >
void Aleph::GenLhashTable< Key, BucketType, Cmp >::set_hash_fct ( Hash_Fct_Ptr  fct)
inlinenoexcept

◆ size()

template<typename Key , class BucketType , class Cmp >
const size_t & Aleph::GenLhashTable< Key, BucketType, Cmp >::size ( ) const
inlinenoexcept

Returns the number of elements contained in the table.

Definition at line 510 of file tpl_lhash.H.

References Aleph::GenLhashTable< Key, BucketType, Cmp >::N.

Referenced by TEST().

◆ swap()

Friends And Related Symbol Documentation

◆ HashStats< GenLhashTable< Key, BucketType, Cmp > >

template<typename Key , class BucketType , class Cmp >
friend class HashStats< GenLhashTable< Key, BucketType, Cmp > >
friend

Definition at line 1 of file tpl_lhash.H.

Member Data Documentation

◆ busy_slots_counter

◆ cmp

◆ hash_fct

◆ len

◆ lower_alpha

◆ N

◆ remove_all_buckets

◆ table

template<typename Key , class BucketType , class Cmp >
BucketList* Aleph::GenLhashTable< Key, BucketType, Cmp >::table
private

◆ upper_alpha

◆ with_resize


The documentation for this class was generated from the following file: