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

Generic linear hash table. More...

#include <tpl_linHash.H>

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

Classes

class  Iterator
 

Public Types

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

Public Member Functions

void set_hash_fct (Hash_Fct fct)
 Sets the hash function of an empty table.
 
void set_hash_fct (Hash_Fct_Ptr fct)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
Hash_Fct get_hash_fct () const noexcept
 
Cmp & get_compare () noexcept
 
const Cmp & get_compare () const noexcept
 
float current_alpha () const noexcept
 return the current table load
 
 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.
 
void swap (GenLinearHashTable &other) noexcept
 Swaps this table with other in constant time.
 
void empty () noexcept
 Empties the table: frees every bucket and gives the table its initial size.
 
void clear () noexcept
 Empties the container.
 
bool contains (const Key &key) const noexcept
 Checks if a key exists in the table.
 
 ~GenLinearHashTable ()
 
 GenLinearHashTable (const GenLinearHashTable &)=delete
 Not copyable: the copy that the compiler would write copies the links of the lists, which would point to the buckets of the other table: destroying one table frees the buckets that the other one still holds.
 
GenLinearHashTable & operator= (const GenLinearHashTable &)=delete
 Not copyable, for the same reason as the copy constructor.
 
Bucket * search (const Key &key) const noexcept
 Search for key in the linear hash table.
 
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
 
const size_t & capacity () const noexcept
 Returns the table capacity.
 
const size_t & busy_slots () const noexcept
 Returns the number of busy slots in the table.
 
const size_t & expansions () const noexcept
 Returns the expansion level performed on the table.
 
Bucket * insert (Bucket *bucket)
 Insert bucket in the table.
 
Bucket * search_or_insert (Bucket *bucket)
 
size_t resize (size_t) noexcept
 Provided for generic programming compatibility.
 
Bucket * remove (Bucket *bucket) noexcept
 Removes bucket from the table, without freeing it.
 
void print ()
 
- Public Member Functions inherited from HashStats< HashTbl >
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.
 
constexpr bool get_with_resize () const noexcept
 Returns whether the table resizes itself.
 
void set_with_resize (const bool with_resize) noexcept
 Turns the automatic resizing on or off.
 

Protected Member Functions

 GenLinearHashTable (size_t __len, Hash_Fct __hash_fct, Cmp __cmp, float __lower_alpha, float __upper_alpha, bool __remove_all_buckets, bool __with_resize)
 
template<typename HashFunc , typename SearchKey >
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.
 

Protected Attributes

Hash_Fct hash_fct
 
Cmp cmp
 
float upper_alpha
 
float lower_alpha
 
size_t len
 

Private Types

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

Private Member Functions

size_t list_index (const size_t hash) const noexcept
 
size_t call_hash_fct (const Key &key) const
 
void expand ()
 
void contract ()
 
template<typename SearchKey >
Bucket * search_in_bucket_list (BucketList *list, const SearchKey &key) const
 
Bucket * remove_bucket (Bucket *bucket) noexcept
 

Static Private Member Functions

static size_t multiply_by_two (size_t n) noexcept
 
static size_t divide_by_two (size_t n) noexcept
 

Private Attributes

DynArray< BucketList > table
 
Dlink entries_list
 
size_t M
 
size_t N
 
size_t busy_slots_counter
 
bool remove_all_buckets
 
bool with_resize
 
size_t p
 
size_t l
 
size_t MP
 
size_t MM
 

Friends

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

Detailed Description

template<typename Key, template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
requires EqualityComparator<Cmp, Key>
class Aleph::GenLinearHashTable< Key, BucketType, Cmp >

Generic linear hash table.

Basically, a linear hash table is a hash table with collision resolution by separate chaining but with the difference that the table size increases dynamically to ensure that the load factor, typically called \(\alpha\), is bounded between two lower and upper values, respectively.

Internally, the table uses a dynamic array of type DynArray. This leads to memory savings for table entries that have not been written.

This type is generic and is not intended to be used directly. Instead, use LinearHashTable or LinearHashTableVtl depending on whether you want buckets without or with a virtual destructor.

