C++ Library Extensions 2022.12.09
To help learn modern C++ programming
018-permutation.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
11 auto nl = tpf::nl;
12 auto endl = tpf::endl;
13
14 using number_t = cmb::permutation::number_t;
15
16 number_t n = 11;
17 number_t r = 10;
18 auto max_mth = cmb::npr(n,r);
19
20 stream << n<<"P"<<r<<" = " << max_mth << endl;
21
23 for(number_t m_th = 0; m_th < max_mth; ++m_th)
24 {
25 cmb::enum_permutation(n, r, m_th) ;
26 }
27
28 auto v_time = sw.elapsed();
29 stream <<"using vector: " << v_time << endl;
30
31 for(number_t m_th = 0; m_th < max_mth; ++m_th)
32 {
33 cmb::enum_permutation(n, r, m_th) ;
34 }
35
36 auto l_time = sw.elapsed();
37 stream <<"using list: " << l_time << endl;
38}
39
41{
42 namespace cmb = tpf::ncrnpr;
43 namespace crs = tpf::chrono_random;
44
46 auto nl = tpf::nl;
47 auto endl = tpf::endl;
48
49 auto range = cmb::split_range_count(3, 0, 11);
50
51 stream << range << endl;
52}
53
55{
56 namespace cmb = tpf::ncrnpr;
57 cmb::range_t range{1, 10};
58
61
63 {
64 std::unique_lock<std::mutex> lock(mutex);
66 });
67}
68
70{
71 namespace cmb = tpf::ncrnpr;
72 namespace crs = tpf::chrono_random;
73
75 auto nl = tpf::nl;
76 auto endl = tpf::endl;
77
78 using number_t = cmb::permutation::number_t;
79
80 number_t max_mth = cmb::npr(n,r);
81
83
84 auto permu_handler = [&stream, &mutex](auto permu)
85 {
86 // std::unique_lock<std::mutex> lock(mutex);
87 // stream << permu << " - " << std::this_thread::get_id() << tpf::endl;
88 };
89
91
92 stream <<"Enumerating in parallel visit ... " << tpf::endl;
93
94 tpf::parallel_visit_permutations({n, r}, permu_handler);
95
96 stream <<"Elapsed: " << sw.elapsed_time<crs::second_t>() << endl << endl;
97
98 #if defined(_MSC_VER)
99 stream <<"Enumerating in visit parallel ... " << tpf::endl;
100 tpf::visit_permutations_parallel(factor, n, r, permu_handler);
101 stream <<"Elapsed: " << sw.elapsed_time<crs::second_t>() << endl << endl;
102 stream << "Enumerating in sequence ..." << tpf::endl;
103 #endif
104
105 auto p = cmb::npr(n-1, r-1);
106
107 for(number_t m_th = 0; m_th < max_mth; ++m_th)
108 {
109 permu_handler(cmb::enum_permutation_static(p, n, r, m_th));
110 }
111 stream <<"Elapsed: " << sw.elapsed_time<crs::second_t>() << endl;
112
113}
114
115int main(int argc, char** argv)
116{
117 unsigned int thread_count =
118 std::thread::hardware_concurrency();
119
120 int n, r;
121
123
124 if(argc > 3)
125 {
126 thread_count *= atoi(argv[1]);
127 n = atoi(argv[2]); r = atoi(argv[3]);
128
129 using number_t = tpf::ncrnpr::permutation::number_t;
130 number_t permu;
131
132 try
133 {
134 permu = tpf::ncrnpr::npr(n, r);
135 }
136 catch(const std::exception&)
137 {
138 stream << "Number Too Big"<< tpf::endl;
139 return 0;
140 }
141
142 stream << "Enumerating " << n << "_P_" <<r
143 <<" = " << permu <<" permutations with "
144 << thread_count << " threads" << tpf::endl;
146 }
147 else
148 {
149 std::cout <<"Usage: " << argv[0] << std::endl;
150 std::cout <<"> " << argv[0] << " thread_count_factor n r" <<std::endl;
151 std::cout <<"\tthread_count = hardware_thread x thread_count_factor" <<std::endl;
152 std::cout <<"\tn P r" << std::endl;
153 }
154}
auto nl
int main(int argc, char **argv)
void test_for_each()
void test_split_range()
void performance_test_for_permutations()
void performance_test_for_permutations_parallel(int factor, int n, int r)
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
auto elapsed(bool bReset=true) const
Implements random number generator and stop watch.
std::ratio< 1 > second_t
Implements nCr, nPr.
Definition: tpf_ncrnpr.hpp:64
range_vector_t split_range_count(CountType count, StartType st, EndType ed)
Definition: tpf_ncrnpr.hpp:225
enable_if_all_in_list_t< types::type_list_t< Type1, Type2 >, integral_list_t, ReturnType > npr(Type1 nn, Type2 rr)
Definition: tpf_ncrnpr.hpp:428
std::vector< T > enum_permutation(T n, T r, T m_th)
std::vector< NRType > enum_permutation_static(PType permu, NRType n, NRType r, MType m_th)
range< size_t > range_t
std::pair< T, T > range
void parallel_visit_permutations(const std::pair< size_t, size_t > permutation_specification, CallbackType &&callback, ArgTypes &&... args)
void visit_permutations_parallel(TCType thread_count, NRType n, NRType r, CallbackType &&callback, ArgTypes &&... args)
Calls callback(enumerated_permutation, args...) in parallel.
constexpr auto endl
Definition: tpf_output.hpp:973
constexpr auto nl
Definition: tpf_output.hpp:971
constexpr int factor
std::atomic< int > thread_count
Stream output operators << are implemented.