Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
lin-hash.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
33
38#include <gtest/gtest.h>
39
40#include <tpl_linHash.H>
41#include <tpl_dynSetHash.H>
42#include <random>
43#include <map>
44#include <set>
45#include <vector>
46#include <algorithm>
47#include <type_traits>
48
49using namespace std;
50using namespace Aleph;
51
52// ============================================================================
53// Type Aliases
54// ============================================================================
55
58
59// ============================================================================
60// Empty Table Tests
61// ============================================================================
62
63TEST(LinearHashTable, EmptyTableProperties)
64{
65 Table table;
66
67 EXPECT_EQ(table.size(), 0u);
68 EXPECT_TRUE(table.is_empty());
69 EXPECT_GT(table.capacity(), 0u);
70 EXPECT_EQ(table.busy_slots(), 0u);
71 EXPECT_EQ(table.expansions(), 0u);
72}
73
74// The copy that the compiler would write copies the links of the lists,
75// which would point to the buckets of the other table: destroying one
76// table freed the buckets that the other one still held. Neither it nor
77// the assignment by copy exist
78TEST(LinearHashTable, ATableCannotBeCopied)
79{
80 static_assert(not std::is_copy_constructible_v<LinearHashTable<int>>);
81 static_assert(not std::is_copy_assignable_v<LinearHashTable<int>>);
82 static_assert(not std::is_copy_constructible_v<LinearHashTableVtl<int>>);
83 static_assert(not std::is_copy_assignable_v<LinearHashTableVtl<int>>);
84
85 // the sets and the maps on a linear table copy their keys
86 static_assert(std::is_copy_constructible_v<DynSetLinHash<int>>);
88 set.insert(1);
90 EXPECT_NE(copy.search(1), nullptr);
91 EXPECT_NE(set.search(1), nullptr);
92}
93
94TEST(LinearHashTable, SearchOnEmptyTableReturnsNull)
95{
96 Table table;
97
98 EXPECT_EQ(table.search(10), nullptr);
99 EXPECT_EQ(table.search(0), nullptr);
100 EXPECT_EQ(table.search(-1), nullptr);
101}
102
103// ============================================================================
104// Insert Tests
105// ============================================================================
106
107TEST(LinearHashTable, InsertSingleElement)
108{
109 Table table;
110
111 auto *bucket = new Bucket(10);
112 auto *inserted = table.insert(bucket);
113
114 EXPECT_EQ(inserted, bucket);
115 EXPECT_EQ(table.size(), 1u);
116 EXPECT_FALSE(table.is_empty());
117 EXPECT_GE(table.busy_slots(), 1u);
118}
119
120TEST(LinearHashTable, InsertMultipleElements)
121{
122 Table table;
123
124 for (int k : {5, 3, 7, 1, 4, 6, 8})
125 {
126 auto *bucket = new Bucket(k);
127 ASSERT_NE(table.insert(bucket), nullptr);
128 }
129
130 EXPECT_EQ(table.size(), 7u);
131
132 // Verify all elements can be found
133 for (int k : {5, 3, 7, 1, 4, 6, 8})
134 EXPECT_NE(table.search(k), nullptr) << "Key " << k << " not found";
135}
136
137TEST(LinearHashTable, InsertRejectsDuplicates)
138{
139 Table table;
140
141 auto *bucket1 = new Bucket(10);
142 auto *bucket2 = new Bucket(10);
143
144 EXPECT_NE(table.insert(bucket1), nullptr);
145 EXPECT_EQ(table.insert(bucket2), nullptr); // duplicate rejected
146
147 EXPECT_EQ(table.size(), 1u);
148 delete bucket2; // cleanup rejected bucket
149}
150
151TEST(LinearHashTable, InsertManyElementsTriggersExpansion)
152{
153 Table table(17); // small initial size
154
155 const size_t initial_capacity = table.capacity();
156
157 // Insert enough elements to trigger expansion
158 for (int k = 0; k < 100; ++k)
159 table.insert(new Bucket(k));
160
161 EXPECT_EQ(table.size(), 100u);
162 EXPECT_GT(table.capacity(), initial_capacity);
163 EXPECT_GT(table.expansions(), 0u);
164
165 // Verify all elements can be found
166 for (int k = 0; k < 100; ++k)
167 EXPECT_NE(table.search(k), nullptr);
168}
169
170// ============================================================================
171// Search Tests
172// ============================================================================
173
174TEST(LinearHashTable, SearchFindsExistingKey)
175{
176 Table table;
177
178 for (int k : {1, 2, 3, 4, 5})
179 table.insert(new Bucket(k));
180
181 for (int k : {1, 2, 3, 4, 5})
182 {
183 auto *found = table.search(k);
184 ASSERT_NE(found, nullptr);
185 EXPECT_EQ(found->get_key(), k);
186 }
187}
188
189TEST(LinearHashTable, SearchReturnsNullForMissingKey)
190{
191 Table table;
192
193 for (int k : {1, 3, 5})
194 table.insert(new Bucket(k));
195
196 EXPECT_EQ(table.search(2), nullptr);
197 EXPECT_EQ(table.search(4), nullptr);
198 EXPECT_EQ(table.search(0), nullptr);
199 EXPECT_EQ(table.search(6), nullptr);
200}
201
202TEST(LinearHashTable, SearchOrInsertReturnsExisting)
203{
204 Table table;
205
206 auto *bucket1 = new Bucket(10);
207 table.insert(bucket1);
208
209 auto *bucket2 = new Bucket(10);
210 auto *found = table.search_or_insert(bucket2);
211
212 EXPECT_EQ(found, bucket1);
213 EXPECT_EQ(table.size(), 1u);
214
215 delete bucket2; // cleanup rejected bucket
216}
217
218TEST(LinearHashTable, SearchOrInsertInsertsNew)
219{
220 Table table;
221
222 table.insert(new Bucket(5));
223
224 auto *bucket = new Bucket(10);
225 auto *result = table.search_or_insert(bucket);
226
227 EXPECT_EQ(result, bucket);
228 EXPECT_EQ(table.size(), 2u);
229 EXPECT_NE(table.search(10), nullptr);
230}
231
232// ============================================================================
233// Remove Tests
234// ============================================================================
235
236TEST(LinearHashTable, RemoveExistingKey)
237{
238 Table table;
239
240 for (int k : {1, 2, 3, 4, 5})
241 table.insert(new Bucket(k));
242
243 auto *found = table.search(3);
244 ASSERT_NE(found, nullptr);
245
246 auto *removed = table.remove(found);
247 EXPECT_EQ(removed->get_key(), 3);
248 delete removed;
249
250 EXPECT_EQ(table.size(), 4u);
251 EXPECT_EQ(table.search(3), nullptr);
252}
253
254TEST(LinearHashTable, RemoveAllElements)
255{
256 Table table;
257
258 for (int k : {5, 3, 7, 1, 4, 6, 8})
259 table.insert(new Bucket(k));
260
261 for (int k : {5, 3, 7, 1, 4, 6, 8})
262 {
263 auto *found = table.search(k);
264 ASSERT_NE(found, nullptr) << "Key " << k << " not found";
265 delete table.remove(found);
266 }
267
268 EXPECT_EQ(table.size(), 0u);
269 EXPECT_TRUE(table.is_empty());
270}
271
272TEST(LinearHashTable, RemoveManyElementsTriggersContraction)
273{
274 Table table(17);
275
276 // Insert many elements
277 for (int k = 0; k < 200; ++k)
278 table.insert(new Bucket(k));
279
280 const size_t expanded_capacity = table.capacity();
281
282 // Remove most elements
283 for (int k = 0; k < 180; ++k)
284 {
285 auto *found = table.search(k);
286 ASSERT_NE(found, nullptr);
287 delete table.remove(found);
288 }
289
290 EXPECT_EQ(table.size(), 20u);
291 // Capacity should have contracted
292 EXPECT_LE(table.capacity(), expanded_capacity);
293}
294
295namespace
296{
297 size_t identity_hash(const int & k) noexcept
298 {
299 return static_cast<size_t>(k);
300 }
301
302 // Number of non-empty lists, counted
303 size_t busy_lists(const Table & table)
304 {
305 const auto stats = table.stats();
306 return table.capacity() - (stats.lens.size() > 0 ? stats.lens(0) : 0);
307 }
308}
309
310// Insertions, also through search_or_insert(), and removals, with the
311// expansion and contraction turned on and off at random. After each step
312// the table holds the oracle's keys and counts its busy lists right;
313// busy_slots() counted a list for each expansion step, also when no key
314// moved, and missed one when contracting merged a list into an empty one.
315// While the table does not resize itself, its capacity stays; while it
316// does, an insertion leaves the load under the upper threshold and a
317// removal over the lower one, unless the table is back at its first size.
318TEST(LinearHashTable, RandomOperationsAgreeWithAnOracle)
319{
320 const std::pair<float, float> thresholds[] =
321 {{0.25f, 0.9f}, {0.5f, 2.0f}, {0.01f, 0.99f}, {0.85f, 0.9f}};
322 std::mt19937 rng(20261021);
323 for (int trial = 0; trial < 200; ++trial)
324 {
325 const auto [lower, upper] = thresholds[rng() % 4];
326 const bool resizing = rng() % 2 == 0;
327 const size_t len = 1 + rng() % 13;
328 Table table(len, identity_hash, Aleph::equal_to<int>(), lower, upper,
329 true, resizing);
330 std::map<int, Bucket *> oracle;
331 for (int step = 0; step < 400; ++step)
332 {
333 const auto where = [&] { return ::testing::Message()
334 << "trial " << trial << ", step " << step; };
335 const int k = static_cast<int>(rng() % 80);
336 const unsigned op = rng() % 9;
337 const bool resizes = table.get_with_resize();
338 const size_t capacity = table.capacity();
339 const bool present = oracle.count(k) > 0;
340
341 if (op < 4)
342 {
343 auto * bucket = new Bucket(k);
344 Bucket * stored = op < 2 ? table.insert(bucket)
345 : table.search_or_insert(bucket);
346 if (present)
347 {
348 ASSERT_EQ(stored, op < 2 ? nullptr : oracle[k]) << where();
349 delete bucket;
350 }
351 else
352 {
353 ASSERT_EQ(stored, bucket) << where();
354 oracle[k] = bucket;
355 if (resizes)
356 {
357 ASSERT_LT(table.current_alpha(), upper) << where();
358 }
359 }
360 }
361 else if (op < 8)
362 {
363 if (present)
364 {
365 delete table.remove(oracle[k]);
366 oracle.erase(k);
367 if (resizes)
368 {
369 ASSERT_TRUE(table.current_alpha() > lower or
370 table.capacity() == len) << where();
371 }
372 }
373 }
374 else if (rng() % 4 == 0)
375 table.set_with_resize(not resizes);
376
377 if (not resizes and not table.get_with_resize())
378 {
379 ASSERT_EQ(table.capacity(), capacity) << where();
380 }
381 ASSERT_GE(table.capacity(), len) << where();
382 ASSERT_EQ(table.size(), oracle.size()) << where();
383 ASSERT_EQ(table.busy_slots(), busy_lists(table)) << where();
384 for (int key = 0; key < 80; ++key)
385 ASSERT_EQ(table.search(key), oracle.count(key) ? oracle[key] : nullptr)
386 << "key " << key << ", " << where();
387 }
388 for (auto & [key, bucket] : oracle)
389 delete table.remove(bucket);
390 }
391}
392
393// The constructor took with_resize and ignored it: a table built without
394// it expanded all the same.
395TEST(LinearHashTable, WithResizeTurnsExpansionAndContractionOnAndOff)
396{
397 Table table(17, Aleph::dft_hash_ptr_fct<int>, Aleph::equal_to<int>(),
399 EXPECT_FALSE(table.get_with_resize());
400 const size_t initial_capacity = table.capacity();
401
402 for (int k = 0; k < 200; ++k)
403 ASSERT_NE(table.insert(new Bucket(k)), nullptr) << "key " << k;
404 EXPECT_EQ(table.capacity(), initial_capacity);
405 EXPECT_EQ(table.expansions(), 0u);
406 for (int k = 0; k < 200; ++k)
407 EXPECT_NE(table.search(k), nullptr) << "key " << k;
408
409 // turned on, the next insertion expands it
410 table.set_with_resize(true);
411 EXPECT_TRUE(table.get_with_resize());
412 ASSERT_NE(table.insert(new Bucket(200)), nullptr);
413 const size_t expanded_capacity = table.capacity();
414 EXPECT_GT(expanded_capacity, initial_capacity);
415
416 // turned off, removals do not contract it
417 table.set_with_resize(false);
418 for (int k = 0; k < 150; ++k)
419 delete table.remove(table.search(k));
420 EXPECT_EQ(table.capacity(), expanded_capacity);
421
422 // and turned on again, they do
423 table.set_with_resize(true);
424 for (int k = 150; k < 190; ++k)
425 delete table.remove(table.search(k));
426 EXPECT_LT(table.capacity(), expanded_capacity);
427
428 EXPECT_EQ(table.size(), 11u);
429 for (int k = 190; k <= 200; ++k)
430 EXPECT_NE(table.search(k), nullptr) << "key " << k;
431}
432
433// ============================================================================
434// Iterator Tests
435// ============================================================================
436
437TEST(LinearHashTable, IteratorTraversesAllElements)
438{
439 Table table;
440
441 std::set<int> inserted;
442 for (int k : {5, 3, 7, 1, 4, 6, 8})
443 {
444 table.insert(new Bucket(k));
445 inserted.insert(k);
446 }
447
448 std::set<int> visited;
449 for (Table::Iterator it(table); it.has_curr(); it.next())
450 visited.insert(it.get_curr()->get_key());
451
452 EXPECT_EQ(visited, inserted);
453}
454
455TEST(LinearHashTable, IteratorOnEmptyTable)
456{
457 Table table;
458 Table::Iterator it(table);
459
460 EXPECT_FALSE(it.has_curr());
461}
462
464{
465 Table table;
466
467 for (int k : {1, 2, 3, 4, 5})
468 table.insert(new Bucket(k));
469
470 // Find and delete element 3 using iterator
471 for (Table::Iterator it(table); it.has_curr(); it.next())
472 {
473 if (it.get_curr()->get_key() == 3)
474 {
475 delete it.del();
476 break;
477 }
478 }
479
480 EXPECT_EQ(table.size(), 4u);
481 EXPECT_EQ(table.search(3), nullptr);
482}
483
484TEST(LinearHashTable, IteratorGetPos)
485{
486 Table table;
487
488 for (int k : {1, 2, 3, 4, 5})
489 table.insert(new Bucket(k));
490
491 Table::Iterator it(table);
492 EXPECT_EQ(it.get_pos(), 0);
493
494 it.next();
495 EXPECT_EQ(it.get_pos(), 1);
496
497 it.next();
498 EXPECT_EQ(it.get_pos(), 2);
499}
500
501TEST(LinearHashTable, IteratorPrev)
502{
503 Table table;
504
505 for (int k : {1, 2, 3})
506 table.insert(new Bucket(k));
507
508 Table::Iterator it(table);
509 it.next();
510 it.next();
511 EXPECT_EQ(it.get_pos(), 2);
512
513 it.prev();
514 EXPECT_EQ(it.get_pos(), 1);
515
516 it.prev();
517 EXPECT_EQ(it.get_pos(), 0);
518}
519
520TEST(LinearHashTable, IteratorNoExceptMethods)
521{
522 Table table;
523
524 for (int k : {1, 2, 3})
525 table.insert(new Bucket(k));
526
527 Table::Iterator it(table);
528
529 // Test get_curr_ne() and next_ne()
530 auto *bucket1 = it.get_curr_ne();
531 EXPECT_NE(bucket1, nullptr);
532
533 it.next_ne();
534 auto *bucket2 = it.get_curr_ne();
535 EXPECT_NE(bucket2, nullptr);
536 EXPECT_NE(bucket1, bucket2);
537}
538
539// ============================================================================
540// Swap Tests
541// ============================================================================
542
543// NOTE: swap() has a known issue with entries_list and embedded Dlink nodes
544// which causes use-after-free in destructors. This test uses remove_all_buckets=false
545// and manually cleans up to avoid the issue.
547{
548 // Use remove_all_buckets=false to avoid destructor cleanup issues after swap
549 Table table1(17, Aleph::dft_hash_fct<int>, Aleph::equal_to<int>(),
551 Table table2(17, Aleph::dft_hash_fct<int>, Aleph::equal_to<int>(),
553
554 std::vector<Bucket*> buckets1, buckets2;
555 for (int k : {1, 2, 3})
556 {
557 auto* b = new Bucket(k);
558 table1.insert(b);
559 buckets1.push_back(b);
560 }
561 for (int k : {10, 20})
562 {
563 auto* b = new Bucket(k);
564 table2.insert(b);
565 buckets2.push_back(b);
566 }
567
568 table1.swap(table2);
569
570 EXPECT_EQ(table1.size(), 2u);
571 EXPECT_EQ(table2.size(), 3u);
572
573 EXPECT_NE(table1.search(10), nullptr);
574 EXPECT_NE(table1.search(20), nullptr);
575 EXPECT_NE(table2.search(1), nullptr);
576 EXPECT_NE(table2.search(2), nullptr);
577 EXPECT_NE(table2.search(3), nullptr);
578
579 // Manual cleanup since remove_all_buckets=false
580 for (auto* b : buckets1) delete b;
581 for (auto* b : buckets2) delete b;
582}
583
584// ============================================================================
585// Empty Method Tests
586// ============================================================================
587
588TEST(LinearHashTable, EmptyMethodClearsTable)
589{
590 Table table;
591
592 for (int k = 0; k < 50; ++k)
593 table.insert(new Bucket(k));
594
595 EXPECT_EQ(table.size(), 50u);
596
597 table.empty();
598
599 EXPECT_EQ(table.size(), 0u);
600 EXPECT_TRUE(table.is_empty());
601 EXPECT_EQ(table.expansions(), 0u);
602
603 // empty() kept the count of busy lists, and later insertions added to it
604 EXPECT_EQ(table.busy_slots(), 0u);
605 for (int k = 0; k < 50; ++k)
606 table.insert(new Bucket(k));
607 EXPECT_EQ(table.busy_slots(), busy_lists(table));
608}
609
610// ============================================================================
611// Load Factor Tests
612// ============================================================================
613
614TEST(LinearHashTable, CurrentAlphaIsCorrect)
615{
616 Table table(100);
617
618 EXPECT_FLOAT_EQ(table.current_alpha(), 0.0f);
619
620 for (int k = 0; k < 50; ++k)
621 table.insert(new Bucket(k));
622
623 float expected_alpha = 50.0f / table.capacity();
624 EXPECT_NEAR(table.current_alpha(), expected_alpha, 0.01f);
625}
626
627// ============================================================================
628// Custom Comparator Tests
629// ============================================================================
630
632{
633 bool operator()(int a, int b) const { return (a % 100) == (b % 100); }
634};
635
636// Custom hash that aligns with ModCompare
637size_t mod_hash(const int& k) { return static_cast<size_t>(std::abs(k % 100)); }
638
639TEST(LinearHashTable, CustomComparator)
640{
641 // Use custom hash that aligns with ModCompare so duplicates are detected
643
644 table.insert(new LinHashBucket<int>(105));
645
646 // 205 should be "equal" to 105 with ModCompare (both hash to 5, compare equal)
647 auto *bucket = new LinHashBucket<int>(205);
648 EXPECT_EQ(table.insert(bucket), nullptr); // rejected as duplicate
649
650 delete bucket;
651
652 // Search for 5 should find 105 (5 % 100 == 105 % 100)
653 EXPECT_NE(table.search(5), nullptr);
654}
655
656// ============================================================================
657// Edge Cases
658// ============================================================================
659
660TEST(LinearHashTable, NegativeKeys)
661{
662 Table table;
663
664 for (int k : {-5, -3, -1, 0, 1, 3, 5})
665 table.insert(new Bucket(k));
666
667 EXPECT_EQ(table.size(), 7u);
668
669 for (int k : {-5, -3, -1, 0, 1, 3, 5})
670 EXPECT_NE(table.search(k), nullptr);
671
672 EXPECT_EQ(table.search(-2), nullptr);
673}
674
675TEST(LinearHashTable, SingleElementOperations)
676{
677 Table table;
678
679 auto *bucket = new Bucket(42);
680 table.insert(bucket);
681
682 EXPECT_EQ(table.size(), 1u);
683 EXPECT_NE(table.search(42), nullptr);
684
685 delete table.remove(bucket);
686
687 EXPECT_EQ(table.size(), 0u);
688 EXPECT_EQ(table.search(42), nullptr);
689}
690
691// ============================================================================
692// Constructor Parameter Validation Tests
693// ============================================================================
694
695TEST(LinearHashTable, ConstructorWithZeroLengthThrows)
696{
697 EXPECT_THROW(
698 (Table(0, Aleph::dft_hash_fct<int>, Aleph::equal_to<int>(),
700 std::length_error);
701}
702
703TEST(LinearHashTable, ConstructorWithInvalidAlphaThrows)
704{
705 // upper_alpha <= lower_alpha should throw
706 EXPECT_THROW(
707 (Table(17, Aleph::dft_hash_fct<int>, Aleph::equal_to<int>(),
708 0.8f, 0.4f, true, true)), // lower > upper
709 std::domain_error);
710
711 EXPECT_THROW(
712 (Table(17, Aleph::dft_hash_fct<int>, Aleph::equal_to<int>(),
713 0.5f, 0.5f, true, true)), // lower == upper
714 std::domain_error);
715}
716
717// ============================================================================
718// Stress Tests
719// ============================================================================
720
721TEST(LinearHashTable, RandomInsertSearchRemove)
722{
723 Table table;
724 std::set<int> oracle;
725
726 std::mt19937 rng(12345);
727 std::uniform_int_distribution<int> dist(0, 999);
728
729 // Insert phase
730 for (int i = 0; i < 500; ++i)
731 {
732 int k = dist(rng);
733 if (oracle.count(k) == 0)
734 {
735 ASSERT_NE(table.insert(new Bucket(k)), nullptr);
736 oracle.insert(k);
737 }
738 }
739
740 EXPECT_EQ(table.size(), oracle.size());
741
742 // Search phase
743 for (int i = 0; i < 200; ++i)
744 {
745 int k = dist(rng);
746 auto *found = table.search(k);
747 if (oracle.count(k))
748 EXPECT_NE(found, nullptr);
749 else
750 EXPECT_EQ(found, nullptr);
751 }
752
753 // Remove phase
754 for (int i = 0; i < 200; ++i)
755 {
756 int k = dist(rng);
757 auto *found = table.search(k);
758 if (oracle.count(k))
759 {
760 ASSERT_NE(found, nullptr);
761 oracle.erase(k);
762 delete table.remove(found);
763 }
764 else
765 {
766 EXPECT_EQ(found, nullptr);
767 }
768 }
769
770 EXPECT_EQ(table.size(), oracle.size());
771
772 // Verify remaining keys
773 for (int k : oracle)
774 EXPECT_NE(table.search(k), nullptr);
775}
776
777TEST(LinearHashTable, LargeTableOperations)
778{
779 Table table;
780
781 const int N = 5000;
782
783 // Insert N elements
784 for (int k = 0; k < N; ++k)
785 table.insert(new Bucket(k));
786
787 EXPECT_EQ(table.size(), static_cast<size_t>(N));
788
789 // Verify all elements present
790 for (int k = 0; k < N; ++k)
791 EXPECT_NE(table.search(k), nullptr);
792
793 // Remove half
794 for (int k = 0; k < N; k += 2)
795 {
796 auto *found = table.search(k);
797 ASSERT_NE(found, nullptr);
798 delete table.remove(found);
799 }
800
801 EXPECT_EQ(table.size(), static_cast<size_t>(N / 2));
802
803 // Verify remaining elements
804 for (int k = 1; k < N; k += 2)
805 EXPECT_NE(table.search(k), nullptr);
806}
807
808// ============================================================================
809// LinearHashTableVtl Tests
810// ============================================================================
811
812TEST(LinearHashTableVtl, BasicOperations)
813{
815
816 for (int k : {1, 2, 3, 4, 5})
817 table.insert(new LinHashBucketVtl<int>(k));
818
819 EXPECT_EQ(table.size(), 5u);
820
821 for (int k : {1, 2, 3, 4, 5})
822 EXPECT_NE(table.search(k), nullptr);
823
824 // Destructor should properly clean up with virtual destructor
825}
826
827// ============================================================================
828// Hash Function Tests
829// ============================================================================
830
831TEST(LinearHashTable, SetHashFunction)
832{
833 Table table;
834
835 // Custom hash function - use std::function explicitly to avoid ambiguity
836 Table::Hash_Fct custom_hash = [](const int& k) -> size_t { return k * 17; };
837
839
840 table.insert(new Bucket(1));
841 table.insert(new Bucket(2));
842 table.insert(new Bucket(3));
843
844 EXPECT_NE(table.search(1), nullptr);
845 EXPECT_NE(table.search(2), nullptr);
846 EXPECT_NE(table.search(3), nullptr);
847}
848
849TEST(LinearHashTable, GetHashFunction)
850{
851 Table table;
852
853 auto hash_fct = table.get_hash_fct();
854 EXPECT_NE(hash_fct, nullptr);
855}
856
858{
859 Table table;
860
861 auto& cmp = table.get_compare();
862 EXPECT_TRUE(cmp(5, 5));
863 EXPECT_FALSE(cmp(5, 6));
864
865 const Table& const_table = table;
866 const auto& const_cmp = const_table.get_compare();
867 EXPECT_TRUE(const_cmp(10, 10));
868}
869
870// ============================================================================
871// Resize Method Tests
872// ============================================================================
873
874TEST(LinearHashTable, ResizeReturnsCapacity)
875{
876 Table table;
877
878 // resize() is a no-op for linear hash tables (provided for compatibility)
879 size_t result = table.resize(1000);
880 EXPECT_EQ(result, table.capacity());
881}
Key * insert(const Key &key)
Inserts key into the hash set.
Key * search(const Key &key) noexcept
Searches for a key in the hash table.
Generic linear hash table.
const size_t & capacity() const noexcept
Returns the table capacity.
void swap(GenLinearHashTable &other) noexcept
Swaps this table with other in constant time.
std::function< size_t(const Key &)> Hash_Fct
The hash function type.
Cmp & get_compare() noexcept
const size_t & size() const noexcept
Returns the number of elements in the table.
bool is_empty() const noexcept
return true is table is empty
void empty() noexcept
Empties the table: frees every bucket and gives the table its initial size.
const size_t & expansions() const noexcept
Returns the expansion level performed on the table.
float current_alpha() const noexcept
return the current table load
Bucket * remove(Bucket *bucket) noexcept
Removes bucket from the table, without freeing it.
BucketType< Key > Bucket
The bucket type.
const size_t & busy_slots() const noexcept
Returns the number of busy slots in the table.
Bucket * insert(Bucket *bucket)
Insert bucket in the table.
Hash_Fct get_hash_fct() const noexcept
Bucket * search_or_insert(Bucket *bucket)
void set_hash_fct(Hash_Fct fct)
Sets the hash function of an empty table.
Bucket * search(const Key &key) const noexcept
Search for key in the linear hash table.
size_t resize(size_t) noexcept
Provided for generic programming compatibility.
Bucket with virtual destructor for a hash table with collision resolution by separate chaining.
Bucket without virtual destructor for a hash table with collision resolution by separate chaining.
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
void set_with_resize(const bool with_resize) noexcept
Turns the automatic resizing on or off.
Definition hashDry.H:1580
Stats stats() const
Computes statistics about chain length distribution.
Definition hashDry.H:1470
constexpr bool get_with_resize() const noexcept
Returns whether the table resizes itself.
Definition hashDry.H:1569
#define TEST(name)
static mt19937 rng
#define N
Definition fib.C:294
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
Table::Bucket Bucket
Definition lin-hash.cc:57
size_t mod_hash(const int &k)
Definition lin-hash.cc:637
LinearHashTable< int > Table
Definition lin-hash.cc:56
Main namespace for Aleph-w library functions.
const float hash_default_upper_alpha
Definition hash-dry.C:40
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
and
Check uniqueness with explicit hash + equality functors.
const float hash_default_lower_alpha
Definition hash-dry.C:38
STL namespace.
bool operator()(int a, int b) const
Definition lin-hash.cc:633
static int * k
size_t custom_hash(const int &key)
Dynamic set implementations based on hash tables.
Linear hashing with chaining.