|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Example demonstrating hash table with automatic resizing. More...
Go to the source code of this file.
Classes | |
| struct | Foo |
Functions | |
| void | fill (size_t n) |
| void | verify () |
| int | main (int argc, char **argv) |
Variables | |
| MapOLhash< int, Foo > | tbl |
| DynArray< Foo > | backup |
Example demonstrating hash table with automatic resizing.
This example demonstrates how MapOLhash (Open Addressing Hash Table) automatically resizes itself to maintain good performance as elements are inserted. Automatic resizing is crucial for maintaining O(1) average performance in hash tables.
Hash table performance degrades when the load factor (elements/buckets) becomes too high:
Without resizing:
| Hash Table Type | Resize Threshold | Reason |
|---|---|---|
| Open addressing | Load factor > 0.7-0.8 | Collisions become frequent |
| Separate chaining | Load factor > 1.0-2.0 | Chains become too long |
MapOLhash uses open addressing (linear probing or double hashing):
Cost: O(n) where n = number of elements Frequency: O(log n) times (each resize doubles size) Amortized cost: O(1) per insertion
insert(key, value)**: Insert key-value pair (may trigger resize)search(key)**: Find value by key (O(1) average)size()**: Get current number of elementscapacity()**: Get current number of bucketsload_factor()**: Get current load factorCommon strategies:
| Operation | Before Resize | After Resize | Amortized |
|---|---|---|---|
| Insert | O(1) → O(n) | O(1) | O(1) |
| Search | O(1) → O(n) | O(1) | O(1) |
| Delete | O(1) → O(n) | O(1) | O(1) |
The program prints progress during insertion and then verifies that every inserted element is still accessible after all insertions (resizes, if any, are handled internally by MapOLhash).
Definition in file hash_resize.C.
| void fill | ( | size_t | n | ) |
Definition at line 184 of file hash_resize.C.
References backup, Aleph::maps(), Aleph::HTList::size(), and tbl.
| int main | ( | int | argc, |
| char ** | argv | ||
| ) |
Definition at line 218 of file hash_resize.C.
References Aleph::fill(), Aleph::maps(), and verify().
| void verify | ( | ) |
Definition at line 201 of file hash_resize.C.
References backup, Aleph::count(), Aleph::maps(), and tbl.
Referenced by main().
Definition at line 182 of file hash_resize.C.
Definition at line 181 of file hash_resize.C.
Referenced by count_bucket_states(), count_odhash_bucket_states(), fill(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), 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(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TYPED_TEST_P(), TYPED_TEST_P(), TYPED_TEST_P(), TYPED_TEST_P(), and verify().