C++ Library Extensions 2022.12.09
To help learn modern C++ programming
019-build_permutations.cpp
Go to the documentation of this file.
1#include <tpf_set.hpp>
3#include <tpf_ncrnpr.hpp>
4#include <tpf_output.hpp>
5
6namespace types = tpf::types;
7namespace set = tpf::set;
8namespace crs = tpf::chrono_random;
9namespace cpr = tpf::ncrnpr;
10
12
14{
15
16 using set_tag = tpf::set_tag<std::vector, char>;
19
21 stream <<"Going in parallel ..." << tpf::endl;
22 auto permutations = set::build_permutations(thread_count, n, r);
23 stream << "Elapsed: " << sw.elapsed_time<crs::second_t>() << tpf::endl;
24 stream << permutations << tpf::endl;
25
26 stream <<"Going in sequence ..." << tpf::endl;
27 sw.reset();
28 permutations = set::build_permutations(n, r);
29 stream << "Elapsed: " << sw.elapsed_time<crs::second_t>() << tpf::endl;
30 stream << permutations << tpf::endl;
31
32}
33
35{
36
37 using set_tag = tpf::set_tag<std::vector, char>;
40
42 stream <<"Going in parallel ..." << tpf::endl;
44 stream << "Elapsed: " << sw.elapsed_time<crs::second_t>() << tpf::endl;
45
46 stream <<"Going in sequence ..." << tpf::endl;
48 stream << "Elapsed: " << sw.elapsed_time<crs::second_t>() << tpf::endl;
49
50}
51
52int main(int argc, char* argv[])
53{
54 if(argc == 4)
55 {
56 auto thread_count = atoi(argv[1]) * std::thread::hardware_concurrency();
57 auto n = atoi(argv[2]);
58 auto r = atoi(argv[3]);
59
60 try
61 {
62 auto permu = cpr::npr(n, r);
63 stream << "Computing "<< n << "_P_" << r <<" = "
64 << permu << " permutations..."<<tpf::endl;
65
66 // examples_for_build_permutations_flat(thread_count, n, r);
68 }
69 catch(const std::exception&)
70 {
71 stream << "Number too big!" << tpf::endl;
72 }
73 }
74 else
75 {
76 stream << argv[0] <<" n r" << tpf::endl;
77 }
78}
int main(int argc, char *argv[])
tpf::sstream stream
void examples_for_build_permutations_flat(unsigned thread_count, int n, int r)
void examples_for_build_permutations(unsigned thread_count, int n, int r)
std::deque< element_t > set_t
Definition: 061-subsets.cpp:9
std::deque< set_t > sets_t
Definition: 061-subsets.cpp:10
std::string elapsed_time(bool bReset=true, TimeUnit dummy_time=TimeUnit{}) const
Implements random number generator and stop watch.
std::ratio< 1 > second_t
Implements nCr, nPr.
Definition: tpf_ncrnpr.hpp:64
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
void build_permutations(ContainerType< EleType, Types... > &L, ContainerType< EleType, Types... > &R, size_t r)
Definition: tpf_set.hpp:1015
Implements set operations.
Definition: tpf_set.hpp:52
auto build_permutations_flat(ThreadCountType thread_count, NType n, RType r)
Definition: tpf_set.hpp:1200
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
typename SetTagType::set_t set_t
Definition: tpf_types.hpp:1966
constexpr auto endl
Definition: tpf_output.hpp:973
typename SetTagType::sets_t sets_t
Definition: tpf_types.hpp:1969
std::atomic< int > thread_count
Stream output operators << are implemented.
Implements set operations.