Parameters
Keythe key type by which the table is indexed.
BucketTypethe bucket type between LinHashBucket or LinHashBucketVtl.
Cmpcomparison class between keys.
See also
DynArray LinearHashTable LinearHashTableVtl

Definition at line 159 of file tpl_linHash.H.

Member Typedef Documentation

◆ Bucket

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
using Aleph::GenLinearHashTable< Key, BucketType, Cmp >::Bucket = BucketType<Key>

The bucket type.

Definition at line 172 of file tpl_linHash.H.

◆ BucketItor

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

Definition at line 185 of file tpl_linHash.H.

◆ BucketList

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
using Aleph::GenLinearHashTable< Key, BucketType, Cmp >::BucketList = Dlink
private

Definition at line 183 of file tpl_linHash.H.

◆ Hash_Fct

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

The hash function type.

Must return pseudo-random positive values, between zero and the largest possible number.

Definition at line 168 of file tpl_linHash.H.

◆ Hash_Fct_Ptr

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

Definition at line 169 of file tpl_linHash.H.

◆ Item_Type

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

Definition at line 176 of file tpl_linHash.H.

◆ Key_Type

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

Definition at line 174 of file tpl_linHash.H.

Constructor & Destructor Documentation

◆ GenLinearHashTable() [1/3]

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
Aleph::GenLinearHashTable< Key, BucketType, Cmp >::GenLinearHashTable ( size_t  __len,
Hash_Fct  __hash_fct,
Cmp  __cmp,
float  __lower_alpha,
float  __upper_alpha,
bool  __remove_all_buckets,
bool  __with_resize 
)
inlineprotected

◆ GenLinearHashTable() [2/3]

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
Aleph::GenLinearHashTable< Key, BucketType, Cmp >::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 
)
inline

Instantiate a generic linear hash table.

Instantiate a generic linear hash table of size len.

Parameters
[in]leninitial and minimum table size.
[in]hash_fcthash function.
[in]cmpcomparison functor for keys.
[in]lower_alphalower threshold at which the table should contract.
[in]upper_alphaupper threshold at which the table should expand.
[in]remove_all_bucketsif true, then the table's buckets are freed. Otherwise, they remain intact. By default the value is true.
[in]with_resizeif true, the table will resize automatically based on load factors; set_with_resize() changes it later.
Exceptions
length_errorif len is equal or greater than the maximum dimension allowed for a dynamic array.
domain_errorif upper_alpha is less than or equal to lower_alpha.
bad_allocif there is not enough memory
overflow_errorif an overflow occurs from DynArray (caused by its internal calculations).

Definition at line 410 of file tpl_linHash.H.

◆ ~GenLinearHashTable()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
Aleph::GenLinearHashTable< Key, BucketType, Cmp >::~GenLinearHashTable ( )
inline

◆ GenLinearHashTable() [3/3]

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

Not copyable: the copy that the compiler would write copies the links of the lists, which would point to the buckets of the other table: destroying one table frees the buckets that the other one still holds.

The table has no moves of its own either; swap() exchanges two tables in constant time.

Member Function Documentation

◆ busy_slots()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
const size_t & Aleph::GenLinearHashTable< Key, BucketType, Cmp >::busy_slots ( ) const
inlinenoexcept

Returns the number of busy slots in the table.

Definition at line 599 of file tpl_linHash.H.

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

Referenced by print_stats(), TEST(), TEST(), TEST(), TEST(), and TEST_F().

◆ call_hash_fct()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
size_t Aleph::GenLinearHashTable< Key, BucketType, Cmp >::call_hash_fct ( const Key &  key) const
inlineprivate

◆ capacity()

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

Returns the table capacity.

Definition at line 596 of file tpl_linHash.H.

References Aleph::GenLinearHashTable< Key, BucketType, Cmp >::MP.

