|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Dynamic hash table mapping keys to records with separate chaining. More...
#include <tpl_dynLhash.H>
Classes | |
| struct | DLBucket |
| class | DLProxy |
Public Types | |
| using | Hash_Fct_Ptr = typename DynLhashTable< Key, Record >::Hash_Fct_Ptr |
| The type of hash function pointer. | |
Public Types inherited from Aleph::LhashTableVtl< Key, Cmp > | |
| using | Base = GenLhashTable< Key, LhashBucketVtl< Key >, Cmp > |
Public Types inherited from Aleph::GenLhashTable< Key, BucketType, Cmp > | |
| 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 | |
| void | swap (DynLhashTable &table) noexcept |
| Swap contents with another table. | |
| DynLhashTable (size_t len=DefaultPrime, Hash_Fct_Ptr hash_fct=dft_hash_fct< Key >) | |
| Construct a dynamic hash table. | |
| DynLhashTable (const DynLhashTable &table) | |
| Copy constructor. | |
| DynLhashTable (DynLhashTable &&table) noexcept | |
| Move constructor. | |
| DynLhashTable & | operator= (const DynLhashTable &table) |
| Copy assignment operator. | |
| DynLhashTable & | operator= (DynLhashTable &&table) noexcept |
| Move assignment operator. | |
| Record * | insert (const Key &key, const Record &record) |
| Insert a key-record pair into the table. | |
| Record * | insert (const Key &key, Record &&record=Record()) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
| Record * | insert (Key &&key, const Record &record) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
| Record * | insert (Key &&key, Record &&record) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
| Record * | search (const Key &key) |
| Search for a key in the table. | |
| void | remove (Record *record) |
| Remove an entry from the table. | |
| DLProxy | operator[] (const Key &key) const |
| Access or insert an entry using subscript notation. | |
| DLProxy | operator[] (const Key &key) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
Public Member Functions inherited from Aleph::GenLhashTable< Key, BucketType, Cmp > | |
| 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 | |
| GenLhashTable & | operator= (const GenLhashTable &)=delete |
| GenLhashTable (GenLhashTable &&other) noexcept | |
| GenLhashTable & | operator= (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 | |
| Bucket * | insert (Bucket *bucket) |
| Inserts bucket into the table and returns its address if the key is not already in the table; otherwise returns nullptr. | |
| Bucket * | search_or_insert (Bucket *bucket) |
| Bucket * | search (const Key &key) const noexcept |
| Search in the table for a bucket with key. | |
| Bucket * | remove (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. | |
| Bucket * | search_next (Bucket *bucket) const |
| Returns the next bucket that collides with bucket. | |
| const size_t & | capacity () const noexcept |
| Returns the table capacity. | |
| const size_t & | size () const noexcept |
| Returns the number of elements contained in the table. | |
| const size_t & | get_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. | |
Private Member Functions | |
| void | copy (const DynLhashTable &table) |
| Record * | __insert (DLBucket *bucket) |
Static Private Member Functions | |
| static DLBucket * | record_to_bucket (Record *rec) |
Additional Inherited Members | |
Protected Member Functions inherited from 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) | |
| template<typename HashFunc , typename SearchKey > | |
| Bucket * | search_with_custom_hash (HashFunc hash_func, const SearchKey &search_key) const noexcept |
| Helper method for derived classes to perform custom heterogeneous searches. | |
Protected Attributes inherited from Aleph::GenLhashTable< Key, BucketType, Cmp > | |
| Hash_Fct | hash_fct |
| size_t | len |
| Cmp | cmp |
| float | lower_alpha |
| float | upper_alpha |
Dynamic hash table mapping keys to records with separate chaining.
DynLhashTable implements a key-value mapping using a hash table with separate chaining for collision resolution. It provides O(1) average-case complexity for insertions, lookups, and deletions.
The table supports both traditional methods (insert, search, remove) and the subscript operator [] for convenient access:
table[key] = record inserts or updates the entryrecord = table[key] retrieves the value (throws if key not found)Pointers returned by insert() and search() remain valid until the corresponding entry is removed, allowing direct modification of stored records.
| Key | Type of the indexing key (must be hashable). |
| Record | Type of the value associated with each key. |
| Cmp | Equality comparison functor for keys (default: Aleph::equal_to<Key>). |
Definition at line 89 of file tpl_dynLhash.H.
| using Aleph::DynLhashTable< Key, Record, Cmp >::Hash_Fct_Ptr = typename DynLhashTable<Key, Record>::Hash_Fct_Ptr |
The type of hash function pointer.
Definition at line 179 of file tpl_dynLhash.H.
|
inline |
Construct a dynamic hash table.
| [in] | len | Initial size of the hash table (default: DefaultPrime). |
| [in] | hash_fct | Hash function to use (default: dft_hash_fct<Key>). |
| std::bad_alloc | If memory allocation fails. |
Definition at line 196 of file tpl_dynLhash.H.
|
inline |
Copy constructor.
| [in] | table | The table to copy from. |
| std::bad_alloc | If memory allocation fails. |
Definition at line 219 of file tpl_dynLhash.H.
References Aleph::DynLhashTable< Key, Record, Cmp >::copy(), and Aleph::GenLhashTable< Key, BucketType, Cmp >::table.
|
inlinenoexcept |
Move constructor.
| [in,out] | table | The table to move from. |
Definition at line 230 of file tpl_dynLhash.H.
|
inlineprivate |
Definition at line 266 of file tpl_dynLhash.H.
References Aleph::GenLhashTable< Key, BucketType, Cmp >::insert(), and Aleph::DynLhashTable< Key, Record, Cmp >::DLBucket::record.
Referenced by Aleph::DynLhashTable< Key, Record, Cmp >::insert(), Aleph::DynLhashTable< Key, Record, Cmp >::insert(), Aleph::DynLhashTable< Key, Record, Cmp >::insert(), and Aleph::DynLhashTable< Key, Record, Cmp >::insert().
|
inlineprivate |
Definition at line 204 of file tpl_dynLhash.H.
References Aleph::GenLhashTable< Key, BucketType, Cmp >::Iterator::has_curr(), Aleph::DynLhashTable< Key, Record, Cmp >::insert(), and Aleph::GenLhashTable< Key, BucketType, Cmp >::table.
Referenced by Aleph::DynLhashTable< Key, Record, Cmp >::DynLhashTable(), and Aleph::DynLhashTable< Key, Record, Cmp >::operator=().
|
inline |
Insert a key-record pair into the table.
If the key already exists, a new entry is added (duplicates allowed). The returned pointer remains valid until the entry is removed.
| [in] | key | The key to insert. |
| [in] | record | The record to associate with the key. |
| std::bad_alloc | If memory allocation fails. |
Definition at line 283 of file tpl_dynLhash.H.
References Aleph::DynLhashTable< Key, Record, Cmp >::__insert().
Referenced by Aleph::DynLhashTable< Key, Record, Cmp >::copy(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST_F().
|
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 289 of file tpl_dynLhash.H.
References Aleph::DynLhashTable< Key, Record, Cmp >::__insert().
|
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 295 of file tpl_dynLhash.H.
References Aleph::DynLhashTable< Key, Record, Cmp >::__insert().
|
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 301 of file tpl_dynLhash.H.
References Aleph::DynLhashTable< Key, Record, Cmp >::__insert().
|
inline |
Copy assignment operator.
| [in] | table | The table to copy from. |
| std::bad_alloc | If memory allocation fails. |
Definition at line 241 of file tpl_dynLhash.H.
References Aleph::DynLhashTable< Key, Record, Cmp >::copy(), Aleph::GenLhashTable< Key, BucketType, Cmp >::empty(), and Aleph::GenLhashTable< Key, BucketType, Cmp >::table.
|
inlinenoexcept |
Move assignment operator.
| [in,out] | table | The table to move from. |
Definition at line 258 of file tpl_dynLhash.H.
References Aleph::GenLhashTable< Key, BucketType, Cmp >::operator=(), and Aleph::GenLhashTable< Key, BucketType, Cmp >::table.
|
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 351 of file tpl_dynLhash.H.
|
inline |
Access or insert an entry using subscript notation.
table[key] = value): inserts or updates the entry.value = table[key]): returns the value or throws.| [in] | key | The key to access. |
| std::invalid_argument | If reading a non-existent key. |
Definition at line 345 of file tpl_dynLhash.H.
|
inlinestaticprivate |
Definition at line 118 of file tpl_dynLhash.H.
References Aleph::maps(), and offset.
Referenced by Aleph::DynLhashTable< Key, Record, Cmp >::DLProxy::DLProxy(), and Aleph::DynLhashTable< Key, Record, Cmp >::remove().
|
inline |
Remove an entry from the table.
The record pointer must have been obtained from insert() or search(). After removal, the pointer becomes invalid.
| [in] | record | Pointer to the record to remove (must be valid). |
Definition at line 329 of file tpl_dynLhash.H.
References Aleph::DynLhashTable< Key, Record, Cmp >::record_to_bucket(), and Aleph::GenLhashTable< Key, BucketType, Cmp >::remove().
|
inline |
Search for a key in the table.
| [in] | key | The key to search for. |
Definition at line 314 of file tpl_dynLhash.H.
References Aleph::DynLhashTable< Key, Record, Cmp >::DLBucket::record, and Aleph::GenLhashTable< Key, BucketType, Cmp >::search().
Referenced by Aleph::DynLhashTable< Key, Record, Cmp >::DLProxy::DLProxy(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlinenoexcept |
Swap contents with another table.
| [in,out] | table | The table to swap with. |
Definition at line 184 of file tpl_dynLhash.H.
References Aleph::GenLhashTable< Key, BucketType, Cmp >::swap(), and Aleph::GenLhashTable< Key, BucketType, Cmp >::table.