Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dynsettree.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 <bit>
40#include <random>
41#include <set>
42#include <stdexcept>
43#include <vector>
44#include <string>
45#include <gtest/gtest.h>
46
47#include <tpl_dynSetTree.H>
48
49using namespace std;
50using namespace Aleph;
51
52// ============================================================================
53// Typed Tests - Run same tests against ALL tree types
54// ============================================================================
55
56template <typename T>
57class DynSetTreeTypedTest : public ::testing::Test
58{
59protected:
61};
62
63using AllTreeTypes = ::testing::Types<
71>;
72
74
75TYPED_TEST(DynSetTreeTypedTest, EmptySetProperties)
76{
77 EXPECT_TRUE(this->set.is_empty());
78 EXPECT_EQ(this->set.size(), 0u);
79 EXPECT_THROW(this->set.min(), domain_error);
80 EXPECT_THROW(this->set.max(), domain_error);
81 EXPECT_THROW(this->set.get_root(), domain_error);
82 EXPECT_FALSE(this->set.contains(42));
83 EXPECT_EQ(this->set.search(42), nullptr);
84}
85
87{
88 for (int i : {5, 3, 7, 1, 9, 2, 8})
89 {
90 auto * p = this->set.insert(i);
91 ASSERT_NE(p, nullptr);
92 EXPECT_EQ(*p, i);
93 }
94
95 EXPECT_EQ(this->set.size(), 7u);
96 EXPECT_EQ(this->set.min(), 1);
97 EXPECT_EQ(this->set.max(), 9);
98
99 for (int i : {1, 2, 3, 5, 7, 8, 9})
100 EXPECT_TRUE(this->set.contains(i));
101
102 EXPECT_FALSE(this->set.contains(4));
103 EXPECT_FALSE(this->set.contains(6));
104}
105
106TYPED_TEST(DynSetTreeTypedTest, InsertRejectsDuplicates)
107{
108 auto * p1 = this->set.insert(42);
109 ASSERT_NE(p1, nullptr);
110 EXPECT_EQ(this->set.size(), 1u);
111
112 auto * p2 = this->set.insert(42);
113 EXPECT_EQ(p2, nullptr);
114 EXPECT_EQ(this->set.size(), 1u);
115}
116
118{
119 for (int i : {5, 3, 7, 1, 9})
120 this->set.insert(i);
121
122 EXPECT_EQ(this->set.size(), 5u);
123
124 this->set.remove(3);
125 EXPECT_EQ(this->set.size(), 4u);
126 EXPECT_FALSE(this->set.contains(3));
127 EXPECT_TRUE(this->set.contains(5));
128}
129
131{
132 auto * p1 = this->set.search_or_insert(42);
133 ASSERT_NE(p1, nullptr);
134 EXPECT_EQ(*p1, 42);
135 EXPECT_EQ(this->set.size(), 1u);
136
137 auto * p2 = this->set.search_or_insert(42);
138 EXPECT_EQ(p1, p2);
139 EXPECT_EQ(this->set.size(), 1u);
140}
141
143{
144 auto [p1, found1] = this->set.contains_or_insert(42);
145 ASSERT_NE(p1, nullptr);
146 EXPECT_FALSE(found1);
147 EXPECT_EQ(this->set.size(), 1u);
148
149 auto [p2, found2] = this->set.contains_or_insert(42);
150 EXPECT_TRUE(found2);
151 EXPECT_EQ(this->set.size(), 1u);
152}
153
155{
156 this->set.insert(42);
157
158 const auto & key = this->set.find(42);
159 EXPECT_EQ(key, 42);
160
161 EXPECT_THROW(this->set.find(99), domain_error);
162
163 int removed = this->set.del(42);
164 EXPECT_EQ(removed, 42);
165 EXPECT_TRUE(this->set.is_empty());
166
167 EXPECT_THROW(this->set.del(42), domain_error);
168}
169
171{
172 for (int i : {1, 2, 3, 4, 5})
173 this->set.insert(i);
174
175 TypeParam copy(this->set);
176
177 EXPECT_EQ(copy.size(), 5u);
178 for (int i : {1, 2, 3, 4, 5})
179 EXPECT_TRUE(copy.contains(i));
180
181 // Verify independence
182 this->set.remove(3);
183 EXPECT_FALSE(this->set.contains(3));
184 EXPECT_TRUE(copy.contains(3));
185}
186
188{
189 for (int i : {1, 2, 3, 4, 5})
190 this->set.insert(i);
191
192 TypeParam moved(std::move(this->set));
193
194 EXPECT_EQ(moved.size(), 5u);
195 for (int i : {1, 2, 3, 4, 5})
196 EXPECT_TRUE(moved.contains(i));
197
198 EXPECT_TRUE(this->set.is_empty());
199}
200
202{
203 TypeParam set2;
204
205 for (int i : {1, 2, 3})
206 this->set.insert(i);
207 for (int i : {10, 20})
208 set2.insert(i);
209
210 this->set.swap(set2);
211
212 EXPECT_EQ(this->set.size(), 2u);
213 EXPECT_TRUE(this->set.contains(10));
214 EXPECT_EQ(set2.size(), 3u);
215 EXPECT_TRUE(set2.contains(1));
216}
217
219{
220 for (int i = 0; i < 50; ++i)
221 this->set.insert(i);
222
223 EXPECT_EQ(this->set.size(), 50u);
224
225 this->set.empty();
226
227 EXPECT_TRUE(this->set.is_empty());
228 EXPECT_EQ(this->set.size(), 0u);
229}
230
232{
233 for (int i : {5, 3, 7, 1, 9})
234 this->set.insert(i);
235
236 vector<int> keys;
237 for (typename TypeParam::Iterator it(this->set); it.has_curr(); it.next_ne())
238 keys.push_back(it.get_curr());
239
240 vector<int> expected = {1, 3, 5, 7, 9};
241 EXPECT_EQ(keys, expected);
242}
243
245{
246 for (int i : {5, 3, 7, 1, 9})
247 this->set.insert(i);
248
249 vector<int> keys;
250 this->set.for_each_inorder([&keys](const int & k) { keys.push_back(k); });
251
252 vector<int> expected = {1, 3, 5, 7, 9};
253 EXPECT_EQ(keys, expected);
254}
255
257{
258 for (int i : {1, 2, 3, 4, 5})
259 this->set.insert(i);
260
261 int sum = 0;
262 bool completed = this->set.traverse([&sum](const int & k) {
263 sum += k;
264 return true;
265 });
266
267 EXPECT_TRUE(completed);
268 EXPECT_EQ(sum, 15);
269}
270
272{
273 for (int i : {5, 3, 7, 1, 9, 2, 8, 4, 6})
274 this->set.insert(i);
275
276 EXPECT_TRUE(this->set.verify());
277}
278
280{
281 // Insert 500 elements
282 for (int i = 0; i < 500; ++i)
283 this->set.insert(i);
284
285 EXPECT_EQ(this->set.size(), 500u);
286 EXPECT_EQ(this->set.min(), 0);
287 EXPECT_EQ(this->set.max(), 499);
288
289 // Remove half
290 for (int i = 0; i < 500; i += 2)
291 this->set.remove(i);
292
293 EXPECT_EQ(this->set.size(), 250u);
294
295 // Verify correct ones remain
296 for (int i = 1; i < 500; i += 2)
297 EXPECT_TRUE(this->set.contains(i));
298}
299
300// ============================================================================
301// Basic Operations Tests (applicable to all tree types)
302// ============================================================================
303
304#if 0
305
306TEST(DynSetTree, EmptySetProperties)
307{
309
310 EXPECT_TRUE(set.is_empty());
311 EXPECT_EQ(set.size(), 0u);
312 EXPECT_THROW(set.min(), domain_error);
313 EXPECT_THROW(set.max(), domain_error);
314 EXPECT_THROW(set.get_root(), domain_error);
315 EXPECT_THROW(set.get_item(), domain_error);
316 EXPECT_FALSE(set.contains(42));
317 EXPECT_EQ(set.search(42), nullptr);
318}
319
320TEST(DynSetTree, InsertSingleElement)
321{
323
324 auto * p = set.insert(42);
325 ASSERT_NE(p, nullptr);
326 EXPECT_EQ(*p, 42);
327 EXPECT_FALSE(set.is_empty());
328 EXPECT_EQ(set.size(), 1u);
329 EXPECT_TRUE(set.contains(42));
330 EXPECT_EQ(set.min(), 42);
331 EXPECT_EQ(set.max(), 42);
332 EXPECT_EQ(set.get_root(), 42);
333}
334
335TEST(DynSetTree, InsertMultipleElements)
336{
338
339 for (int i : {5, 3, 7, 1, 9, 2, 8})
340 {
341 auto * p = set.insert(i);
342 ASSERT_NE(p, nullptr);
343 EXPECT_EQ(*p, i);
344 }
345
346 EXPECT_EQ(set.size(), 7u);
347 EXPECT_EQ(set.min(), 1);
348 EXPECT_EQ(set.max(), 9);
349
350 for (int i : {1, 2, 3, 5, 7, 8, 9})
351 EXPECT_TRUE(set.contains(i));
352
353 EXPECT_FALSE(set.contains(4));
354 EXPECT_FALSE(set.contains(6));
355}
356
357TEST(DynSetTree, InsertRejectsDuplicates)
358{
360
361 auto * p1 = set.insert(42);
362 ASSERT_NE(p1, nullptr);
363 EXPECT_EQ(*p1, 42);
364 EXPECT_EQ(set.size(), 1u);
365
366 auto * p2 = set.insert(42);
367 EXPECT_EQ(p2, nullptr); // Duplicate rejected
368 EXPECT_EQ(set.size(), 1u); // Size unchanged
369}
370
371TEST(DynSetTree, InsertDupAllowsDuplicates)
372{
374
375 auto * p1 = set.insert_dup(42);
376 ASSERT_NE(p1, nullptr);
377 EXPECT_EQ(*p1, 42);
378
379 auto * p2 = set.insert_dup(42);
380 ASSERT_NE(p2, nullptr);
381 EXPECT_EQ(*p2, 42);
382
383 EXPECT_EQ(set.size(), 2u);
384}
385
386TEST(DynSetTree, SearchFindsExistingKey)
387{
389
390 set.insert(10);
391 set.insert(20);
392 set.insert(30);
393
394 auto * p = set.search(20);
395 ASSERT_NE(p, nullptr);
396 EXPECT_EQ(*p, 20);
397}
398
399TEST(DynSetTree, SearchReturnsNullForMissingKey)
400{
402
403 set.insert(10);
404 set.insert(30);
405
406 EXPECT_EQ(set.search(20), nullptr);
407}
408
409TEST(DynSetTree, SearchOrInsertBehavior)
410{
412
413 // First call: inserts
414 auto * p1 = set.search_or_insert(42);
415 ASSERT_NE(p1, nullptr);
416 EXPECT_EQ(*p1, 42);
417 EXPECT_EQ(set.size(), 1u);
418
419 // Second call: returns existing
420 auto * p2 = set.search_or_insert(42);
421 ASSERT_NE(p2, nullptr);
422 EXPECT_EQ(*p2, 42);
423 EXPECT_EQ(set.size(), 1u); // Size unchanged
424 EXPECT_EQ(p1, p2); // Same pointer
425}
426
427TEST(DynSetTree, ContainsOrInsertBehavior)
428{
430
431 // First call: inserts, returns false
432 auto [p1, found1] = set.contains_or_insert(42);
433 ASSERT_NE(p1, nullptr);
434 EXPECT_EQ(*p1, 42);
435 EXPECT_FALSE(found1);
436 EXPECT_EQ(set.size(), 1u);
437
438 // Second call: finds, returns true
439 auto [p2, found2] = set.contains_or_insert(42);
440 ASSERT_NE(p2, nullptr);
441 EXPECT_EQ(*p2, 42);
442 EXPECT_TRUE(found2);
443 EXPECT_EQ(set.size(), 1u);
444}
445
446TEST(DynSetTree, FindReturnsReference)
447{
449
450 set.insert(42);
451
452 const auto & key = set.find(42);
453 EXPECT_EQ(key, 42);
454}
455
456TEST(DynSetTree, FindThrowsIfNotFound)
457{
459
460 set.insert(10);
461
462 EXPECT_THROW(set.find(42), domain_error);
463}
464
465// ============================================================================
466// Remove Tests
467// ============================================================================
468
469TEST(DynSetTree, RemoveExistingKey)
470{
472
473 set.insert(10);
474 set.insert(20);
475 set.insert(30);
476
477 size_t new_size = set.remove(20);
478 EXPECT_EQ(new_size, 2u);
479 EXPECT_EQ(set.size(), 2u);
480 EXPECT_FALSE(set.contains(20));
481 EXPECT_TRUE(set.contains(10));
482 EXPECT_TRUE(set.contains(30));
483}
484
485TEST(DynSetTree, RemoveReturnsCurrentSizeIfNotFound)
486{
488
489 set.insert(10);
490 set.insert(30);
491
492 size_t new_size = set.remove(20);
493 EXPECT_EQ(new_size, 2u);
494 EXPECT_EQ(set.size(), 2u);
495}
496
497TEST(DynSetTree, RemoveFromEmptySet)
498{
500
501 size_t new_size = set.remove(42);
502 EXPECT_EQ(new_size, 0u);
503 EXPECT_TRUE(set.is_empty());
504}
505
506TEST(DynSetTree, DelReturnsRemovedKey)
507{
509
510 set.insert(42);
511
512 int removed = set.del(42);
513 EXPECT_EQ(removed, 42);
514 EXPECT_FALSE(set.contains(42));
515 EXPECT_EQ(set.size(), 0u);
516}
517
518TEST(DynSetTree, DelThrowsIfNotFound)
519{
521
522 set.insert(10);
523
524 EXPECT_THROW(set.del(42), domain_error);
525}
526
527TEST(DynSetTree, RemoveAllElements)
528{
530
531 for (int i = 0; i < 10; ++i)
532 set.insert(i);
533
534 EXPECT_EQ(set.size(), 10u);
535
536 for (int i = 0; i < 10; ++i)
537 set.remove(i);
538
539 EXPECT_TRUE(set.is_empty());
540 EXPECT_EQ(set.size(), 0u);
541}
542
543TEST(DynSetTree, EmptyMethod)
544{
546
547 for (int i = 0; i < 100; ++i)
548 set.insert(i);
549
550 EXPECT_EQ(set.size(), 100u);
551
552 set.empty();
553
554 EXPECT_TRUE(set.is_empty());
555 EXPECT_EQ(set.size(), 0u);
556}
557
558// ============================================================================
559// Copy/Move Semantics Tests
560// ============================================================================
561
562TEST(DynSetTree, CopyConstructor)
563{
565
566 for (int i : {1, 2, 3, 4, 5})
567 set1.insert(i);
568
569 DynSetAvlTree<int> set2(set1);
570
571 EXPECT_EQ(set2.size(), 5u);
572 for (int i : {1, 2, 3, 4, 5})
573 EXPECT_TRUE(set2.contains(i));
574
575 // Verify independence
576 set1.remove(3);
577 EXPECT_FALSE(set1.contains(3));
578 EXPECT_TRUE(set2.contains(3));
579}
580
581TEST(DynSetTree, CopyAssignment)
582{
583 DynSetAvlTree<int> set1, set2;
584
585 for (int i : {1, 2, 3})
586 set1.insert(i);
587
588 for (int i : {10, 20})
589 set2.insert(i);
590
591 set2 = set1;
592
593 EXPECT_EQ(set2.size(), 3u);
594 for (int i : {1, 2, 3})
595 EXPECT_TRUE(set2.contains(i));
596 EXPECT_FALSE(set2.contains(10));
597 EXPECT_FALSE(set2.contains(20));
598}
599
600TEST(DynSetTree, SelfAssignment)
601{
603
604 for (int i : {1, 2, 3})
605 set.insert(i);
606
607 set = set;
608
609 EXPECT_EQ(set.size(), 3u);
610 for (int i : {1, 2, 3})
611 EXPECT_TRUE(set.contains(i));
612}
613
614TEST(DynSetTree, MoveConstructor)
615{
617
618 for (int i : {1, 2, 3, 4, 5})
619 set1.insert(i);
620
621 DynSetAvlTree<int> set2(std::move(set1));
622
623 EXPECT_EQ(set2.size(), 5u);
624 for (int i : {1, 2, 3, 4, 5})
625 EXPECT_TRUE(set2.contains(i));
626
627 EXPECT_TRUE(set1.is_empty());
628}
629
630TEST(DynSetTree, MoveAssignment)
631{
632 DynSetAvlTree<int> set1, set2;
633
634 for (int i : {1, 2, 3})
635 set1.insert(i);
636
637 for (int i : {10, 20})
638 set2.insert(i);
639
640 set2 = std::move(set1);
641
642 EXPECT_EQ(set2.size(), 3u);
643 for (int i : {1, 2, 3})
644 EXPECT_TRUE(set2.contains(i));
645
646 EXPECT_TRUE(set1.is_empty());
647}
648
649TEST(DynSetTree, Swap)
650{
651 DynSetAvlTree<int> set1, set2;
652
653 for (int i : {1, 2, 3})
654 set1.insert(i);
655
656 for (int i : {10, 20})
657 set2.insert(i);
658
659 set1.swap(set2);
660
661 EXPECT_EQ(set1.size(), 2u);
662 EXPECT_TRUE(set1.contains(10));
663 EXPECT_TRUE(set1.contains(20));
664
665 EXPECT_EQ(set2.size(), 3u);
666 EXPECT_TRUE(set2.contains(1));
667 EXPECT_TRUE(set2.contains(2));
668 EXPECT_TRUE(set2.contains(3));
669}
670
671// ============================================================================
672// Iterator Tests
673// ============================================================================
674
675TEST(DynSetTree, IteratorEmptySet)
676{
678
680 EXPECT_FALSE(it.has_curr());
681}
682
683TEST(DynSetTree, IteratorTraversesInOrder)
684{
686
687 for (int i : {5, 3, 7, 1, 9})
688 set.insert(i);
689
690 vector<int> keys;
691 for (DynSetAvlTree<int>::Iterator it(set); it.has_curr(); it.next_ne())
692 keys.push_back(it.get_curr());
693
694 vector<int> expected = {1, 3, 5, 7, 9};
695 EXPECT_EQ(keys, expected);
696}
697
698TEST(DynSetTree, IteratorReset)
699{
701
702 for (int i : {1, 2, 3})
703 set.insert(i);
704
706
707 ASSERT_TRUE(it.has_curr());
708 EXPECT_EQ(it.get_curr(), 1);
709
710 it.next_ne();
711 EXPECT_EQ(it.get_curr(), 2);
712
713 it.reset_first();
714 EXPECT_EQ(it.get_curr(), 1);
715}
716
717// ============================================================================
718// Traversal Tests (for_each_*)
719// ============================================================================
720
721TEST(DynSetTree, ForEachInorder)
722{
724
725 for (int i : {5, 3, 7, 1, 9})
726 set.insert(i);
727
728 vector<int> keys;
729 set.for_each_inorder([&keys](const int & k) { keys.push_back(k); });
730
731 vector<int> expected = {1, 3, 5, 7, 9};
732 EXPECT_EQ(keys, expected);
733}
734
735TEST(DynSetTree, ForEachPreorder)
736{
738
739 for (int i : {5, 3, 7, 1, 9})
740 set.insert(i);
741
742 vector<int> keys;
743 set.for_each_preorder([&keys](const int & k) { keys.push_back(k); });
744
745 // Just verify we got all keys (order depends on tree structure)
746 EXPECT_EQ(keys.size(), 5u);
747 for (int i : {1, 3, 5, 7, 9})
748 EXPECT_TRUE(find(keys.begin(), keys.end(), i) != keys.end());
749}
750
751TEST(DynSetTree, ForEachPostorder)
752{
754
755 for (int i : {5, 3, 7, 1, 9})
756 set.insert(i);
757
758 vector<int> keys;
759 set.for_each_postorder([&keys](const int & k) { keys.push_back(k); });
760
761 // Just verify we got all keys
762 EXPECT_EQ(keys.size(), 5u);
763 for (int i : {1, 3, 5, 7, 9})
764 EXPECT_TRUE(find(keys.begin(), keys.end(), i) != keys.end());
765}
766
767TEST(DynSetTree, Traverse)
768{
770
771 for (int i : {1, 2, 3, 4, 5})
772 set.insert(i);
773
774 int sum = 0;
775 bool completed = set.traverse([&sum](const int & k) {
776 sum += k;
777 return true;
778 });
779
780 EXPECT_TRUE(completed);
781 EXPECT_EQ(sum, 15);
782}
783
784TEST(DynSetTree, TraverseEarlyExit)
785{
787
788 for (int i : {1, 2, 3, 4, 5})
789 set.insert(i);
790
791 int count = 0;
792 bool completed = set.traverse([&count](const int &) {
793 ++count;
794 return count < 3; // Stop after 3 elements
795 });
796
797 EXPECT_FALSE(completed);
798 EXPECT_EQ(count, 3);
799}
800
801#endif
802
803// ============================================================================
804// Range Operations Tests (only for trees with rank support)
805// ============================================================================
806
807TEST(DynSetTreapRk, SelectByPosition)
808{
810
811 for (int i : {5, 3, 7, 1, 9, 2, 8})
812 set.insert(i);
813
814 // Sorted order: 1, 2, 3, 5, 7, 8, 9
815 EXPECT_EQ(set.select(0), 1);
816 EXPECT_EQ(set.select(1), 2);
817 EXPECT_EQ(set.select(2), 3);
818 EXPECT_EQ(set.select(3), 5);
819 EXPECT_EQ(set.select(6), 9);
820}
821
823{
825
826 for (int i : {5, 3, 7, 1, 9})
827 set.insert(i);
828
829 // Sorted order: 1, 3, 5, 7, 9
830 EXPECT_EQ(set.position(1), 0);
831 EXPECT_EQ(set.position(3), 1);
832 EXPECT_EQ(set.position(5), 2);
833 EXPECT_EQ(set.position(7), 3);
834 EXPECT_EQ(set.position(9), 4);
835 EXPECT_EQ(set.position(99), -1); // Not found
836}
837
838TEST(DynSetTreapRk, FindPosition)
839{
841
842 for (int i : {5, 3, 7, 1, 9})
843 set.insert(i);
844
845 auto [pos, key_ptr] = set.find_position(5);
846 EXPECT_EQ(pos, 2);
847 ASSERT_NE(key_ptr, nullptr);
848 EXPECT_EQ(*key_ptr, 5);
849
850 auto [pos2, key_ptr2] = set.find_position(6); // Not found
851 EXPECT_GE(pos2, 2); // Position where it would be
852 ASSERT_NE(key_ptr2, nullptr);
853}
854
856{
858
859 for (int i : {5, 3, 7, 1, 9})
860 set.insert(i);
861
862 // Remove element at position 2 (which is 5 in sorted order 1,3,5,7,9)
863 int removed = set.remove_pos(2);
864 EXPECT_EQ(removed, 5);
865 EXPECT_EQ(set.size(), 4u);
866 EXPECT_FALSE(set.contains(5));
867}
868
869TEST(DynSetTreapRk, OperatorParenthesis)
870{
872
873 for (int i : {5, 3, 7, 1, 9})
874 set.insert(i);
875
876 // Sorted order: 1, 3, 5, 7, 9
877 EXPECT_EQ(set(0), 1);
878 EXPECT_EQ(set(2), 5);
879 EXPECT_EQ(set(4), 9);
880}
881
882// ============================================================================
883// Set Operations Tests (join, split)
884// ============================================================================
885
887{
889
890 // Insert even numbers only, so we can split on an odd number not in the tree
891 for (int i : {2, 4, 6, 8, 10, 12, 14, 16, 18})
892 set.insert(i);
893
894 DynSetTreapRk<int> left, right;
895
896 // Split on 11, which is not in the tree
897 bool success = set.split_key(11, left, right);
898
899 EXPECT_TRUE(success);
900 EXPECT_TRUE(set.is_empty());
901
902 // left should have {2, 4, 6, 8, 10} - keys < 11
903 EXPECT_EQ(left.size(), 5u);
904 for (int i : {2, 4, 6, 8, 10})
905 EXPECT_TRUE(left.contains(i));
906
907 // right should have {12, 14, 16, 18} - keys > 11
908 EXPECT_EQ(right.size(), 4u);
909 for (int i : {12, 14, 16, 18})
910 EXPECT_TRUE(right.contains(i));
911
912 // 11 is not in either (it was never inserted)
913 EXPECT_FALSE(left.contains(11));
914 EXPECT_FALSE(right.contains(11));
915}
916
917TEST(DynSetTreapRk, SplitKeyDup)
918{
920
921 for (int i : {1, 2, 3, 4, 5, 6, 7, 8, 9})
922 set.insert(i);
923
924 DynSetTreapRk<int> left, right;
925
926 set.split_key_dup(5, left, right);
927
928 EXPECT_TRUE(set.is_empty());
929
930 // left should have {1, 2, 3, 4, 5} - keys <= 5
931 EXPECT_EQ(left.size(), 5u);
932 EXPECT_TRUE(left.contains(5));
933
934 // right should have {6, 7, 8, 9} - keys > 5
935 EXPECT_EQ(right.size(), 4u);
936 EXPECT_FALSE(right.contains(5));
937}
938
940{
942
943 for (int i : {1, 2, 3, 4, 5, 6, 7, 8, 9})
944 set.insert(i);
945
946 DynSetTreapRk<int> left, right;
947
948 set.split_pos(4, left, right);
949
950 EXPECT_TRUE(set.is_empty());
951
952 // left should have first 5 elements: {1, 2, 3, 4, 5}
953 EXPECT_EQ(left.size(), 5u);
954 for (int i : {1, 2, 3, 4, 5})
955 EXPECT_TRUE(left.contains(i));
956
957 // right should have rest: {6, 7, 8, 9}
958 EXPECT_EQ(right.size(), 4u);
959 for (int i : {6, 7, 8, 9})
960 EXPECT_TRUE(right.contains(i));
961}
962
963TEST(DynSetTreapRk, SplitPosEdgeCases)
964{
966
967 for (int i : {1, 2, 3, 4, 5})
968 set.insert(i);
969
970 // pos == 0 => left has {1}, right has {2,3,4,5}
971 {
972 DynSetTreapRk<int> tmp(set);
973 DynSetTreapRk<int> left, right;
974 tmp.split_pos(0, left, right);
975 EXPECT_TRUE(tmp.is_empty());
976 EXPECT_EQ(left.size(), 1u);
977 EXPECT_EQ(right.size(), 4u);
978 EXPECT_TRUE(left.contains(1));
979 EXPECT_TRUE(right.contains(5));
980 }
981
982 // pos == size-1 => left has all, right empty
983 {
984 DynSetTreapRk<int> tmp(set);
985 DynSetTreapRk<int> left, right;
986 tmp.split_pos(tmp.size() - 1, left, right);
987 EXPECT_TRUE(tmp.is_empty());
988 EXPECT_EQ(left.size(), 5u);
989 EXPECT_TRUE(right.is_empty());
990 }
991
992 // out of range
993 {
994 DynSetTreapRk<int> tmp(set);
995 DynSetTreapRk<int> left, right;
996 EXPECT_THROW(tmp.split_pos(tmp.size(), left, right), out_of_range);
997 EXPECT_THROW(tmp.split_pos(tmp.size() + 10, left, right), out_of_range);
998 }
999}
1000
1001TEST(DynSetTreapRk, RankOperationsOutOfRange)
1002{
1004 for (int i : {1, 2, 3})
1005 set.insert(i);
1006
1007 EXPECT_THROW(set.select(3), out_of_range);
1008 EXPECT_THROW(set.remove_pos(3), out_of_range);
1009
1010 // empty
1011 DynSetTreapRk<int> empty;
1012 EXPECT_THROW(empty.select(0), out_of_range);
1013 EXPECT_THROW(empty.remove_pos(0), out_of_range);
1014 DynSetTreapRk<int> left, right;
1015 EXPECT_THROW(empty.split_pos(0, left, right), out_of_range);
1016}
1017
1019{
1020 DynSetTreapRk<int> set1, set2;
1021
1022 for (int i : {1, 3, 5})
1023 set1.insert(i);
1024
1025 for (int i : {2, 4, 6})
1026 set2.insert(i);
1027
1029 set1.join(set2, dup);
1030
1031 EXPECT_TRUE(set2.is_empty());
1032 EXPECT_EQ(set1.size(), 6u);
1033 for (int i : {1, 2, 3, 4, 5, 6})
1034 EXPECT_TRUE(set1.contains(i));
1035
1036 EXPECT_TRUE(dup.is_empty());
1037}
1038
1039TEST(DynSetTreapRk, JoinWithDuplicates)
1040{
1041 DynSetTreapRk<int> set1, set2;
1042
1043 for (int i : {1, 2, 3, 4, 5})
1044 set1.insert(i);
1045
1046 for (int i : {3, 4, 5, 6, 7})
1047 set2.insert(i);
1048
1050 set1.join(set2, dup);
1051
1052 EXPECT_TRUE(set2.is_empty());
1053 EXPECT_EQ(set1.size(), 7u);
1054 for (int i : {1, 2, 3, 4, 5, 6, 7})
1055 EXPECT_TRUE(set1.contains(i));
1056
1057 EXPECT_EQ(dup.size(), 3u);
1058 for (int i : {3, 4, 5})
1059 EXPECT_TRUE(dup.contains(i));
1060}
1061
1063{
1064 DynSetTreapRk<int> set1, set2;
1065
1066 for (int i : {1, 2, 3})
1067 set1.insert(i);
1068
1069 for (int i : {3, 4, 5})
1070 set2.insert(i);
1071
1072 set1.join_dup(set2);
1073
1074 EXPECT_TRUE(set2.is_empty());
1075 EXPECT_EQ(set1.size(), 6u); // Duplicates allowed (3 appears twice)
1076}
1077
1078// =========================================================================
1079// insert_dup() Tests
1080// =========================================================================
1081
1082template <typename T>
1084{
1085 T set;
1086 set.insert_dup(42);
1087 set.insert_dup(42);
1088 set.insert_dup(7);
1089
1090 EXPECT_EQ(set.size(), 3u);
1091
1092 size_t count_42 = 0;
1093 for (typename T::Iterator it(set); it.has_curr(); it.next_ne())
1094 if (it.get_curr() == 42)
1095 ++count_42;
1096
1097 EXPECT_EQ(count_42, 2u);
1098}
1099
1100TEST(DynSetTree, InsertDupTraversesDuplicates)
1101{
1102 insert_dup_traversal_test<DynSetRbTree<int>>();
1103 insert_dup_traversal_test<DynSetTreap<int>>();
1104 insert_dup_traversal_test<DynSetTreapRk<int>>();
1105 insert_dup_traversal_test<DynSetSplayTree<int>>();
1106 insert_dup_traversal_test<DynSetRandTree<int>>();
1107 insert_dup_traversal_test<DynSetBinTree<int>>();
1108 insert_dup_traversal_test<DynSetAvlTree<int>>();
1109}
1110
1111// =========================================================================
1112// Range APIs on non-rank trees must throw domain_error
1113// =========================================================================
1114
1115template <typename T>
1117{
1118 T set;
1119
1120 // On empty: must still report "unsupported" rather than pretending it works.
1121 EXPECT_THROW(set.position(1), domain_error);
1122 EXPECT_THROW(set.find_position(1), domain_error);
1123 EXPECT_THROW(set.select(0), domain_error);
1124 EXPECT_THROW(set.remove_pos(0), domain_error);
1125 T left, right;
1126 EXPECT_THROW(set.split_pos(0, left, right), domain_error);
1127}
1128
1129TEST(DynSetTree, RangeMethodsThrowOnNonRankTrees)
1130{
1131 range_methods_throw_domain_error_test<DynSetAvlTree<int>>();
1132 range_methods_throw_domain_error_test<DynSetRbTree<int>>();
1133 range_methods_throw_domain_error_test<DynSetSplayTree<int>>();
1134 range_methods_throw_domain_error_test<DynSetTreap<int>>();
1135 range_methods_throw_domain_error_test<DynSetBinTree<int>>();
1136}
1137
1138// ============================================================================
1139// Different Tree Types Tests
1140// ============================================================================
1141
1142#if 0
1143
1144TEST(DynSetBinTree, BasicOperations)
1145{
1147
1148 for (int i : {5, 3, 7, 1, 9})
1149 set.insert(i);
1150
1151 EXPECT_EQ(set.size(), 5u);
1152 EXPECT_TRUE(set.contains(3));
1153 EXPECT_FALSE(set.contains(4));
1154}
1155
1156TEST(DynSetSplayTree, BasicOperations)
1157{
1159
1160 for (int i : {5, 3, 7, 1, 9})
1161 set.insert(i);
1162
1163 EXPECT_EQ(set.size(), 5u);
1164 EXPECT_TRUE(set.contains(3));
1165}
1166
1167TEST(DynSetRandTree, BasicOperations)
1168{
1170
1171 for (int i : {5, 3, 7, 1, 9})
1172 set.insert(i);
1173
1174 EXPECT_EQ(set.size(), 5u);
1175 EXPECT_TRUE(set.contains(3));
1176}
1177
1178TEST(DynSetTreap, BasicOperations)
1179{
1180 DynSetTreap<int> set;
1181
1182 for (int i : {5, 3, 7, 1, 9})
1183 set.insert(i);
1184
1185 EXPECT_EQ(set.size(), 5u);
1186 EXPECT_TRUE(set.contains(3));
1187}
1188
1189#endif
1190
1191// ============================================================================
1192// Custom Comparator Tests
1193// ============================================================================
1194
1195TEST(DynSetTree, CustomComparatorGreater)
1196{
1198
1199 for (int i : {5, 3, 7, 1, 9})
1200 set.insert(i);
1201
1202 EXPECT_EQ(set.min(), 9); // Max in normal order
1203 EXPECT_EQ(set.max(), 1); // Min in normal order
1204
1205 vector<int> keys;
1206 for (DynSetTree<int, Avl_Tree, std::greater<int>>::Iterator it(set); it.has_curr(); it.next_ne())
1207 keys.push_back(it.get_curr());
1208
1209 vector<int> expected = {9, 7, 5, 3, 1};
1210 EXPECT_EQ(keys, expected);
1211}
1212
1213// ============================================================================
1214// Operator[] Tests
1215// ============================================================================
1216
1217TEST(DynSetTree, OperatorBracketConstThrows)
1218{
1219 const DynSetAvlTree<int> set;
1220
1221 EXPECT_THROW(set[42], domain_error);
1222}
1223
1224TEST(DynSetTree, OperatorBracketNonConstInserts)
1225{
1227
1228 EXPECT_FALSE(set.contains(42));
1229
1230 const int & ref = set[42];
1231 EXPECT_EQ(ref, 42);
1232 EXPECT_TRUE(set.contains(42));
1233 EXPECT_EQ(set.size(), 1u);
1234}
1235
1236// ============================================================================
1237// Verify Tests
1238// ============================================================================
1239
1240TEST(DynSetTree, VerifyValidTree)
1241{
1243
1244 for (int i : {5, 3, 7, 1, 9, 2, 8, 4, 6})
1245 set.insert(i);
1246
1247 EXPECT_TRUE(set.verify());
1248}
1249
1250// ============================================================================
1251// Stress Tests
1252// ============================================================================
1253
1254TEST(DynSetTree, LargeSetOperations)
1255{
1257
1258 // Insert 1000 elements
1259 for (int i = 0; i < 1000; ++i)
1260 set.insert(i);
1261
1262 EXPECT_EQ(set.size(), 1000u);
1263 EXPECT_EQ(set.min(), 0);
1264 EXPECT_EQ(set.max(), 999);
1265
1266 // Verify all present
1267 for (int i = 0; i < 1000; ++i)
1268 EXPECT_TRUE(set.contains(i));
1269
1270 // Remove half
1271 for (int i = 0; i < 1000; i += 2)
1272 set.remove(i);
1273
1274 EXPECT_EQ(set.size(), 500u);
1275
1276 // Verify correct ones remain
1277 for (int i = 0; i < 1000; ++i)
1278 {
1279 if (i % 2 == 0)
1280 EXPECT_FALSE(set.contains(i));
1281 else
1282 EXPECT_TRUE(set.contains(i));
1283 }
1284}
1285
1286TEST(DynSetTree, RandomInsertRemove)
1287{
1289 vector<int> inserted;
1290
1291 // Random insertions
1292 for (int i = 0; i < 200; ++i)
1293 {
1294 if (int key = rand() % 500; set.insert(key) != nullptr)
1295 inserted.push_back(key);
1296 }
1297
1298 // Verify all inserted keys are present
1299 for (int key : inserted)
1300 EXPECT_TRUE(set.contains(key));
1301
1302 EXPECT_EQ(set.size(), inserted.size());
1303
1304 // Random removals
1305 for (size_t i = 0; i < inserted.size() / 2; ++i)
1306 {
1307 int idx = rand() % inserted.size();
1308 set.remove(inserted[idx]);
1309 inserted.erase(inserted.begin() + idx);
1310 }
1311
1312 EXPECT_EQ(set.size(), inserted.size());
1313 for (int key : inserted)
1314 EXPECT_TRUE(set.contains(key));
1315}
1316
1317// ============================================================================
1318// String Key Tests
1319// ============================================================================
1320
1321TEST(DynSetTree, StringKeys)
1322{
1324
1325 set.insert("apple");
1326 set.insert("banana");
1327 set.insert("cherry");
1328
1329 EXPECT_EQ(set.size(), 3u);
1330 EXPECT_TRUE(set.contains("apple"));
1331 EXPECT_TRUE(set.contains("banana"));
1332 EXPECT_FALSE(set.contains("date"));
1333
1334 EXPECT_EQ(set.min(), "apple");
1335 EXPECT_EQ(set.max(), "cherry");
1336}
1337
1338// ============================================================================
1339// Edge Cases
1340// ============================================================================
1341
1342TEST(DynSetTree, SingleElement)
1343{
1345
1346 set.insert(42);
1347
1348 EXPECT_EQ(set.min(), 42);
1349 EXPECT_EQ(set.max(), 42);
1350 EXPECT_EQ(set.get_root(), 42);
1351 EXPECT_EQ(set.size(), 1u);
1352
1353 set.remove(42);
1354 EXPECT_TRUE(set.is_empty());
1355}
1356
1357TEST(DynSetTree, InsertRemoveRepeated)
1358{
1360
1361 for (int cycle = 0; cycle < 10; ++cycle)
1362 {
1363 for (int i = 0; i < 50; ++i)
1364 set.insert(i);
1365
1366 EXPECT_EQ(set.size(), 50u);
1367
1368 for (int i = 0; i < 50; ++i)
1369 set.remove(i);
1370
1371 EXPECT_TRUE(set.is_empty());
1372 }
1373}
1374
1375TEST(DynSetTree, AccessMethods)
1376{
1378
1379 for (int i : {1, 2, 3, 4, 5})
1380 set.insert(i);
1381
1382 EXPECT_EQ(set.get_first(), 1);
1383 EXPECT_EQ(set.get_last(), 5);
1384 EXPECT_EQ(set.get(), 5);
1385 EXPECT_NO_THROW(set.get_item());
1386}
1387
1388namespace {
1389
1390struct TrackedKey
1391{
1392 int v;
1393 inline static int alive = 0;
1394
1395 TrackedKey() : v(0) { ++alive; }
1396 explicit TrackedKey(int vv) : v(vv) { ++alive; }
1397 TrackedKey(const TrackedKey & other) : v(other.v) { ++alive; }
1398 TrackedKey(TrackedKey && other) noexcept : v(other.v) { ++alive; }
1399 ~TrackedKey() { --alive; }
1400};
1401
1402inline bool operator < (const TrackedKey & a, const TrackedKey & b)
1403{
1404 return a.v < b.v;
1405}
1406
1407template <typename Key, class Compare>
1408class ThrowingSearchOrInsertTree
1409{
1410public:
1411
1412 using Node = BinNode<Key>;
1413
1414 inline static bool enabled = false;
1415
1416 ThrowingSearchOrInsertTree(Compare c = Compare()) : cmp(c) { /* empty */ }
1417
1418 Compare get_compare() const { return cmp; }
1419
1420 Node *& getRoot() noexcept { return root; }
1421 Node * getRoot() const noexcept { return root; }
1422
1423 void swap(ThrowingSearchOrInsertTree & other) noexcept
1424 {
1425 std::swap(root, other.root);
1426 std::swap(cmp, other.cmp);
1427 }
1428
1429 // Stubs required by BSTPolicy concept. Only search_or_insert is exercised
1430 // by the exception-safety tests below; the rest are intentionally minimal
1431 // and not functionally correct tree operations.
1432 Node * search(const Key &) noexcept { return root; }
1433
1434 Node * search_or_insert(Node * p)
1435 {
1436 if (enabled)
1437 throw std::runtime_error("search_or_insert failed");
1438
1439 if (root == Node::NullPtr)
1440 return root = p;
1441
1442 return root;
1443 }
1444
1445 Node * insert_dup(Node * p) noexcept
1446 {
1447 if (root == Node::NullPtr)
1448 return root = p;
1449 return p;
1450 }
1451
1452 Node * remove(const Key &) noexcept { return nullptr; }
1453
1454 bool verify() const { return true; }
1455
1456private:
1457
1459 Compare cmp;
1460};
1461
1463
1464} // namespace
1465
1466TEST(DynSetTreeHardening, InsertDoesNotLeakOnThrow)
1467{
1468 TrackedKey::alive = 0;
1469 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled = false;
1470
1471 {
1472 ThrowingSet set;
1473 const int baseline = TrackedKey::alive;
1474 set.insert(TrackedKey(1));
1475 EXPECT_EQ(set.size(), 1u);
1476 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1477
1478 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled = true;
1479 EXPECT_THROW((void) set.insert(TrackedKey(2)), std::runtime_error);
1480 EXPECT_EQ(set.size(), 1u);
1481 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1482 }
1483
1484 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled = false;
1485 EXPECT_EQ(TrackedKey::alive, 0);
1486}
1487
1488TEST(DynSetTreeHardening, SearchOrInsertDoesNotLeakOnThrow)
1489{
1490 TrackedKey::alive = 0;
1491 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled = false;
1492
1493 {
1494 ThrowingSet set;
1495 const int baseline = TrackedKey::alive;
1496 set.insert(TrackedKey(1));
1497 EXPECT_EQ(set.size(), 1u);
1498 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1499
1500 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled = true;
1501 EXPECT_THROW((void) set.search_or_insert(TrackedKey(2)), std::runtime_error);
1502 EXPECT_EQ(set.size(), 1u);
1503 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1504 }
1505
1506 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled = false;
1507 EXPECT_EQ(TrackedKey::alive, 0);
1508}
1509
1510TEST(DynSetTreeHardening, ContainsOrInsertDoesNotLeakOnThrow)
1511{
1512 TrackedKey::alive = 0;
1513 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled = false;
1514
1515 {
1516 ThrowingSet set;
1517 const int baseline = TrackedKey::alive;
1518 set.insert(TrackedKey(1));
1519 EXPECT_EQ(set.size(), 1u);
1520 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1521
1522 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled = true;
1523 EXPECT_THROW((void) set.contains_or_insert(TrackedKey(2)), std::runtime_error);
1524 EXPECT_EQ(set.size(), 1u);
1525 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1526 }
1527
1528 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled = false;
1529 EXPECT_EQ(TrackedKey::alive, 0);
1530}
1531
1532// ============================================================================
1533// Missing Coverage Tests - Append and Put Methods
1534// ============================================================================
1535
1537{
1538 auto * p1 = this->set.append(10);
1539 ASSERT_NE(p1, nullptr);
1540 EXPECT_EQ(*p1, 10);
1541 EXPECT_EQ(this->set.size(), 1u);
1542
1543 auto * p2 = this->set.append(20);
1544 ASSERT_NE(p2, nullptr);
1545 EXPECT_EQ(*p2, 20);
1546 EXPECT_EQ(this->set.size(), 2u);
1547
1548 // Duplicate should fail
1549 auto * p3 = this->set.append(10);
1550 EXPECT_EQ(p3, nullptr);
1551 EXPECT_EQ(this->set.size(), 2u);
1552}
1553
1555{
1556 auto * p1 = this->set.put(10);
1557 ASSERT_NE(p1, nullptr);
1558 EXPECT_EQ(*p1, 10);
1559 EXPECT_EQ(this->set.size(), 1u);
1560
1561 auto * p2 = this->set.put(20);
1562 ASSERT_NE(p2, nullptr);
1563 EXPECT_EQ(*p2, 20);
1564 EXPECT_EQ(this->set.size(), 2u);
1565
1566 // Duplicate should fail (put is alias for insert)
1567 auto * p3 = this->set.put(10);
1568 EXPECT_EQ(p3, nullptr);
1569 EXPECT_EQ(this->set.size(), 2u);
1570}
1571
1572// ============================================================================
1573// Missing Coverage Tests - Rvalue Insert Variants
1574// ============================================================================
1575
1577{
1578 string s = "test";
1579 // Using int for simplicity - rvalue path tested via move
1580 int val = 42;
1581 auto * p = this->set.insert(std::move(val));
1582 ASSERT_NE(p, nullptr);
1583 EXPECT_EQ(*p, 42);
1584}
1585
1587{
1588 int val = 42;
1589 auto * p = this->set.append(std::move(val));
1590 ASSERT_NE(p, nullptr);
1591 EXPECT_EQ(*p, 42);
1592}
1593
1595{
1596 int val = 42;
1597 auto * p = this->set.put(std::move(val));
1598 ASSERT_NE(p, nullptr);
1599 EXPECT_EQ(*p, 42);
1600}
1601
1602TYPED_TEST(DynSetTreeTypedTest, SearchOrInsertRvalue)
1603{
1604 int val = 42;
1605 auto * p = this->set.search_or_insert(std::move(val));
1606 ASSERT_NE(p, nullptr);
1607 EXPECT_EQ(*p, 42);
1608 EXPECT_EQ(this->set.size(), 1u);
1609}
1610
1611TYPED_TEST(DynSetTreeTypedTest, ContainsOrInsertRvalue)
1612{
1613 int val = 42;
1614 auto [p, found] = this->set.contains_or_insert(std::move(val));
1615 ASSERT_NE(p, nullptr);
1616 EXPECT_FALSE(found);
1617 EXPECT_EQ(*p, 42);
1618}
1619
1621{
1622 int val1 = 42;
1623 auto * p1 = this->set.insert_dup(std::move(val1));
1624 ASSERT_NE(p1, nullptr);
1625
1626 int val2 = 42;
1627 auto * p2 = this->set.insert_dup(std::move(val2));
1628 ASSERT_NE(p2, nullptr);
1629
1630 EXPECT_EQ(this->set.size(), 2u);
1631}
1632
1633// ============================================================================
1634// Missing Coverage Tests - Tree Metrics
1635// ============================================================================
1636
1638{
1639 EXPECT_EQ(this->set.height(), 0u);
1640
1641 this->set.insert(1);
1642 EXPECT_GE(this->set.height(), 1u);
1643
1644 for (int i = 2; i <= 10; ++i)
1645 this->set.insert(i);
1646
1647 size_t h = this->set.height();
1648 EXPECT_GE(h, 1u);
1649 EXPECT_LE(h, 10u); // Should be balanced, much less than 10
1650}
1651
1653{
1654 // Empty tree
1655 EXPECT_EQ(this->set.internal_path_length(), 0u);
1656
1657 this->set.insert(1);
1658 EXPECT_EQ(this->set.internal_path_length(), 0u); // Root has depth 0
1659
1660 this->set.insert(2);
1661 this->set.insert(3);
1662 EXPECT_GT(this->set.internal_path_length(), 0u);
1663}
1664
1666{
1667 // Empty tree - root may be nullptr or a sentinel node depending on tree type
1668 // Just verify that after insert, the root is valid and different
1669
1670 this->set.insert(42);
1671 auto * node = this->set.get_root_node();
1672 ASSERT_NE(node, nullptr);
1673
1674 // Verify it's a real node with the key
1675 EXPECT_EQ(node->get_key(), 42);
1676}
1677
1678// ============================================================================
1679// Missing Coverage Tests - Access Methods
1680// ============================================================================
1681
1683{
1684 for (int i : {5, 3, 7, 1, 9})
1685 this->set.insert(i);
1686
1687 EXPECT_EQ(this->set.get_first(), 1);
1688 EXPECT_EQ(this->set.get_last(), 9);
1689}
1690
1692{
1693 for (int i : {5, 3, 7, 1, 9})
1694 this->set.insert(i);
1695
1696 // get() is alias for max()
1697 EXPECT_EQ(this->set.get(), 9);
1698}
1699
1701{
1702 this->set.insert(42);
1703
1704 // get_item() returns any element (same as get_root())
1705 EXPECT_NO_THROW(this->set.get_item());
1706}
1707
1708TYPED_TEST(DynSetTreeTypedTest, ExistHasContainsConsistency)
1709{
1710 this->set.insert(42);
1711
1712 // All three should behave identically
1713 EXPECT_EQ(this->set.exist(42), this->set.has(42));
1714 EXPECT_EQ(this->set.has(42), this->set.contains(42));
1715
1716 EXPECT_EQ(this->set.exist(99), this->set.has(99));
1717 EXPECT_EQ(this->set.has(99), this->set.contains(99));
1718}
1719
1720// ============================================================================
1721// Missing Coverage Tests - Assignment Operators
1722// ============================================================================
1723
1725{
1726 TypeParam set2;
1727
1728 for (int i : {1, 2, 3})
1729 this->set.insert(i);
1730
1731 for (int i : {10, 20})
1732 set2.insert(i);
1733
1734 set2 = this->set;
1735
1736 EXPECT_EQ(set2.size(), 3u);
1737 for (int i : {1, 2, 3})
1738 EXPECT_TRUE(set2.contains(i));
1739 EXPECT_FALSE(set2.contains(10));
1740}
1741
1743{
1744 for (int i : {1, 2, 3})
1745 this->set.insert(i);
1746
1747 this->set = this->set;
1748
1749 EXPECT_EQ(this->set.size(), 3u);
1750 for (int i : {1, 2, 3})
1751 EXPECT_TRUE(this->set.contains(i));
1752}
1753
1755{
1756 TypeParam set2;
1757
1758 for (int i : {1, 2, 3})
1759 this->set.insert(i);
1760
1761 for (int i : {10, 20})
1762 set2.insert(i);
1763
1764 set2 = std::move(this->set);
1765
1766 EXPECT_EQ(set2.size(), 3u);
1767 for (int i : {1, 2, 3})
1768 EXPECT_TRUE(set2.contains(i));
1769
1770 EXPECT_TRUE(this->set.is_empty());
1771}
1772
1774{
1775 for (int i : {1, 2, 3})
1776 this->set.insert(i);
1777
1778 // Self move-assignment should be safe (no-op)
1779#if defined(__clang__)
1780# pragma clang diagnostic push
1781# pragma clang diagnostic ignored "-Wself-move"
1782#elif defined(__GNUC__)
1783# pragma GCC diagnostic push
1784# pragma GCC diagnostic ignored "-Wself-move"
1785#endif
1786 this->set = std::move(this->set);
1787#if defined(__clang__)
1788# pragma clang diagnostic pop
1789#elif defined(__GNUC__)
1790# pragma GCC diagnostic pop
1791#endif
1792
1793 // After self-move, behavior is unspecified but should not crash
1794 // The set should still be in a valid state
1795}
1796
1797// ============================================================================
1798// Missing Coverage Tests - Initializer List Construction
1799// ============================================================================
1800
1801TEST(DynSetTree, InitializerListConstruction)
1802{
1803 DynSetAvlTree<int> set = {5, 3, 7, 1, 9};
1804
1805 EXPECT_EQ(set.size(), 5u);
1806 for (int i : {1, 3, 5, 7, 9})
1807 EXPECT_TRUE(set.contains(i));
1808}
1809
1810TEST(DynSetTree, InitializerListConstructionWithDuplicates)
1811{
1812 // Duplicates in initializer list should be ignored
1813 DynSetAvlTree<int> set = {1, 2, 3, 2, 1};
1814
1815 EXPECT_EQ(set.size(), 3u);
1816}
1817
1818TEST(DynSetTree, InitializerListEmpty)
1819{
1820 DynSetAvlTree<int> set = {};
1821
1822 EXPECT_TRUE(set.is_empty());
1823}
1824
1825// ============================================================================
1826// Missing Coverage Tests - Split Operations Edge Cases
1827// ============================================================================
1828
1829TEST(DynSetTreapRk, SplitKeyWhenKeyExists)
1830{
1832
1833 for (int i : {1, 2, 3, 4, 5})
1834 set.insert(i);
1835
1836 DynSetTreapRk<int> left, right;
1837
1838 // Split on existing key should fail
1839 bool success = set.split_key(3, left, right);
1840
1841 EXPECT_FALSE(success);
1842 // Original set should be unchanged
1843 EXPECT_EQ(set.size(), 5u);
1844 EXPECT_TRUE(left.is_empty());
1845 EXPECT_TRUE(right.is_empty());
1846}
1847
1848TEST(DynSetTreapRk, SplitKeyEmptySet)
1849{
1851 DynSetTreapRk<int> left, right;
1852
1853 bool success = set.split_key(42, left, right);
1854
1855 EXPECT_TRUE(success);
1856 EXPECT_TRUE(left.is_empty());
1857 EXPECT_TRUE(right.is_empty());
1858}
1859
1860TEST(DynSetTreapRk, SplitKeyDupEmptySet)
1861{
1863 DynSetTreapRk<int> left, right;
1864
1865 EXPECT_NO_THROW(set.split_key_dup(42, left, right));
1866 EXPECT_TRUE(left.is_empty());
1867 EXPECT_TRUE(right.is_empty());
1868}
1869
1870// ============================================================================
1871// Missing Coverage Tests - Rank Operations on TreapRk
1872// ============================================================================
1873
1874TEST(DynSetTreapRk, AccessMethod)
1875{
1877
1878 for (int i : {5, 3, 7, 1, 9})
1879 set.insert(i);
1880
1881 // access() is alias for select()
1882 EXPECT_EQ(set.access(0), 1);
1883 EXPECT_EQ(set.access(2), 5);
1884 EXPECT_EQ(set.access(4), 9);
1885}
1886
1887TEST(DynSetTreapRk, SelectOutOfRange)
1888{
1890
1891 for (int i : {1, 2, 3})
1892 set.insert(i);
1893
1894 EXPECT_THROW(set.select(3), out_of_range);
1895 EXPECT_THROW(set.select(100), out_of_range);
1896}
1897
1898TEST(DynSetTreapRk, SelectOnEmptySet)
1899{
1901
1902 EXPECT_THROW(set.select(0), out_of_range);
1903}
1904
1905TEST(DynSetTreapRk, RemovePosOutOfRange)
1906{
1908
1909 for (int i : {1, 2, 3})
1910 set.insert(i);
1911
1912 EXPECT_THROW(set.remove_pos(3), out_of_range);
1913 EXPECT_THROW(set.remove_pos(100), out_of_range);
1914}
1915
1916TEST(DynSetTreapRk, FindPositionOnEmptySet)
1917{
1919
1920 auto [pos, ptr] = set.find_position(42);
1921 EXPECT_EQ(pos, 0);
1922 EXPECT_EQ(ptr, nullptr);
1923}
1924
1925TEST(DynSetTreapRk, PositionNotFound)
1926{
1928
1929 for (int i : {1, 3, 5, 7, 9})
1930 set.insert(i);
1931
1932 // Position of non-existent key
1933 EXPECT_EQ(set.position(2), -1);
1934 EXPECT_EQ(set.position(4), -1);
1935 EXPECT_EQ(set.position(100), -1);
1936}
1937
1938TEST(DynSetTreapRk, ConstSelect)
1939{
1941
1942 for (int i : {5, 3, 7, 1, 9})
1943 set.insert(i);
1944
1945 const auto & const_set = set;
1946
1947 EXPECT_EQ(const_set.select(0), 1);
1948 EXPECT_EQ(const_set.select(4), 9);
1949}
1950
1951// ============================================================================
1952// Missing Coverage Tests - Iterator Edge Cases
1953// ============================================================================
1954
1956{
1957 typename TypeParam::Iterator it(this->set);
1958
1959 EXPECT_FALSE(it.has_curr());
1960}
1961
1963{
1964 for (int i : {1, 2, 3})
1965 this->set.insert(i);
1966
1967 typename TypeParam::Iterator it(this->set);
1968
1969 // Advance to end
1970 while (it.has_curr())
1971 it.next_ne();
1972
1973 it.reset_first();
1974 EXPECT_TRUE(it.has_curr());
1975 EXPECT_EQ(it.get_curr(), 1);
1976}
1977
1979{
1980 for (int i : {1, 2, 3})
1981 this->set.insert(i);
1982
1983 typename TypeParam::Iterator it(this->set);
1984
1985 it.reset_last();
1986 EXPECT_TRUE(it.has_curr());
1987 EXPECT_EQ(it.get_curr(), 3);
1988}
1989
1990// ============================================================================
1991// Missing Coverage Tests - Traverse Early Exit
1992// ============================================================================
1993
1995{
1996 for (int i : {1, 2, 3, 4, 5})
1997 this->set.insert(i);
1998
1999 int count = 0;
2000 bool completed = this->set.traverse([&count](const int &) {
2001 ++count;
2002 return count < 3; // Stop after 3 elements
2003 });
2004
2005 EXPECT_FALSE(completed);
2006 EXPECT_EQ(count, 3);
2007}
2008
2010{
2011 for (int i : {1, 2, 3, 4, 5})
2012 this->set.insert(i);
2013
2014 const auto & const_set = this->set;
2015
2016 int sum = 0;
2017 bool completed = const_set.traverse([&sum](const int & k) {
2018 sum += k;
2019 return true;
2020 });
2021
2022 EXPECT_TRUE(completed);
2023 EXPECT_EQ(sum, 15);
2024}
2025
2026// ============================================================================
2027// Missing Coverage Tests - operator() for Non-Rank Trees
2028// ============================================================================
2029
2030TEST(DynSetTree, OperatorParenthesisOnNonRankTreeThrows)
2031{
2033
2034 for (int i : {1, 2, 3})
2035 set.insert(i);
2036
2037 // operator() calls select(), which should throw on non-rank trees
2038 EXPECT_THROW(set(0), domain_error);
2039}
2040
2041// ============================================================================
2042// Missing Coverage Tests - Join Edge Cases
2043// ============================================================================
2044
2045TEST(DynSetTreapRk, JoinEmptySets)
2046{
2047 DynSetTreapRk<int> set1, set2;
2049
2050 set1.join(set2, dup);
2051
2052 EXPECT_TRUE(set1.is_empty());
2053 EXPECT_TRUE(set2.is_empty());
2054 EXPECT_TRUE(dup.is_empty());
2055}
2056
2057TEST(DynSetTreapRk, JoinWithEmptySet)
2058{
2059 DynSetTreapRk<int> set1, set2;
2061
2062 for (int i : {1, 2, 3})
2063 set1.insert(i);
2064
2065 set1.join(set2, dup);
2066
2067 EXPECT_EQ(set1.size(), 3u);
2068 EXPECT_TRUE(set2.is_empty());
2069 EXPECT_TRUE(dup.is_empty());
2070}
2071
2072TEST(DynSetTreapRk, JoinDupEmptySets)
2073{
2074 DynSetTreapRk<int> set1, set2;
2075
2076 set1.join_dup(set2);
2077
2078 EXPECT_TRUE(set1.is_empty());
2079 EXPECT_TRUE(set2.is_empty());
2080}
2081
2082// ============================================================================
2083// Missing Coverage Tests - Verify on Empty and Single Element
2084// ============================================================================
2085
2087{
2088 EXPECT_TRUE(this->set.verify());
2089}
2090
2091TYPED_TEST(DynSetTreeTypedTest, VerifySingleElement)
2092{
2093 this->set.insert(42);
2094 EXPECT_TRUE(this->set.verify());
2095}
2096
2097// ============================================================================
2098// Missing Coverage Tests - String Type with Rvalues
2099// ============================================================================
2100
2101TEST(DynSetTree, StringRvalueInsert)
2102{
2104
2105 string s = "hello";
2106 auto * p = set.insert(std::move(s));
2107
2108 ASSERT_NE(p, nullptr);
2109 EXPECT_EQ(*p, "hello");
2110 EXPECT_EQ(set.size(), 1u);
2111}
2112
2113TEST(DynSetTree, StringRvalueSearchOrInsert)
2114{
2116
2117 string s1 = "hello";
2118 auto * p1 = set.search_or_insert(std::move(s1));
2119 ASSERT_NE(p1, nullptr);
2120 EXPECT_EQ(*p1, "hello");
2121
2122 string s2 = "hello";
2123 auto * p2 = set.search_or_insert(std::move(s2));
2124 EXPECT_EQ(p1, p2); // Same pointer (found existing)
2125}
2126
2127// ============================================================================
2128// Missing Coverage Tests - All Tree Type Aliases
2129// ============================================================================
2130
2131TEST(DynSetTreeAliases, DynSetBinTreeWorks)
2132{
2134 set.insert(1);
2135 set.insert(2);
2136 EXPECT_EQ(set.size(), 2u);
2137 EXPECT_TRUE(set.verify());
2138}
2139
2140TEST(DynSetTreeAliases, DynSetAvlTreeWorks)
2141{
2143 set.insert(1);
2144 set.insert(2);
2145 EXPECT_EQ(set.size(), 2u);
2146 EXPECT_TRUE(set.verify());
2147}
2148
2149TEST(DynSetTreeAliases, DynSetSplayTreeWorks)
2150{
2152 set.insert(1);
2153 set.insert(2);
2154 EXPECT_EQ(set.size(), 2u);
2155 EXPECT_TRUE(set.verify());
2156}
2157
2158TEST(DynSetTreeAliases, DynSetRandTreeWorks)
2159{
2161 set.insert(1);
2162 set.insert(2);
2163 EXPECT_EQ(set.size(), 2u);
2164 EXPECT_TRUE(set.verify());
2165}
2166
2167TEST(DynSetTreeAliases, DynSetTreapWorks)
2168{
2169 DynSetTreap<int> set;
2170 set.insert(1);
2171 set.insert(2);
2172 EXPECT_EQ(set.size(), 2u);
2173 EXPECT_TRUE(set.verify());
2174}
2175
2176TEST(DynSetTreeAliases, DynSetTreapRkWorks)
2177{
2179 set.insert(1);
2180 set.insert(2);
2181 EXPECT_EQ(set.size(), 2u);
2182 EXPECT_TRUE(set.verify());
2183}
2184
2185TEST(DynSetTreeAliases, DynSetRbTreeWorks)
2186{
2188 set.insert(1);
2189 set.insert(2);
2190 EXPECT_EQ(set.size(), 2u);
2191 EXPECT_TRUE(set.verify());
2192}
2193
2194// ============================================================================
2195// Stress and Fuzz Tests
2196// ============================================================================
2197
2198TYPED_TEST(DynSetTreeTypedTest, Stress_AscendingInsertion)
2199{
2200 const int N = 5000;
2201 for (int k = 0; k < N; ++k)
2202 this->set.insert(k);
2203
2204 EXPECT_EQ(this->set.size(), static_cast<size_t>(N));
2205 EXPECT_TRUE(this->set.verify());
2206 EXPECT_EQ(this->set.min(), 0);
2207 EXPECT_EQ(this->set.max(), N - 1);
2208}
2209
2210TYPED_TEST(DynSetTreeTypedTest, Stress_DescendingInsertion)
2211{
2212 const int N = 5000;
2213 for (int k = N - 1; k >= 0; --k)
2214 this->set.insert(k);
2215
2216 EXPECT_EQ(this->set.size(), static_cast<size_t>(N));
2217 EXPECT_TRUE(this->set.verify());
2218}
2219
2220TYPED_TEST(DynSetTreeTypedTest, Stress_BulkInsertBulkRemove)
2221{
2222 const int N = 3000;
2223
2224 for (int k = 0; k < N; ++k)
2225 this->set.insert(k);
2226
2227 EXPECT_EQ(this->set.size(), static_cast<size_t>(N));
2228
2229 // Remove all
2230 for (int k = 0; k < N; ++k)
2231 this->set.remove(k);
2232
2233 EXPECT_TRUE(this->set.is_empty());
2234}
2235
2236TYPED_TEST(DynSetTreeTypedTest, Fuzz_RandomOperations)
2237{
2238 std::set<int> oracle;
2239 std::mt19937 gen(12345);
2240 std::uniform_int_distribution<> key_dist(0, 1000);
2241 std::uniform_int_distribution<> op_dist(0, 2);
2242
2243 for (int iter = 0; iter < 5000; ++iter)
2244 {
2245 int key = key_dist(gen);
2246 int op = op_dist(gen);
2247
2248 if (op == 0) // insert
2249 {
2250 if (this->set.insert(key) != nullptr)
2251 oracle.insert(key);
2252 }
2253 else if (op == 1 && !oracle.empty()) // remove
2254 {
2255 auto it = oracle.begin();
2256 std::advance(it, gen() % oracle.size());
2257 int k = *it;
2258
2259 this->set.remove(k);
2260 oracle.erase(k);
2261 }
2262 else // search
2263 {
2264 bool in_set = this->set.contains(key);
2265 bool in_oracle = oracle.count(key) > 0;
2266 EXPECT_EQ(in_set, in_oracle);
2267 }
2268
2269 EXPECT_EQ(this->set.size(), oracle.size());
2270 }
2271
2272 EXPECT_TRUE(this->set.verify());
2273}
2274
2275TYPED_TEST(DynSetTreeTypedTest, Stress_AlternatingInsertRemove)
2276{
2277 std::set<int> oracle;
2278 std::mt19937 gen(54321);
2279 std::uniform_int_distribution<> key_dist(0, 500);
2280
2281 for (int iter = 0; iter < 3000; ++iter)
2282 {
2283 int key = key_dist(gen);
2284
2285 if (iter % 2 == 0) // insert
2286 {
2287 if (this->set.insert(key) != nullptr)
2288 oracle.insert(key);
2289 }
2290 else if (!oracle.empty()) // remove random existing
2291 {
2292 auto it = oracle.begin();
2293 std::advance(it, gen() % oracle.size());
2294 int k = *it;
2295
2296 this->set.remove(k);
2297 oracle.erase(k);
2298 }
2299
2300 EXPECT_EQ(this->set.size(), oracle.size());
2301 }
2302
2303 EXPECT_TRUE(this->set.verify());
2304}
2305
2306TEST(DynSetTreeStress, AllTypesLargeScale)
2307{
2308 // Test each tree type with larger data
2309 auto test_tree = [](auto & tree) {
2310 std::set<int> oracle;
2311 std::mt19937 gen(99999);
2312 std::uniform_int_distribution<> key_dist(0, 10000);
2313 std::uniform_int_distribution<> op_dist(0, 2);
2314
2315 for (int iter = 0; iter < 10000; ++iter)
2316 {
2317 int key = key_dist(gen);
2318 int op = op_dist(gen);
2319
2320 if (op == 0)
2321 {
2322 if (tree.insert(key) != nullptr)
2323 oracle.insert(key);
2324 }
2325 else if (op == 1 && !oracle.empty())
2326 {
2327 auto it = oracle.begin();
2328 std::advance(it, gen() % oracle.size());
2329 int k = *it;
2330 tree.remove(k);
2331 oracle.erase(k);
2332 }
2333 else
2334 {
2335 EXPECT_EQ(tree.contains(key), oracle.count(key) > 0);
2336 }
2337 }
2338
2339 EXPECT_EQ(tree.size(), oracle.size());
2340 EXPECT_TRUE(tree.verify());
2341 };
2342
2343 {
2344 DynSetAvlTree<int> tree;
2345 test_tree(tree);
2346 }
2347 {
2348 DynSetRbTree<int> tree;
2349 test_tree(tree);
2350 }
2351 {
2352 DynSetTreap<int> tree;
2353 test_tree(tree);
2354 }
2355 {
2357 test_tree(tree);
2358 }
2359}
2360
2361// ============================================================================
2362// Copies carry the comparison criteria; degenerate trees
2363// ============================================================================
2364
2365namespace
2366{
2367 // Orders ascending or descending according to its state.
2368 struct Switchable_Order
2369 {
2370 bool descending = false;
2371
2372 bool operator () (const int a, const int b) const noexcept
2373 {
2374 return descending ? b < a : a < b;
2375 }
2376 };
2377
2378 // Key that moves freely but whose copies throw on demand.
2379 struct Throwing_Copy_Key
2380 {
2381 static inline bool fail = false;
2382
2383 int value = 0;
2384
2385 Throwing_Copy_Key() = default;
2386 Throwing_Copy_Key(const int v) : value(v) {}
2387 Throwing_Copy_Key(const Throwing_Copy_Key & key) : value(key.value)
2388 {
2389 if (fail)
2390 throw std::runtime_error("copy of key");
2391 }
2392 Throwing_Copy_Key(Throwing_Copy_Key &&) noexcept = default;
2393 Throwing_Copy_Key & operator = (const Throwing_Copy_Key &) = default;
2394 Throwing_Copy_Key & operator = (Throwing_Copy_Key &&) noexcept = default;
2395
2396 bool operator < (const Throwing_Copy_Key & key) const noexcept
2397 {
2398 return value < key.value;
2399 }
2400 };
2401
2402 template <class Set>
2403 vector<int> keys_in_order(const Set & s)
2404 {
2405 vector<int> keys;
2406 for (auto it = s.get_it(); it.has_curr(); it.next_ne())
2407 keys.push_back(it.get_curr());
2408 return keys;
2409 }
2410
2411 // A set that received the keys of a descending set must order, search and
2412 // grow as that set does, whatever the criteria it had before.
2413 template <class Set>
2414 void expect_descending(Set & s, const char * how)
2415 {
2416 SCOPED_TRACE(how);
2417 EXPECT_EQ(keys_in_order(s), (vector<int>{5, 4, 3, 2, 1}));
2418 EXPECT_TRUE(s.verify());
2419 for (const int k : {1, 2, 3, 4, 5})
2420 EXPECT_TRUE(s.contains(k)) << "key " << k;
2421 EXPECT_FALSE(s.contains(0));
2422
2423 s.insert(0);
2424 s.insert(6);
2425 EXPECT_EQ(keys_in_order(s), (vector<int>{6, 5, 4, 3, 2, 1, 0}));
2426 EXPECT_TRUE(s.verify());
2427 }
2428
2429 template <template <typename, class> class Tree>
2430 void check_that_copies_carry_the_order(const char * tree_name)
2431 {
2432 SCOPED_TRACE(tree_name);
2434
2435 Set source(Switchable_Order{true});
2436 for (const int k : {3, 1, 5, 2, 4})
2437 source.insert(k);
2438 ASSERT_EQ(keys_in_order(source), (vector<int>{5, 4, 3, 2, 1}));
2439
2440 Set assigned(Switchable_Order{false});
2441 for (const int k : {10, 20})
2442 assigned.insert(k);
2443 assigned = source;
2444 expect_descending(assigned, "copy assignment");
2445
2446 Set copied(source);
2447 expect_descending(copied, "copy construction");
2448
2449 Set to_move(source);
2450 Set moved(std::move(to_move));
2451 expect_descending(moved, "move construction");
2452
2453 Set move_assigned(Switchable_Order{false});
2454 move_assigned.insert(10);
2455 move_assigned = Set(source);
2456 expect_descending(move_assigned, "move assignment");
2457
2458 Set swapped(Switchable_Order{false});
2459 swapped.insert(10);
2460 Set other(source);
2461 swapped.swap(other);
2462 expect_descending(swapped, "swap");
2463 EXPECT_EQ(keys_in_order(other), (vector<int>{10}));
2464
2465 // the source did not change
2466 EXPECT_EQ(keys_in_order(source), (vector<int>{5, 4, 3, 2, 1}));
2467 EXPECT_TRUE(source.verify());
2468 }
2469} // namespace
2470
2471TEST(DynSetTree, CopiesAssignmentsAndSwapsCarryAStatefulComparator)
2472{
2473 check_that_copies_carry_the_order<BinTree>("BinTree");
2474 check_that_copies_carry_the_order<Avl_Tree>("Avl_Tree");
2475 check_that_copies_carry_the_order<Splay_Tree>("Splay_Tree");
2476 check_that_copies_carry_the_order<Rand_Tree>("Rand_Tree");
2477 check_that_copies_carry_the_order<Treap>("Treap");
2478 check_that_copies_carry_the_order<Treap_Rk>("Treap_Rk");
2479 check_that_copies_carry_the_order<Rb_Tree>("Rb_Tree");
2480}
2481
2482TEST(DynSetTree, CopyAssignmentThatThrowsLeavesTheTargetUntouched)
2483{
2484 DynSetTree<Throwing_Copy_Key> source, target;
2485 for (const int k : {1, 2, 3, 4, 5})
2486 source.insert(Throwing_Copy_Key(k));
2487 for (const int k : {10, 20})
2488 target.insert(Throwing_Copy_Key(k));
2489
2490 Throwing_Copy_Key::fail = true;
2491 EXPECT_THROW(target = source, std::runtime_error);
2492 Throwing_Copy_Key::fail = false;
2493
2494 EXPECT_EQ(target.size(), 2u);
2495 EXPECT_TRUE(target.contains(Throwing_Copy_Key(10)));
2496 EXPECT_TRUE(target.contains(Throwing_Copy_Key(20)));
2497 EXPECT_TRUE(target.verify());
2498
2499 target = source;
2500 EXPECT_EQ(target.size(), 5u);
2501 EXPECT_TRUE(target.contains(Throwing_Copy_Key(3)));
2502}
2503
2504// The copy allocates with new. A target that kept its arena would later
2505// release those nodes as if they belonged to the arena.
2506TEST(DynSetTree, CopyAssignmentToASetWithArenaLeavesItWithoutArena)
2507{
2508 DynSetTree<int> target(static_cast<size_t>(1) << 16);
2509 for (const int k : {1, 2, 3})
2510 target.insert(k);
2511 ASSERT_TRUE(target.uses_arena());
2512
2513 DynSetTree<int> source;
2514 for (const int k : {10, 20, 30, 40})
2515 source.insert(k);
2516
2517 target = source;
2518
2519 EXPECT_FALSE(target.uses_arena());
2520 EXPECT_EQ(keys_in_order(target), (vector<int>{10, 20, 30, 40}));
2521 target.remove(10);
2522 target.insert(50);
2523 EXPECT_EQ(keys_in_order(target), (vector<int>{20, 30, 40, 50}));
2524 EXPECT_TRUE(target.verify());
2525}
2526
2527namespace
2528{
2529 // A tree of the user that can be copied, and whose copy shares the nodes
2530 // of the other tree, as a copy member by member of a tree that does not
2531 // own its nodes does. It has no move of its own: std::move() of it
2532 // copies it
2533 template <typename Key, class Compare>
2534 struct Shared_Copy_Tree : public BinTree<Key, Compare>
2535 {
2536 using Base = BinTree<Key, Compare>;
2537 using Base::Base;
2538
2539 Shared_Copy_Tree() = default;
2540
2541 Shared_Copy_Tree(const Shared_Copy_Tree & other)
2542 : Base(const_cast<Shared_Copy_Tree &>(other).key_comp())
2543 {
2544 this->getRoot() = const_cast<Shared_Copy_Tree &>(other).getRoot();
2545 }
2546 };
2547} // namespace
2548
2549// The move of a set takes the tree of the other set with the move of the
2550// tree, when the tree cannot be copied, as those of the library. On a tree
2551// that can be copied, whose "move" may be such a copy, it builds an empty
2552// tree and swaps the two sets, as it did before 7.9.0: the nodes go to one
2553// set alone, which frees each of them once
2554TEST(DynSetTree, AMoveOnATreeThatCanBeCopiedLeavesTheNodesInOneSet)
2555{
2557 static_assert(std::is_copy_constructible_v<Shared_Copy_Tree<int, Aleph::less<int>>>);
2558
2559 Set source;
2560 for (const int k : {5, 3, 8, 1, 4})
2561 source.insert(k);
2562 Set target(std::move(source));
2563 EXPECT_EQ(target.size(), 5u);
2564 for (const int k : {1, 3, 4, 5, 8})
2565 EXPECT_TRUE(target.contains(k)) << k;
2566 EXPECT_TRUE(source.is_empty());
2567 EXPECT_EQ(source.search(5), nullptr) << "the two sets hold the same nodes";
2568 EXPECT_TRUE(target.verify());
2569
2570 // each set takes keys of its own
2571 source.insert(2);
2572 target.insert(9);
2573 EXPECT_EQ(source.size(), 1u);
2574 EXPECT_TRUE(source.contains(2));
2575 EXPECT_FALSE(source.contains(9));
2576 EXPECT_EQ(target.size(), 6u);
2577 EXPECT_TRUE(target.contains(9));
2578 EXPECT_FALSE(target.contains(2));
2579 EXPECT_TRUE(source.verify());
2580 EXPECT_TRUE(target.verify());
2581}
2582
2583// A move takes the arena of the other set with the nodes that it holds:
2584// the set that took them goes on allocating in it, and the one that was
2585// moved from has none, and allocates with new. A set that took the nodes
2586// and not the arena would release them with delete
2587TEST(DynSetTree, AMoveTakesTheArenaWithItsNodes)
2588{
2589 DynSetTree<int> source(static_cast<size_t>(1) << 16);
2590 for (const int k : {1, 2, 3})
2591 source.insert(k);
2592 ASSERT_TRUE(source.uses_arena());
2593
2594 DynSetTree<int> target(std::move(source));
2595 EXPECT_TRUE(target.uses_arena());
2596 EXPECT_FALSE(source.uses_arena());
2597 EXPECT_EQ(target.size(), 3u);
2598 EXPECT_TRUE(source.is_empty());
2599
2600 target.remove(2); // released to the arena
2601 target.insert(4); // and allocated in it
2602 EXPECT_EQ(target.size(), 3u);
2603 EXPECT_TRUE(target.contains(1));
2604 EXPECT_TRUE(target.contains(3));
2605 EXPECT_TRUE(target.contains(4));
2606 EXPECT_TRUE(target.verify());
2607
2608 source.insert(7); // with new
2609 source.insert(8);
2610 EXPECT_EQ(source.size(), 2u);
2611 EXPECT_TRUE(source.contains(7));
2612 EXPECT_TRUE(source.verify());
2613}
2614
2615// Keys inserted in order leave a splay tree as a chain as tall as its size.
2616// Destroying, copying or measuring it by a recursion as deep as the tree
2617// exhausted the call stack.
2618TEST(DynSetTree, DegenerateSplayTreeIsCopiedMeasuredAndDestroyed)
2619{
2620 constexpr long n = 1'000'000;
2622 for (long i = 0; i < n; ++i)
2623 set.insert(i);
2624 ASSERT_EQ(set.size(), static_cast<size_t>(n));
2625 EXPECT_EQ(set.height(), static_cast<size_t>(n));
2626
2627 DynSetTree<long, Splay_Tree> copied(set);
2628 EXPECT_EQ(copied.size(), static_cast<size_t>(n));
2629 EXPECT_EQ(copied.height(), static_cast<size_t>(n));
2630
2632 assigned.insert(-1);
2633 assigned = set;
2634 EXPECT_EQ(assigned.size(), static_cast<size_t>(n));
2635 EXPECT_EQ(assigned.min(), 0);
2636 EXPECT_EQ(assigned.max(), n - 1);
2637
2638 assigned.empty();
2639 EXPECT_TRUE(assigned.is_empty());
2640} // the three sets are destroyed here
2641
2642// ---------------------------------------------------------------------------
2643// A set with an arena reuses the memory of the nodes that it releases
2644// ---------------------------------------------------------------------------
2645
2646namespace
2647{
2648
2649template <class Set>
2650class DynSetTreeArena : public ::testing::Test {};
2651
2652using ArenaTreeTypes = ::testing::Types<
2656
2657TYPED_TEST_SUITE(DynSetTreeArena, ArenaTreeTypes);
2658
2659constexpr size_t Arena_Bytes = 4096;
2660constexpr int Arena_Rounds = 2000;
2661
2662TYPED_TEST(DynSetTreeArena, EmptyReleasesTheWholeArena)
2663{
2664 TypeParam set(Arena_Bytes);
2665 for (int round = 0; round < Arena_Rounds; ++round)
2666 {
2667 for (int i = 0; i < 10; ++i)
2668 ASSERT_NO_THROW(set.insert(i)) << "round " << round;
2669 if (round % 2 == 0)
2670 set.empty();
2671 else
2672 set.clear();
2673 ASSERT_EQ(set.size(), 0u);
2674 ASSERT_EQ(set.arena_allocated_size(), 0u);
2675 }
2676}
2677
2678// The node that leaves is the oldest one, never the last one allocated
2679TYPED_TEST(DynSetTreeArena, RemovalsInTheOrderOfInsertionDoNotExhaustTheArena)
2680{
2681 TypeParam set(Arena_Bytes);
2682 for (int i = 0; i < 8; ++i)
2683 set.insert(i);
2684 for (int i = 8; i < Arena_Rounds; ++i)
2685 {
2686 ASSERT_NO_THROW(set.insert(i)) << "insertion " << i;
2687 set.remove(i - 8);
2688 ASSERT_EQ(set.size(), 8u);
2689 ASSERT_EQ(set.min(), i - 7);
2690 ASSERT_EQ(set.max(), i);
2691 }
2692 EXPECT_TRUE(set.verify());
2693}
2694
2695// Every presence or absence of 8 keys is walked in the order of a Gray
2696// code, which inserts and removes in mixed orders
2697TYPED_TEST(DynSetTreeArena, InsertionsAndRemovalsInMixedOrders)
2698{
2699 TypeParam set(Arena_Bytes);
2700 unsigned present = 0;
2701 for (int step = 1; step < 4096; ++step)
2702 {
2703 const int key = std::countr_zero(static_cast<unsigned>(step)) % 8;
2704 if (present & (1u << key))
2705 set.remove(key);
2706 else
2707 ASSERT_NE(set.insert(key), nullptr) << "step " << step;
2708 present ^= 1u << key;
2709 for (int k = 0; k < 8; ++k)
2710 ASSERT_EQ(set.contains(k), (present >> k & 1u) != 0) << "step " << step;
2711 }
2712}
2713
2714TYPED_TEST(DynSetTreeArena, AFullArenaTakesAsManyKeysAfterRemovingThem)
2715{
2716 TypeParam set(Arena_Bytes);
2717 const auto fill = [&set]
2718 {
2719 int count = 0;
2720 try
2721 {
2722 while (true)
2723 {
2724 set.insert(count);
2725 ++count;
2726 }
2727 }
2728 catch (const std::bad_alloc &) {}
2729 return count;
2730 };
2731
2732 const int capacity = fill();
2733 ASSERT_GT(capacity, 1);
2734 for (int turn = 0; turn < 3; ++turn)
2735 {
2736 for (int i = 0; i < capacity; ++i)
2737 set.remove(i);
2738 ASSERT_EQ(set.size(), 0u);
2739 ASSERT_EQ(fill(), capacity) << "turn " << turn;
2740 }
2741 set.empty();
2742 EXPECT_EQ(fill(), capacity);
2743}
2744
2745TYPED_TEST(DynSetTreeArena, ARepeatedKeyTakesNoRoom)
2746{
2747 TypeParam set(Arena_Bytes);
2748 set.insert(1);
2749 set.insert(2);
2750 for (int i = 0; i < Arena_Rounds; ++i)
2751 ASSERT_NO_THROW(set.insert(1)) << "attempt " << i;
2752 EXPECT_EQ(set.size(), 2u);
2753}
2754
2755// A key that owns memory, and whose copy throws when told to
2756struct Arena_Key
2757{
2758 static inline bool fail = false;
2759 std::string text;
2760
2761 Arena_Key() = default;
2762 Arena_Key(const int i) : text(std::string(40, 'k') + std::to_string(1000 + i)) {}
2763 Arena_Key(const Arena_Key & other) : text(other.text)
2764 {
2765 if (fail)
2766 throw std::runtime_error("copy");
2767 }
2768 Arena_Key(Arena_Key &&) = default;
2769 Arena_Key & operator = (const Arena_Key &) = default;
2770 Arena_Key & operator = (Arena_Key &&) = default;
2771 bool operator < (const Arena_Key & other) const { return text < other.text; }
2772};
2773
2774TEST(DynSetTreeArenaKeys, AKeyWhoseCopyThrowsTakesNoRoom)
2775{
2776 DynSetTree<Arena_Key> set(Arena_Bytes);
2777 const Arena_Key first(1), second(2);
2778 set.insert(first);
2779
2780 Arena_Key::fail = true;
2781 for (int i = 0; i < Arena_Rounds; ++i)
2782 ASSERT_THROW(set.insert(second), std::runtime_error) << "attempt " << i;
2783 Arena_Key::fail = false;
2784
2785 ASSERT_NO_THROW(set.insert(second));
2786 EXPECT_EQ(set.size(), 2u);
2787}
2788
2789TEST(DynSetTreeArenaKeys, KeysThatOwnMemoryAreReleasedAndTheirRoomReused)
2790{
2791 DynSetTree<Arena_Key> set(Arena_Bytes);
2792 for (int i = 0; i < 500; ++i)
2793 {
2794 ASSERT_NO_THROW(set.insert(Arena_Key(i)));
2795 if (i >= 4)
2796 set.remove(Arena_Key(i - 4));
2797 if (i % 50 == 49)
2798 {
2799 set.empty();
2800 ASSERT_EQ(set.arena_allocated_size(), 0u);
2801 }
2802 }
2803}
2804
2805TEST(DynSetTreeArenaKeys, AnExternalBufferIsReused)
2806{
2807 alignas(std::max_align_t) static char buffer[Arena_Bytes];
2808 DynSetTree<int> set(buffer + 1, Arena_Bytes - 1); // not aligned
2809 for (int i = 0; i < Arena_Rounds; ++i)
2810 {
2811 ASSERT_NO_THROW(set.insert(i));
2812 if (i >= 8)
2813 set.remove(i - 8);
2814 }
2815 set.empty();
2816 EXPECT_EQ(set.arena_allocated_size(), 0u);
2817}
2818
2819// After a set has given its nodes to another one, emptying it and inserting
2820// again must not write over them. The sets that receive them have an arena
2821// too, and are destroyed before the set whose arena holds the nodes
2822template <class Set, class Transfer>
2823void check_given_nodes_survive(Transfer && transfer)
2824{
2825 constexpr size_t bytes = 1 << 16;
2826 Set lender(bytes), first(bytes), second(bytes);
2827 for (int i = 0; i < 10; ++i)
2828 lender.insert(i);
2829
2830 transfer(lender, first, second);
2831 ASSERT_EQ(lender.size(), 0u);
2832 ASSERT_EQ(first.size() + second.size(), 10u);
2833
2834 const auto reuse = [&lender]
2835 {
2836 for (int turn = 0; turn < 3; ++turn)
2837 {
2838 lender.empty();
2839 for (int i = 100; i < 120; ++i)
2840 lender.insert(i);
2841 for (int i = 100; i < 120; ++i)
2842 lender.remove(i);
2843 for (int i = 200; i < 220; ++i)
2844 lender.insert(i);
2845 }
2846 };
2847 const auto check = [&first, &second]
2848 {
2849 for (int i = 0; i < 10; ++i)
2850 ASSERT_TRUE(first.contains(i) or second.contains(i)) << "key " << i;
2851 ASSERT_TRUE(first.verify());
2852 ASSERT_TRUE(second.verify());
2853 };
2854
2855 reuse();
2856 check();
2857
2858 // A receiver that removes a node that is not its own must not reuse it
2859 for (Set * receiver : {&first, &second})
2860 if (not receiver->is_empty())
2861 {
2862 const int key = receiver->min();
2863 receiver->remove(key);
2864 receiver->insert(1000);
2865 lender.insert(300);
2866 EXPECT_TRUE(receiver->contains(1000));
2867 receiver->remove(1000);
2868 receiver->insert(key);
2869 lender.remove(300);
2870 }
2871 reuse();
2872 check();
2873}
2874
2875// A receiver that has removed every node that it received no longer needs
2876// the set that gave them: its new nodes must be in its own arena
2877TEST(DynSetTreeArenaSplitPos, AReceiverDoesNotBuildInTheArenaOfAnother)
2878{
2879 using Set = DynSetTree<int, Treap_Rk>;
2880 constexpr size_t bytes = 1 << 16;
2881 Set first(bytes), second(bytes);
2882 {
2883 Set lender(bytes);
2884 for (int i = 0; i < 10; ++i)
2885 lender.insert(i);
2886 lender.split_pos(4, first, second);
2887 for (int i = 0; i < 10; ++i)
2888 (i < 5 ? first : second).remove(i);
2889 for (int i = 0; i < 20; ++i)
2890 first.insert(100 + i);
2891 } // the lender and its arena are destroyed here
2892
2893 for (int i = 0; i < 20; ++i)
2894 EXPECT_TRUE(first.contains(100 + i));
2895 EXPECT_TRUE(first.verify());
2896}
2897
2898// join() and split_key() exist only on the randomized trees
2899template <class Set>
2900class DynSetTreeArenaJoins : public ::testing::Test {};
2901
2902using JoinableArenaTreeTypes = ::testing::Types<
2905
2906TYPED_TEST_SUITE(DynSetTreeArenaJoins, JoinableArenaTreeTypes);
2907
2908TYPED_TEST(DynSetTreeArenaJoins, GivenNodesSurviveAJoin)
2909{
2910 check_given_nodes_survive<TypeParam>(
2911 [] (auto & lender, auto & first, auto & second)
2912 { first.join(lender, second); });
2913}
2914
2915// In a join(t, dup) of two sets with keys in common, the node of such a
2916// key that goes to dup may be the one of this, as Treap, Treap_Rk and
2917// Rand_Tree do: emptying this and filling it again must leave it in dup.
2918// The three sets are destroyed in the reverse order of their declaration,
2919// dup first
2920TYPED_TEST(DynSetTreeArenaJoins, TheDuplicatesSurviveWhenTheJoinedSetIsEmptied)
2921{
2922 constexpr size_t bytes = 1 << 16;
2923 TypeParam joined(bytes), other(bytes), dup(bytes);
2924 for (int i = 0; i < 10; ++i)
2925 joined.insert(i);
2926 for (int i = 5; i < 15; ++i)
2927 other.insert(i);
2928 joined.join(other, dup);
2929 ASSERT_EQ(joined.size(), 15u);
2930 ASSERT_EQ(dup.size(), 5u);
2931
2932 for (int turn = 0; turn < 3; ++turn)
2933 {
2934 joined.empty();
2935 for (int i = 100; i < 140; ++i)
2936 joined.insert(i);
2937 }
2938
2939 size_t count = 0;
2940 dup.for_each([&count] (const int & key)
2941 {
2942 ++count;
2943 EXPECT_TRUE(key >= 5 and key < 10) << "key " << key;
2944 });
2945 EXPECT_EQ(count, 5u);
2946 for (int i = 5; i < 10; ++i)
2947 EXPECT_TRUE(dup.contains(i)) << "key " << i;
2948 EXPECT_TRUE(dup.verify());
2949}
2950
2951// A join without keys in common pins only the arena of t: emptying this
2952// gives its whole arena back
2953TYPED_TEST(DynSetTreeArenaJoins, AJoinWithoutDuplicatesLeavesTheArenaOfThisFree)
2954{
2955 constexpr size_t bytes = 1 << 16;
2956 TypeParam joined(bytes), other(bytes), dup(bytes);
2957 for (int i = 0; i < 10; ++i)
2958 joined.insert(i);
2959 for (int i = 10; i < 20; ++i)
2960 other.insert(i);
2961 joined.join(other, dup);
2962 ASSERT_EQ(joined.size(), 20u);
2963 ASSERT_EQ(dup.size(), 0u);
2964
2965 joined.empty();
2966 EXPECT_EQ(joined.arena_allocated_size(), 0u);
2967}
2968
2969TYPED_TEST(DynSetTreeArenaJoins, GivenNodesSurviveAJoinWithRepetitions)
2970{
2971 check_given_nodes_survive<TypeParam>(
2972 [] (auto & lender, auto & first, auto &) { first.join_dup(lender); });
2973}
2974
2975TYPED_TEST(DynSetTreeArenaJoins, GivenNodesSurviveASplitByKey)
2976{
2977 check_given_nodes_survive<TypeParam>(
2978 [] (auto & lender, auto & l, auto & r)
2979 {
2980 lender.remove(5);
2981 ASSERT_TRUE(lender.split_key(5, l, r));
2982 r.insert(5);
2983 });
2984}
2985
2986TYPED_TEST(DynSetTreeArenaJoins, GivenNodesSurviveASplitByKeyWithRepetitions)
2987{
2988 check_given_nodes_survive<TypeParam>(
2989 [] (auto & lender, auto & l, auto & r)
2990 { lender.split_key_dup(5, l, r); });
2991}
2992
2993// split_pos() needs the ranks of the nodes
2994TEST(DynSetTreeArenaSplitPos, GivenNodesSurviveASplitByPosition)
2995{
2996 const auto split = [] (auto & lender, auto & l, auto & r)
2997 { lender.split_pos(4, l, r); };
2998 check_given_nodes_survive<DynSetTree<int, Treap_Rk>>(split);
2999 check_given_nodes_survive<DynSetTree<int, Rand_Tree>>(split);
3000}
3001
3002} // namespace
3003
3004int main(int argc, char **argv)
3005{
3006 ::testing::InitGoogleTest(&argc, argv);
3007 return RUN_ALL_TESTS();
3008}
bool operator<(const Time &l, const Time &r)
Definition ah-time.H:143
WeightedDigraph::Node Node
int main()
long double h
Definition btreepic.C:154
size_t size_t int32_t value
Definition ca-c-api.h:116
Node for binary search tree.
static BinNode *const NullPtr
Dynamic set implemented using AVL binary search trees of type Avl_Tree<Key>.
Dynamic set implemented using binary search trees of type BinTree<Key>.
Dynamic set implemented using randomized binary search trees of type Rand_Tree<Key>.
Dynamic set implemented using Red-Black binary search trees of type Rb_Tree<Key> (bottom-up implement...
Dynamic set implemented using splay binary search trees of type Splay_Tree<Key>.
Dynamic set implemented using extended treap binary search trees with rank support of type Treap_Rk<K...
Dynamic set implemented using randomized treap binary search trees of type Treap<Key>.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key & access(size_t i)
const Key & get_first() const
See min.
size_t height() const
Calculates and returns the height of the binary search tree.
const Key & min() const
Returns the smallest key contained in the set according to the criterion comparison given.
long position(const Key &key) const
Returns the infix (ordered) position of the key.
Key * append(const Key &key)
Key & find(const Key &key)
Returns a modifiable reference to an element within the set.
const Key & get_last() const
See max.
bool split_key(const Key &key, DynSetTree &l, DynSetTree &r)
Partitions the binary search tree based on a key.
const Key & get_item() const
Returns any element of the set.
const size_t & size() const
Returns the cardinality of the set.
Key remove_pos(const size_t i)
Removes the key at the inorder position i.
Key del(const Key &key)
Deletes key and returns a full copy of stored key.
void clear()
Empties the container.
std::pair< long, Key * > find_position(const Key &key) const
Returns the infix (ordinate) position of the key key or whatever It would be your position of belongi...
Key * insert(const Key &key)
Inserts a key into the dynamic set.
const Key & max() const
Returns the largest key contained in the set according to the criteria comparison given.
bool exist(const Key &key) const
Returns true if key belongs to the dynamic set.
void split_key_dup(const Key &key, DynSetTree &l, DynSetTree &r)
Partitions the binary search tree based on a key that may be present in the tree.
bool has(const Key &key) const
void swap(DynSetTree &dset) noexcept(noexcept(tree.swap(dset.tree)) and noexcept(std::swap(num_nodes, dset.num_nodes)) and noexcept(std::swap(arena_allocator, dset.arena_allocator)))
Exchange all elements of this set with dset in constant time (and extremely fast).
void for_each_postorder(Key_Op &key_op) const
Performs a postfix traversal over all keys in the set and invokes operation Op.
Key * put(const Key &key)
size_t internal_path_length() const
Calculates and returns the length of the internal path of the tree search binary.
size_t remove(const Key &key)
Removes a key from the dynamic set.
bool contains(const Key &key) const
Checks if a key exists in the set.
const Key & get() const
Synonym of max.
const Key & get_root() const
Key * search(const Key &key)
Find an element in the set.
Key * insert_dup(const Key &key)
void split_pos(const size_t pos, DynSetTree &l, DynSetTree &r)
Partitions the binary search tree based on an infix position.
size_t arena_allocated_size() const noexcept
Returns the allocated size from the arena (0 if not using arena)
Key & select(size_t i)
Returns the ith node in infix position.
void empty()
Remove all the elements of the set.
bool uses_arena() const noexcept
Returns true if the set is using an arena allocator.
bool traverse(Operation &op)
Traverse all the set of pairs and for each key executes the operation op.
Key * search_or_insert(const Key &key)
Look for the key in the binary search tree or inserts it if it is not found.
void for_each_preorder(Key_Op &key_op) const
Performs a prefix traversal over all keys in the set and invokes operation Op.
void for_each_inorder(Key_Op &key_op) const
Performs an infix traversal over all keys in the set and invokes operation Op.
std::pair< Key *, bool > contains_or_insert(const Key &key)
bool is_empty() const
returns true if the set is empty
Node * get_root_node() const
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Minimal std::expected-style result type for C++20.
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
#define TEST(name)
::testing::Types< DynSetBinTree< int >, DynSetAvlTree< int >, DynSetRbTree< int >, DynSetSplayTree< int >, DynSetTreap< int >, DynSetRandTree< int >, DynSetTreapRk< int > > AllTreeTypes
static void range_methods_throw_domain_error_test()
static void insert_dup_traversal_test()
TYPED_TEST(DynSetTreeTypedTest, EmptySetProperties)
Definition dynsettree.cc:75
TYPED_TEST_SUITE(DynSetTreeTypedTest, AllTreeTypes)
#define N
Definition fib.C:294
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
DynSetTree & join(DynSetTree &t, DynSetTree &dup)
Union of two binary search trees.
DynSetTree & join_dup(DynSetTree &t)
Union of two binary search trees.
void verify()
Main namespace for Aleph-w library functions.
bool completed() const noexcept
Return true if all underlying iterators are finished.
Definition ah-zip.H:183
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:123
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
void fill(Itor beg, const Itor &end, const T &value)
Fill a range with a value.
Definition ahAlgo.H:707
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
Definition ahAlgo.H:230
and
Check uniqueness with explicit hash + equality functors.
Fw_Itor remove(Fw_Itor __first, const Fw_Itor &__last, const T &__value)
Remove elements equal to a value.
Definition ahAlgo.H:962
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
Definition ahAlgo.H:309
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
std::vector< std::string > & split(const std::string &s, const char delim, std::vector< std::string > &elems)
Split a std::string by a single delimiter character.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
STL namespace.
AVL binary search tree with nodes without a virtual destructor.
Definition tpl_avl.H:831
Binary search tree with nodes without virtual destructors,.
GenBinTree< BinNode, Key, Compare > Base
void test_tree(int n)
bool check(GT &g)
int keys[]
static int * k
Gsl_Compatible_Rng r
Dynamic set implementations based on balanced binary search trees.
DynList< int > l