217using namespace Aleph;
221 cout <<
"Usage: " <<
prog <<
" [-s <chaining|linear|open|performance|all>] [--help]\n";
222 cout <<
"\nIf no selector is given, all demos are executed.\n";
232 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
233 cout <<
"║ EXAMPLE 1: DynSetLhash (Separate Chaining) ║\n";
234 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
236 cout <<
"DynSetLhash uses linked lists to handle collisions.\n";
237 cout <<
"Each bucket is a list of entries with the same hash.\n\n";
242 vector<int> values = {10, 20, 30, 40, 50, 60, 70, 80};
244 cout <<
"Inserting values:\n";
245 for (
int v : values) {
247 cout <<
" ✓ " << v <<
"\n";
250 cout <<
"\nSet size: " << set.size() <<
"\n";
252 cout <<
"\n--- Membership testing ---\n\n";
256 cout <<
" contains(" << key <<
"): " << (
found ?
"YES" :
"NO") <<
"\n";
264 cout <<
"\n--- Removal ---\n\n";
266 cout <<
"Removing 40...\n";
269 cout <<
"Removing 70...\n";
275 cout <<
"\nFinal size: " << set.size() <<
"\n";
277 cout <<
"\n--- Iteration ---\n\n";
278 cout <<
"All elements: ";
290 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
291 cout <<
"║ EXAMPLE 2: DynSetLinHash (Linear Hashing) ║\n";
292 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
294 cout <<
"Linear hashing grows the table incrementally, one bucket at a time.\n";
295 cout <<
"This avoids expensive full-table rehashing operations.\n\n";
299 cout <<
"Inserting 100 random integers...\n\n";
304 for (
int i = 0; i < 100; ++i)
307 cout <<
"Set size: " << set.size() <<
"\n";
309 cout <<
"\n--- Membership testing ---\n\n";
313 for (
int i = 0; i < 5; ++i) {
315 cout <<
" contains(" << v <<
"): "
316 << (set.
contains(v) ?
"YES" :
"NO") <<
"\n";
319 cout <<
" contains(99999): "
320 << (set.
contains(99999) ?
"YES" :
"NO") <<
"\n";
330 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
331 cout <<
"║ EXAMPLE 3: ODhashTable (Open Addressing) ║\n";
332 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
334 cout <<
"ODhashTable stores all entries in a contiguous array.\n";
335 cout <<
"Collisions are resolved by double hashing + linear probing.\n\n";
340 cout <<
"Table capacity: 17 (prime number recommended)\n\n";
343 vector<int> values = {10, 20, 30, 40, 50, 60, 70, 80};
345 cout <<
"Inserting values:\n";
346 for (
int v : values) {
347 int* result = table.
insert(v);
349 cout <<
" ✓ Inserted: " << v <<
"\n";
351 cout <<
" ✗ Failed (duplicate?): " << v <<
"\n";
354 cout <<
"\nTable statistics:\n";
355 cout <<
" Size: " << table.
size() <<
"\n";
360 cout <<
"\n--- Search operations ---\n\n";
364 cout <<
" search(" << key <<
"): "
365 << (
found ?
"FOUND" :
"NOT FOUND") <<
"\n";
373 cout <<
"\n--- Remove operations ---\n\n";
375 cout <<
"Removing 40...\n";
380 cout <<
"\nFinal size: " << table.
size() <<
"\n";
390 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
391 cout <<
"║ EXAMPLE 4: Performance Comparison ║\n";
392 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
395 cout <<
"Benchmark: " <<
N <<
" insertions + " <<
N <<
" lookups\n\n";
399 for (
int i = 0; i <
N; ++i)
400 values.push_back(
rng());
405 auto start = chrono::high_resolution_clock::now();
410 auto mid = chrono::high_resolution_clock::now();
417 auto end = chrono::high_resolution_clock::now();
419 auto insert_ms = chrono::duration_cast<chrono::milliseconds>(
mid - start).count();
420 auto search_ms = chrono::duration_cast<chrono::milliseconds>(end -
mid).count();
422 cout <<
"DynSetLhash (chaining):\n";
423 cout <<
" Insert: " << insert_ms <<
" ms\n";
424 cout <<
" Search: " << search_ms <<
" ms (found " <<
found <<
")\n";
425 cout <<
" Size: " << set.size() <<
"\n\n";
431 auto start = chrono::high_resolution_clock::now();
436 auto mid = chrono::high_resolution_clock::now();
443 auto end = chrono::high_resolution_clock::now();
445 auto insert_ms = chrono::duration_cast<chrono::milliseconds>(
mid - start).count();
446 auto search_ms = chrono::duration_cast<chrono::milliseconds>(end -
mid).count();
448 cout <<
"DynSetLinHash (linear hashing):\n";
449 cout <<
" Insert: " << insert_ms <<
" ms\n";
450 cout <<
" Search: " << search_ms <<
" ms (found " <<
found <<
")\n";
451 cout <<
" Size: " << set.size() <<
"\n\n";
457 auto start = chrono::high_resolution_clock::now();
462 auto mid = chrono::high_resolution_clock::now();
469 auto end = chrono::high_resolution_clock::now();
471 auto insert_ms = chrono::duration_cast<chrono::milliseconds>(
mid - start).count();
472 auto search_ms = chrono::duration_cast<chrono::milliseconds>(end -
mid).count();
474 cout <<
"ODhashTable (open addressing):\n";
475 cout <<
" Insert: " << insert_ms <<
" ms\n";
476 cout <<
" Search: " << search_ms <<
" ms (found " <<
found <<
")\n";
477 cout <<
" Size: " << table.
size() <<
"\n\n";
488 for (
int i = 1; i <
argc; ++i)
491 if (
arg ==
"--help" ||
arg ==
"-h")
507 cout <<
"Unknown argument: " <<
arg <<
"\n";
516 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
517 cout <<
"║ Hash Tables in Aleph-w Library ║\n";
519 cout <<
"║ Aleph-w Library - https://github.com/lrleon/Aleph-w ║\n";
520 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n";
522 const bool run_all = (
selector ==
"all");
523 if (run_all ||
selector ==
"chaining")
525 if (run_all ||
selector ==
"linear")
529 if (run_all ||
selector ==
"performance")
533 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
534 cout <<
"║ Hash Table Summary ║\n";
535 cout <<
"╠══════════════════════════════════════════════════════════════════╣\n";
536 cout <<
"║ DynSetLhash: Separate chaining, flexible, easy deletion ║\n";
537 cout <<
"║ DynSetLinHash: Linear hashing, smooth growth, no spikes ║\n";
538 cout <<
"║ ODhashTable: Open addressing, cache-friendly, fast ║\n";
540 cout <<
"║ Choose based on your use case: ║\n";
541 cout <<
"║ • DynSetLhash: General purpose, many deletions ║\n";
542 cout <<
"║ • DynSetLinHash: Realtime, predictable performance ║\n";
543 cout <<
"║ • ODhashTable: Known size, maximum speed ║\n";
544 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
bool contains(const Key &key) const noexcept
Alias for has()
void remove(Key *key)
Removes a key from the hash table using its pointer.
Key * insert(const Key &key)
Inserts key into the hash set.
void empty() noexcept
empty the list
Open addressing hash table with double hashing collision resolution.
Key * search(const Key &key) const noexcept
searches the table for the key.
void remove(const Key &key)
Remove a key from the hash table.
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
constexpr size_t capacity() const noexcept
Returns the current capacity of the table.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
void demo_dynset_linhash()
static void print_usage(const char *prog)
Main namespace for Aleph-w library functions.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Dynamic set implementations based on hash tables.
Open addressing hash table with double hashing.