Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
htlist_test.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
43#include <gtest/gtest.h>
44#include <vector>
45#include <string>
46#include <algorithm>
47#include <random>
48#include <memory>
49#include <htlist.H>
50
51using namespace std;
52using namespace testing;
53using namespace Aleph;
54
55// =============================================================================
56// Slinknc Tests
57// =============================================================================
58
59class SlinkcTest : public Test
60{
61protected:
63};
64
65TEST_F(SlinkcTest, DefaultConstructor)
66{
67 EXPECT_TRUE(link.is_empty());
68 EXPECT_EQ(link.get_next(), nullptr);
69}
70
71TEST_F(SlinkcTest, CopyConstructorResetsLink)
72{
73 auto other = std::make_unique<Slinknc>();
74 link.insert(other.get());
75 EXPECT_FALSE(link.is_empty());
76
77 Slinknc copy(link);
78 EXPECT_TRUE(copy.is_empty());
79
80 link.reset();
81}
82
83TEST_F(SlinkcTest, AssignmentResetsLink)
84{
85 auto other = std::make_unique<Slinknc>();
86 Slinknc another;
87 link.insert(other.get());
88
89 another = link;
90 EXPECT_TRUE(another.is_empty());
91
92 link.reset();
93}
94
95TEST_F(SlinkcTest, InsertAndRemove)
96{
97 Slinknc n1, n2, n3;
98
99 link.insert(&n1);
100 EXPECT_FALSE(link.is_empty());
101 EXPECT_EQ(link.get_next(), &n1);
102
103 link.insert(&n2);
104 EXPECT_EQ(link.get_next(), &n2);
105 EXPECT_EQ(n2.get_next(), &n1);
106
107 Slinknc *removed = link.remove_next();
108 EXPECT_EQ(removed, &n2);
109 EXPECT_TRUE(n2.is_empty());
110 EXPECT_EQ(link.get_next(), &n1);
111
112 link.reset();
113}
114
116{
117 Slinknc other;
118 link.insert(&other);
119 EXPECT_FALSE(link.is_empty());
120
121 link.reset();
122 EXPECT_TRUE(link.is_empty());
123}
124
125// =============================================================================
126// Snodenc Tests
127// =============================================================================
128
129TEST(SnodencTest, DefaultConstructor)
130{
131 Snodenc<int> node;
132 EXPECT_TRUE(node.is_empty());
133}
134
135TEST(SnodencTest, CopyConstructor)
136{
137 Snodenc<int> node(42);
138 EXPECT_EQ(node.get_data(), 42);
139}
140
141TEST(SnodencTest, MoveConstructor)
142{
143 string str = "Hello World";
144 Snodenc<string> node(std::move(str));
145 EXPECT_EQ(node.get_data(), "Hello World");
146}
147
148TEST(SnodencTest, GetDataModification)
149{
150 Snodenc<int> node(10);
151 node.get_data() = 20;
152 EXPECT_EQ(node.get_data(), 20);
153}
154
155TEST(SnodencTest, ChainOfNodes)
156{
157 Snodenc<int> n1(1), n2(2), n3(3);
158
159 n1.insert(&n2);
160 n2.insert(&n3);
161
162 EXPECT_EQ(n1.get_next()->get_data(), 2);
163 EXPECT_EQ(n1.get_next()->get_next()->get_data(), 3);
164}
165
166TEST(SnodencTest, ToSnodencConversion)
167{
168 Snodenc<int> node(42);
169 Slinknc *link = &node;
170
171 Snodenc<int> *converted = link->to_snodenc<int>();
172 EXPECT_EQ(converted, &node);
173 EXPECT_EQ(converted->get_data(), 42);
174}
175
176TEST(SnodencTest, ToDataConversion)
177{
178 Snodenc<int> node(42);
179 Slinknc *link = &node;
180
181 EXPECT_EQ(link->to_data<int>(), 42);
182
183 link->to_data<int>() = 100;
184 EXPECT_EQ(node.get_data(), 100);
185}
186
187// =============================================================================
188// HTList Basic Tests
189// =============================================================================
190
191class HTListBasicTest : public Test
192{
193protected:
195};
196
197TEST_F(HTListBasicTest, DefaultConstructor)
198{
199 EXPECT_TRUE(list.is_empty());
200 EXPECT_FALSE(list.is_unitarian());
201 EXPECT_TRUE(list.is_unitarian_or_empty());
202 EXPECT_EQ(list.get_head(), nullptr);
203 EXPECT_EQ(list.get_tail(), nullptr);
204}
205
206TEST_F(HTListBasicTest, InsertSingleElement)
207{
208 auto *node = new Snodenc<int>(1);
209 list.insert(node);
210
211 EXPECT_FALSE(list.is_empty());
212 EXPECT_TRUE(list.is_unitarian());
213 EXPECT_EQ(list.get_first(), node);
214 EXPECT_EQ(list.get_last(), node);
215
216 delete list.remove_first();
217}
218
219TEST_F(HTListBasicTest, AppendSingleElement)
220{
221 auto *node = new Snodenc<int>(1);
222 list.append(node);
223
224 EXPECT_FALSE(list.is_empty());
225 EXPECT_TRUE(list.is_unitarian());
226 EXPECT_EQ(list.get_first(), node);
227 EXPECT_EQ(list.get_last(), node);
228
229 delete list.remove_first();
230}
231
232TEST_F(HTListBasicTest, InsertMaintainsOrder)
233{
234 // Insert 3, 2, 1 -> list should be 1, 2, 3
235 list.insert(new Snodenc<int>(3));
236 list.insert(new Snodenc<int>(2));
237 list.insert(new Snodenc<int>(1));
238
239 EXPECT_EQ(list.get_first()->to_data<int>(), 1);
240 EXPECT_EQ(list.get_last()->to_data<int>(), 3);
241 EXPECT_EQ(list.size(), 3);
242
243 list.remove_all_and_delete();
244}
245
246TEST_F(HTListBasicTest, AppendMaintainsOrder)
247{
248 // Append 1, 2, 3 -> list should be 1, 2, 3
249 list.append(new Snodenc<int>(1));
250 list.append(new Snodenc<int>(2));
251 list.append(new Snodenc<int>(3));
252
253 EXPECT_EQ(list.get_first()->to_data<int>(), 1);
254 EXPECT_EQ(list.get_last()->to_data<int>(), 3);
255 EXPECT_EQ(list.size(), 3);
256
257 list.remove_all_and_delete();
258}
259
260// =============================================================================
261// HTList Insert with Link Bug Fix Test
262// =============================================================================
263
265{
266protected:
268
269 void SetUp() override
270 {
271 // Create list: 1, 2, 3, 4, 5
272 for (int i = 1; i <= 5; ++i)
273 list.append(new Snodenc<int>(i));
274 }
275
276 void TearDown() override
277 {
279 }
280};
281
282TEST_F(HTListInsertBugFixTest, InsertListAfterFirstElement)
283{
284 // This tests the bug fix: insert(link, list) should preserve elements after link
285 // list: 1, 2, 3, 4, 5
286 // Insert {10, 11, 12} after element 1
287 // Expected: 1, 10, 11, 12, 2, 3, 4, 5
288
289 HTList toInsert;
290 toInsert.append(new Snodenc<int>(10));
291 toInsert.append(new Snodenc<int>(11));
292 toInsert.append(new Snodenc<int>(12));
293
294 Slinknc *first = list.get_first();
295 list.insert(first, toInsert);
296
297 EXPECT_TRUE(toInsert.is_empty());
298 EXPECT_EQ(list.size(), 8);
299
300 // Verify order: 1, 10, 11, 12, 2, 3, 4, 5
301 vector<int> expected = {1, 10, 11, 12, 2, 3, 4, 5};
302 HTList::Iterator it(list);
303 for (int val : expected)
304 {
305 ASSERT_TRUE(it.has_curr()) << "List ended prematurely";
306 EXPECT_EQ(it.get_curr()->to_data<int>(), val);
307 it.next();
308 }
309 EXPECT_FALSE(it.has_curr());
310}
311
312TEST_F(HTListInsertBugFixTest, InsertListInMiddle)
313{
314 // list: 1, 2, 3, 4, 5
315 // Insert {20, 21} after element 3
316 // Expected: 1, 2, 3, 20, 21, 4, 5
317
318 HTList toInsert;
319 toInsert.append(new Snodenc<int>(20));
320 toInsert.append(new Snodenc<int>(21));
321
322 // Find element 3
323 HTList::Iterator it(list);
324 while (it.get_curr()->to_data<int>() != 3)
325 it.next();
326
327 Slinknc *link = it.get_curr();
328 list.insert(link, toInsert);
329
330 EXPECT_TRUE(toInsert.is_empty());
331 EXPECT_EQ(list.size(), 7);
332
333 // Verify order
334 vector<int> expected = {1, 2, 3, 20, 21, 4, 5};
335 it.reset();
336 for (int val : expected)
337 {
338 ASSERT_TRUE(it.has_curr());
339 EXPECT_EQ(it.get_curr()->to_data<int>(), val);
340 it.next();
341 }
342}
343
345{
346 // list: 1, 2, 3, 4, 5
347 // Insert {30, 31} after element 5 (tail)
348 // Expected: 1, 2, 3, 4, 5, 30, 31
349
350 HTList toInsert;
351 toInsert.append(new Snodenc<int>(30));
352 toInsert.append(new Snodenc<int>(31));
353
354 Slinknc *last = list.get_last();
355 list.insert(last, toInsert);
356
357 EXPECT_TRUE(toInsert.is_empty());
358 EXPECT_EQ(list.size(), 7);
359 EXPECT_EQ(list.get_last()->to_data<int>(), 31);
360
361 // Verify order
362 vector<int> expected = {1, 2, 3, 4, 5, 30, 31};
363 HTList::Iterator it(list);
364 for (int val : expected)
365 {
366 ASSERT_TRUE(it.has_curr());
367 EXPECT_EQ(it.get_curr()->to_data<int>(), val);
368 it.next();
369 }
370}
371
373{
374 HTList emptyList;
375
376 Slinknc *first = list.get_first();
377 list.insert(first, emptyList);
378
379 // List should be unchanged
380 EXPECT_EQ(list.size(), 5);
381
382 HTList::Iterator it(list);
383 for (int i = 1; i <= 5; ++i, it.next())
384 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
385}
386
387// =============================================================================
388// HTList Stack Operations Tests
389// =============================================================================
390
391TEST(HTListStack, PushPopSequence)
392{
393 HTList stack;
394 Slinknc n1, n2, n3;
395
396 stack.push(&n1);
397 stack.push(&n2);
398 stack.push(&n3);
399
400 EXPECT_EQ(stack.top(), &n3);
401 EXPECT_EQ(stack.pop(), &n3);
402 EXPECT_EQ(stack.top(), &n2);
403 EXPECT_EQ(stack.pop(), &n2);
404 EXPECT_EQ(stack.top(), &n1);
405 EXPECT_EQ(stack.pop(), &n1);
406 EXPECT_TRUE(stack.is_empty());
407}
408
409TEST(HTListStack, EmptyStackThrows)
410{
411 HTList stack;
412 EXPECT_THROW(stack.top(), std::underflow_error);
413 EXPECT_THROW(stack.pop(), std::underflow_error);
414}
415
416// =============================================================================
417// HTList Split and Concat Tests
418// =============================================================================
419
420class HTListSplitTest : public Test
421{
422protected:
424
425 void TearDown() override
426 {
428 }
429};
430
431TEST_F(HTListSplitTest, SplitEmptyList)
432{
433 HTList l, r;
434 size_t count = list.split(l, r);
435
436 EXPECT_EQ(count, 0);
437 EXPECT_TRUE(l.is_empty());
438 EXPECT_TRUE(r.is_empty());
439}
440
441TEST_F(HTListSplitTest, SplitUnitaryList)
442{
443 list.append(new Snodenc<int>(1));
444
445 HTList l, r;
446 size_t count = list.split(l, r);
447
448 EXPECT_EQ(count, 1);
449 EXPECT_TRUE(list.is_empty());
450 EXPECT_TRUE(l.is_unitarian());
451 EXPECT_TRUE(r.is_empty());
452
454}
455
456TEST_F(HTListSplitTest, SplitEvenList)
457{
458 for (int i = 1; i <= 10; ++i)
459 list.append(new Snodenc<int>(i));
460
461 HTList l, r;
462 const size_t count = list.split(l, r);
463
464 // split() gave one less than the number of items
465 EXPECT_EQ(count, 10u);
466 EXPECT_TRUE(list.is_empty());
467 EXPECT_EQ(l.size(), 5u);
468 EXPECT_EQ(r.size(), 5u);
469
470 // Verify that concatenating restores the list
471 l.concat(r);
472 EXPECT_TRUE(r.is_empty());
473 EXPECT_EQ(l.size(), 10);
474
476}
477
479{
480 for (int i = 1; i <= 7; ++i)
481 list.append(new Snodenc<int>(i));
482
483 HTList l, r;
484 const size_t count = list.split(l, r);
485
486 EXPECT_EQ(count, 7u);
487 EXPECT_TRUE(list.is_empty());
488 EXPECT_EQ(l.size(), 4u); // the first takes the middle item
489 EXPECT_EQ(r.size(), 3u);
490
491 l.concat(r);
493}
494
495TEST_F(HTListSplitTest, SplitGivesTheNumberOfItemsAndKeepsTheirOrder)
496{
497 for (int n = 0; n <= 9; ++n)
498 {
499 SCOPED_TRACE(n);
500 for (int i = 1; i <= n; ++i)
501 list.append(new Snodenc<int>(i));
502
503 HTList l, r;
504 EXPECT_EQ(list.split(l, r), size_t(n));
505 EXPECT_TRUE(list.is_empty());
506 EXPECT_EQ(l.size(), size_t(n - n / 2));
507 EXPECT_EQ(r.size(), size_t(n / 2));
508
509 l.concat(r);
510 int expected = 1;
511 for (HTList::Iterator it(l); it.has_curr(); it.next_ne(), ++expected)
512 EXPECT_EQ(static_cast<Snodenc<int> *>(it.get_curr())->get_data(), expected);
513 EXPECT_EQ(expected, n + 1);
515 }
516}
517
518// =============================================================================
519// HTList Reverse Tests
520// =============================================================================
521
522TEST(HTListReverse, ReverseEmptyList)
523{
524 HTList list;
525 size_t count = list.reverse();
526 EXPECT_EQ(count, 0);
527 EXPECT_TRUE(list.is_empty());
528}
529
530TEST(HTListReverse, ReverseUnitaryList)
531{
532 HTList list;
533 list.append(new Snodenc<int>(1));
534
535 size_t count = list.reverse();
536 EXPECT_EQ(count, 1);
537 EXPECT_EQ(list.get_first()->to_data<int>(), 1);
538
540}
541
542TEST(HTListReverse, ReverseMultipleElements)
543{
544 HTList list;
545 for (int i = 1; i <= 5; ++i)
546 list.append(new Snodenc<int>(i));
547
548 list.reverse();
549
550 int expected = 5;
551 for (HTList::Iterator it(list); it.has_curr(); it.next(), --expected)
552 EXPECT_EQ(it.get_curr()->to_data<int>(), expected);
553
555}
556
557// =============================================================================
558// HTList Rotation Tests
559// =============================================================================
560
561TEST(HTListRotation, RotateEmptyListThrows)
562{
563 HTList list;
564 EXPECT_THROW(list.rotate_left(1), std::domain_error);
565 EXPECT_NO_THROW(list.rotate_left(0));
566}
567
568TEST(HTListRotation, RotateLeftOne)
569{
570 HTList list;
571 for (int i = 1; i <= 5; ++i)
572 list.append(new Snodenc<int>(i));
573
574 list.rotate_left(1);
575
576 // Expected: 2, 3, 4, 5, 1
577 vector<int> expected = {2, 3, 4, 5, 1};
578 HTList::Iterator it(list);
579 for (int val : expected)
580 {
581 EXPECT_EQ(it.get_curr()->to_data<int>(), val);
582 it.next();
583 }
584
586}
587
588// =============================================================================
589// DynList Tests
590// =============================================================================
591
592class DynListBasicTest : public Test
593{
594protected:
596};
597
598TEST_F(DynListBasicTest, DefaultConstructor)
599{
600 EXPECT_TRUE(list.is_empty());
601 EXPECT_EQ(list.size(), 0);
602}
603
604TEST_F(DynListBasicTest, InsertAndRemove)
605{
606 list.insert(1);
607 list.insert(2);
608 list.insert(3);
609
610 EXPECT_EQ(list.size(), 3);
611 EXPECT_EQ(list.get_first(), 3);
612 EXPECT_EQ(list.get_last(), 1);
613
614 EXPECT_EQ(list.remove_first(), 3);
615 EXPECT_EQ(list.remove_first(), 2);
616 EXPECT_EQ(list.remove_first(), 1);
617 EXPECT_TRUE(list.is_empty());
618}
619
620TEST_F(DynListBasicTest, AppendAndRemove)
621{
622 list.append(1);
623 list.append(2);
624 list.append(3);
625
626 EXPECT_EQ(list.size(), 3);
627 EXPECT_EQ(list.get_first(), 1);
628 EXPECT_EQ(list.get_last(), 3);
629}
630
632{
633 list.push(1);
634 list.push(2);
635 list.push(3);
636
637 EXPECT_EQ(list.top(), 3);
638 EXPECT_EQ(list.pop(), 3);
639 EXPECT_EQ(list.pop(), 2);
640 EXPECT_EQ(list.pop(), 1);
641 EXPECT_TRUE(list.is_empty());
642}
643
645{
646 for (int i = 0; i < 5; ++i)
647 list.append(i);
648
649 for (int i = 0; i < 5; ++i)
650 EXPECT_EQ(list[i], i);
651}
652
653TEST_F(DynListBasicTest, GetByIndexThrowsOnOverflow)
654{
655 list.append(1);
656 list.append(2);
657
658 EXPECT_NO_THROW(list[0]);
659 EXPECT_NO_THROW(list[1]);
660 EXPECT_THROW(list[2], std::overflow_error);
661}
662
663// =============================================================================
664// DynList Remove with Predicate Bug Fix Test
665// =============================================================================
666
668{
669protected:
671
672 void SetUp() override
673 {
674 for (int i = 1; i <= 10; ++i)
675 list.append(i);
676 }
677};
678
679TEST_F(DynListRemovePredicateTest, RemoveExistingElement)
680{
681 // Remove element equal to 5
682 int removed = list.remove([](const int &x)
683 { return x == 5; });
684
685 EXPECT_EQ(removed, 5);
686 EXPECT_EQ(list.size(), 9);
687
688 // Verify 5 is not in the list
689 for (auto it = list.get_it(); it.has_curr(); it.next())
690 EXPECT_NE(it.get_curr(), 5);
691}
692
694{
695 int removed = list.remove([](const int &x)
696 { return x == 1; });
697
698 EXPECT_EQ(removed, 1);
699 EXPECT_EQ(list.get_first(), 2);
700}
701
703{
704 int removed = list.remove([](const int &x)
705 { return x == 10; });
706
707 EXPECT_EQ(removed, 10);
708 EXPECT_EQ(list.get_last(), 9);
709}
710
711TEST_F(DynListRemovePredicateTest, RemoveNonExistentThrows)
712{
713 // This tests the bug fix: should throw, not infinite loop
714 EXPECT_THROW(
715 list.remove([](const int &x)
716 { return x == 100; }),
717 std::domain_error);
718}
719
720TEST_F(DynListRemovePredicateTest, RemoveNeNonExistentReturnsDefault)
721{
722 // This tests the bug fix: remove_ne should return default value, not infinite loop
723 int result = list.remove_ne([](const int &x)
724 { return x == 100; });
725
726 // Should return default-constructed int (0), not hang
727 EXPECT_EQ(result, 0);
728}
729
730TEST_F(DynListRemovePredicateTest, RemoveNeExistingElement)
731{
732 int removed = list.remove_ne([](const int &x)
733 { return x == 5; });
734
735 EXPECT_EQ(removed, 5);
736 EXPECT_EQ(list.size(), 9);
737}
738
739TEST_F(DynListRemovePredicateTest, RemoveAllMatchingElements)
740{
741 // Remove all even numbers one by one
742 for (int i = 2; i <= 10; i += 2)
743 {
744 int target = i;
745 list.remove([target](const int &x)
746 { return x == target; });
747 }
748
749 EXPECT_EQ(list.size(), 5);
750
751 // Verify only odd numbers remain
752 for (auto it = list.get_it(); it.has_curr(); it.next())
753 EXPECT_TRUE(it.get_curr() % 2 == 1);
754}
755
756// =============================================================================
757// DynList Copy and Move Semantics Tests
758// =============================================================================
759
760TEST(DynListSemantics, CopyConstructor)
761{
762 DynList<int> original;
763 for (int i = 1; i <= 5; ++i)
764 original.append(i);
765
766 DynList<int> copy(original);
767
768 EXPECT_EQ(copy.size(), original.size());
769
770 auto it1 = original.get_it();
771 auto it2 = copy.get_it();
772 while (it1.has_curr())
773 {
774 EXPECT_EQ(it1.get_curr(), it2.get_curr());
775 it1.next();
776 it2.next();
777 }
778}
779
780TEST(DynListSemantics, MoveConstructor)
781{
782 DynList<int> original;
783 for (int i = 1; i <= 5; ++i)
784 original.append(i);
785
786 DynList<int> moved(std::move(original));
787
788 EXPECT_TRUE(original.is_empty());
789 EXPECT_EQ(moved.size(), 5);
790}
791
792TEST(DynListSemantics, CopyAssignment)
793{
794 DynList<int> original;
795 for (int i = 1; i <= 5; ++i)
796 original.append(i);
797
799 copy.append(100); // Add something to ensure it gets cleared
800
801 copy = original;
802
803 EXPECT_EQ(copy.size(), original.size());
804 EXPECT_EQ(copy.get_first(), 1);
805}
806
807TEST(DynListSemantics, MoveAssignment)
808{
809 DynList<int> original;
810 for (int i = 1; i <= 5; ++i)
811 original.append(i);
812
813 DynList<int> moved;
814 moved = std::move(original);
815
816 EXPECT_TRUE(original.is_empty());
817 EXPECT_EQ(moved.size(), 5);
818}
819
820TEST(DynListSemantics, SelfAssignment)
821{
822 DynList<int> list;
823 for (int i = 1; i <= 5; ++i)
824 list.append(i);
825
826 list = list;
827
828 EXPECT_EQ(list.size(), 5);
829}
830
831// =============================================================================
832// DynList Append and Insert Lists Tests
833// =============================================================================
834
835TEST(DynListConcatenation, AppendByMove)
836{
837 DynList<int> list1, list2;
838
839 for (int i = 1; i <= 3; ++i)
840 list1.append(i);
841 for (int i = 4; i <= 6; ++i)
842 list2.append(i);
843
844 list1.append(std::move(list2));
845
846 EXPECT_TRUE(list2.is_empty());
847 EXPECT_EQ(list1.size(), 6);
848
849 int expected = 1;
850 for (auto it = list1.get_it(); it.has_curr(); it.next(), ++expected)
851 EXPECT_EQ(it.get_curr(), expected);
852}
853
854TEST(DynListConcatenation, AppendByCopy)
855{
856 DynList<int> list1, list2;
857
858 for (int i = 1; i <= 3; ++i)
859 list1.append(i);
860 for (int i = 4; i <= 6; ++i)
861 list2.append(i);
862
863 list1.append(list2);
864
865 EXPECT_FALSE(list2.is_empty()); // list2 should not be modified
866 EXPECT_EQ(list1.size(), 6);
867}
868
869TEST(DynListConcatenation, InsertByMove)
870{
871 DynList<int> list1, list2;
872
873 for (int i = 4; i <= 6; ++i)
874 list1.append(i);
875 for (int i = 1; i <= 3; ++i)
876 list2.append(i);
877
878 list1.insert(std::move(list2));
879
880 EXPECT_TRUE(list2.is_empty());
881 EXPECT_EQ(list1.size(), 6);
882 EXPECT_EQ(list1.get_first(), 1);
883}
884
885// =============================================================================
886// DynList Reverse Tests
887// =============================================================================
888
889TEST(DynListReverse, ReverseMutating)
890{
891 DynList<int> list;
892 for (int i = 1; i <= 5; ++i)
893 list.append(i);
894
895 list.reverse();
896
897 int expected = 5;
898 for (auto it = list.get_it(); it.has_curr(); it.next(), --expected)
899 EXPECT_EQ(it.get_curr(), expected);
900}
901
902TEST(DynListReverse, ReverseConst)
903{
904 DynList<int> list;
905 for (int i = 1; i <= 5; ++i)
906 list.append(i);
907
908 const DynList<int> &constRef = list;
909 DynList<int> reversed = constRef.reverse();
910
911 // Original should be unchanged
912 int expected = 1;
913 for (auto it = list.get_it(); it.has_curr(); it.next(), ++expected)
914 EXPECT_EQ(it.get_curr(), expected);
915
916 // Reversed should be in reverse order
917 expected = 5;
918 for (auto it = reversed.get_it(); it.has_curr(); it.next(), --expected)
919 EXPECT_EQ(it.get_curr(), expected);
920}
921
922// =============================================================================
923// DynList Iterator Tests
924// =============================================================================
925
927{
928protected:
930
931 void SetUp() override
932 {
933 for (int i = 1; i <= 5; ++i)
934 list.append(i);
935 }
936};
937
938TEST_F(DynListIteratorTest, ForwardTraversal)
939{
940 int expected = 1;
941 for (auto it = list.get_it(); it.has_curr(); it.next(), ++expected)
942 EXPECT_EQ(it.get_curr(), expected);
943}
944
946{
947 DynList<int>::Iterator it(list);
948
949 // Delete first element
950 int deleted = it.del();
951 EXPECT_EQ(deleted, 1);
952 EXPECT_EQ(list.size(), 4);
953 EXPECT_EQ(list.get_first(), 2);
954}
955
956TEST_F(DynListIteratorTest, IteratorDeleteMiddle)
957{
958 DynList<int>::Iterator it(list);
959 it.next();
960 it.next(); // Now at element 3
961
962 int deleted = it.del();
963 EXPECT_EQ(deleted, 3);
964 EXPECT_EQ(list.size(), 4);
965}
966
967TEST_F(DynListIteratorTest, EmptyListIterator)
968{
969 DynList<int> empty;
970 DynList<int>::Iterator it(empty);
971
972 EXPECT_FALSE(it.has_curr());
973 EXPECT_THROW(it.get_curr(), std::overflow_error);
974}
975
976// =============================================================================
977// DynList with Complex Types Tests
978// =============================================================================
979
980TEST(DynListComplexTypes, StringList)
981{
982 DynList<string> list;
983
984 list.append("Hello");
985 list.append("World");
986 list.append("!");
987
988 EXPECT_EQ(list.get_first(), "Hello");
989 EXPECT_EQ(list.get_last(), "!");
990 EXPECT_EQ(list.size(), 3);
991}
992
993TEST(DynListComplexTypes, VectorList)
994{
996
997 list.append({1, 2, 3});
998 list.append({4, 5, 6});
999
1000 EXPECT_EQ(list.get_first().size(), 3);
1001 EXPECT_EQ(list.get_first()[0], 1);
1002 EXPECT_EQ(list.get_last()[0], 4);
1003}
1004
1006{
1007 int x;
1008 string name;
1009
1010 bool operator==(const TestStruct &other) const
1011 {
1012 return x == other.x && name == other.name;
1013 }
1014};
1015
1016TEST(DynListComplexTypes, StructList)
1017{
1019
1020 list.append({1, "one"});
1021 list.append({2, "two"});
1022
1023 EXPECT_EQ(list.get_first().x, 1);
1024 EXPECT_EQ(list.get_first().name, "one");
1025}
1026
1027// =============================================================================
1028// Stress Tests
1029// =============================================================================
1030
1031TEST(DynListStress, LargeList)
1032{
1033 constexpr size_t N = 100000;
1034 DynList<int> list;
1035
1036 for (size_t i = 0; i < N; ++i)
1037 list.append(static_cast<int>(i));
1038
1039 EXPECT_EQ(list.size(), N);
1040 EXPECT_EQ(list.get_first(), 0);
1041 EXPECT_EQ(list.get_last(), static_cast<int>(N - 1));
1042
1043 // Reverse and verify
1044 list.reverse();
1045 EXPECT_EQ(list.get_first(), static_cast<int>(N - 1));
1046 EXPECT_EQ(list.get_last(), 0);
1047}
1048
1049TEST(DynListStress, RandomOperations)
1050{
1051 constexpr size_t OPS = 10000;
1052
1053 DynList<int> list;
1054 std::mt19937 rng(42);
1055 std::uniform_int_distribution<int> opDist(0, 3);
1056 std::uniform_int_distribution<int> valDist(0, 1000);
1057
1058 for (size_t i = 0; i < OPS; ++i)
1059 {
1060 int op = opDist(rng);
1061
1062 switch (op)
1063 {
1064 case 0: // Insert
1065 list.insert(valDist(rng));
1066 break;
1067 case 1: // Append
1068 list.append(valDist(rng));
1069 break;
1070 case 2: // Remove first
1071 if (!list.is_empty())
1072 list.remove_first();
1073 break;
1074 case 3: // Pop
1075 if (!list.is_empty())
1076 list.pop();
1077 break;
1078 }
1079 }
1080
1081 // Just verify the list is in a consistent state
1082 size_t count = 0;
1083 for (auto it = list.get_it(); it.has_curr(); it.next())
1084 ++count;
1085
1086 EXPECT_EQ(count, list.size());
1087}
1088
1089// =============================================================================
1090// Edge Cases
1091// =============================================================================
1092
1093TEST(DynListEdgeCases, EmptyListOperations)
1094{
1095 DynList<int> list;
1096
1097 EXPECT_THROW(list.get_first(), std::underflow_error);
1098 EXPECT_THROW(list.get_last(), std::underflow_error);
1099 EXPECT_THROW(list.remove(), std::underflow_error);
1100 EXPECT_THROW(list.pop(), std::underflow_error);
1101 EXPECT_THROW(list.top(), std::underflow_error);
1102}
1103
1104TEST(DynListEdgeCases, UnitaryListOperations)
1105{
1106 DynList<int> list;
1107 list.append(42);
1108
1109 EXPECT_TRUE(list.is_unitarian());
1110 EXPECT_EQ(list.get_first(), list.get_last());
1111 EXPECT_EQ(list.top(), 42);
1112
1113 int removed = list.pop();
1114 EXPECT_EQ(removed, 42);
1115 EXPECT_TRUE(list.is_empty());
1116}
1117
1118TEST(DynListEdgeCases, SplitAndMerge)
1119{
1120 DynList<int> list;
1121 for (int i = 1; i <= 10; ++i)
1122 list.append(i);
1123
1124 DynList<int> l, r;
1125 list.split_list(l, r);
1126
1127 EXPECT_TRUE(list.is_empty());
1128 EXPECT_EQ(l.size() + r.size(), 10);
1129
1130 // Merge back
1131 l.append(std::move(r));
1132 EXPECT_EQ(l.size(), 10);
1133}
1134
1135TEST(HTListRemoveByPointer, HeadMiddleTailAndNotFound)
1136{
1137 HTList list;
1138 auto * n1 = new Snodenc<int>(1);
1139 auto * n2 = new Snodenc<int>(2);
1140 auto * n3 = new Snodenc<int>(3);
1141
1142 list.append(n1);
1143 list.append(n2);
1144 list.append(n3);
1145
1146 EXPECT_TRUE(list.remove(n1));
1147 delete n1;
1148 EXPECT_EQ(list.get_first()->to_data<int>(), 2);
1149
1150 EXPECT_TRUE(list.remove(n2));
1151 delete n2;
1152 EXPECT_TRUE(list.is_unitarian());
1153 EXPECT_EQ(list.get_first()->to_data<int>(), 3);
1154 EXPECT_EQ(list.get_last()->to_data<int>(), 3);
1155
1156 EXPECT_TRUE(list.remove(n3));
1157 delete n3;
1158 EXPECT_TRUE(list.is_empty());
1159
1160 auto * stray = new Snodenc<int>(99);
1161 EXPECT_THROW(list.remove(stray), std::underflow_error);
1162 delete stray;
1163}
1164
1165TEST(HTListIterator, ResetLastEndAndAssignmentCopiesPos)
1166{
1167 HTList list;
1168 list.append(new Snodenc<int>(1));
1169 list.append(new Snodenc<int>(2));
1170 list.append(new Snodenc<int>(3));
1171
1172 HTList::Iterator it(list);
1173 it.reset_last();
1174 ASSERT_TRUE(it.has_curr());
1175 EXPECT_EQ(it.get_curr()->to_data<int>(), 3);
1176 EXPECT_TRUE(it.is_last());
1177 EXPECT_EQ(it.get_pos(), 2);
1178
1179 it.end();
1180 EXPECT_FALSE(it.has_curr());
1181 EXPECT_THROW(it.get_curr(), std::overflow_error);
1182
1183 HTList::Iterator a(list);
1184 a.next();
1185 ASSERT_TRUE(a.has_curr());
1186 EXPECT_EQ(a.get_curr()->to_data<int>(), 2);
1187 EXPECT_EQ(a.get_pos(), 1);
1188
1189 HTList::Iterator b(list);
1190 b.reset_last();
1191 b = a;
1192 EXPECT_TRUE(b.has_curr());
1193 EXPECT_EQ(b.get_curr()->to_data<int>(), 2);
1194 EXPECT_EQ(b.get_pos(), 1);
1195
1196 list.remove_all_and_delete();
1197}
1198
1199TEST(HTListSynonyms, PutConcatListSplitListNeReverseListCutList)
1200{
1201 HTList list;
1202 list.put(new Snodenc<int>(1));
1203 list.put(new Snodenc<int>(2));
1204 list.put(new Snodenc<int>(3));
1205 EXPECT_EQ(list.size(), 3);
1206 EXPECT_EQ(list.get_first()->to_data<int>(), 1);
1207 EXPECT_EQ(list.get_last()->to_data<int>(), 3);
1208
1209 HTList l, r;
1210 list.split_list_ne(l, r);
1211 EXPECT_TRUE(list.is_empty());
1212 EXPECT_EQ(l.size() + r.size(), 3);
1213
1214 l.concat_list(r);
1215 EXPECT_TRUE(r.is_empty());
1216 EXPECT_EQ(l.size(), 3);
1217
1218 EXPECT_EQ(l.reverse_list(), 3u);
1219 EXPECT_EQ(l.get_first()->to_data<int>(), 3);
1220 EXPECT_EQ(l.get_last()->to_data<int>(), 1);
1221
1222 HTList::Iterator it(l);
1223 it.next();
1224 ASSERT_TRUE(it.has_curr());
1225 EXPECT_EQ(it.get_curr()->to_data<int>(), 2);
1226
1227 HTList cut;
1228 l.cut_list(it.get_curr(), cut);
1229 EXPECT_EQ(l.get_last()->to_data<int>(), 2);
1230 EXPECT_EQ(cut.get_first()->to_data<int>(), 1);
1231
1234}
1235
1236TEST(SlinkncIterator, TraversalAndOverflow)
1237{
1238 Slinknc head;
1239 Slinknc n1;
1240 Slinknc n2;
1241
1242 head.insert(&n1);
1243 head.insert(&n2);
1244
1245 Slinknc::Iterator it(head);
1246 ASSERT_TRUE(it.has_curr());
1247 EXPECT_EQ(it.get_curr(), &n2);
1248 it.next();
1249 ASSERT_TRUE(it.has_curr());
1250 EXPECT_EQ(it.get_curr(), &n1);
1251 it.next();
1252 EXPECT_FALSE(it.has_curr());
1253 EXPECT_THROW(it.get_curr(), std::overflow_error);
1254}
1255
1256// =============================================================================
1257// DynList keeps the number of its items
1258// =============================================================================
1259
1260namespace
1261{
1262 // The number of items of l, counted by walking its nodes
1263 template <typename T>
1264 size_t recount(const DynList<T> & l)
1265 {
1266 return static_cast<const HTList &>(l).HTList::size();
1267 }
1268
1269 template <typename T>
1270 vector<T> items_of(const DynList<T> & l)
1271 {
1272 vector<T> out;
1273 l.for_each([&out] (const T & x) { out.push_back(x); });
1274 return out;
1275 }
1276}
1277
1278TEST(DynListCount, EveryOperationKeepsTheCount)
1279{
1280 DynList<string> a = {"a", "b", "c"};
1281 EXPECT_EQ(a.size(), 3u);
1282 a.insert("x");
1283 a.append(string("y"));
1284 a.push("z");
1285 a.put("w");
1286 EXPECT_EQ(a.size(), 7u);
1287 (void) a.remove_first();
1288 (void) a.remove();
1289 (void) a.pop();
1290 (void) a.get();
1291 (void) a.remove_ne();
1292 EXPECT_EQ(a.size(), 2u);
1293 EXPECT_EQ(a.size(), recount(a));
1294
1295 vector<string> v = {"1", "2", "3"};
1296 DynList<string> range(v.begin(), v.end());
1297 EXPECT_EQ(range.size(), 3u);
1298 DynList<string> from_list(range);
1299 EXPECT_EQ(from_list.size(), 3u);
1300
1301 a.append(std::move(range));
1302 EXPECT_EQ(a.size(), 5u);
1303 EXPECT_EQ(range.size(), 0u);
1304 a.insert(std::move(from_list));
1305 EXPECT_EQ(a.size(), 8u);
1306 EXPECT_EQ(from_list.size(), 0u);
1307 const DynList<string> d = {"4", "5"};
1308 a.append(d);
1309 a.insert(d);
1310 EXPECT_EQ(a.size(), 12u);
1311 EXPECT_EQ(a.size(), recount(a));
1312
1313 DynList<string> b = a;
1314 EXPECT_EQ(b.size(), 12u);
1315 DynList<string> c = std::move(b);
1316 EXPECT_EQ(c.size(), 12u);
1317 EXPECT_EQ(b.size(), 0u);
1318 b = c;
1319 EXPECT_EQ(b.size(), 12u);
1320 b = std::move(a);
1321 EXPECT_EQ(b.size(), 12u);
1322 b.swap(a);
1323 EXPECT_EQ(a.size(), 12u);
1324 EXPECT_EQ(b.size(), 12u);
1325
1326 {
1328 (void) it.del();
1329 it.next();
1330 delete static_cast<Snodenc<string> *>(it.del_ne());
1331 }
1332 EXPECT_EQ(a.size(), 10u);
1333 (void) a.remove([] (const string & s) { return s == "4"; });
1334 (void) a.remove_ne([] (const string & s) { return s == "5"; });
1335 EXPECT_EQ(a.size(), 8u);
1336 EXPECT_EQ(a.size(), recount(a));
1337
1338 a.concat(b);
1339 EXPECT_EQ(a.size(), 20u);
1340 EXPECT_EQ(b.size(), 0u);
1341 c.concat_list(a);
1342 EXPECT_EQ(c.size(), 32u);
1343
1344 DynList<string> rest;
1345 Slinknc * third = static_cast<HTList &>(c).get_first()->get_next()->get_next();
1346 c.cut(third, rest);
1347 EXPECT_EQ(c.size(), 3u);
1348 EXPECT_EQ(rest.size(), 29u);
1349 EXPECT_EQ(rest.size(), recount(rest));
1350 rest.cut_list(static_cast<HTList &>(rest).get_last(), a);
1351 EXPECT_EQ(rest.size(), 29u);
1352 EXPECT_EQ(a.size(), 0u);
1353
1354 delete static_cast<Snodenc<string> *>(rest.remove_head());
1355 delete static_cast<Snodenc<string> *>(rest.remove_head_ne());
1356 EXPECT_EQ(rest.size(), 27u);
1357 EXPECT_EQ(rest.size(), recount(rest));
1358
1359 rest.reverse();
1360 rest.rotate_left(5);
1361 EXPECT_EQ(rest.size(), 27u);
1362
1363 vector<Slinknc *> nodes;
1364 for (HTList::Iterator it(rest); it.has_curr(); it.next_ne())
1365 nodes.push_back(it.get_curr_ne());
1366 rest.reset();
1367 EXPECT_EQ(rest.size(), 0u);
1368 EXPECT_TRUE(rest.is_empty());
1369 for (Slinknc * p : nodes)
1370 delete static_cast<Snodenc<string> *>(p);
1371
1373 EXPECT_EQ(c.size(), 0u);
1374 DynList<int> e = {1, 2, 3};
1375 e.empty();
1376 EXPECT_EQ(e.size(), 0u);
1377 e = {4, 5};
1378 e.clear();
1379 EXPECT_EQ(e.size(), 0u);
1380}
1381
1382TEST(DynListCount, SplitGivesTheFirstHalfToTheLeft)
1383{
1384 for (size_t n = 0; n <= 9; ++n)
1385 {
1386 DynList<int> list, l, r;
1387 for (size_t i = 0; i < n; ++i)
1388 list.append(int(i));
1389
1390 size_t count = 0;
1391 if (n % 3 == 0)
1392 count = list.split(l, r);
1393 else if (n % 3 == 1)
1394 count = list.split_list(l, r);
1395 else
1396 count = list.split_list_ne(l, r);
1397
1398 EXPECT_EQ(count, n);
1399 EXPECT_TRUE(list.is_empty());
1400 EXPECT_EQ(list.size(), 0u);
1401 ASSERT_EQ(l.size(), n - n / 2) << "n = " << n;
1402 ASSERT_EQ(r.size(), n / 2) << "n = " << n;
1403 EXPECT_EQ(recount(l), l.size());
1404 EXPECT_EQ(recount(r), r.size());
1405
1406 vector<int> left, right;
1407 for (size_t i = 0; i < n; ++i)
1408 (i < n - n / 2 ? left : right).push_back(int(i));
1409 EXPECT_EQ(items_of(l), left) << "n = " << n;
1410 EXPECT_EQ(items_of(r), right) << "n = " << n;
1411 }
1412}
1413
1414// Random sequences of the operations that change the lists, against a
1415// vector for each list
1416TEST(DynListCount, RandomSequencesKeepTheCount)
1417{
1418 mt19937 rng(20261103);
1419 DynList<int> lists[3];
1420 vector<int> model[3];
1421 int next_value = 0;
1422
1423 auto pick = [&rng] (size_t n) { return uniform_int_distribution<size_t>(0, n - 1)(rng); };
1424
1425 for (int step = 0; step < 3000; ++step)
1426 {
1427 const size_t i = pick(3), j = (i + 1 + pick(2)) % 3;
1428 DynList<int> & l = lists[i];
1429 vector<int> & m = model[i];
1430 switch (pick(13))
1431 {
1432 case 0:
1434 m.insert(m.begin(), next_value++);
1435 break;
1436 case 1:
1437 case 2:
1439 m.push_back(next_value++);
1440 break;
1441 case 3:
1442 if (not m.empty())
1443 {
1444 EXPECT_EQ(l.remove_first(), m.front());
1445 m.erase(m.begin());
1446 }
1447 break;
1448 case 4: // delete through an iterator at a random position
1449 if (not m.empty())
1450 {
1451 const size_t k = pick(m.size());
1453 for (size_t q = 0; q < k; ++q)
1454 it.next_ne();
1455 EXPECT_EQ(it.del(), m[k]);
1456 m.erase(m.begin() + long(k));
1457 }
1458 break;
1459 case 5: // remove by equality
1460 if (not m.empty())
1461 {
1462 const int x = m[pick(m.size())];
1463 EXPECT_EQ(l.remove([x] (int y) { return y == x; }), x);
1464 m.erase(find(m.begin(), m.end(), x));
1465 }
1466 break;
1467 case 6:
1468 l.append(std::move(lists[j]));
1469 m.insert(m.end(), model[j].begin(), model[j].end());
1470 model[j].clear();
1471 break;
1472 case 7:
1473 l.insert(std::move(lists[j]));
1474 m.insert(m.begin(), model[j].begin(), model[j].end());
1475 model[j].clear();
1476 break;
1477 case 8:
1478 l.swap(lists[j]);
1479 m.swap(model[j]);
1480 break;
1481 case 9:
1482 lists[j] = l;
1483 model[j] = m;
1484 break;
1485 case 10: // split and join back
1486 {
1487 DynList<int> a, b;
1488 l.split(a, b);
1489 EXPECT_EQ(a.size() + b.size(), m.size());
1490 l.append(std::move(a));
1491 l.append(std::move(b));
1492 }
1493 break;
1494 case 11: // cut after a random node and join back
1495 if (not m.empty())
1496 {
1497 const size_t k = pick(m.size());
1498 Slinknc * p = static_cast<HTList &>(l).get_first();
1499 for (size_t q = 0; q < k; ++q)
1500 p = p->get_next();
1501 DynList<int> tail;
1502 l.cut(p, tail);
1503 EXPECT_EQ(l.size(), k + 1);
1504 EXPECT_EQ(tail.size(), m.size() - k - 1);
1505 l.concat(tail);
1506 }
1507 break;
1508 case 12:
1509 l.reverse();
1510 std::reverse(m.begin(), m.end());
1511 break;
1512 }
1513
1514 for (size_t q = 0; q < 3; ++q)
1515 {
1516 ASSERT_EQ(lists[q].size(), model[q].size()) << "step " << step;
1517 ASSERT_EQ(recount(lists[q]), model[q].size()) << "step " << step;
1518 }
1519 }
1520
1521 for (size_t q = 0; q < 3; ++q)
1522 EXPECT_EQ(items_of(lists[q]), model[q]);
1523}
size_t size_t int32_t * out
Definition ca-c-api.h:120
Iterator on the items of list.
Definition htlist.H:1614
T & get_curr() const
Return the current item.
Definition htlist.H:1679
T del()
Remove the current item of the iterator.
Definition htlist.H:1695
Slinknc * del_ne() noexcept
Remove the current node, which the caller takes, and move the iterator forward; the iterator must hav...
Definition htlist.H:1717
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & get_first()
Return the first item of the list.
Definition htlist.H:1548
void remove_all_and_delete() noexcept
Delete all the items, as empty() does.
Definition htlist.H:2009
size_t split_list_ne(DynList &l, DynList &r) noexcept
Split the list in two.
Definition htlist.H:2033
void cut_list(Slinknc *link, DynList &list) noexcept
See cut(Slinknc * link, DynList & list).
Definition htlist.H:1979
T pop()
See remove().
Definition htlist.H:1479
size_t split(DynList &l, DynList &r) noexcept
See split_list_ne(DynList & l, DynList & r).
Definition htlist.H:2061
T & append(const T &item)
Definition htlist.H:1362
T remove_ne() noexcept(std::is_nothrow_move_constructible_v< T >)
Remove the first item of the list, which must not be empty.
Definition htlist.H:1423
size_t split_list(DynList &l, DynList &r) noexcept
See split_list_ne(DynList & l, DynList & r).
Definition htlist.H:2055
T & insert(const T &item)
Definition htlist.H:1308
void concat(DynList &l) noexcept
Append the items of l to this, in constant time; l becomes empty.
Definition htlist.H:1947
T get()
See remove().
Definition htlist.H:1485
T & get_last()
Return the last item of the list.
Definition htlist.H:1529
T remove()
Remove the first item of the list.
Definition htlist.H:1447
void cut(Slinknc *link, DynList &list) noexcept
Move to list, which must be empty, the items that follow the node link of this.
Definition htlist.H:1967
Slinknc * remove_head_ne() noexcept
Remove the first node of the list, which must not be empty; the caller takes the node,...
Definition htlist.H:1989
void clear() noexcept
Empties the container.
Definition htlist.H:1599
void concat_list(DynList &l) noexcept
See concat(DynList & l).
Definition htlist.H:1953
T remove_first()
See remove().
Definition htlist.H:1473
T & top()
See get_first().
Definition htlist.H:1563
void reset() noexcept
Forget all the nodes of the list, without deleting them, as HTList::reset() does; the list becomes em...
Definition htlist.H:2017
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
T & push(const T &item)
See insert(const T & item).
Definition htlist.H:1329
T & put(const T &item)
See insert(const T & item).
Definition htlist.H:1341
DynList & reverse() noexcept
Definition htlist.H:1728
void empty() noexcept
empty the list
Definition htlist.H:1576
DynList & swap(DynList &l) noexcept
Definition htlist.H:1253
Slinknc * remove_head()
Remove the first node of the list; the caller takes the node, and its item.
Definition htlist.H:2001
long get_pos() const noexcept
Definition htlist.H:925
void reset_last()
It has O(n) of performance.
Definition htlist.H:937
void next_ne() noexcept
Move the iterator one position forward guaranteeing no exception.
Definition htlist.H:1012
bool is_last() const noexcept
Definition htlist.H:974
Slinknc * get_curr() const
Definition htlist.H:997
bool has_curr() const noexcept
Definition htlist.H:969
void reset() noexcept
Reset the iterator at the first item.
Definition htlist.H:919
void end() noexcept
Set the iterator to its end position, which has not.
Definition htlist.H:953
Single linked list of nodes.
Definition htlist.H:420
Slinknc * get_first() const noexcept
Definition htlist.H:463
void push(Slinknc *link) noexcept
Definition htlist.H:717
void put(Slinknc *link) noexcept
Definition htlist.H:553
size_t reverse_list() noexcept
See reverse().
Definition htlist.H:833
void remove_all_and_delete() noexcept
Definition htlist.H:879
Slinknc * top() const
Definition htlist.H:739
size_t reverse() noexcept
It inverts all list elements. It returns list size.
Definition htlist.H:816
constexpr bool is_empty() const noexcept
Definition htlist.H:434
void rotate_left(size_t n)
Rotate to left the list n positions.
Definition htlist.H:1144
Slinknc * pop()
Remove the first item of this.
Definition htlist.H:733
void append(Slinknc *link) noexcept
Definition htlist.H:518
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1119
bool remove(Slinknc *link)
Definition htlist.H:691
bool is_unitarian() const noexcept
Return true if the list contains exactly just one.
Definition htlist.H:441
Slinknc * get_last() const noexcept
Return the last item of the list (nullptr if the list is empty)
Definition htlist.H:475
size_t split_list_ne(HTList &l, HTList &r) noexcept
Definition htlist.H:803
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
void swap(ODhashTable &other) noexcept
Swaps this table with other in constant time.
Definition tpl_odhash.H:470
bool has_curr() const noexcept
Return true if the iterator is positioned on a valid link.
Definition htlist.H:231
Slinknc * get_curr() const
Definition htlist.H:241
Link of a single linked list non-circular and without header node.
Definition htlist.H:95
T & to_data() noexcept
Return the item of the Snodenc<T> that this is.
Definition htlist.H:382
Slinknc *& get_next() noexcept
Definition htlist.H:134
constexpr bool is_empty() const noexcept
Return true if this is empty.
Definition htlist.H:102
Snodenc< T > * to_snodenc() noexcept
Convert this to a Snodenc<T>.
Definition htlist.H:370
Slinknc * remove_next() noexcept
Definition htlist.H:159
void insert(Slinknc *p) noexcept
insert(p) inserts the node pointed by p after this.
Definition htlist.H:146
T & get_data() noexcept
Return a modifiable reference to the data.
Definition htlist.H:295
Minimal std::expected-style result type for C++20.
DynList< int > list
DynList< int > list
void SetUp() override
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
void SetUp() override
void TearDown() override
void TearDown() override
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Definition hashDry.H:954
void empty()
Clears all entries and resets to default capacity.
Definition hashDry.H:933
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Definition hashDry.H:507
Slinknc link
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
#define TEST(name)
static mt19937 rng
#define N
Definition fib.C:294
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
Singly linked list implementations with head-tail access.
TEST_F(SlinkcTest, DefaultConstructor)
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
double next_value(const double val)
Return the next representable floating-point value to val
Definition ahUtils.H:445
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
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
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
STL namespace.
static StlIterator begin(SetType &s)
Create an iterator positioned at the first element of the container.
static StlIterator end(SetType &s)
Create an end iterator for the container.
bool operator==(const TestStruct &other) const
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dnode< int > Test
Definition testDnode.C:42
static int * k
Gsl_Compatible_Rng r
DynList< int > l