Referenced by print_stats(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST_F().

◆ clear()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
void Aleph::GenLinearHashTable< Key, BucketType, Cmp >::clear ( )
inlinenoexcept

Empties the container.

Exceptions
none
See also
empty()

Definition at line 492 of file tpl_linHash.H.

References Aleph::GenLinearHashTable< Key, BucketType, Cmp >::empty().

◆ contains()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
bool Aleph::GenLinearHashTable< Key, BucketType, Cmp >::contains ( const Key &  key) const
inlinenoexcept

Checks if a key exists in the table.

Parameters
[in]keykey to search for.
Returns
true if key is present, false otherwise.
Exceptions
none

Definition at line 500 of file tpl_linHash.H.

References Aleph::GenLinearHashTable< Key, BucketType, Cmp >::search().

◆ contract()

◆ current_alpha()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
float Aleph::GenLinearHashTable< Key, BucketType, Cmp >::current_alpha ( ) const
inlinenoexcept

return the current table load

Definition at line 363 of file tpl_linHash.H.

References Aleph::GenLinearHashTable< Key, BucketType, Cmp >::MP.

Referenced by TEST(), and TEST().

◆ divide_by_two()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
static size_t Aleph::GenLinearHashTable< Key, BucketType, Cmp >::divide_by_two ( size_t  n)
inlinestaticprivatenoexcept

◆ empty()

◆ expand()

◆ expansions()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
const size_t & Aleph::GenLinearHashTable< Key, BucketType, Cmp >::expansions ( ) const
inlinenoexcept

Returns the expansion level performed on the table.

Definition at line 602 of file tpl_linHash.H.

References Aleph::GenLinearHashTable< Key, BucketType, Cmp >::l.

Referenced by print_stats(), TEST(), TEST(), TEST(), and TEST().

◆ get_compare() [1/2]

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
const Cmp & Aleph::GenLinearHashTable< Key, BucketType, Cmp >::get_compare ( ) const
inlinenoexcept

◆ get_compare() [2/2]

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
Cmp & Aleph::GenLinearHashTable< Key, BucketType, Cmp >::get_compare ( )
inlinenoexcept

Definition at line 358 of file tpl_linHash.H.

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

Referenced by TEST(), TEST(), and TEST().

◆ get_hash_fct()

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

Definition at line 356 of file tpl_linHash.H.

References Aleph::GenLinearHashTable< Key, BucketType, Cmp >::hash_fct.

Referenced by TEST().

◆ insert()

◆ is_empty()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
bool Aleph::GenLinearHashTable< Key, BucketType, Cmp >::is_empty ( ) const
inlinenoexcept

◆ list_index()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
size_t Aleph::GenLinearHashTable< Key, BucketType, Cmp >::list_index ( const size_t  hash) const
inlineprivatenoexcept

◆ multiply_by_two()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
static size_t Aleph::GenLinearHashTable< Key, BucketType, Cmp >::multiply_by_two ( size_t  n)
inlinestaticprivatenoexcept

◆ operator=()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
GenLinearHashTable & Aleph::GenLinearHashTable< Key, BucketType, Cmp >::operator= ( const GenLinearHashTable< Key, BucketType, Cmp > &  )
delete

Not copyable, for the same reason as the copy constructor.

◆ print()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
void Aleph::GenLinearHashTable< Key, BucketType, Cmp >::print ( )
inline

◆ remove()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
Bucket * Aleph::GenLinearHashTable< Key, BucketType, Cmp >::remove ( Bucket *  bucket)
inlinenoexcept

Removes bucket from the table, without freeing it.

Parameters
[in]bucketthe bucket to remove.
Returns
bucket.
Exceptions
none
Precondition
bucket is in the table; it is not checked.
Note
The table may contract afterwards, which may need memory. If there is none, the table keeps the capacity of the last complete step of the contraction, so the removal never ends the program.

Definition at line 692 of file tpl_linHash.H.

References Aleph::GenLinearHashTable< Key, BucketType, Cmp >::remove_bucket().

Referenced by main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST_F().

◆ remove_bucket()

◆ resize()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
size_t Aleph::GenLinearHashTable< Key, BucketType, Cmp >::resize ( size_t  )
inlinenoexcept

Provided for generic programming compatibility.

Definition at line 646 of file tpl_linHash.H.

References Aleph::GenLinearHashTable< Key, BucketType, Cmp >::MP.

Referenced by TEST().

◆ search()

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

◆ search_in_bucket_list()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
template<typename SearchKey >
Bucket * Aleph::GenLinearHashTable< Key, BucketType, Cmp >::search_in_bucket_list ( BucketList *  list,
const SearchKey &  key 
) const
inlineprivate

◆ search_or_insert()

◆ search_with_custom_hash()

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

Searches for a key of another type, without building a Key.

For the derived classes whose keys hold what they search by, as DynMapHashTable, whose keys are pairs: it searches by the first member alone, so it neither copies that member nor builds a Data, which may allocate or throw.

Parameters
[in]hash_functhe hash of search_key, which must be the hash that the table gives to the keys equivalent to search_key. It is taken by reference: a copy of a function object may allocate.
[in]search_keywhat to search for; the comparator of the table must compare it with a Key.
Returns
the bucket whose key is equivalent to search_key, or nullptr if there is none.
Exceptions
noneas search(), it requires that the hash function and the comparator do not throw.

Definition at line 577 of file tpl_linHash.H.

References Aleph::Dlink::is_empty(), Aleph::GenLinearHashTable< Key, BucketType, Cmp >::list_index(), Aleph::GenLinearHashTable< Key, BucketType, Cmp >::search_in_bucket_list(), and Aleph::GenLinearHashTable< Key, BucketType, Cmp >::table.

◆ set_hash_fct() [1/2]

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
void Aleph::GenLinearHashTable< Key, BucketType, Cmp >::set_hash_fct ( Hash_Fct  fct)
inline

Sets the hash function of an empty table.

Parameters
[in]fctThe new hash function.
Exceptions
std::domain_errorif the table is not empty: the buckets already stored would stay in the lists of the old function, where their keys may not be found, and linear hashing has no operation that places them again. The table then keeps its function.

Definition at line 341 of file tpl_linHash.H.

References ah_domain_error_if, Aleph::GenLinearHashTable< Key, BucketType, Cmp >::hash_fct, and Aleph::GenLinearHashTable< Key, BucketType, Cmp >::is_empty().

Referenced by TEST().

◆ set_hash_fct() [2/2]

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
void Aleph::GenLinearHashTable< Key, BucketType, Cmp >::set_hash_fct ( Hash_Fct_Ptr  fct)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 349 of file tpl_linHash.H.

References ah_domain_error_if, Aleph::GenLinearHashTable< Key, BucketType, Cmp >::hash_fct, and Aleph::GenLinearHashTable< Key, BucketType, Cmp >::is_empty().

◆ size()

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

◆ swap()

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
void Aleph::GenLinearHashTable< Key, BucketType, Cmp >::swap ( GenLinearHashTable< Key, BucketType, Cmp > &  other)
inlinenoexcept

Friends And Related Symbol Documentation

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

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
friend class HashStats< GenLinearHashTable< Key, BucketType, Cmp > >
friend

Definition at line 128 of file tpl_linHash.H.

Member Data Documentation

◆ busy_slots_counter

◆ cmp

◆ entries_list

◆ hash_fct

◆ l

◆ len

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
size_t Aleph::GenLinearHashTable< Key, BucketType, Cmp >::len
mutableprotected

◆ lower_alpha

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
float Aleph::GenLinearHashTable< Key, BucketType, Cmp >::lower_alpha
protected

◆ M

◆ MM

◆ MP

◆ N

◆ p

◆ remove_all_buckets

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
bool Aleph::GenLinearHashTable< Key, BucketType, Cmp >::remove_all_buckets
private

◆ table

◆ upper_alpha

template<typename Key , template< class > class BucketType, class Cmp = Aleph::equal_to<Key>>
float Aleph::GenLinearHashTable< Key, BucketType, Cmp >::upper_alpha
protected

◆ with_resize


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