Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dynsethash.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
38#include <algorithm>
39#include <stdexcept>
40#include <vector>
41#include <string>
42#include <type_traits>
43#include <gtest/gtest.h>
44
45#include <tpl_dynSetHash.H>
46
47using namespace std;
48using namespace Aleph;
49
50// ============================================================================
51// Basic Operations Tests - DynHashTable
52// ============================================================================
53
54TEST(DynHashTable, EmptyTableProperties)
55{
56 DynSetLhash<int> table;
57
58 EXPECT_TRUE(table.is_empty());
59 EXPECT_EQ(table.size(), 0u);
60 EXPECT_FALSE(table.contains(42));
61 EXPECT_EQ(table.search(42), nullptr);
62 EXPECT_FALSE(table.has(42));
63}
64
65TEST(DynHashTable, InsertSingleElement)
66{
67 DynSetLhash<int> table;
68
69 auto * p = table.insert(42);
70 ASSERT_NE(p, nullptr);
71 EXPECT_EQ(*p, 42);
72 EXPECT_FALSE(table.is_empty());
73 EXPECT_EQ(table.size(), 1u);
74 EXPECT_TRUE(table.contains(42));
75 EXPECT_TRUE(table.has(42));
76}
77
78TEST(DynHashTable, InsertMultipleElements)
79{
80 DynSetLhash<int> table;
81
82 for (int i : {5, 3, 7, 1, 9, 2, 8})
83 {
84 auto * p = table.insert(i);
85 ASSERT_NE(p, nullptr);
86 EXPECT_EQ(*p, i);
87 }
88
89 EXPECT_EQ(table.size(), 7u);
90
91 for (int i : {1, 2, 3, 5, 7, 8, 9})
92 EXPECT_TRUE(table.contains(i));
93
94 EXPECT_FALSE(table.contains(4));
95 EXPECT_FALSE(table.contains(6));
96}
97
98TEST(DynHashTable, InsertRejectsDuplicates)
99{
100 DynSetLhash<int> table;
101
102 auto * p1 = table.insert(42);
103 ASSERT_NE(p1, nullptr);
104 EXPECT_EQ(*p1, 42);
105 EXPECT_EQ(table.size(), 1u);
106
107 auto * p2 = table.insert(42);
108 EXPECT_EQ(p2, nullptr); // Duplicate rejected
109 EXPECT_EQ(table.size(), 1u); // Size unchanged
110}
111
112TEST(DynHashTable, SearchFindsExistingKey)
113{
114 DynSetLhash<int> table;
115
116 table.insert(10);
117 table.insert(20);
118 table.insert(30);
119
120 auto * p = table.search(20);
121 ASSERT_NE(p, nullptr);
122 EXPECT_EQ(*p, 20);
123}
124
125TEST(DynHashTable, SearchReturnsNullForMissingKey)
126{
127 DynSetLhash<int> table;
128
129 table.insert(10);
130 table.insert(30);
131
132 EXPECT_EQ(table.search(20), nullptr);
133}
134
135TEST(DynHashTable, FindReturnsReference)
136{
137 DynSetLhash<int> table;
138
139 table.insert(42);
140
141 const auto & key = table.find(42);
142 EXPECT_EQ(key, 42);
143
144 // Non-const version returns modifiable reference
145 auto & key2 = table.find(42);
146 EXPECT_EQ(key2, 42);
147
148 // WARNING: Modifying a key through the reference breaks the hash table invariants!
149 // The key is stored at hash(42)'s bucket, but if we change it to 99,
150 // it won't be found at hash(99)'s bucket. This is UNDEFINED BEHAVIOR.
151 // So we just verify that find() returns a non-const reference without actually modifying it.
152}
153
154TEST(DynHashTable, FindThrowsIfNotFound)
155{
156 DynSetLhash<int> table;
157
158 table.insert(10);
159
160 EXPECT_THROW(table.find(42), domain_error);
161}
162
163// ============================================================================
164// Insert Variations Tests
165// ============================================================================
166
167TEST(DynHashTable, SearchOrInsertBehavior)
168{
169 DynSetLhash<int> table;
170
171 // First call: inserts
172 auto * p1 = table.search_or_insert(42);
173 ASSERT_NE(p1, nullptr);
174 EXPECT_EQ(*p1, 42);
175 EXPECT_EQ(table.size(), 1u);
176
177 // Second call: returns existing
178 auto * p2 = table.search_or_insert(42);
179 ASSERT_NE(p2, nullptr);
180 EXPECT_EQ(*p2, 42);
181 EXPECT_EQ(table.size(), 1u); // Size unchanged
182 EXPECT_EQ(p1, p2); // Same pointer
183}
184
185TEST(DynHashTable, ContainsOrInsertBehavior)
186{
187 DynSetLhash<int> table;
188
189 // First call: inserts, returns {ptr, false}
190 auto [p1, found1] = table.contains_or_insert(42);
191 ASSERT_NE(p1, nullptr);
192 EXPECT_EQ(*p1, 42);
193 EXPECT_FALSE(found1); // Was inserted
194 EXPECT_EQ(table.size(), 1u);
195
196 // Second call: finds, returns {ptr, true}
197 auto [p2, found2] = table.contains_or_insert(42);
198 ASSERT_NE(p2, nullptr);
199 EXPECT_EQ(*p2, 42);
200 EXPECT_TRUE(found2); // Already existed
201 EXPECT_EQ(table.size(), 1u);
202 EXPECT_EQ(p1, p2); // Same pointer
203}
204
205TEST(DynHashTable, AddAndAppendAliases)
206{
207 DynSetLhash<int> table;
208
209 auto * p1 = table.add(10);
210 ASSERT_NE(p1, nullptr);
211 EXPECT_EQ(*p1, 10);
212
213 auto * p2 = table.append(20);
214 ASSERT_NE(p2, nullptr);
215 EXPECT_EQ(*p2, 20);
216
217 EXPECT_EQ(table.size(), 2u);
218}
219
220TEST(DynHashTable, MoveInsert)
221{
223
224 string s = "hello";
225 auto * p = table.insert(std::move(s));
226 ASSERT_NE(p, nullptr);
227 EXPECT_EQ(*p, "hello");
228 // s should be in moved-from state (typically empty, but not guaranteed)
229}
230
231// ============================================================================
232// Remove Tests
233// ============================================================================
234
235TEST(DynHashTable, RemoveByKey)
236{
237 DynSetLhash<int> table;
238
239 table.insert(10);
240 table.insert(20);
241 table.insert(30);
242
243 int removed = table.remove(20);
244 EXPECT_EQ(removed, 20);
245 EXPECT_EQ(table.size(), 2u);
246 EXPECT_FALSE(table.contains(20));
247 EXPECT_TRUE(table.contains(10));
248 EXPECT_TRUE(table.contains(30));
249}
250
251TEST(DynHashTable, RemoveThrowsIfNotFound)
252{
253 DynSetLhash<int> table;
254
255 table.insert(10);
256
257 EXPECT_THROW(table.remove(42), domain_error);
258}
259
260TEST(DynHashTable, RemoveByPointer)
261{
262 DynSetLhash<int> table;
263
264 table.insert(10);
265 auto * p20 = table.insert(20);
266 table.insert(30);
267
268 ASSERT_NE(p20, nullptr);
269
270 // Remove by pointer
271 table.remove(p20);
272
273 EXPECT_EQ(table.size(), 2u);
274 EXPECT_FALSE(table.contains(20));
275 EXPECT_TRUE(table.contains(10));
276 EXPECT_TRUE(table.contains(30));
277}
278
279TEST(DynHashTable, RemoveAllElements)
280{
281 DynSetLhash<int> table;
282
283 for (int i = 0; i < 50; ++i)
284 table.insert(i);
285
286 EXPECT_EQ(table.size(), 50u);
287
288 for (int i = 0; i < 50; ++i)
289 table.remove(i);
290
291 EXPECT_TRUE(table.is_empty());
292 EXPECT_EQ(table.size(), 0u);
293}
294
295TEST(DynHashTable, EmptyMethod)
296{
297 DynSetLhash<int> table;
298
299 for (int i = 0; i < 100; ++i)
300 table.insert(i);
301
302 EXPECT_EQ(table.size(), 100u);
303
304 table.empty();
305
306 EXPECT_TRUE(table.is_empty());
307 EXPECT_EQ(table.size(), 0u);
308
309 table.insert(1);
310 table.clear();
311 EXPECT_TRUE(table.is_empty());
312}
313
314// ============================================================================
315// Copy/Move Semantics Tests
316// ============================================================================
317
318TEST(DynHashTable, CopyConstructor)
319{
320 DynSetLhash<int> table1;
321
322 for (int i : {1, 2, 3, 4, 5})
323 table1.insert(i);
324
325 DynSetLhash<int> table2(table1);
326
327 EXPECT_EQ(table2.size(), 5u);
328 for (int i : {1, 2, 3, 4, 5})
329 EXPECT_TRUE(table2.contains(i));
330
331 // Verify independence
332 table1.remove(3);
333 EXPECT_FALSE(table1.contains(3));
334 EXPECT_TRUE(table2.contains(3));
335}
336
337TEST(DynHashTable, CopyAssignment)
338{
339 DynSetLhash<int> table1, table2;
340
341 for (int i : {1, 2, 3})
342 table1.insert(i);
343
344 for (int i : {10, 20})
345 table2.insert(i);
346
347 table2 = table1;
348
349 EXPECT_EQ(table2.size(), 3u);
350 for (int i : {1, 2, 3})
351 EXPECT_TRUE(table2.contains(i));
352 EXPECT_FALSE(table2.contains(10));
353 EXPECT_FALSE(table2.contains(20));
354}
355
356TEST(DynHashTable, SelfAssignment)
357{
358 DynSetLhash<int> table;
359
360 for (int i : {1, 2, 3})
361 table.insert(i);
362
363 table = table;
364
365 EXPECT_EQ(table.size(), 3u);
366 for (int i : {1, 2, 3})
367 EXPECT_TRUE(table.contains(i));
368}
369
370TEST(DynHashTable, MoveConstructor)
371{
372 DynSetLhash<int> table1;
373
374 for (int i : {1, 2, 3, 4, 5})
375 table1.insert(i);
376
377 DynSetLhash<int> table2(std::move(table1));
378
379 EXPECT_EQ(table2.size(), 5u);
380 for (int i : {1, 2, 3, 4, 5})
381 EXPECT_TRUE(table2.contains(i));
382
383 EXPECT_TRUE(table1.is_empty());
384}
385
386TEST(DynHashTable, MoveAssignment)
387{
388 DynSetLhash<int> table1, table2;
389
390 for (int i : {1, 2, 3})
391 table1.insert(i);
392
393 for (int i : {10, 20})
394 table2.insert(i);
395
396 table2 = std::move(table1);
397
398 EXPECT_EQ(table2.size(), 3u);
399 for (int i : {1, 2, 3})
400 EXPECT_TRUE(table2.contains(i));
401
402 // After fix: Move assignment should leave table1 empty
403 EXPECT_TRUE(table1.is_empty());
404 EXPECT_EQ(table1.size(), 0u);
405}
406
408{
409 DynSetLhash<int> table1, table2;
410
411 for (int i : {1, 2, 3})
412 table1.insert(i);
413
414 for (int i : {10, 20})
415 table2.insert(i);
416
417 table1.swap(table2);
418
419 EXPECT_EQ(table1.size(), 2u);
420 EXPECT_TRUE(table1.contains(10));
421 EXPECT_TRUE(table1.contains(20));
422
423 EXPECT_EQ(table2.size(), 3u);
424 EXPECT_TRUE(table2.contains(1));
425 EXPECT_TRUE(table2.contains(2));
426 EXPECT_TRUE(table2.contains(3));
427}
428
429// ============================================================================
430// Iterator Tests
431// ============================================================================
432
433TEST(DynHashTable, IteratorEmptyTable)
434{
435 DynSetLhash<int> table;
436
438 EXPECT_FALSE(it.has_curr());
439}
440
441TEST(DynHashTable, IteratorTraversal)
442{
443 DynSetLhash<int> table;
444
445 for (int i : {5, 3, 7, 1, 9})
446 table.insert(i);
447
448 vector<int> keys;
449 for (DynSetLhash<int>::Iterator it(table); it.has_curr(); it.next_ne())
450 keys.push_back(it.get_curr());
451
452 EXPECT_EQ(keys.size(), 5u);
453
454 // Hash table doesn't guarantee order, just verify all keys present
455 sort(keys.begin(), keys.end());
456 vector<int> expected = {1, 3, 5, 7, 9};
457 EXPECT_EQ(keys, expected);
458}
459
460TEST(DynHashTable, IteratorDel)
461{
462 DynSetLhash<int> table;
463
464 for (int i : {1, 2, 3, 4, 5})
465 table.insert(i);
466
468 ASSERT_TRUE(it.has_curr());
469
470 int first = it.get_curr();
471 it.del();
472
473 EXPECT_EQ(table.size(), 4u);
474 EXPECT_FALSE(table.contains(first));
475}
476
477TEST(DynHashTable, GetFirstAndLast)
478{
479 DynSetLhash<int> table;
480
481 table.insert(10);
482 table.insert(20);
483 table.insert(30);
484
485 // Just verify they don't throw
486 EXPECT_NO_THROW(table.get_first());
487 EXPECT_NO_THROW(table.get_last());
488}
489
490// ============================================================================
491// String Keys Tests
492// ============================================================================
493
494TEST(DynHashTable, StringKeys)
495{
497
498 table.insert("apple");
499 table.insert("banana");
500 table.insert("cherry");
501
502 EXPECT_EQ(table.size(), 3u);
503 EXPECT_TRUE(table.contains("apple"));
504 EXPECT_TRUE(table.contains("banana"));
505 EXPECT_FALSE(table.contains("date"));
506}
507
508// ============================================================================
509// Stress Tests
510// ============================================================================
511
512TEST(DynHashTable, LargeNumberOfInsertions)
513{
514 DynSetLhash<int> table;
515
516 // Insert 10000 elements
517 for (int i = 0; i < 10000; ++i)
518 table.insert(i);
519
520 EXPECT_EQ(table.size(), 10000u);
521
522 // Verify all present
523 for (int i = 0; i < 10000; ++i)
524 EXPECT_TRUE(table.contains(i));
525}
526
527TEST(DynHashTable, ManyCollisions)
528{
529 // Custom hash that always returns same value -> forces collisions
530 auto bad_hash = [](const int &) -> size_t { return 42; };
531
532 DynSetLhash<int> table(100, bad_hash);
533
534 for (int i = 0; i < 100; ++i)
535 table.insert(i);
536
537 EXPECT_EQ(table.size(), 100u);
538
539 for (int i = 0; i < 100; ++i)
540 EXPECT_TRUE(table.contains(i));
541}
542
543TEST(DynHashTable, RandomInsertRemove)
544{
545 DynSetLhash<int> table;
546 vector<int> inserted;
547
548 // Random insertions
549 for (int i = 0; i < 500; ++i)
550 {
551 if (int key = rand() % 1000; table.insert(key) != nullptr)
552 inserted.push_back(key);
553 }
554
555 // Verify all inserted keys are present
556 for (int key : inserted)
557 EXPECT_TRUE(table.contains(key));
558
559 EXPECT_EQ(table.size(), inserted.size());
560
561 // Random removals
562 for (size_t i = 0; i < inserted.size() / 2; ++i)
563 {
564 int idx = rand() % inserted.size();
565 table.remove(inserted[idx]);
566 inserted.erase(inserted.begin() + idx);
567 }
568
569 EXPECT_EQ(table.size(), inserted.size());
570 for (int key : inserted)
571 EXPECT_TRUE(table.contains(key));
572}
573
574TEST(DynHashTable, RehashingBehavior)
575{
576 DynSetLhash<int> table(10); // Start small
577
578 // Insert many elements to force rehashing
579 for (int i = 0; i < 1000; ++i)
580 table.insert(i);
581
582 EXPECT_EQ(table.size(), 1000u);
583
584 // Verify all still accessible
585 for (int i = 0; i < 1000; ++i)
586 EXPECT_TRUE(table.contains(i));
587
588 // Remove many to trigger shrinking
589 for (int i = 0; i < 950; ++i)
590 table.remove(i);
591
592 EXPECT_EQ(table.size(), 50u);
593
594 for (int i = 950; i < 1000; ++i)
595 EXPECT_TRUE(table.contains(i));
596}
597
598// ============================================================================
599// DynMapHashTable Tests
600// ============================================================================
601
602TEST(DynMapHashTable, EmptyMapProperties)
603{
605
606 EXPECT_TRUE(map.is_empty());
607 EXPECT_EQ(map.size(), 0u);
608 EXPECT_FALSE(map.contains(42));
609 EXPECT_EQ(map.search(42), nullptr);
610}
611
613{
615
616 auto * p = map.insert(1, "one");
617 ASSERT_NE(p, nullptr);
618 EXPECT_EQ(p->first, 1);
619 EXPECT_EQ(p->second, "one");
620 EXPECT_EQ(map.size(), 1u);
621}
622
623TEST(DynMapHashTable, InsertMultiplePairs)
624{
626
627 map.insert(1, "one");
628 map.insert(2, "two");
629 map.insert(3, "three");
630
631 EXPECT_EQ(map.size(), 3u);
632 EXPECT_TRUE(map.contains(1));
633 EXPECT_TRUE(map.contains(2));
634 EXPECT_TRUE(map.contains(3));
635}
636
637TEST(DynMapHashTable, InsertRejectsDuplicateKeys)
638{
640
641 auto * p1 = map.insert(1, "one");
642 ASSERT_NE(p1, nullptr);
643
644 auto * p2 = map.insert(1, "uno");
645 EXPECT_EQ(p2, nullptr); // Duplicate key rejected
646 EXPECT_EQ(map.size(), 1u);
647
648 // Original value unchanged
649 EXPECT_EQ(map.find(1), "one");
650}
651
653{
655
656 map.insert(1, "one");
657 map.insert(2, "two");
658
659 auto * p = map.search(2);
660 ASSERT_NE(p, nullptr);
661 EXPECT_EQ(p->first, 2);
662 EXPECT_EQ(p->second, "two");
663
664 EXPECT_EQ(map.search(99), nullptr);
665}
666
668{
670
671 map.insert(1, "one");
672
673 EXPECT_EQ(map.find(1), "one");
674 EXPECT_THROW(map.find(99), domain_error);
675}
676
677namespace
678{
679 // Mapped type without a default constructor.
680 struct No_Default_Data
681 {
682 int value;
683 explicit No_Default_Data(const int v) : value(v) {}
684 };
685}
686
687// search(), has() and contains() look a key up without building a
688// Pair(key, Data()), so they compile when Data has no default constructor.
689// MSVC once took the Pair(key, Data()) fallback for every map.
690TEST(DynMapHashTable, SearchWithoutDefaultConstructibleData)
691{
692 static_assert(not std::is_default_constructible_v<No_Default_Data>);
693
695 map.insert(1, No_Default_Data(10));
696 map.insert(2, No_Default_Data(20));
697
698 ASSERT_NE(map.search(1), nullptr);
699 EXPECT_EQ(map.search(1)->second.value, 10);
700 EXPECT_EQ(map.search(3), nullptr);
701 EXPECT_NE(map.search(2 + 0), nullptr); // rvalue key
702
703 const auto & const_map = map;
704 ASSERT_NE(const_map.search(2), nullptr);
705 EXPECT_EQ(const_map.search(2)->second.value, 20);
706 EXPECT_TRUE(const_map.has(1));
707 EXPECT_FALSE(const_map.has(3));
708 EXPECT_TRUE(const_map.contains(2));
709 EXPECT_FALSE(const_map.contains(4));
710}
711
712TEST(DynMapHashTable, OperatorBracketInsert)
713{
715
716 // Non-const operator[] inserts if key doesn't exist
717 map[1] = "one";
718 EXPECT_EQ(map.size(), 1u);
719 EXPECT_EQ(map[1], "one");
720
721 // Can modify existing
722 map[1] = "uno";
723 EXPECT_EQ(map[1], "uno");
724 EXPECT_EQ(map.size(), 1u);
725}
726
727TEST(DynMapHashTable, OperatorBracketConstThrows)
728{
730 map.insert(1, "one");
731
732 const auto & const_map = map;
733 EXPECT_EQ(const_map[1], "one");
734 EXPECT_THROW(const_map[99], domain_error);
735}
736
738{
740
741 map.insert(1, "one");
742 map.insert(2, "two");
743 map.insert(3, "three");
744
745 string removed = map.remove(2);
746 EXPECT_EQ(removed, "two");
747 EXPECT_EQ(map.size(), 2u);
748 EXPECT_FALSE(map.contains(2));
749
750 map.clear();
751 EXPECT_TRUE(map.is_empty());
752 EXPECT_EQ(map.size(), 0u);
753}
754
756{
758
759 map.insert(1, "one");
760 map.insert(2, "two");
761 map.insert(3, "three");
762
763 auto keys = map.keys();
764 EXPECT_EQ(keys.size(), 3u);
765
766 vector<int> sorted_keys;
767 keys.for_each([&sorted_keys](int k) { sorted_keys.push_back(k); });
768 sort(sorted_keys.begin(), sorted_keys.end());
769
770 vector<int> expected = {1, 2, 3};
771 EXPECT_EQ(sorted_keys, expected);
772}
773
774// values() was declared to return DynList<Key>, so it only compiled when the
775// values converted to keys. Here they do not.
777{
779 static_assert(is_same_v<decltype(by_name.values()), DynList<int>>);
780 EXPECT_TRUE(by_name.values().is_empty());
781
782 by_name.insert("one", 1);
783 by_name.insert("two", 2);
784 by_name.insert("three", 3);
785
786 vector<int> values;
787 by_name.values().for_each([&values](int v) { values.push_back(v); });
788 sort(values.begin(), values.end());
789 EXPECT_EQ(values, (vector<int>{1, 2, 3}));
790
792 static_assert(is_same_v<decltype(by_number.values()), DynList<string>>);
793 by_number.insert(1, "one");
794 by_number.insert(2, "two");
795
796 vector<string> names;
797 by_number.values().for_each([&names](const string & v) { names.push_back(v); });
798 sort(names.begin(), names.end());
799 EXPECT_EQ(names, (vector<string>{"one", "two"}));
800}
801
803{
805
806 map.insert(1, "one");
807 map.insert(2, "two");
808
809 auto ptrs = map.values_ptr();
810 EXPECT_EQ(ptrs.size(), 2u);
811
812 // Modify through pointer
813 ptrs.for_each([](string * p) {
814 if (*p == "one")
815 *p = "ONE";
816 });
817
818 EXPECT_EQ(map.find(1), "ONE");
819}
820
822{
824
825 map.insert(1, "one");
826 map.insert(2, "two");
827
828 auto items = map.items_ptr();
829 EXPECT_EQ(items.size(), 2u);
830}
831
832TEST(DynMapHashTable, MoveSemantics)
833{
835
836 string s = "hello";
837 map.insert(1, std::move(s));
838 EXPECT_EQ(map.find(1), "hello");
839
840 int k = 2;
841 map.insert(std::move(k), "world");
842 EXPECT_EQ(map.find(2), "world");
843}
844
845// ============================================================================
846// Free Functions Tests (join, intercept, unique, repeated)
847// ============================================================================
848
849TEST(DynHashTableFunctions, Join)
850{
851 DynList<int> l1 = {1, 2, 3, 4};
852 DynList<int> l2 = {3, 4, 5, 6};
853
854 auto result = join(l1, l2);
855
856 // Result should be union: {1, 2, 3, 4, 5, 6}
857 EXPECT_EQ(result.size(), 6u);
858
859 DynSetLhash<int> result_set(result);
860 for (int i : {1, 2, 3, 4, 5, 6})
861 EXPECT_TRUE(result_set.contains(i));
862}
863
864TEST(DynHashTableFunctions, Intercept)
865{
866 DynList<int> l1 = {1, 2, 3, 4, 5};
867 DynList<int> l2 = {3, 4, 5, 6, 7};
868
869 auto result = intercept(l1, l2);
870
871 // Result should be intersection: {3, 4, 5}
872 EXPECT_EQ(result.size(), 3u);
873
874 vector<int> vec;
875 result.for_each([&vec](int i) { vec.push_back(i); });
876 sort(vec.begin(), vec.end());
877
878 vector<int> expected = {3, 4, 5};
879 EXPECT_EQ(vec, expected);
880}
881
882TEST(DynHashTableFunctions, Unique)
883{
884 DynList<int> l = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
885
886 auto result = unique(l);
887
888 // Result should be {1, 2, 3, 4}
889 EXPECT_EQ(result.size(), 4u);
890
891 DynSetLhash<int> result_set(result);
892 for (int i : {1, 2, 3, 4})
893 EXPECT_TRUE(result_set.contains(i));
894}
895
896TEST(DynHashTableFunctions, Repeated)
897{
898 DynList<int> l = {1, 2, 2, 3, 3, 3, 4, 5};
899
900 auto result = repeated(l);
901
902 // Result contains all occurrences of duplicated values
903 // The function returns ALL instances of keys that appear more than once
904 // So we get 3 elements: one 2 (at index 2), and two 3s (at indices 3 and 4)
905 EXPECT_GE(result.size(), 2u); // At least 2 and 3 are duplicated
906
907 DynSetLhash<int> result_set(result);
908 EXPECT_TRUE(result_set.contains(2));
909 EXPECT_TRUE(result_set.contains(3));
910 EXPECT_FALSE(result_set.contains(1)); // 1 only appears once
911 EXPECT_FALSE(result_set.contains(4)); // 4 only appears once
912 EXPECT_FALSE(result_set.contains(5)); // 5 only appears once
913}
914
915TEST(DynHashTableFunctions, RepeatedWithIndex)
916{
917 DynList<int> l = {1, 2, 2, 3, 4, 3};
918
919 auto result = repeated_with_index(l);
920
921 // Result should be: {(2, 2), (3, 5)}
922 EXPECT_EQ(result.size(), 2u);
923
924 bool found_2 = false, found_3 = false;
925 result.for_each([&](const pair<int, size_t> & p) {
926 if (p.first == 2)
927 {
928 EXPECT_EQ(p.second, 2u);
929 found_2 = true;
930 }
931 if (p.first == 3)
932 {
933 EXPECT_EQ(p.second, 5u);
934 found_3 = true;
935 }
936 });
937
938 EXPECT_TRUE(found_2);
939 EXPECT_TRUE(found_3);
940}
941
942// ============================================================================
943// Different Hash Table Types Tests
944// ============================================================================
945
946TEST(DynHashTable, LinearHashTableBasics)
947{
948 DynSetLinHash<int> table;
949
950 for (int i : {1, 2, 3, 4, 5})
951 table.insert(i);
952
953 EXPECT_EQ(table.size(), 5u);
954 for (int i : {1, 2, 3, 4, 5})
955 EXPECT_TRUE(table.contains(i));
956}
957
958TEST(DynHashTable, DynSetHashAlias)
959{
960 DynSetHash<int> table;
961
962 for (int i : {10, 20, 30})
963 table.insert(i);
964
965 EXPECT_EQ(table.size(), 3u);
966 EXPECT_TRUE(table.contains(20));
967}
968
969// ============================================================================
970// Edge Cases
971// ============================================================================
972
973TEST(DynHashTable, SingleElement)
974{
975 DynSetLhash<int> table;
976
977 table.insert(42);
978
979 EXPECT_EQ(table.size(), 1u);
980 EXPECT_TRUE(table.contains(42));
981
982 table.remove(42);
983 EXPECT_TRUE(table.is_empty());
984}
985
986TEST(DynHashTable, InsertRemoveRepeated)
987{
988 DynSetLhash<int> table;
989
990 for (int cycle = 0; cycle < 10; ++cycle)
991 {
992 for (int i = 0; i < 50; ++i)
993 table.insert(i);
994
995 EXPECT_EQ(table.size(), 50u);
996
997 for (int i = 0; i < 50; ++i)
998 table.remove(i);
999
1000 EXPECT_TRUE(table.is_empty());
1001 }
1002}
1003
1004TEST(DynHashTable, CustomHashFunction)
1005{
1006 // Custom hash: just return the value modulo 100
1007 auto custom_hash = [](const int & k) -> size_t { return k % 100; };
1008
1009 DynSetLhash<int> table(100, custom_hash);
1010
1011 table.insert(1);
1012 table.insert(101);
1013 table.insert(201);
1014
1015 EXPECT_EQ(table.size(), 3u);
1016 EXPECT_TRUE(table.contains(1));
1017 EXPECT_TRUE(table.contains(101));
1018 EXPECT_TRUE(table.contains(201));
1019}
1020
1021// ============================================================================
1022// Custom Types Tests
1023// ============================================================================
1024
1025struct Point
1026{
1027 int x, y;
1028
1029 bool operator == (const Point & other) const
1030 {
1031 return x == other.x && y == other.y;
1032 }
1033};
1034
1035namespace std
1036{
1037 template <>
1038 struct hash<Point>
1039 {
1040 size_t operator()(const Point & p) const
1041 {
1042 return hash<int>()(p.x) ^ (hash<int>()(p.y) << 1);
1043 }
1044 };
1045}
1046
1047size_t point_hash(const Point & p)
1048{
1049 return std::hash<Point>()(p);
1050}
1051
1052TEST(DynHashTable, CustomType)
1053{
1054 DynSetLhash<Point> table(100, point_hash);
1055
1056 Point p1{1, 2};
1057 Point p2{3, 4};
1058 Point p3{5, 6};
1059
1060 table.insert(p1);
1061 table.insert(p2);
1062 table.insert(p3);
1063
1064 EXPECT_EQ(table.size(), 3u);
1065 EXPECT_TRUE(table.contains(p1));
1066 EXPECT_TRUE(table.contains(p2));
1067 EXPECT_TRUE(table.contains(p3));
1068
1069 Point p4{7, 8};
1070 EXPECT_FALSE(table.contains(p4));
1071}
1072
1073// ============================================================================
1074// A map and the hash function of its keys stay together
1075// ============================================================================
1076
1077namespace
1078{
1079 size_t map_hash_a(const long & k) noexcept
1080 {
1081 return static_cast<size_t>(k) * 2654435761u;
1082 }
1083
1084 size_t map_hash_b(const long & k) noexcept
1085 {
1086 return static_cast<size_t>(k) * 40503u + 17;
1087 }
1088
1089 // Every key in [from, to) must be found, with the value it was given.
1090 template <class Map>
1091 ::testing::AssertionResult maps_keys(const Map & map, const long from,
1092 const long to, const int value)
1093 {
1094 if (map.size() != static_cast<size_t>(to - from))
1095 return ::testing::AssertionFailure()
1096 << "size is " << map.size() << ", expected " << to - from;
1097 for (long k = from; k < to; ++k)
1098 {
1099 const auto * pair = map.search(k);
1100 if (pair == nullptr)
1101 return ::testing::AssertionFailure() << "key " << k << " not found";
1102 if (pair->second != value or not map.has(k) or map.find(k) != value)
1103 return ::testing::AssertionFailure() << "key " << k << " is wrong";
1104 }
1105 return ::testing::AssertionSuccess();
1106 }
1107
1108 template <class Map>
1109 void insert_keys(Map & map, const long from, const long to,
1110 const int value)
1111 {
1112 for (long k = from; k < to; ++k)
1113 ASSERT_NE(map.insert(k, value), nullptr) << "key " << k;
1114 }
1115
1116 template <class Map>
1117 struct DynMapHashFunction : public ::testing::Test {};
1118
1119 using Chained_Maps =
1120 ::testing::Types<DynMapHash<long, int>, DynMapLinHash<long, int>>;
1121 TYPED_TEST_SUITE(DynMapHashFunction, Chained_Maps);
1122
1123 // DynMapHash keeps the hash function of the keys apart from the one that
1124 // its table applies to the pairs. swap() exchanged only the second, and
1125 // after it neither map found its keys.
1126 TYPED_TEST(DynMapHashFunction, SwapKeepsEachMapFindingItsKeys)
1127 {
1128 TypeParam a(101, map_hash_a), b(101, map_hash_b);
1129 insert_keys(a, 0, 50, 1);
1130 insert_keys(b, 1000, 1050, 2);
1131
1132 a.swap(b);
1133
1134 EXPECT_TRUE(maps_keys(a, 1000, 1050, 2));
1135 EXPECT_TRUE(maps_keys(b, 0, 50, 1));
1136
1137 insert_keys(a, 1050, 1300, 2);
1138 insert_keys(b, 50, 300, 1);
1139 EXPECT_TRUE(maps_keys(a, 1000, 1300, 2));
1140 EXPECT_TRUE(maps_keys(b, 0, 300, 1));
1141 }
1142
1143 // The assignment copied the hash function of the keys, but placed the
1144 // pairs with the one that the target already had.
1145 TYPED_TEST(DynMapHashFunction, CopyAssignmentBringsTheHashFunction)
1146 {
1147 TypeParam source(101, map_hash_b), target(101, map_hash_a);
1148 insert_keys(source, 0, 50, 2);
1149 insert_keys(target, 500, 520, 1);
1150
1151 target = source;
1152
1153 EXPECT_TRUE(maps_keys(target, 0, 50, 2));
1154 EXPECT_TRUE(maps_keys(source, 0, 50, 2));
1155 insert_keys(target, 50, 300, 2);
1156 EXPECT_TRUE(maps_keys(target, 0, 300, 2));
1157 EXPECT_TRUE(maps_keys(source, 0, 50, 2));
1158 }
1159
1160 // The source is left with the table that the target had; it must be
1161 // left with its hash function too.
1162 TYPED_TEST(DynMapHashFunction, MoveAssignmentLeavesBothMapsUsable)
1163 {
1164 TypeParam source(101, map_hash_b), target(101, map_hash_a);
1165 insert_keys(source, 0, 50, 2);
1166 insert_keys(target, 500, 520, 1);
1167
1168 target = std::move(source);
1169
1170 EXPECT_TRUE(maps_keys(target, 0, 50, 2));
1171 EXPECT_TRUE(source.is_empty());
1172
1173 insert_keys(source, 0, 300, 3);
1174 insert_keys(target, 50, 300, 2);
1175 EXPECT_TRUE(maps_keys(source, 0, 300, 3));
1176 EXPECT_TRUE(maps_keys(target, 0, 300, 2));
1177 }
1178
1179 TYPED_TEST(DynMapHashFunction, MoveConstructionLeavesBothMapsUsable)
1180 {
1181 TypeParam source(101, map_hash_b);
1182 insert_keys(source, 0, 50, 2);
1183
1184 TypeParam target(std::move(source));
1185
1186 EXPECT_TRUE(maps_keys(target, 0, 50, 2));
1187 EXPECT_TRUE(source.is_empty());
1188
1189 insert_keys(source, 0, 300, 3);
1190 insert_keys(target, 50, 300, 2);
1191 EXPECT_TRUE(maps_keys(source, 0, 300, 3));
1192 EXPECT_TRUE(maps_keys(target, 0, 300, 2));
1193 }
1194
1195 TYPED_TEST(DynMapHashFunction, CopyConstructionBringsTheHashFunction)
1196 {
1197 TypeParam source(101, map_hash_b);
1198 insert_keys(source, 0, 50, 2);
1199
1200 TypeParam copy(source);
1201
1202 EXPECT_TRUE(maps_keys(copy, 0, 50, 2));
1203 insert_keys(copy, 50, 300, 2);
1204 EXPECT_TRUE(maps_keys(copy, 0, 300, 2));
1205 EXPECT_TRUE(maps_keys(source, 0, 50, 2));
1206 }
1207
1208 // ==========================================================================
1209 // The load thresholds of a chained table
1210 // ==========================================================================
1211
1212 template <class Set>
1213 struct DynHashTableThresholds : public ::testing::Test {};
1214
1215 using Chained_Sets = ::testing::Types<DynSetLhash<int>, DynSetLinHash<int>>;
1216 TYPED_TEST_SUITE(DynHashTableThresholds, Chained_Sets);
1217
1218 // The accessors read and wrote a pair of values of their own, which
1219 // nothing initialised and the table never looked at.
1220 TYPED_TEST(DynHashTableThresholds, AccessorsReadWhatTheTableWasGiven)
1221 {
1222 const TypeParam table(101, Aleph::dft_hash_ptr_fct<int>,
1223 Aleph::equal_to<int>(), 0.2f, 0.6f);
1224
1225 EXPECT_EQ(table.get_lower_alpha(), 0.2f);
1226 EXPECT_EQ(table.get_upper_alpha(), 0.6f);
1227 }
1228
1229 TYPED_TEST(DynHashTableThresholds, SettersRejectThresholdsOutOfOrder)
1230 {
1231 TypeParam table(101, Aleph::dft_hash_ptr_fct<int>,
1232 Aleph::equal_to<int>(), 0.2f, 0.6f);
1233
1234 table.set_upper_alpha(0.8f);
1235 table.set_lower_alpha(0.3f);
1236 EXPECT_EQ(table.get_lower_alpha(), 0.3f);
1237 EXPECT_EQ(table.get_upper_alpha(), 0.8f);
1238
1239 EXPECT_THROW(table.set_upper_alpha(0.3f), std::domain_error);
1240 EXPECT_THROW(table.set_lower_alpha(0.8f), std::domain_error);
1241 EXPECT_EQ(table.get_lower_alpha(), 0.3f);
1242 EXPECT_EQ(table.get_upper_alpha(), 0.8f);
1243 }
1244
1245 // With the upper threshold raised to 0.9, a load of 0.79 must not make
1246 // the table grow; with the 0.5 it was built with, it would.
1247 TYPED_TEST(DynHashTableThresholds, TheTableObeysTheSetters)
1248 {
1249 TypeParam raised(101, Aleph::dft_hash_ptr_fct<int>,
1250 Aleph::equal_to<int>(), 0.1f, 0.5f);
1251 TypeParam as_built(101, Aleph::dft_hash_ptr_fct<int>,
1252 Aleph::equal_to<int>(), 0.1f, 0.5f);
1253 const size_t capacity = raised.capacity();
1254 ASSERT_EQ(as_built.capacity(), capacity);
1255
1256 raised.set_upper_alpha(0.9f);
1257 for (int k = 0; k < 80; ++k)
1258 {
1259 ASSERT_NE(raised.insert(k), nullptr);
1260 ASSERT_NE(as_built.insert(k), nullptr);
1261 }
1262
1263 EXPECT_EQ(raised.capacity(), capacity);
1264 EXPECT_GT(as_built.capacity(), capacity);
1265 }
1266} // namespace
1267
1268// ==========================================================================
1269// The move of a table of chained lists
1270// ==========================================================================
1271
1272namespace
1273{
1274 size_t chained_hash(const int & k) noexcept
1275 {
1276 return static_cast<size_t>(k) * 2654435761u + 13;
1277 }
1278
1279 size_t chained_key_hash(const long & k) noexcept
1280 {
1281 return static_cast<size_t>(k) * 40503u + 7;
1282 }
1283} // namespace
1284
1285// A move built, for the table that it moved from, the lists of a table of
1286// its capacity, in a constructor that is noexcept: megabytes for a table
1287// of a million keys, which it could not say that it had no memory for. It
1288// takes the lists now: the table that it moved from has none, and takes
1289// those of the default capacity when a key enters it
1290TEST(DynSetLhashMove, TakesTheListsAndLeavesAUsableTableWithoutThem)
1291{
1292 static_assert(std::is_nothrow_move_constructible_v<DynSetLhash<int>>);
1293
1294 DynSetLhash<int> source;
1295 for (int i = 0; i < 2000; ++i)
1296 ASSERT_NE(source.insert(i), nullptr);
1297 const size_t capacity = source.capacity();
1298 const int * const where = source.search(1234);
1299
1300 DynSetLhash<int> target(std::move(source));
1301
1302 ASSERT_EQ(target.size(), 2000u);
1303 EXPECT_EQ(target.capacity(), capacity);
1304 EXPECT_EQ(target.search(1234), where); // the very bucket
1305 for (int i = 0; i < 2000; ++i)
1306 ASSERT_TRUE(target.contains(i));
1307
1308 EXPECT_TRUE(source.is_empty());
1309 EXPECT_EQ(source.size(), 0u);
1310 EXPECT_EQ(source.capacity(), 0u);
1311 EXPECT_EQ(source.current_alpha(), 0.0f);
1312 EXPECT_EQ(source.search(5), nullptr);
1313 EXPECT_FALSE(source.contains(5));
1314 EXPECT_THROW(source.remove(5), std::domain_error);
1315 EXPECT_FALSE(source.get_it().has_curr());
1316 size_t visited = 0;
1317 source.for_each([&visited] (const int &) { ++visited; });
1318 EXPECT_EQ(visited, 0u);
1319 source.empty();
1320
1321 ASSERT_NE(source.insert(7), nullptr); // its first key
1322 EXPECT_EQ(source.capacity(), Primes::DefaultPrime);
1323 for (int i = 0; i < 1000; ++i)
1324 (void) source.insert(i);
1325 ASSERT_EQ(source.size(), 1000u);
1326 for (int i = 0; i < 1000; ++i)
1327 ASSERT_TRUE(source.contains(i));
1328 for (int i = 0; i < 1000; i += 2)
1329 source.remove(i);
1330 EXPECT_EQ(source.size(), 500u);
1331 EXPECT_FALSE(source.contains(0));
1332 EXPECT_TRUE(source.contains(1));
1333
1334 ASSERT_NE(target.insert(5000), nullptr); // the target goes on as any table
1335 EXPECT_EQ(target.size(), 2001u);
1336}
1337
1338namespace
1339{
1340 // The hash function of the tests above, which counts its calls
1341 size_t counted_hash_calls = 0;
1342
1343 size_t counted_hash(const int & k) noexcept
1344 {
1345 ++counted_hash_calls;
1346 return chained_hash(k);
1347 }
1348}
1349
1350// The table that was moved from keeps what rules it: its hash function, its
1351// load thresholds and whether it resizes itself
1352TEST(DynSetLhashMove, TheTableThatWasMovedFromKeepsItsFunctionAndItsPolicy)
1353{
1354 DynSetLhash<int> source(101, counted_hash);
1355 for (int i = 0; i < 300; ++i)
1356 ASSERT_NE(source.insert(i), nullptr);
1357 source.set_with_resize(false);
1358
1359 DynSetLhash<int> target(std::move(source));
1360 EXPECT_FALSE(target.get_with_resize());
1361 EXPECT_FALSE(source.get_with_resize());
1362 counted_hash_calls = 0;
1363 for (int i = 0; i < 300; ++i)
1364 ASSERT_TRUE(target.contains(i));
1365 EXPECT_EQ(counted_hash_calls, 300u); // the keys went with their function
1366
1367 // without resizing, the lists of the default capacity are the ones that
1368 // it keeps
1369 counted_hash_calls = 0;
1370 for (int i = 0; i < 1000; ++i)
1371 ASSERT_NE(source.insert(i), nullptr);
1372 EXPECT_EQ(counted_hash_calls, 1000u); // and the source keeps its own
1373 EXPECT_EQ(source.capacity(), Primes::DefaultPrime);
1374 for (int i = 0; i < 1000; ++i)
1375 ASSERT_TRUE(source.contains(i));
1376}
1377
1378// With thresholds that are not the default ones: the table that was moved
1379// from grows when it holds twice as many keys as lists, where one with
1380// the default thresholds would have grown long before
1381TEST(DynSetLhashMove, TheTableThatWasMovedFromKeepsItsLoadThresholds)
1382{
1383 ASSERT_LT(hash_default_upper_alpha, 1.5f);
1384 DynSetLhash<int> source(101, counted_hash, Aleph::equal_to<int>(), 0.5f, 2.0f);
1385 for (int i = 0; i < 50; ++i)
1386 ASSERT_NE(source.insert(i), nullptr);
1387
1388 DynSetLhash<int> target(std::move(source));
1389 ASSERT_EQ(source.capacity(), 0u);
1390 EXPECT_EQ(target.size(), 50u);
1391
1392 const int lists = static_cast<int>(Primes::DefaultPrime);
1393 for (int i = 0; i < 2 * lists - 1; ++i)
1394 ASSERT_NE(source.insert(i), nullptr);
1395 EXPECT_EQ(source.capacity(), Primes::DefaultPrime); // a load below 2
1396 ASSERT_NE(source.insert(2 * lists - 1), nullptr); // and a load of 2
1397 EXPECT_GT(source.capacity(), Primes::DefaultPrime);
1398 for (int i = 0; i < 2 * lists; ++i)
1399 ASSERT_TRUE(source.contains(i));
1400}
1401
1402// A table without lists is copied, moved, swapped, assigned and resized,
1403// and it takes another hash function
1404TEST(DynSetLhashMove, ATableWithoutListsIsATableLikeAnyOther)
1405{
1406 DynSetLhash<int> source;
1407 for (int i = 0; i < 50; ++i)
1408 ASSERT_NE(source.insert(i), nullptr);
1409 DynSetLhash<int> target(std::move(source));
1410 ASSERT_EQ(source.capacity(), 0u);
1411
1412 DynSetLhash<int> copy(source); // a copy has the lists of the default capacity
1413 EXPECT_TRUE(copy.is_empty());
1414 EXPECT_EQ(copy.capacity(), Primes::DefaultPrime);
1415 ASSERT_NE(copy.insert(3), nullptr);
1416 EXPECT_TRUE(copy.contains(3));
1417
1418 DynSetLhash<int> moved(std::move(source)); // neither has lists
1419 EXPECT_EQ(moved.capacity(), 0u);
1420 EXPECT_EQ(source.capacity(), 0u);
1421 ASSERT_NE(moved.insert(4), nullptr);
1422 EXPECT_TRUE(moved.contains(4));
1423
1424 source.swap(target);
1425 EXPECT_EQ(source.size(), 50u);
1426 EXPECT_EQ(target.capacity(), 0u);
1427 target = source; // the copy assignment
1428 EXPECT_EQ(target.size(), 50u);
1429 EXPECT_TRUE(target.contains(49));
1430 moved = std::move(target); // the move assignment: the tables change hands
1431 EXPECT_EQ(moved.size(), 50u);
1432 EXPECT_TRUE(target.is_empty());
1433
1434 DynSetLhash<int> without(std::move(moved));
1435 ASSERT_EQ(moved.capacity(), 0u);
1436 EXPECT_EQ(moved.resize(31), 31u);
1437 EXPECT_EQ(moved.capacity(), 31u);
1438 ASSERT_NE(moved.insert(6), nullptr);
1439 EXPECT_TRUE(moved.contains(6));
1440
1441 DynSetLhash<int> rehashed(std::move(without));
1442 ASSERT_EQ(without.capacity(), 0u);
1443 without.set_hash_fct(chained_hash);
1444 ASSERT_NE(without.insert(8), nullptr);
1445 EXPECT_TRUE(without.contains(8));
1446 EXPECT_EQ(rehashed.size(), 50u);
1447}
1448
1449// The maps: a move takes the pairs, and the map that was moved from finds
1450// nothing and takes pairs again, with its hash function of the keys
1451TEST(DynMapHashMove, TakesThePairsAndLeavesAUsableMap)
1452{
1453 static_assert((std::is_nothrow_move_constructible_v<DynMapHash<long, std::string>>));
1454
1455 DynMapHash<long, std::string> source(101, chained_key_hash);
1456 for (long k = 0; k < 500; ++k)
1457 source.insert(k, std::to_string(k));
1458 const auto * const where = source.search(77);
1459
1460 DynMapHash<long, std::string> target(std::move(source));
1461
1462 ASSERT_EQ(target.size(), 500u);
1463 EXPECT_EQ(target.search(77), where); // the very pair
1464 EXPECT_EQ(target.find(499), "499");
1465 EXPECT_TRUE(source.is_empty());
1466 EXPECT_EQ(source.capacity(), 0u);
1467 EXPECT_EQ(source.search(77), nullptr);
1468 EXPECT_FALSE(source.has(77));
1469
1470 source.insert(77, "again");
1471 EXPECT_EQ(source.find(77), "again");
1472 for (long k = 1000; k < 1300; ++k)
1473 source.insert(k, std::to_string(k));
1474 EXPECT_EQ(source.size(), 301u);
1475 EXPECT_EQ(source.find(1299), "1299");
1476}
1477
1478int main(int argc, char **argv)
1479{
1480 ::testing::InitGoogleTest(&argc, argv);
1481 return RUN_ALL_TESTS();
1482}
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
TYPED_TEST(ArcHeapLifecycle, ClearNullsTheHandles)
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Self-adjusting dynamic hash table.
Key * add(const Key &key)
Key * search_or_insert(const Key &key)
Key * append(const Key &key)
bool contains(const Key &key) const noexcept
Return true if the key exists in the table.
void remove(Key *key)
Removes a key from the hash table using its pointer.
const Key & get_last() const
Key * insert(const Key &key)
Inserts key into the hash set.
const Key & find(const Key &key) const
void clear()
Empties the container.
std::pair< Key *, bool > contains_or_insert(const Key &key)
Key * search(const Key &key) noexcept
Searches for a key in the hash table.
bool has(const Key &key) const noexcept
Checks if a key exists in the hash table.
bool is_empty() const noexcept
Checks if the container is empty.
const Key & get_first() const
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
DynList< Data > values() const
Returns the values of the map.
DynList< Data * > values_ptr()
Pair * insert(const Key &key, const Data &data)
Inserts into the hash map the pair (key, record) indexed by key.
Data remove(const Key &key)
Removes a key-value pair from the map and returns the value.
Pair * search(const Key &key) noexcept(nothrow_search< const Key & >)
Searches for the pair whose key is key.
DynList< Pair * > items_ptr()
DynList< Key > keys() const
Data & find(const Key &key)
Finds and returns a reference to the value associated with key.
bool contains(const Key &key) const noexcept(nothrow_search< const Key & >)
Checks if a key exists in the map; an alias for has().
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:221
bool operator==(const Point &point) const noexcept
Checks for exact equality between two points.
Definition point.H:259
Minimal std::expected-style result type for C++20.
#define TEST(name)
size_t point_hash(const Point &p)
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
Itor unique(Itor __first, Itor __last, BinaryPredicate __binary_pred=BinaryPredicate())
Remove consecutive duplicates in place.
Definition ahAlgo.H:1058
DynList< T > intercept(const Container< T > &c1, const Container< T > &c2)
Return intersection of two containers as a DynList.
DynList< std::pair< T, size_t > > repeated_with_index(const Container< T > &c)
Return repeated elements paired with their occurrence count.
DynList< T > repeated(const Container< T > &c)
Return elements that appear more than once in the container.
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
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:234
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
const unsigned long DefaultPrime
Default prime number used when no specific size is requested.
Definition primes.C:381
STL namespace.
Hash-based dynamic set (defined in tpl_dynSetHash.H).
size_t operator()(const Point &p) const
DynList< int > l1
DynList< int > l2
int keys[]
static int * k
size_t custom_hash(const int &key)
Dynamic set implementations based on hash tables.
DynList< int > l