Samchon Framework for CPP  1.0.0
PermutationGenerator.hpp
1 #pragma once
2 #include <samchon/library/CaseGenerator.hpp>
3 
4 #define TEMPLATE_FACTORIAL_CASE_MACRO(N) case N: size_ = factorial<N>::value; break;
5 
6 namespace samchon
7 {
8 namespace library
9 {
21  : public CaseGenerator
22  {
23  private:
24  typedef CaseGenerator super;
25 
26  std::vector<size_t> atoms;
27 
28  template<const size_t N> struct factorial
29  {
30  enum { value = N * factorial<N - 1>::value };
31  };
32  template<> struct factorial<1>
33  {
34  enum {value = 1};
35  };
36 
37  public:
43  PermutationGenerator(size_t n, size_t r)
44  : super(n, r)
45  {
46  if (n == r)
47  if (n <= 15)
48  switch (n)
49  {
50  TEMPLATE_FACTORIAL_CASE_MACRO(1)
51  TEMPLATE_FACTORIAL_CASE_MACRO(2)
52  TEMPLATE_FACTORIAL_CASE_MACRO(3)
53  TEMPLATE_FACTORIAL_CASE_MACRO(4)
54  TEMPLATE_FACTORIAL_CASE_MACRO(5)
55  TEMPLATE_FACTORIAL_CASE_MACRO(6)
56  TEMPLATE_FACTORIAL_CASE_MACRO(7)
57  TEMPLATE_FACTORIAL_CASE_MACRO(8)
58  TEMPLATE_FACTORIAL_CASE_MACRO(9)
59  TEMPLATE_FACTORIAL_CASE_MACRO(10)
60  TEMPLATE_FACTORIAL_CASE_MACRO(11)
61  TEMPLATE_FACTORIAL_CASE_MACRO(12)
62  TEMPLATE_FACTORIAL_CASE_MACRO(13)
63  TEMPLATE_FACTORIAL_CASE_MACRO(14)
64  TEMPLATE_FACTORIAL_CASE_MACRO(15)
65  }
66  else
67  {
68  size_ = factorial<15>::value;
69  for (size_t i = 16; i < n; i++)
70  size_ *= i;
71  }
72  else
73  {
74  size_ = 1;
75  for (size_t i = n - r + 1; i <= n; i++)
76  size_ *= i;
77  }
78 
79  atoms.assign(n, 0);
80  for (size_t i = 0; i < n; i++)
81  atoms[i] = i;
82  };
83  virtual ~PermutationGenerator() = default;
84 
85  virtual auto operator[](size_t index) const->std::vector<size_t> override
86  {
87  std::vector<size_t> atoms = this->atoms;
88  std::vector<size_t> row(r_, NULL);
89 
90  for (size_t i = 0; i < row.size(); i++)
91  {
92  size_t item = index % atoms.size();
93  index = index / atoms.size();
94 
95  row[i] = atoms[item];
96  atoms.erase(atoms.begin() + item);
97  }
98  return row;
99  };
100  };
101 };
102 };
size_t size_
Size, the number of all cases.
size_t r_
R, size of elements of each case.
PermutationGenerator(size_t n, size_t r)
Construct from size of N and R.
virtual auto operator[](size_t index) const -> std::vector< size_t > override
Get x&#39;th case.
auto r() const -> size_t
Get size of the R.
auto n() const -> size_t
Get size of the N.