|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Hash tables with collision resolution by separate chaining. More...
#include <iostream>#include <new>#include <primes.H>#include <dlink.H>#include <htlist.H>#include <tpl_dynArray.H>#include <hashDry.H>#include <hash-fct.H>#include <hash-dry.H>#include <ah-errors.H>#include <ah-concepts.H>Go to the source code of this file.
Classes | |
| class | Aleph::GenLhashTable< Key, BucketType, Cmp > |
| Generic hash table with collision resolution by separate chaining. More... | |
| class | Aleph::GenLhashTable< Key, BucketType, Cmp >::Iterator |
| Iterator over a GenLhashTable hash table. More... | |
| struct | Aleph::LhashBucketVtl< Key > |
| Bucket with virtual destructor for a hash table with collision resolution by separate chaining. More... | |
| struct | Aleph::LhashTable< Key, Cmp > |
| Generic hash table with collision resolution by separate chaining and buckets without virtual destructor. More... | |
| struct | Aleph::LhashTableVtl< Key, Cmp > |
| Generic hash table with collision resolution by separate chaining and buckets with virtual destructor. More... | |
Namespaces | |
| namespace | Aleph |
| Main namespace for Aleph-w library functions. | |
Typedefs | |
| template<typename Key > | |
| using | Aleph::LhashBucket = Dnode< Key > |
| Bucket without virtual destructor for a hash table with collision resolution by separate chaining. | |
Hash tables with collision resolution by separate chaining.
This file implements GenLhashTable and its two forms, LhashTable and LhashTableVtl, whose buckets have no virtual destructor or have one. The table is an array of doubly linked lists: each key lives in a bucket, linked into the list that its hash value selects. The caller allocates the buckets, and the table only links them in and out: it never copies a key or moves a bucket, so a bucket keeps its address while it is in the table, even when the table resizes.
insert() returns nullptr if the key is already in the table, and the bucket stays with the callerremove() unlinks a bucket of the table and returns it; the caller frees itremove_all_buckets set to falseset_with_resize() turns it on or offThe load factor is the average number of keys per list. A table that resizes itself rebuilds its array of lists when the load factor crosses one of two thresholds, and links every bucket again:
upper_alpha (0.9 by default): when an insertion brings the load factor to it, the table grows to about twice as many lists.lower_alpha (0.25 by default): when a removal leaves the load factor below it, the table shrinks to about half as many lists, provided that its load factor stays below upper_alpha there. If there is no memory for the smaller array, the table keeps its capacity.The constructor rounds the requested capacity up to a prime, and the table grows and shrinks to primes. resize() takes the capacity it is given, unless the table resizes itself and its load factor there reaches upper_alpha: then it takes the capacity that the table would grow to as it inserted the keys, and allocates it before a key moves. Removing through Iterator::del() never resizes the table. A table that does not resize itself keeps its capacity, and its lists get longer as keys come in. For linear hashing, which grows one list at a time instead of rebuilding the table, see tpl_linHash.H.
| Operation | Average | Worst Case |
|---|---|---|
| insert | O(1) | O(n) |
| search | O(1) | O(n) |
| remove | O(1) | O(1) |
| resize | O(n + m) | O(n^2 + m) |
Here n is the number of keys and m the number of lists. A resize inserts every bucket again, and each insertion looks for its key in its list: when all the keys fall in one list, that is quadratic. An insertion or a removal that resizes the table costs as much as a resize.
| Feature | LhashTable | ODhashTable | OLhashTable |
|---|---|---|---|
| Collision | Chaining | Double hash | Linear probe |
| Keys move on resize | No | Yes | Yes |
| Memory | Extra pointers | Compact | Compact |
Definition in file tpl_lhash.H.