Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ahAlgo.cc
Go to the documentation of this file.
1
8#include <gtest/gtest.h>
9#include <vector>
10
11#include <ahAlgo.H>
12#include <tpl_dynDlist.H>
13#include <tpl_array.H>
14
15using namespace std;
16using namespace Aleph;
17
18// ============================================================================
19// Test Suite: for_each (using std::vector for STL-compatible iterators)
20// ============================================================================
21
23{
24 vector<int> v = {1, 2, 3, 4, 5};
25 int sum = 0;
26 Aleph::for_each(v.begin(), v.end(), [&sum](int x) { sum += x; });
27 EXPECT_EQ(sum, 15);
28}
29
31{
32 vector<int> v = {1, 2, 3};
33
34 Aleph::for_each(v.begin(), v.end(), [](int& x) { x *= 2; });
35
36 EXPECT_EQ(v[0], 2);
37 EXPECT_EQ(v[1], 4);
38 EXPECT_EQ(v[2], 6);
39}
40
42{
43 vector<int> empty;
44 int count = 0;
45 Aleph::for_each(empty.begin(), empty.end(), [&count](int) { ++count; });
46 EXPECT_EQ(count, 0);
47}
48
49// ============================================================================
50// Test Suite: count / count_if
51// ============================================================================
52
54{
55 vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};
56 auto n = Aleph::count_if(v.begin(), v.end(),
57 [](int x) { return x % 2 == 0; });
58 EXPECT_EQ(n, 4);
59}
60
62{
63 vector<int> v = {1, 3, 5, 7, 9};
64 auto n = Aleph::count_if(v.begin(), v.end(),
65 [](int x) { return x % 2 == 0; });
66 EXPECT_EQ(n, 0);
67}
68
70{
71 vector<int> v = {2, 4, 6, 8};
72 auto n = Aleph::count_if(v.begin(), v.end(),
73 [](int x) { return x % 2 == 0; });
74 EXPECT_EQ(n, 4);
75}
76
78{
79 vector<int> v = {1, 2, 3, 2, 4, 2, 5};
80 auto n = Aleph::count(v.begin(), v.end(), 2);
81 EXPECT_EQ(n, 3);
82}
83
85{
86 vector<int> v = {1, 2, 3, 4, 5};
87 auto n = Aleph::count(v.begin(), v.end(), 10);
88 EXPECT_EQ(n, 0);
89}
90
91// ============================================================================
92// Test Suite: min_element / max_element
93// ============================================================================
94
96{
97 vector<int> v = {5, 2, 8, 1, 9};
98
99 auto it = Aleph::min_element(v.begin(), v.end());
100 EXPECT_EQ(*it, 1);
101}
102
104{
105 vector<int> empty;
106 auto it = Aleph::min_element(empty.begin(), empty.end());
107 EXPECT_EQ(it, empty.end());
108}
109
111{
112 vector<int> v = {5, 2, 8, 1, 9};
113
114 auto it = Aleph::max_element(v.begin(), v.end());
115 EXPECT_EQ(*it, 9);
116}
117
118// ============================================================================
119// Test Suite: find / find_if
120// ============================================================================
121
123{
124 vector<int> v = {1, 3, 5, 4, 7, 8};
125 auto it = Aleph::find_if(v.begin(), v.end(),
126 [](int x) { return x % 2 == 0; });
127 EXPECT_NE(it, v.end());
128 EXPECT_EQ(*it, 4);
129}
130
132{
133 vector<int> v = {1, 3, 5, 7, 9};
134 auto it = Aleph::find_if(v.begin(), v.end(),
135 [](int x) { return x % 2 == 0; });
136 EXPECT_EQ(it, v.end());
137}
138
140{
141 vector<int> v = {1, 2, 3, 4, 5};
142 auto it = Aleph::find(v.begin(), v.end(), 3);
143 EXPECT_NE(it, v.end());
144 EXPECT_EQ(*it, 3);
145}
146
148{
149 vector<int> v = {1, 2, 3, 4, 5};
150 auto it = Aleph::find(v.begin(), v.end(), 10);
151 EXPECT_EQ(it, v.end());
152}
153
154// ============================================================================
155// Test Suite: search_n
156// ============================================================================
157
159{
160 vector<int> v = {1, 2, 2, 2, 3, 4};
161 auto it = Aleph::search_n(v.begin(), v.end(), 3, 2);
162 EXPECT_NE(it, v.end());
163 EXPECT_EQ(*it, 2);
164}
165
167{
168 vector<int> v = {1, 2, 2, 3, 2, 2, 4};
169 auto it = Aleph::search_n(v.begin(), v.end(), 3, 2);
170 EXPECT_EQ(it, v.end());
171}
172
174{
175 vector<int> v = {1, 2, 3};
176 auto it = Aleph::search_n(v.begin(), v.end(), 0, 2);
177 EXPECT_EQ(it, v.end());
178}
179
180// ============================================================================
181// Test Suite: adjacent_find
182// ============================================================================
183
185{
186 vector<int> v = {1, 2, 3, 3, 4, 5};
187 auto it = Aleph::adjacent_find(v.begin(), v.end());
188 EXPECT_NE(it, v.end());
189 EXPECT_EQ(*it, 3);
190}
191
193{
194 vector<int> v = {1, 2, 3, 4, 5};
195 auto it = Aleph::adjacent_find(v.begin(), v.end());
196 EXPECT_EQ(it, v.end());
197}
198
200{
201 vector<int> empty;
202 auto it = Aleph::adjacent_find(empty.begin(), empty.end());
203 EXPECT_EQ(it, empty.end());
204}
205
206// ============================================================================
207// Test Suite: equal
208// ============================================================================
209
211{
212 vector<int> v1 = {1, 2, 3, 4, 5};
213 vector<int> v2 = {1, 2, 3, 4, 5};
214 EXPECT_TRUE(Aleph::equal(v1.begin(), v1.end(), v2.begin()));
215}
216
218{
219 vector<int> v1 = {1, 2, 3, 4, 5};
220 vector<int> v2 = {1, 2, 3, 4, 6};
221 EXPECT_FALSE(Aleph::equal(v1.begin(), v1.end(), v2.begin()));
222}
223
224// ============================================================================
225// Test Suite: lexicographical_compare
226// ============================================================================
227
229{
230 vector<int> v1 = {1, 2, 3};
231 vector<int> v2 = {1, 2, 4};
233 v1.begin(), v1.end(), v2.begin(), v2.end()));
234}
235
237{
238 vector<int> v1 = {1, 2, 4};
239 vector<int> v2 = {1, 2, 3};
241 v1.begin(), v1.end(), v2.begin(), v2.end()));
242}
243
245{
246 vector<int> v1 = {1, 2};
247 vector<int> v2 = {1, 2, 3};
249 v1.begin(), v1.end(), v2.begin(), v2.end()));
250}
251
252// ============================================================================
253// Test Suite: copy / copy_backward
254// ============================================================================
255
257{
258 vector<int> src = {1, 2, 3};
259 vector<int> dst(3, 0);
260
261 Aleph::copy(src.begin(), src.end(), dst.begin());
262
263 EXPECT_EQ(dst[0], 1);
264 EXPECT_EQ(dst[1], 2);
265 EXPECT_EQ(dst[2], 3);
266}
267
269{
270 vector<int> src = {1, 2, 3};
271 vector<int> dst(3, 0);
272
273 Aleph::copy_backward(src.begin(), src.end(), dst.end());
274
275 EXPECT_EQ(dst[0], 1);
276 EXPECT_EQ(dst[1], 2);
277 EXPECT_EQ(dst[2], 3);
278}
279
280// ============================================================================
281// Test Suite: transform
282// ============================================================================
283
285{
286 vector<int> src = {1, 2, 3};
287 vector<int> dst(3, 0);
288
289 Aleph::transform(src.begin(), src.end(), dst.begin(),
290 [](int x) { return x * 2; });
291
292 EXPECT_EQ(dst[0], 2);
293 EXPECT_EQ(dst[1], 4);
294 EXPECT_EQ(dst[2], 6);
295}
296
298{
299 vector<int> src1 = {1, 2, 3};
300 vector<int> src2 = {10, 20, 30};
301 vector<int> dst(3, 0);
302
304 [](int a, int b) { return a + b; });
305
306 EXPECT_EQ(dst[0], 11);
307 EXPECT_EQ(dst[1], 22);
308 EXPECT_EQ(dst[2], 33);
309}
310
311// ============================================================================
312// Test Suite: swap_ranges
313// ============================================================================
314
316{
317 vector<int> v1 = {1, 2, 3};
318 vector<int> v2 = {4, 5, 6};
319
320 Aleph::swap_ranges(v1.begin(), v1.end(), v2.begin());
321
322 EXPECT_EQ(v1[0], 4);
323 EXPECT_EQ(v1[1], 5);
324 EXPECT_EQ(v1[2], 6);
325 EXPECT_EQ(v2[0], 1);
326 EXPECT_EQ(v2[1], 2);
327 EXPECT_EQ(v2[2], 3);
328}
329
330// ============================================================================
331// Test Suite: fill / fill_n
332// ============================================================================
333
335{
336 vector<int> v = {1, 2, 3};
337
338 Aleph::fill(v.begin(), v.end(), 42);
339
340 EXPECT_EQ(v[0], 42);
341 EXPECT_EQ(v[1], 42);
342 EXPECT_EQ(v[2], 42);
343}
344
346{
347 vector<int> v = {1, 2, 3, 4, 5};
348
349 Aleph::fill_n(v.begin(), 3, 99);
350
351 EXPECT_EQ(v[0], 99);
352 EXPECT_EQ(v[1], 99);
353 EXPECT_EQ(v[2], 99);
354 EXPECT_EQ(v[3], 4); // unchanged
355 EXPECT_EQ(v[4], 5); // unchanged
356}
357
358// ============================================================================
359// Test Suite: generate / generate_n
360// ============================================================================
361
363{
364 vector<int> v(3, 0);
365
366 int counter = 0;
367 Aleph::generate(v.begin(), v.end(), [&counter]() { return ++counter; });
368
369 EXPECT_EQ(v[0], 1);
370 EXPECT_EQ(v[1], 2);
371 EXPECT_EQ(v[2], 3);
372}
373
375{
376 vector<int> v(5, 0);
377
378 int counter = 10;
379 Aleph::generate_n(v.begin(), 3, [&counter]() { return counter++; });
380
381 EXPECT_EQ(v[0], 10);
382 EXPECT_EQ(v[1], 11);
383 EXPECT_EQ(v[2], 12);
384 EXPECT_EQ(v[3], 0); // unchanged
385}
386
387// ============================================================================
388// Test Suite: replace / replace_if
389// ============================================================================
390
392{
393 vector<int> v = {1, 2, 3, 4, 5};
394
395 Aleph::replace_if(v.begin(), v.end(),
396 [](int x) { return x % 2 == 0; }, 0);
397
398 EXPECT_EQ(v[0], 1);
399 EXPECT_EQ(v[1], 0);
400 EXPECT_EQ(v[2], 3);
401 EXPECT_EQ(v[3], 0);
402 EXPECT_EQ(v[4], 5);
403}
404
406{
407 vector<int> v = {1, 2, 1, 3, 1};
408
409 Aleph::replace(v.begin(), v.end(), 1, 99);
410
411 EXPECT_EQ(v[0], 99);
412 EXPECT_EQ(v[1], 2);
413 EXPECT_EQ(v[2], 99);
414 EXPECT_EQ(v[3], 3);
415 EXPECT_EQ(v[4], 99);
416}
417
418// ============================================================================
419// Test Suite: replace_copy_if
420// ============================================================================
421
423{
424 vector<int> src = {1, 2, 3, 4, 5};
425 vector<int> dst(5, 0);
426
427 Aleph::replace_copy_if(src.begin(), src.end(), dst.begin(),
428 [](int x) { return x % 2 == 0; }, 0);
429
430 EXPECT_EQ(dst[0], 1);
431 EXPECT_EQ(dst[1], 0);
432 EXPECT_EQ(dst[2], 3);
433 EXPECT_EQ(dst[3], 0);
434 EXPECT_EQ(dst[4], 5);
435}
436
437// ============================================================================
438// Test Suite: reverse
439// ============================================================================
440
442{
443 vector<int> v = {1, 2, 3, 4, 5};
444
445 Aleph::reverse(v.begin(), v.end());
446
447 EXPECT_EQ(v[0], 5);
448 EXPECT_EQ(v[1], 4);
449 EXPECT_EQ(v[2], 3);
450 EXPECT_EQ(v[3], 2);
451 EXPECT_EQ(v[4], 1);
452}
453
455{
456 vector<int> v = {42};
457
458 Aleph::reverse(v.begin(), v.end());
459
460 EXPECT_EQ(v[0], 42);
461}
462
464{
465 vector<int> v;
466 EXPECT_NO_THROW(Aleph::reverse(v.begin(), v.end()));
467}
468
469// ============================================================================
470// Test Suite: reverse_copy
471// ============================================================================
472
474{
475 vector<int> src = {1, 2, 3, 4, 5};
476 vector<int> dst(5, 0);
477
478 Aleph::reverse_copy(src.begin(), src.end(), dst.begin());
479
480 EXPECT_EQ(dst[0], 5);
481 EXPECT_EQ(dst[1], 4);
482 EXPECT_EQ(dst[2], 3);
483 EXPECT_EQ(dst[3], 2);
484 EXPECT_EQ(dst[4], 1);
485}
486
487// ============================================================================
488// Test Suite: rotate
489// ============================================================================
490
492{
493 vector<int> v = {1, 2, 3, 4, 5};
494
495 Aleph::rotate(v.begin(), v.begin() + 2, v.end());
496
497 EXPECT_EQ(v[0], 3);
498 EXPECT_EQ(v[1], 4);
499 EXPECT_EQ(v[2], 5);
500 EXPECT_EQ(v[3], 1);
501 EXPECT_EQ(v[4], 2);
502}
503
504// ============================================================================
505// Test Suite: lower_bound / upper_bound
506// ============================================================================
507
509{
510 vector<int> v = {1, 2, 4, 5, 6};
511 auto it = Aleph::lower_bound(v.begin(), v.end(), 4);
512 EXPECT_NE(it, v.end());
513 EXPECT_EQ(*it, 4);
514}
515
517{
518 vector<int> v = {1, 2, 4, 5, 6};
519 auto it = Aleph::lower_bound(v.begin(), v.end(), 3);
520 EXPECT_NE(it, v.end());
521 EXPECT_EQ(*it, 4); // First element >= 3
522}
523
525{
526 vector<int> v = {1, 2, 4, 5, 6};
527 auto it = Aleph::upper_bound(v.begin(), v.end(), 4);
528 EXPECT_NE(it, v.end());
529 EXPECT_EQ(*it, 5); // First element > 4
530}
531
532// ============================================================================
533// Test Suite: binary_search
534// ============================================================================
535
537{
538 vector<int> v = {1, 2, 3, 4, 5};
539 EXPECT_TRUE(Aleph::binary_search(v.begin(), v.end(), 3));
540}
541
543{
544 vector<int> v = {1, 2, 3, 4, 5};
545 EXPECT_FALSE(Aleph::binary_search(v.begin(), v.end(), 10));
546}
547
548// ============================================================================
549// Test Suite: equal_range
550// ============================================================================
551
553{
554 vector<int> v = {1, 2, 2, 2, 3, 4};
555 auto range = Aleph::equal_range(v.begin(), v.end(), 2);
556
557 EXPECT_EQ(*range.first, 2);
558 EXPECT_EQ(*range.second, 3);
559}
560
561// ============================================================================
562// Test Suite: includes
563// ============================================================================
564
566{
567 vector<int> v1 = {1, 2, 3, 4, 5, 6, 7};
568 vector<int> v2 = {2, 4, 6};
569 EXPECT_TRUE(Aleph::includes(v1.begin(), v1.end(), v2.begin(), v2.end()));
570}
571
573{
574 vector<int> v1 = {1, 2, 3, 4, 5};
575 vector<int> v2 = {2, 4, 8};
576 EXPECT_FALSE(Aleph::includes(v1.begin(), v1.end(), v2.begin(), v2.end()));
577}
578
580{
581 vector<int> v1 = {1, 2, 3};
582 vector<int> v2;
583 EXPECT_TRUE(Aleph::includes(v1.begin(), v1.end(), v2.begin(), v2.end()));
584}
585
586// ============================================================================
587// Test Suite: merge
588// ============================================================================
589
591{
592 vector<int> v1 = {1, 3, 5};
593 vector<int> v2 = {2, 4, 6};
594 vector<int> result(6, 0);
595
596 Aleph::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), result.begin());
597
598 EXPECT_EQ(result[0], 1);
599 EXPECT_EQ(result[1], 2);
600 EXPECT_EQ(result[2], 3);
601 EXPECT_EQ(result[3], 4);
602 EXPECT_EQ(result[4], 5);
603 EXPECT_EQ(result[5], 6);
604}
605
607{
608 vector<int> v1 = {1, 2, 3};
609 vector<int> v2;
610 vector<int> result(3, 0);
611
612 Aleph::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), result.begin());
613
614 EXPECT_EQ(result[0], 1);
615 EXPECT_EQ(result[1], 2);
616 EXPECT_EQ(result[2], 3);
617}
618
619// ============================================================================
620// Test Suite: accumulate
621// ============================================================================
622
624{
625 vector<int> v = {1, 2, 3, 4, 5};
626 auto sum = Aleph::accumulate(v.begin(), v.end(), 0);
627 EXPECT_EQ(sum, 15);
628}
629
631{
632 vector<int> v = {1, 2, 3, 4, 5};
633 auto product = Aleph::accumulate(v.begin(), v.end(), 1,
634 [](int a, int b) { return a * b; });
635 EXPECT_EQ(product, 120);
636}
637
639{
640 vector<int> v = {1, 2, 3};
641 auto sum = Aleph::accumulate(v.begin(), v.end(), 10);
642 EXPECT_EQ(sum, 16);
643}
644
646{
647 vector<int> empty;
648 auto sum = Aleph::accumulate(empty.begin(), empty.end(), 42);
649 EXPECT_EQ(sum, 42);
650}
651
652// ============================================================================
653// Test Suite: inner_product
654// ============================================================================
655
657{
658 vector<int> v1 = {1, 2, 3};
659 vector<int> v2 = {4, 5, 6};
660
661 auto result = Aleph::inner_product(v1.begin(), v1.end(), v2.begin(), 0);
662 EXPECT_EQ(result, 32); // 1*4 + 2*5 + 3*6
663}
664
666{
667 vector<int> v1 = {1, 2, 3};
668 vector<int> v2 = {1, 1, 1};
669
670 // Sum of differences
671 auto result = Aleph::inner_product(v1.begin(), v1.end(), v2.begin(), 0,
672 [](int a, int b) { return a + b; },
673 [](int a, int b) { return a - b; });
674 EXPECT_EQ(result, 3); // (1-1) + (2-1) + (3-1)
675}
676
677// ============================================================================
678// Test Suite: partial_sum
679// ============================================================================
680
682{
683 vector<int> src = {1, 2, 3, 4, 5};
684 vector<int> dst(5, 0);
685
686 Aleph::partial_sum(src.begin(), src.end(), dst.begin());
687
688 EXPECT_EQ(dst[0], 1);
689 EXPECT_EQ(dst[1], 3);
690 EXPECT_EQ(dst[2], 6);
691 EXPECT_EQ(dst[3], 10);
692 EXPECT_EQ(dst[4], 15);
693}
694
696{
697 vector<int> src = {1, 2, 3, 4};
698 vector<int> dst(4, 0);
699
700 Aleph::partial_sum(src.begin(), src.end(), dst.begin(),
701 [](int a, int b) { return a * b; });
702
703 EXPECT_EQ(dst[0], 1);
704 EXPECT_EQ(dst[1], 2); // 1 * 2
705 EXPECT_EQ(dst[2], 6); // 2 * 3
706 EXPECT_EQ(dst[3], 24); // 6 * 4
707}
708
710{
711 vector<int> empty;
712 vector<int> dst;
713
714 auto it = Aleph::partial_sum(empty.begin(), empty.end(), dst.begin());
715 EXPECT_EQ(it, dst.begin());
716}
717
718// ============================================================================
719// Test Suite: adjacent_difference
720// ============================================================================
721
723{
724 vector<int> src = {1, 3, 6, 10, 15};
725 vector<int> dst(5, 0);
726
727 Aleph::adjacent_difference(src.begin(), src.end(), dst.begin());
728
729 EXPECT_EQ(dst[0], 1); // First element unchanged
730 EXPECT_EQ(dst[1], 2); // 3 - 1
731 EXPECT_EQ(dst[2], 3); // 6 - 3
732 EXPECT_EQ(dst[3], 4); // 10 - 6
733 EXPECT_EQ(dst[4], 5); // 15 - 10
734}
735
737{
738 vector<int> src = {1, 2, 4, 8};
739 vector<int> dst(4, 0);
740
741 Aleph::adjacent_difference(src.begin(), src.end(), dst.begin(),
742 [](int a, int b) { return a / b; });
743
744 EXPECT_EQ(dst[0], 1); // First unchanged
745 EXPECT_EQ(dst[1], 2); // 2 / 1
746 EXPECT_EQ(dst[2], 2); // 4 / 2
747 EXPECT_EQ(dst[3], 2); // 8 / 4
748}
749
750// ============================================================================
751// Test Suite: unique
752// ============================================================================
753
755{
756 vector<int> v = {1, 1, 2, 2, 2, 3, 3};
757
758 auto it = Aleph::unique(v.begin(), v.end());
759
760 EXPECT_EQ(v[0], 1);
761 EXPECT_EQ(v[1], 2);
762 EXPECT_EQ(v[2], 3);
763 EXPECT_EQ(std::distance(v.begin(), it), 3);
764}
765
767{
768 vector<int> v = {1, 2, 3, 4};
769
770 auto it = Aleph::unique(v.begin(), v.end());
771 EXPECT_EQ(it, v.end());
772}
773
774// ============================================================================
775// Test Suite: remove / remove_if
776// ============================================================================
777
779{
780 vector<int> v = {1, 2, 3, 4, 5, 6};
781
782 auto it = Aleph::remove_if(v.begin(), v.end(),
783 [](int x) { return x % 2 == 0; });
784
785 EXPECT_EQ(v[0], 1);
786 EXPECT_EQ(v[1], 3);
787 EXPECT_EQ(v[2], 5);
788 EXPECT_EQ(std::distance(v.begin(), it), 3);
789}
790
792{
793 vector<int> v = {1, 2, 1, 3, 1};
794
795 auto it = Aleph::remove(v.begin(), v.end(), 1);
796
797 EXPECT_EQ(v[0], 2);
798 EXPECT_EQ(v[1], 3);
799 EXPECT_EQ(std::distance(v.begin(), it), 2);
800}
801
802// ============================================================================
803// Test Suite: remove_copy_if
804// ============================================================================
805
807{
808 vector<int> src = {1, 2, 3, 4, 5, 6};
809 vector<int> dst;
810 dst.reserve(6);
811
812 auto it = Aleph::remove_copy_if(src.begin(), src.end(),
813 std::back_inserter(dst),
814 [](int x) { return x % 2 == 0; });
815 (void)it;
816
817 ASSERT_EQ(dst.size(), 3);
818 EXPECT_EQ(dst[0], 1);
819 EXPECT_EQ(dst[1], 3);
820 EXPECT_EQ(dst[2], 5);
821}
822
823// ============================================================================
824// Test Suite: unique_copy
825// ============================================================================
826
828{
829 vector<int> src = {1, 1, 2, 2, 3, 3};
830 vector<int> dst;
831
832 Aleph::unique_copy(src.begin(), src.end(), std::back_inserter(dst));
833
834 ASSERT_EQ(dst.size(), 3);
835 EXPECT_EQ(dst[0], 1);
836 EXPECT_EQ(dst[1], 2);
837 EXPECT_EQ(dst[2], 3);
838}
839
840// ============================================================================
841// Test Suite: Edge Cases
842// ============================================================================
843
845{
846 vector<int> v = {42};
847
848 // count
849 EXPECT_EQ(Aleph::count(v.begin(), v.end(), 42), 1);
850
851 // min/max
852 EXPECT_EQ(*Aleph::min_element(v.begin(), v.end()), 42);
853
854 // accumulate
855 EXPECT_EQ(Aleph::accumulate(v.begin(), v.end(), 0), 42);
856}
857
859{
860 const int N = 10000;
861 vector<int> v(N);
862 for (int i = 0; i < N; ++i)
863 v[i] = i;
864
865 // Count all even
866 auto count = Aleph::count_if(v.begin(), v.end(),
867 [](int x) { return x % 2 == 0; });
868 EXPECT_EQ(count, N / 2);
869
870 // Sum all
871 auto sum = Aleph::accumulate(v.begin(), v.end(), 0L);
872 EXPECT_EQ(sum, (long)(N - 1) * N / 2);
873}
874
875// ============================================================================
876// Test Suite: mismatch
877// ============================================================================
878
880{
881 vector<int> v1 = {1, 2, 3, 4, 5};
882 vector<int> v2 = {1, 2, 3, 9, 5};
883
884 auto result = Aleph::mismatch(v1.begin(), v1.end(), v2.begin());
885
886 EXPECT_EQ(*result.first, 4);
887 EXPECT_EQ(*result.second, 9);
888}
889
891{
892 vector<int> v1 = {1, 2, 3};
893 vector<int> v2 = {1, 2, 3};
894
895 auto result = Aleph::mismatch(v1.begin(), v1.end(), v2.begin());
896
897 EXPECT_EQ(result.first, v1.end());
898}
899
900// ============================================================================
901// Test Suite: search
902// ============================================================================
903
905{
906 vector<int> v = {1, 2, 3, 4, 5, 6};
907 vector<int> sub = {3, 4, 5};
908
909 auto it = Aleph::search(v.begin(), v.end(), sub.begin(), sub.end());
910
911 EXPECT_NE(it, v.end());
912 EXPECT_EQ(*it, 3);
913}
914
916{
917 vector<int> v = {1, 2, 3, 4, 5};
918 vector<int> sub = {3, 5, 4};
919
920 auto it = Aleph::search(v.begin(), v.end(), sub.begin(), sub.end());
921
922 EXPECT_EQ(it, v.end());
923}
Aleph-w implementation of STL-like algorithms.
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
#define TEST(name)
#define N
Definition fib.C:294
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Itor lower_bound(Itor beg, Itor end, const T &value)
Find lower bound in a sorted range.
Definition ahAlgo.H:1190
std::pair< Itor1, Itor2 > mismatch(Itor1 beg, const Itor1 &end, Itor2 cmpBeg, BinaryPredicate op=BinaryPredicate())
Find the first mismatching elements in two ranges.
Definition ahAlgo.H:516
void reverse(Itor beg, Itor end)
Reverse elements in a range.
Definition ahAlgo.H:1094
Itor unique(Itor __first, Itor __last, BinaryPredicate __binary_pred=BinaryPredicate())
Remove consecutive duplicates in place.
Definition ahAlgo.H:1058
Itor2 partial_sum(Itor1 sourceBeg, Itor1 sourceEnd, Itor2 destBeg)
Compute partial sums of a range.
Definition ahAlgo.H:1596
Itor2 copy_backward(const Itor1 &sourceBeg, Itor1 sourceEnd, Itor2 destEnd)
Copy elements backward from one range to another.
Definition ahAlgo.H:607
void generate(Itor beg, const Itor &end, Func op)
Generate values for a range.
Definition ahAlgo.H:746
T inner_product(Itor1 beg1, Itor1 end1, Itor2 beg2, T initValue)
Compute inner product of two ranges.
Definition ahAlgo.H:1543
void replace_if(Itor beg, const Itor &end, UnaryPredicate op, const T &value)
Replace elements satisfying a predicate.
Definition ahAlgo.H:788
Out_Itor unique_copy(In_Itor __first, In_Itor __last, Out_Itor __result)
Copy unique elements.
Definition ahAlgo.H:1006
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
bool lexicographical_compare(Itor1 beg1, const Itor1 &end1, Itor2 beg2, const Itor2 &end2, Comp op=Comp())
Lexicographical comparison of two ranges.
Definition ahAlgo.H:547
void fill(Itor beg, const Itor &end, const T &value)
Fill a range with a value.
Definition ahAlgo.H:707
Out_Itor remove_copy_if(In_Itor __first, const In_Itor &__last, Out_Itor __result, Predicate __pred)
Copy elements not satisfying a predicate.
Definition ahAlgo.H:905
Itor2 adjacent_difference(Itor1 sourceBeg, Itor1 sourceEnd, Itor2 destBeg)
Compute adjacent differences.
Definition ahAlgo.H:1667
Itor adjacent_find(Itor beg, const Itor &end, BinaryPredicate op=BinaryPredicate())
Find first pair of adjacent equal elements.
Definition ahAlgo.H:443
T accumulate(Itor beg, Itor end, T initValue)
Accumulate values in a range.
Definition ahAlgo.H:1493
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
Definition ahAlgo.H:230
Itor2 swap_ranges(Itor1 beg1, const Itor1 &end1, Itor2 beg2)
Swap elements between two ranges.
Definition ahAlgo.H:686
Itor min_element(Itor beg, const Itor &end, CompFunc op=CompFunc())
Find the minimum element in a range.
Definition ahAlgo.H:148
Itor2 replace_copy_if(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg, UnaryPredicate op, const T &value)
Copy and replace elements satisfying a predicate.
Definition ahAlgo.H:842
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
Itor upper_bound(Itor beg, Itor end, const T &value)
Find upper bound in a sorted range.
Definition ahAlgo.H:1239
Fw_Itor remove(Fw_Itor __first, const Fw_Itor &__last, const T &__value)
Remove elements equal to a value.
Definition ahAlgo.H:962
Itor search_n(Itor beg, const Itor &end, Size count, const T &value, BinaryPredicate op=BinaryPredicate())
Find n consecutive elements equal to a value.
Definition ahAlgo.H:255
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
bool equal(Itor1 beg, const Itor1 &end, Itor2 cmpBeg, BinaryPredicate op=BinaryPredicate())
Test if two ranges are equal.
Definition ahAlgo.H:482
Itor2 transform(Itor1 sourceBeg, Itor1 sourceEnd, Itor2 destBeg, UnaryFunc op)
Transform elements using a unary operation.
Definition ahAlgo.H:632
Operation for_each(Itor beg, const Itor &end, Operation op)
Apply an operation to each element in a range.
Definition ahAlgo.H:76
void fill_n(Itor beg, Size num, const T &value)
Fill n elements with a value.
Definition ahAlgo.H:727
Fw_Itor remove_if(Fw_Itor __first, const Fw_Itor &__last, Predicate __pred)
Remove elements satisfying a predicate.
Definition ahAlgo.H:934
Itor3 merge(Itor1 source1Beg, Itor1 source1End, Itor2 source2Beg, Itor2 source2End, Itor3 destBeg)
Merge two sorted ranges.
Definition ahAlgo.H:1410
bool binary_search(Itor beg, Itor end, const T &value)
Binary search for a value.
Definition ahAlgo.H:1284
Itor::difference_type count_if(Itor beg, const Itor &end, Operation op)
Count elements satisfying a predicate.
Definition ahAlgo.H:100
void generate_n(Itor beg, Size num, Func op)
Generate values for n elements.
Definition ahAlgo.H:766
bool includes(Itor1 beg, Itor1 end, Itor2 searchBeg, Itor2 searchEnd)
Test if one sorted range includes another.
Definition ahAlgo.H:1373
std::pair< Itor, Itor > equal_range(Itor beg, Itor end, const T &value)
Find equal range in a sorted sequence.
Definition ahAlgo.H:1330
Itor2 reverse_copy(Itor1 sourceBeg, Itor1 sourceEnd, Itor2 destBeg)
Copy elements in reverse order.
Definition ahAlgo.H:1114
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 max_element(const Itor &beg, const Itor &end, CompFunc op=CompFunc())
Find the maximum element in a range.
Definition ahAlgo.H:184
void replace(Itor beg, const Itor &end, const T &old_value, const T &new_value)
Replace elements equal to a value.
Definition ahAlgo.H:815
Itor find_if(Itor beg, const Itor &end, UnaryPredicate op)
Find the first element satisfying a predicate.
Definition ahAlgo.H:205
void rotate(Itor beg, Itor pos, Itor end)
Rotate elements in a range.
Definition ahAlgo.H:1138
DynList< T > maps(const C &c, Op op)
Classic map operation.
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.
Dynamic array container with automatic resizing.
Dynamic doubly linked list implementation.