|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Hash table implementations in Aleph-w: A comprehensive guide. More...
#include <iostream>#include <string>#include <chrono>#include <random>#include <iomanip>#include <tpl_dynSetHash.H>#include <tpl_odhash.H>Go to the source code of this file.
Functions | |
| static void | print_usage (const char *prog) |
| void | demo_dynset_lhash () |
| void | demo_dynset_linhash () |
| void | demo_odhash () |
| void | demo_performance () |
| int | main (int argc, char *argv[]) |
Hash table implementations in Aleph-w: A comprehensive guide.
This example demonstrates the various hash table implementations available in Aleph-w, each optimized for different use cases. Hash tables provide O(1) average-case operations for insert, search, and delete, making them essential for high-performance applications.
A hash table is a data structure that maps keys to values using a hash function. It provides:
Trade-off: No ordering guarantee, worst case can be O(n)
Strategy: Each bucket contains a linked list of elements
How it works:
Characteristics:
Strategy: Incremental hash table growth (linear hashing)
How it works:
Characteristics:
Strategy: Store elements directly in array, use double hashing for collision resolution
How it works:
h2(key)(h1(key) + i × h2(key)) mod mCharacteristics:
Strategy: Store elements in array, use linear probing for collisions
How it works:
(h(key) + i) mod mCharacteristics:
| Operation | Average Case | Worst Case | Notes |
|---|---|---|---|
| Insert | O(1) | O(n) | Depends on load factor |
| Search | O(1) | O(n) | Hash collisions |
| Delete | O(1) | O(n) | Varies by implementation |
Load factor (α = n/m): Ratio of elements to buckets
| Strategy | Method | Pros | Cons |
|---|---|---|---|
| Separate Chaining | Linked lists | Simple deletion | Pointer overhead |
| Linear Probing | Check next slot | Cache-friendly | Clustering |
| Double Hashing | Second hash function | Less clustering | More computation |
| Quadratic Probing | Quadratic sequence | Moderate clustering | Complex |
✅ Good for:
❌ Not good for:
| Feature | Hash Table | Balanced Tree |
|---|---|---|
| Average lookup | O(1) | O(log n) |
| Worst-case lookup | O(n) | O(log n) |
| Ordered iteration | No | Yes |
| Range queries | No | Yes |
| Memory overhead | Lower | Higher |
A good hash function should:
Poor hash functions lead to:
Definition in file hash_tables_example.C.
| void demo_dynset_lhash | ( | ) |
Definition at line 229 of file hash_tables_example.C.
References Aleph::DynHashTable< Key, HashTable, Cmp >::contains(), FunctionalMethods< Container, T >::for_each(), Aleph::DynHashTable< Key, HashTable, Cmp >::insert(), Aleph::maps(), and Aleph::DynHashTable< Key, HashTable, Cmp >::remove().
Referenced by main().
| void demo_dynset_linhash | ( | ) |
Definition at line 287 of file hash_tables_example.C.
References Aleph::DynHashTable< Key, HashTable, Cmp >::contains(), Aleph::DynHashTable< Key, HashTable, Cmp >::insert(), Aleph::maps(), and rng.
Referenced by main().
| void demo_odhash | ( | ) |
Definition at line 327 of file hash_tables_example.C.
References OhashCommon< HashTbl, Key >::capacity(), OhashCommon< HashTbl, Key >::insert(), Aleph::maps(), Aleph::ODhashTable< Key, Cmp >::remove(), Aleph::ODhashTable< Key, Cmp >::search(), and OhashCommon< HashTbl, Key >::size().
Referenced by main().
| void demo_performance | ( | ) |
Definition at line 387 of file hash_tables_example.C.
References Aleph::DynHashTable< Key, HashTable, Cmp >::contains(), OhashCommon< HashTbl, Key >::insert(), Aleph::DynHashTable< Key, HashTable, Cmp >::insert(), Aleph::maps(), N, rng, Aleph::ODhashTable< Key, Cmp >::search(), and OhashCommon< HashTbl, Key >::size().
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 485 of file hash_tables_example.C.
References demo_dynset_lhash(), demo_dynset_linhash(), demo_odhash(), demo_performance(), Aleph::DynList< T >::empty(), Aleph::maps(), and print_usage().
|
static |
Definition at line 219 of file hash_tables_example.C.
References Aleph::maps().
Referenced by main().