-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_functions.h
1502 lines (1352 loc) · 58.6 KB
/
basic_functions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Developed by Jimmy Hu */
#ifndef TINYDIP_BASIC_FUNCTIONS_H
#define TINYDIP_BASIC_FUNCTIONS_H
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <concepts>
#include <deque>
#include <execution>
#include <exception>
#include <functional>
#include <future>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <mutex>
#include <numeric>
#include <optional>
#include <ranges>
#include <random>
#include <stdexcept>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>
//#define USE_BOOST_MULTIDIMENSIONAL_ARRAY
#ifdef USE_BOOST_MULTIDIMENSIONAL_ARRAY
#include <boost/multi_array.hpp>
#include <boost/multi_array/algorithm.hpp>
#include <boost/multi_array/base.hpp>
#include <boost/multi_array/collection_concept.hpp>
#endif
namespace TinyDIP
{
template<typename T>
concept is_back_inserterable = requires(T x)
{
std::back_inserter(x);
};
template<typename T>
concept is_inserterable = requires(T x)
{
std::inserter(x, std::ranges::end(x));
};
template<typename T>
concept is_minusable = requires(T x) { x - x; };
template<typename T1, typename T2>
concept is_minusable2 = requires(T1 x1, T2 x2) { x1 - x2; };
template<typename T>
concept is_multiplicable = requires(T x)
{
x * x;
};
template<typename T>
concept is_divisible = requires(T x)
{
x / x;
};
#ifdef USE_BOOST_MULTIDIMENSIONAL_ARRAY
template<typename T>
concept is_multi_array = requires(T x)
{
x.num_dimensions();
x.shape();
boost::multi_array(x);
};
template<typename T1, typename T2>
concept is_multi_array_dimensionality_equal =
is_multi_array<T1> && is_multi_array<T2> && requires(T1 x, T2 y)
{
T1::dimensionality == T2::dimensionality;
};
#endif
template<typename T1, typename T2>
concept is_std_powable = requires(T1 x1, T2 x2)
{
std::pow(x1, x2);
};
// Reference: https://stackoverflow.com/a/64287611/6667035
template <typename T>
struct is_complex : std::false_type {};
template <typename T>
struct is_complex<std::complex<T>> : std::true_type {};
// Reference: https://stackoverflow.com/a/48458312/6667035
template <typename>
struct is_tuple : std::false_type {};
template <typename ...T>
struct is_tuple<std::tuple<T...>> : std::true_type {};
// recursive_unwrap_type_t struct implementation
template<std::size_t, typename, typename...>
struct recursive_unwrap_type { };
template<class...Ts1, template<class...>class Container1, typename... Ts>
struct recursive_unwrap_type<1, Container1<Ts1...>, Ts...>
{
using type = std::ranges::range_value_t<Container1<Ts1...>>;
};
template<std::size_t unwrap_level, class...Ts1, template<class...>class Container1, typename... Ts>
requires ( std::ranges::input_range<Container1<Ts1...>> &&
requires { typename recursive_unwrap_type<
unwrap_level - 1,
std::ranges::range_value_t<Container1<Ts1...>>,
std::ranges::range_value_t<Ts>...>::type; }) // The rest arguments are ranges
struct recursive_unwrap_type<unwrap_level, Container1<Ts1...>, Ts...>
{
using type = typename recursive_unwrap_type<
unwrap_level - 1,
std::ranges::range_value_t<Container1<Ts1...>>
>::type;
};
template<std::size_t unwrap_level, typename T1, typename... Ts>
using recursive_unwrap_type_t = typename recursive_unwrap_type<unwrap_level, T1, Ts...>::type;
// recursive_invoke_result_t implementation
template<std::size_t, typename, typename>
struct recursive_invoke_result { };
template<typename T, typename F>
struct recursive_invoke_result<0, F, T> { using type = std::invoke_result_t<F, T>; };
template<std::size_t unwrap_level, std::copy_constructible F, template<typename...> typename Container, typename... Ts>
requires (std::ranges::input_range<Container<Ts...>> &&
requires { typename recursive_invoke_result<unwrap_level - 1, F, std::ranges::range_value_t<Container<Ts...>>>::type; })
struct recursive_invoke_result<unwrap_level, F, Container<Ts...>>
{
using type = Container<typename recursive_invoke_result<unwrap_level - 1, F, std::ranges::range_value_t<Container<Ts...>>>::type>;
};
template<std::size_t unwrap_level, std::copy_constructible F, typename T>
using recursive_invoke_result_t = typename recursive_invoke_result<unwrap_level, F, T>::type;
// recursive_variadic_invoke_result_t implementation
template<std::size_t, typename, typename, typename...>
struct recursive_variadic_invoke_result { };
template<std::copy_constructible F, class...Ts1, template<class...>class Container1, typename... Ts>
struct recursive_variadic_invoke_result<1, F, Container1<Ts1...>, Ts...>
{
using type = Container1<std::invoke_result_t<F,
std::ranges::range_value_t<Container1<Ts1...>>,
std::ranges::range_value_t<Ts>...>>;
};
template<std::size_t unwrap_level, std::copy_constructible F, class...Ts1, template<class...>class Container1, typename... Ts>
requires ( std::ranges::input_range<Container1<Ts1...>> &&
requires { typename recursive_variadic_invoke_result<
unwrap_level - 1,
F,
std::ranges::range_value_t<Container1<Ts1...>>,
std::ranges::range_value_t<Ts>...>::type; }) // The rest arguments are ranges
struct recursive_variadic_invoke_result<unwrap_level, F, Container1<Ts1...>, Ts...>
{
using type = Container1<
typename recursive_variadic_invoke_result<
unwrap_level - 1,
F,
std::ranges::range_value_t<Container1<Ts1...>>,
std::ranges::range_value_t<Ts>...
>::type>;
};
template<std::size_t unwrap_level, std::copy_constructible F, typename T1, typename... Ts>
using recursive_variadic_invoke_result_t = typename recursive_variadic_invoke_result<unwrap_level, F, T1, Ts...>::type;
// recursive_array_invoke_result struct implementation
template<std::size_t, typename, typename, typename...>
struct recursive_array_invoke_result { };
template< typename F,
template<class, std::size_t> class Container,
typename T,
std::size_t N,
typename... Ts>
struct recursive_array_invoke_result<1, F, Container<T, N>, Ts...>
{
using type = Container<
std::invoke_result_t<F, std::ranges::range_value_t<Container<T, N>>,
std::ranges::range_value_t<Ts>...>,
N>;
};
template< std::size_t unwrap_level,
typename F,
template<class, std::size_t> class Container,
typename T,
std::size_t N,
typename... Ts>
requires ( std::ranges::input_range<Container<T, N>> &&
requires { typename recursive_array_invoke_result<
unwrap_level - 1,
F,
std::ranges::range_value_t<Container<T, N>>,
std::ranges::range_value_t<Ts>...>::type; }) // The rest arguments are ranges
struct recursive_array_invoke_result<unwrap_level, F, Container<T, N>, Ts...>
{
using type = Container<
typename recursive_array_invoke_result<
unwrap_level - 1,
F,
std::ranges::range_value_t<Container<T, N>>,
std::ranges::range_value_t<Ts>...
>::type, N>;
};
template< std::size_t unwrap_level,
typename F,
template<class, std::size_t> class Container,
typename T,
std::size_t N,
typename... Ts>
using recursive_array_invoke_result_t = typename recursive_array_invoke_result<unwrap_level, F, Container<T, N>, Ts...>::type;
// Reference: https://stackoverflow.com/a/58067611/6667035
template <typename T>
concept arithmetic = std::is_arithmetic_v<T>;
constexpr bool is_integer()
{
return false;
}
// Reference: https://codereview.stackexchange.com/q/288432/231235
template<std::floating_point T>
constexpr bool is_integer(T input)
{
T integer_part;
return std::modf(input, &integer_part) == 0;
}
template<std::integral T>
constexpr bool is_integer(T input)
{
return true;
}
#ifdef USE_BOOST_MULTIDIMENSIONAL_ARRAY
// Multiplication
template<is_multiplicable T1, is_multiplicable T2>
constexpr auto element_wise_multiplication(const T1& input1, const T2& input2)
{
return input1 * input2;
}
template<is_multi_array T1, is_multi_array T2>
requires (is_multi_array_dimensionality_equal<T1, T2>)
constexpr auto element_wise_multiplication(const T1& input1, const T2& input2)
{
if (input1.num_dimensions() != input2.num_dimensions()) // Dimensions are different, unable to perform element-wise add operation
throw std::logic_error("Array dimensions are different");
if (*input1.shape() != *input2.shape()) // Shapes are different, unable to perform element-wise add operation
throw std::logic_error("Array shapes are different");
boost::multi_array output(input1); // drawback to be improved: avoiding copying whole input1 array into output, but with appropriate memory allocation
for (decltype(+input1.shape()[0]) i = 0; i < input1.shape()[0]; ++i)
output[i] = element_wise_multiplication(input1[i], input2[i]);
return output;
}
// Division
template<is_divisible T1, is_divisible T2>
constexpr auto element_wise_division(const T1& input1, const T2& input2)
{
if (input2 == 0)
throw std::logic_error("Divide by zero exception"); // Handle the case of dividing by zero exception
return input1 / input2;
}
template<is_multi_array T1, is_multi_array T2>
requires (is_multi_array_dimensionality_equal<T1, T2>)
constexpr auto element_wise_division(const T1& input1, const T2& input2)
{
if (input1.num_dimensions() != input2.num_dimensions()) // Dimensions are different, unable to perform element-wise add operation
throw std::logic_error("Array dimensions are different");
if (*input1.shape() != *input2.shape()) // Shapes are different, unable to perform element-wise add operation
throw std::logic_error("Array shapes are different");
boost::multi_array output(input1); // drawback to be improved: avoiding copying whole input1 array into output, but with appropriate memory allocation
for (decltype(+input1.shape()[0]) i = 0; i < input1.shape()[0]; ++i)
output[i] = element_wise_division(input1[i], input2[i]);
return output;
}
#endif
// recursive_depth function implementation
template<typename T>
constexpr std::size_t recursive_depth()
{
return std::size_t{0};
}
template<std::ranges::input_range Range>
constexpr std::size_t recursive_depth()
{
return recursive_depth<std::ranges::range_value_t<Range>>() + std::size_t{1};
}
// recursive_depth template function implementation with target type
template<typename T_Base, typename T>
constexpr std::size_t recursive_depth()
{
return std::size_t{0};
}
template<typename T_Base, std::ranges::input_range Range>
requires (!std::same_as<Range, T_Base>)
constexpr std::size_t recursive_depth()
{
return recursive_depth<T_Base, std::ranges::range_value_t<Range>>() + std::size_t{1};
}
// is_recursive_invocable template function implementation
template<std::size_t unwrap_level, class F, class... T>
requires(unwrap_level <= recursive_depth<T...>())
static constexpr bool is_recursive_invocable()
{
if constexpr (unwrap_level == 0) {
return std::invocable<F, T...>;
} else {
return is_recursive_invocable<
unwrap_level - 1,
F,
std::ranges::range_value_t<T>...>();
}
}
// recursive_invocable concept
template<std::size_t unwrap_level, class F, class... T>
concept recursive_invocable =
is_recursive_invocable<unwrap_level, F, T...>();
// is_recursive_project_invocable template function implementation
template<std::size_t unwrap_level, class Proj, class F, class... T>
requires(unwrap_level <= recursive_depth<T...>() &&
recursive_invocable<unwrap_level, Proj, T...>)
static constexpr bool is_recursive_project_invocable()
{
if constexpr (unwrap_level == 0) {
return std::invocable<F, std::invoke_result_t<Proj, T...>>;
} else {
return is_recursive_project_invocable<
unwrap_level - 1,
Proj,
F,
std::ranges::range_value_t<T>...>();
}
}
// recursive_project_invocable concept
template<std::size_t unwrap_level, class Proj, class F, class... T>
concept recursive_project_invocable =
is_recursive_project_invocable<unwrap_level, Proj, F, T...>();
/* recursive_all_of template function implementation with unwrap level
*/
template<std::size_t unwrap_level, class T, class Proj = std::identity, class UnaryPredicate>
requires( recursive_project_invocable<unwrap_level, Proj, UnaryPredicate, T>)
constexpr auto recursive_all_of(T&& value, UnaryPredicate&& p, Proj&& proj = {}) {
if constexpr (unwrap_level > 0)
{
return std::ranges::all_of(value, [&](auto&& element) {
return recursive_all_of<unwrap_level - 1>(element, p, proj);
});
}
else
{
return std::invoke(p, std::invoke(proj, value));
}
}
/* recursive_find template function implementation with unwrap level
*/
template<std::size_t unwrap_level, class R, class T, class Proj = std::identity>
requires(recursive_invocable<unwrap_level, Proj, R>)
constexpr auto recursive_find(R&& range, T&& target, Proj&& proj = {})
{
if constexpr (unwrap_level)
{
return std::ranges::find_if(range, [&](auto& element) {
return recursive_find<unwrap_level - 1>(element, target, proj);
}) != std::ranges::end(range);
}
else
{
return target == std::invoke(proj, range);
}
}
/* recursive_find template function implementation with unwrap level, execution policy
*/
template<std::size_t unwrap_level, class ExecutionPolicy, class R, class T, class Proj = std::identity>
requires(recursive_invocable<unwrap_level, Proj, R>&&
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>)
constexpr auto recursive_find(ExecutionPolicy execution_policy, R&& range, T&& target, Proj&& proj = {})
{
if constexpr (unwrap_level)
{
return std::find_if(execution_policy,
std::ranges::begin(range),
std::ranges::end(range),
[&](auto& element) {
return recursive_find<unwrap_level - 1>(execution_policy, element, target, proj);
}) != std::ranges::end(range);
}
else
{
return target == std::invoke(proj, range);
}
}
/* recursive_find_if template function implementation with unwrap level
*/
template<std::size_t unwrap_level, class T, class Proj = std::identity, class UnaryPredicate>
requires( recursive_project_invocable<unwrap_level, Proj, UnaryPredicate, T>)
constexpr auto recursive_find_if(T&& value, UnaryPredicate&& p, Proj&& proj = {}) {
if constexpr (unwrap_level > 0)
{
return std::ranges::find_if(value, [&](auto& element) {
return recursive_find_if<unwrap_level - 1>(element, p, proj);
}) != std::ranges::end(value);
}
else
{
return std::invoke(p, std::invoke(proj, value));
}
}
// recursive_any_of template function implementation with unwrap level
template<std::size_t unwrap_level, class T, class Proj = std::identity, class UnaryPredicate>
requires(recursive_project_invocable<unwrap_level, Proj, UnaryPredicate, T>)
constexpr auto recursive_any_of(T&& value, UnaryPredicate&& p, Proj&& proj = {})
{
return recursive_find_if<unwrap_level>(value, p, proj);
}
// recursive_none_of template function implementation with unwrap level
template<std::size_t unwrap_level, class T, class Proj = std::identity, class UnaryPredicate>
constexpr auto recursive_none_of(T&& value, UnaryPredicate&& p, Proj&& proj = {})
{
return !recursive_any_of<unwrap_level>(value, p, proj);
}
template<std::size_t index = 1, typename Arg, typename... Args>
constexpr static auto& get_from_variadic_template(const Arg& first, const Args&... inputs)
{
if constexpr (index > 1)
return get_from_variadic_template<index - 1>(inputs...);
else
return first;
}
// first_of template function implementation
template<typename... Args>
constexpr static auto& first_of(const Args&... inputs) {
return get_from_variadic_template<1>(inputs...);
}
template<std::size_t, typename, typename...>
struct get_from_variadic_template_struct { };
template<typename T1, typename... Ts>
struct get_from_variadic_template_struct<1, T1, Ts...>
{
using type = T1;
};
template<std::size_t index, typename T1, typename... Ts>
requires ( requires { typename get_from_variadic_template_struct<index - 1, Ts...>::type; })
struct get_from_variadic_template_struct<index, T1, Ts...>
{
using type = typename get_from_variadic_template_struct<index - 1, Ts...>::type;
};
template<std::size_t index, typename... Ts>
using get_from_variadic_template_t = typename get_from_variadic_template_struct<index, Ts...>::type;
// recursive_count implementation
// recursive_count implementation (the version with unwrap_level)
template<std::size_t unwrap_level, class T>
requires(unwrap_level <= recursive_depth<T>())
constexpr auto recursive_count(const T& input, const auto& target)
{
if constexpr (unwrap_level > 0)
{
return std::transform_reduce(std::ranges::cbegin(input), std::ranges::cend(input), std::size_t{}, std::plus<std::size_t>(), [&target](auto&& element) {
return recursive_count<unwrap_level - 1>(element, target);
});
}
else
{
return (input == target) ? 1 : 0;
}
}
// recursive_count implementation (the version with unwrap_level and execution policy)
template<class ExPo, std::size_t unwrap_level, class T>
requires(unwrap_level <= recursive_depth<T>() &&
std::is_execution_policy_v<std::remove_cvref_t<ExPo>>)
constexpr auto recursive_count(ExPo execution_policy, const T& input, const auto& target)
{
if constexpr (unwrap_level > 0)
{
return std::transform_reduce(std::ranges::cbegin(input), std::ranges::cend(input), std::size_t{}, std::plus<std::size_t>(), [&target](auto&& element) {
return recursive_count<unwrap_level - 1>(element, target);
});
}
else
{
return (input == target) ? 1 : 0;
}
}
// recursive_count_if implementation
template<class T, std::invocable<T> Pred>
constexpr std::size_t recursive_count_if(const T& input, const Pred& predicate)
{
return predicate(input) ? std::size_t{1} : std::size_t{0};
}
template<std::ranges::input_range Range, class Pred>
requires (!std::invocable<Pred, Range>)
constexpr auto recursive_count_if(const Range& input, const Pred& predicate)
{
return std::transform_reduce(std::ranges::cbegin(input), std::ranges::cend(input), std::size_t{}, std::plus<std::size_t>(), [predicate](auto&& element) {
return recursive_count_if(element, predicate);
});
}
template<std::size_t unwrap_level, class T, class Pred>
requires(unwrap_level <= recursive_depth<T>())
constexpr auto recursive_count_if(const T& input, const Pred& predicate)
{
if constexpr (unwrap_level > 0)
{
return std::transform_reduce(std::ranges::cbegin(input), std::ranges::cend(input), std::size_t{}, std::plus<std::size_t>(), [predicate](auto&& element) {
return recursive_count_if<unwrap_level - 1>(element, predicate);
});
}
else
{
return predicate(input) ? 1 : 0;
}
}
// recursive_count_if implementation (with execution policy)
template<class ExPo, class T, std::invocable<T> Pred>
requires (std::is_execution_policy_v<std::remove_cvref_t<ExPo>>)
constexpr std::size_t recursive_count_if(ExPo execution_policy, const T& input, const Pred& predicate)
{
return predicate(input) ? std::size_t{1} : 0;
}
template<class ExPo, std::ranges::input_range Range, class Pred>
requires ((std::is_execution_policy_v<std::remove_cvref_t<ExPo>>) && (!std::invocable<Pred, Range>))
constexpr auto recursive_count_if(ExPo execution_policy, const Range& input, const Pred& predicate)
{
return std::transform_reduce(execution_policy, std::ranges::cbegin(input), std::ranges::cend(input), std::size_t{}, std::plus<std::size_t>(), [predicate](auto&& element) {
return recursive_count_if(element, predicate);
});
}
// recursive_count_if implementation (the version with unwrap_level)
template<std::size_t unwrap_level = 1, std::ranges::range T, class Pred>
requires(unwrap_level <= recursive_depth<T>())
constexpr auto recursive_count_if(const T& input, const Pred& predicate)
{
if constexpr (unwrap_level > 1)
{
return std::transform_reduce(std::ranges::cbegin(input), std::ranges::cend(input), std::size_t{}, std::plus<std::size_t>(), [predicate](auto&& element) {
return recursive_count_if<unwrap_level - 1>(element, predicate);
});
}
else
{
return std::count_if(std::ranges::cbegin(input), std::ranges::cend(input), predicate);
}
}
// batch_recursive_count_if implementation (the version with unwrap_level)
template<std::size_t unwrap_level = 1, std::ranges::range T, class Pred1>
requires(unwrap_level <= recursive_depth<T>())
constexpr auto batch_recursive_count_if(const T& input, const Pred1& predicate1)
{
std::vector<decltype(recursive_count_if<unwrap_level>(input, predicate1))> output;
output.push_back(recursive_count_if<unwrap_level>(input, predicate1));
return output;
}
template<std::size_t unwrap_level = 1, std::ranges::range T, class Pred1, class... Preds>
requires(unwrap_level <= recursive_depth<T>())
constexpr auto batch_recursive_count_if(const T& input, const Pred1& predicate1, const Preds&... predicates)
{
auto output1 = batch_recursive_count_if<unwrap_level>(input, predicate1);
auto output2 = batch_recursive_count_if<unwrap_level>(input, predicates...);
output1.insert(std::ranges::cend(output1), std::ranges::cbegin(output2), std::ranges::cend(output2));
return output1;
}
// recursive_max template function implementation
template<class T, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<const T*, Proj>> Comp = std::ranges::less>
requires(!std::ranges::input_range<T>) // non-range overloading
static inline T recursive_max(T inputNumber, Comp comp = {}, Proj proj = {})
{
return std::invoke(proj, inputNumber);
}
template<std::ranges::input_range T, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<const T*, Proj>> Comp = std::ranges::less>
static inline auto recursive_max(const T& numbers, Comp comp = {}, Proj proj = {})
{
auto output = recursive_max(numbers.at(0), comp, proj);
for (auto& element : numbers)
{
output = std::ranges::max(
output,
recursive_max(element, comp, proj),
comp,
proj);
}
return output;
}
// recursive_min template function implementation
template<class T, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<const T*, Proj>> Comp = std::ranges::less>
requires(!std::ranges::input_range<T>) // non-range overloading
static inline T recursive_min(T inputNumber, Comp comp = {}, Proj proj = {})
{
return std::invoke(proj, inputNumber);
}
template<std::ranges::input_range T, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<const T*, Proj>> Comp = std::ranges::less>
static inline auto recursive_min(const T& numbers, Comp comp = {}, Proj proj = {})
{
auto output = recursive_min(numbers.at(0), comp, proj);
for (auto& element : numbers)
{
output = std::ranges::min(
output,
recursive_min(element, comp, proj),
comp,
proj);
}
return output;
}
// recursive_minmax template function implementation
// Reference: https://codereview.stackexchange.com/q/288208/231235
template<class T, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<const T*, Proj>> Comp = std::ranges::less>
requires(!(std::ranges::forward_range<T>)) // non-range overloading
constexpr auto recursive_minmax(const T& input, Comp comp = {}, Proj proj = {})
{
return input;
}
template<std::ranges::forward_range T, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<const T*, Proj>> Comp = std::ranges::less>
constexpr auto recursive_minmax(const T& numbers, Comp comp = {}, Proj proj = {})
{
return std::make_pair(
recursive_min(numbers, comp, proj),
recursive_max(numbers, comp, proj)
);
}
// recursive_print implementation
template<typename T>
constexpr void recursive_print(const T& input, const std::size_t level = 0)
{
std::cout << std::string(level, ' ') << input << '\n';
}
template<std::ranges::input_range Range>
constexpr void recursive_print(const Range& input, const std::size_t level = 0)
{
std::cout << std::string(level, ' ') << "Level " << level << ":" << std::endl;
std::ranges::for_each(input, [level](auto&& element) {
recursive_print(element, level + 1);
});
}
// recursive_size template function implementation
template<class T> requires (!std::ranges::range<T>)
constexpr auto recursive_size(const T& input)
{
return std::size_t{1};
}
template<std::ranges::range Range> requires (!(std::ranges::input_range<std::ranges::range_value_t<Range>>))
constexpr auto recursive_size(const Range& input)
{
return std::ranges::size(input);
}
template<std::ranges::range Range> requires (std::ranges::input_range<std::ranges::range_value_t<Range>>)
constexpr auto recursive_size(const Range& input)
{
return std::transform_reduce(std::ranges::begin(input), std::ranges::end(input), std::size_t{}, std::plus<std::size_t>(), [](auto& element) {
return recursive_size(element);
});
}
// recursive_size template function implementation (the version with unwrap_level)
template<std::size_t unwrap_level, std::ranges::range T>
requires(unwrap_level <= recursive_depth<T>())
constexpr auto recursive_size(const T input)
{
if constexpr (unwrap_level > 1)
{
return recursive_size<unwrap_level - 1>(input.at(0));
}
else
{
return std::ranges::size(input);
}
}
/* recursive_reduce_all template function performs operation on input container exhaustively
*/
template<arithmetic T>
constexpr auto recursive_reduce_all(const T& input)
{
return input;
}
// recursive_reduce template function implementation
// Reference: https://codereview.stackexchange.com/a/251310/231235
template<class T, class ValueType, class Function = std::plus<ValueType>>
requires(std::regular_invocable<Function, ValueType, T>)
constexpr auto recursive_reduce(const T& input, ValueType init, const Function& f)
{
return std::invoke(f, init, input);
}
template<std::ranges::range Container, class ValueType, class Function = std::plus<ValueType>>
constexpr auto recursive_reduce(const Container& input, ValueType init, const Function& f = std::plus<ValueType>())
{
for (const auto& element : input) {
init = recursive_reduce(element, init, f);
}
return init;
}
template<typename T>
concept is_recursive_reduceable = requires(T x)
{
recursive_reduce(x, T{});
};
template<typename T>
concept is_recursive_sizeable = requires(T x)
{
recursive_size(x);
};
// arithmetic_mean implementation
template<class T = double, is_recursive_sizeable Container>
constexpr auto arithmetic_mean(const Container& input)
{
if (recursive_size(input) == 0) // Check the case of dividing by zero exception
{
throw std::logic_error("Divide by zero exception"); // Handle the case of dividing by zero exception
}
return (recursive_reduce(input, T{})) / (recursive_size(input));
}
// recursive_for_each function implementation
template<std::size_t unwrap_level = 1, class UnaryFunction, typename Range>
requires(unwrap_level <= recursive_depth<Range>())
constexpr UnaryFunction recursive_for_each(UnaryFunction op, Range& input)
{
if constexpr (unwrap_level > 1)
{
std::for_each(
std::ranges::begin(input),
std::ranges::end(input),
[&](auto&& element) { return recursive_for_each<unwrap_level - 1>(op, element); }
);
return op;
}
else
{
std::for_each(
std::ranges::cbegin(input),
std::ranges::cend(input),
op);
return op;
}
}
// recursive_for_each function implementation (with execution policy)
template<std::size_t unwrap_level = 1, class ExPo, class UnaryFunction, typename Range>
requires (unwrap_level <= recursive_depth<Range>() &&
std::is_execution_policy_v<std::remove_cvref_t<ExPo>>)
constexpr UnaryFunction recursive_for_each(ExPo execution_policy, UnaryFunction op, Range& input)
{
if constexpr (unwrap_level > 1)
{
std::for_each(
execution_policy,
std::ranges::begin(input),
std::ranges::end(input),
[&](auto&& element) { return recursive_for_each<unwrap_level - 1>(execution_policy, op, element); }
);
return op;
}
else
{
std::for_each(
execution_policy,
std::ranges::cbegin(input),
std::ranges::cend(input),
op);
return op;
}
}
namespace impl {
template<class F, class Proj = std::identity>
struct recursive_for_each_state {
F f;
Proj proj;
};
// recursive_foreach_all template function implementation
template<class T, class State>
requires (recursive_depth<T>() == 0)
constexpr void recursive_foreach_all(T& value, State& state) {
std::invoke(state.f, std::invoke(state.proj, value));
}
template<class T, class State>
requires (recursive_depth<T>() != 0)
constexpr void recursive_foreach_all(T& inputRange, State& state) {
for (auto& item: inputRange)
impl::recursive_foreach_all(item, state);
}
// recursive_reverse_foreach_all template function implementation
template<class T, class State>
requires (recursive_depth<T>() == 0)
constexpr void recursive_reverse_foreach_all(T& value, State& state) {
std::invoke(state.f, std::invoke(state.proj, value));
}
template<class T, class State>
requires (recursive_depth<T>() != 0)
constexpr void recursive_reverse_foreach_all(T& inputRange, State& state) {
for (auto& item: inputRange | std::views::reverse)
impl::recursive_reverse_foreach_all(item, state);
}
}
template<class T, class Proj = std::identity, class F>
constexpr auto recursive_reverse_foreach_all(T& inputRange, F f, Proj proj = {})
{
impl::recursive_for_each_state state(std::move(f), std::move(proj));
impl::recursive_reverse_foreach_all(inputRange, state);
return std::make_pair(inputRange.end(), std::move(state.f));
}
// recursive_fold_right_all template function implementation
// https://codereview.stackexchange.com/q/287842/231235
template<class T, class I, class F>
constexpr auto recursive_fold_right_all(const T& inputRange, I init, F f)
{
recursive_reverse_foreach_all(inputRange, [&](auto& value) {
init = std::invoke(f, value, init);
});
return init;
}
// recursive_replace_copy_if template function implementation
template<std::size_t unwrap_level = 1, std::ranges::input_range Range, class UnaryPredicate, class T>
requires(unwrap_level <= recursive_depth<Range>() &&
(recursive_invocable<unwrap_level, UnaryPredicate, Range>))
constexpr auto recursive_replace_copy_if(const Range& input, const UnaryPredicate& unary_predicate, const T& new_value)
{
if constexpr(unwrap_level == 1)
{
Range output{};
std::ranges::replace_copy_if(
std::ranges::cbegin(input),
std::ranges::cend(input),
std::inserter(output, std::ranges::end(output)),
unary_predicate,
new_value);
return output;
}
else
{
Range output{};
std::ranges::transform(
std::ranges::cbegin(input),
std::ranges::cend(input),
std::inserter(output, std::ranges::end(output)),
[&](auto&& element) { return recursive_replace_copy_if<unwrap_level - 1>(element, unary_predicate, new_value); }
);
return output;
}
}
// recursive_remove_copy_if function implementation with unwrap level
template <std::size_t unwrap_level, std::ranges::input_range Range, class UnaryPredicate>
requires(recursive_invocable<unwrap_level, UnaryPredicate, Range> &&
is_inserterable<Range> &&
unwrap_level > 0 &&
unwrap_level <= recursive_depth<Range>())
constexpr auto recursive_remove_copy_if(const Range& input, const UnaryPredicate& unary_predicate)
{
if constexpr(unwrap_level > 1)
{
Range output{};
std::ranges::transform(
std::ranges::cbegin(input),
std::ranges::cend(input),
std::inserter(output, std::ranges::end(output)),
[&unary_predicate](auto&& element) { return recursive_remove_copy_if<unwrap_level - 1>(element, unary_predicate); }
);
return output;
}
else
{
Range output{};
std::ranges::remove_copy_if(std::ranges::cbegin(input), std::ranges::cend(input),
std::inserter(output, std::ranges::end(output)),
unary_predicate);
return output;
}
}
// recursive_remove_copy_if function implementation with unwrap level, execution policy
template<std::size_t unwrap_level, class ExPo, std::ranges::input_range Range, class UnaryPredicate>
requires(std::is_execution_policy_v<std::remove_cvref_t<ExPo>> &&
recursive_invocable<unwrap_level, UnaryPredicate, Range> &&
is_inserterable<Range> &&
unwrap_level > 0 &&
unwrap_level <= recursive_depth<Range>())
constexpr auto recursive_remove_copy_if(ExPo execution_policy, const Range& input, const UnaryPredicate& unary_predicate)
{
if constexpr(unwrap_level > 1)
{
Range output{};
std::ranges::transform(
std::ranges::cbegin(input),
std::ranges::cend(input),
std::inserter(output, std::ranges::end(output)),
[&](auto&& element) {
return recursive_remove_copy_if<unwrap_level - 1>(execution_policy, element, unary_predicate);
}
);
return output;
}
else
{
Range output{};
std::remove_copy_if(execution_policy, std::ranges::cbegin(input), std::ranges::cend(input),
std::inserter(output, std::ranges::end(output)),
unary_predicate);
return output;
}