Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_lhash.H File Reference

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>
Include dependency graph for tpl_lhash.H:
This graph shows which files directly or indirectly include this file:

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.
 

Detailed Description

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.

Key Features

  • Unique keys: insert() returns nullptr if the key is already in the table, and the bucket stays with the caller
  • remove() unlinks a bucket of the table and returns it; the caller frees it
  • The destructor frees the buckets left in the table, unless the table was built with remove_all_buckets set to false
  • Automatic resizing, on by default; set_with_resize() turns it on or off

Resizing

The 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.

Complexity

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.

Usage Example

using Table = LhashTable<int>;
Table table;
table.insert(new Bucket(42));
table.insert(new Bucket(17));
auto * repeated = new Bucket(17);
if (table.insert(repeated) == nullptr) // 17 is already in the table
delete repeated;
if (Bucket * found = table.search(42); found != nullptr)
{
std::cout << "Found: " << found->get_key() << "\n";
delete table.remove(found);
}
Generic linear hash table.
Bucket * remove(Bucket *bucket) noexcept
Removes bucket from the table, without freeing it.
BucketType< Key > Bucket
The bucket type.
Bucket * insert(Bucket *bucket)
Insert bucket in the table.
Bucket * search(const Key &key) const noexcept
Search for key in the linear hash table.
Table::Bucket Bucket
Definition lin-hash.cc:57
DynList< T > repeated(const Container< T > &c)
Return elements that appear more than once in the container.

Comparison with Other Hash Tables

Feature LhashTable ODhashTable OLhashTable
Collision Chaining Double hash Linear probe
Keys move on resize No Yes Yes
Memory Extra pointers Compact Compact
See also
tpl_linHash.H Linear hashing with chaining
tpl_odhash.H Open addressing with double hashing
tpl_olhash.H Open addressing with linear probing
tpl_dynSetHash.H High-level hash set interface
Author
Leandro Rabindranath León

Definition in file tpl_lhash.H.