|
Aleph-w 7.10.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 Base::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_ptr_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_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 hash table. | |
| void | swap (GenLhashTable &other) noexcept |
Swaps this table with other in constant time. | |
| GenLhashTable (const GenLhashTable &)=delete | |
| GenLhashTable & | operator= (const GenLhashTable &)=delete |
| GenLhashTable (GenLhashTable &&other) noexcept | |
Takes the lists and the buckets of other. | |
| GenLhashTable & | operator= (GenLhashTable &&other) noexcept |
Frees the buckets and the lists of this table, and takes those of other, with what rules them: the hash function, the comparator, the load thresholds and the policies. | |
| void | empty () noexcept |
| Empties the hash table; frees memory of all buckets. | |
| void | clear () noexcept |
| Empties the container. | |
| bool | contains (const Key &key) const noexcept |
| Checks if a key exists in the table. | |
| Hash_Fct | get_hash_fct () const noexcept |
| void | set_hash_fct (Hash_Fct fct) |
| Sets the hash function. | |
| 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. | |
| float | current_alpha () const noexcept |
| return the current table load; zero for a table without lists | |
| 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 table to new_size lists and places every bucket again. | |
| 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. | |
| 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. | |
Private Types | |
| using | Base = LhashTableVtl< Key, Cmp > |
Private Member Functions | |
| void | copy (const DynLhashTable &table) |
| DLBucket * | take (DLBucket *bucket) |
| 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 91 of file tpl_dynLhash.H.
|
private |
Definition at line 93 of file tpl_dynLhash.H.
| using Aleph::DynLhashTable< Key, Record, Cmp >::Hash_Fct_Ptr = typename Base::Hash_Fct_Ptr |
The type of hash function pointer.
Definition at line 181 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 198 of file tpl_dynLhash.H.
|
inline |
Copy constructor.
The copy has the hash function, the comparator, the thresholds, the capacity and the resizing of table, and copies of its keys and records.
| [in] | table | The table to copy from. |
| std::bad_alloc | If memory allocation fails. |
Definition at line 226 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 239 of file tpl_dynLhash.H.
|
inlineprivate |
Definition at line 301 of file tpl_dynLhash.H.
References hash_dry_detail::free_bucket_not_taken(), 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 206 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().
|
inline |
Insert a key-record pair into the table.
If the key is already in the table, nothing is inserted. 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 334 of file tpl_dynLhash.H.
References Aleph::DynLhashTable< Key, Record, Cmp >::__insert().
Referenced by Aleph::DynLhashTable< Key, Record, Cmp >::copy(), main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), 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 340 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 346 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 352 of file tpl_dynLhash.H.
References Aleph::DynLhashTable< Key, Record, Cmp >::__insert().
|
inline |
Copy assignment operator.
This table takes the hash function, the comparator, the thresholds, the capacity and the resizing of table, and copies of its keys and records, as the copy constructor gives them.
| [in] | table | The table to copy from. |
| std::bad_alloc | If memory allocation fails; this table is then left as it was. |
Definition at line 256 of file tpl_dynLhash.H.
References Aleph::DynLhashTable< Key, Record, Cmp >::swap(), and Aleph::GenLhashTable< Key, BucketType, Cmp >::table.
|
inlinenoexcept |
Move assignment operator.
| [in,out] | table | The table to move from. |
Definition at line 273 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 402 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 396 of file tpl_dynLhash.H.
|
inlinestaticprivate |
Definition at line 122 of file tpl_dynLhash.H.
References 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 380 of file tpl_dynLhash.H.
References Aleph::DynLhashTable< Key, Record, Cmp >::record_to_bucket(), and Aleph::GenLhashTable< Key, BucketType, Cmp >::remove().
Referenced by main(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inline |
Search for a key in the table.
| [in] | key | The key to search for. |
Definition at line 365 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(), main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
|
inlinenoexcept |
Swap contents with another table.
| [in,out] | table | The table to swap with. |
Definition at line 186 of file tpl_dynLhash.H.
References Aleph::GenLhashTable< Key, BucketType, Cmp >::swap(), and Aleph::GenLhashTable< Key, BucketType, Cmp >::table.
Referenced by Aleph::DynLhashTable< Key, Record, Cmp >::operator=(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 285 of file tpl_dynLhash.H.
References hash_dry_detail::free_bucket_not_taken(), and Aleph::GenLhashTable< Key, BucketType, Cmp >::insert().
Referenced by Aleph::DynLhashTable< Key, Record, Cmp >::DLProxy::operator=(), and Aleph::DynLhashTable< Key, Record, Cmp >::DLProxy::operator=().