C++ Library Extensions 2022.12.09
To help learn modern C++ programming
018-combination.cpp
Go to the documentation of this file.
1#include <tpf_ncrnpr.hpp>
2#include <tpf_output.hpp>
4
6{
7 namespace cmb = tpf::ncrnpr;
8 namespace crs = tpf::chrono_random;
9
12
13 auto nl = tpf::nl;
14 auto endl = tpf::endl;
15
16 using number_t = cmb::combination::number_t;
17
18 auto max_mth = cmb::ncr(n,r);
19
20 auto combination_handler = [&stream, &mutex](auto&& combination)
21 {
22 // std::unique_lock<std::mutex> lock{mutex};
23 // stream << combination << tpf::endl;
24 };
25
27 stream <<"Enumerating in parallel with selected only ... " << tpf::endl;
28 tpf::parallel_visit_combinations(n, r, combination_handler);
29 stream <<"Elapsed: " << sw.elapsed_time<crs::second_t>() << endl << endl;
30
31 stream <<"Enumerating in parallel with complement ... " << tpf::endl;
32 tpf::parallel_visit_combinations_and_complements(n, r, combination_handler);
33 stream <<"Elapsed: " << sw.elapsed_time<crs::second_t>() << endl << endl;
34
35 stream << "Enumerating in sequence ..." << tpf::endl;
36 for(number_t m_th = 0; m_th < max_mth; ++m_th)
37 {
38 combination_handler(cmb::enumerate_combination_and_complement(m_th, n, r));
39 }
40 stream <<" Elapsed: " << sw.elapsed_time<crs::second_t>() << endl;
41
42 }
43
44int main(int argc, char** argv)
45{
46 unsigned int thread_count =
47 std::thread::hardware_concurrency();
48
49 int n, r;
50
52
53 if(argc > 3)
54 {
55 thread_count *= atoi(argv[1]);
56 n = atoi(argv[2]); r = atoi(argv[3]);
57
59
60 try
61 {
62 cmb = tpf::ncrnpr::ncr(n, r);
63 }
64 catch(const std::exception&)
65 {
66 stream << "Number Too Big!" << tpf::endl;
67 return 0;
68 }
69
70 stream << "Enumerating " << n << "_C_" <<r
71 <<" = " << cmb
72 <<" combinations with " << thread_count
73 << " threads" << tpf::endl;
74
76 }
77 else
78 {
79 std::cout <<"Usage: " << argv[0] << std::endl;
80 std::cout <<"> " << argv[0] << " thread_count_factor n r" <<std::endl;
81 std::cout <<"\tthread_count = hardware_thread x thread_count_factor" <<std::endl;
82 std::cout <<"\tn P r" << std::endl;
83 }
84}
auto nl
void performance_test_for_combination_parallel(unsigned int thread_count, int n, int r)
int main(int argc, char **argv)
std::mutex mutex
Definition: 022-mutex.cpp:12
auto & cout
auto & endl
tpf::sstream stream
std::string elapsed_time(bool bReset=true, TimeUnit dummy_time=TimeUnit{}) const
big_unsigned_t number_t
Definition: tpf_ncrnpr.hpp:460
Implements random number generator and stop watch.
std::ratio< 1 > second_t
Implements nCr, nPr.
Definition: tpf_ncrnpr.hpp:64
ReturnType enumerate_combination_and_complement(combination cmb, MthType m_th, SetContainer< EleType, ContainerTails... > from_set, SelectCountType select_count)
Definition: tpf_ncrnpr.hpp:792
enable_if_all_in_list_t< types::type_list_t< Type1, Type2 >, integral_list_t, ReturnType > ncr(Type1 nn, Type2 rr)
Definition: tpf_ncrnpr.hpp:390
void parallel_visit_combinations_and_complements(NRType n, NRType r, CallbackType &&callback, ArgTypes &&... args)
void parallel_visit_combinations(NRType n, NRType r, CallbackType &&callback, ArgTypes &&... args)
constexpr auto endl
Definition: tpf_output.hpp:973
constexpr auto nl
Definition: tpf_output.hpp:971
std::atomic< int > thread_count
Stream output operators << are implemented.