Go to the documentation of this file. 1 #ifndef TPCC_COMBINATIONS_H
2 #define TPCC_COMBINATIONS_H
16 template <
typename T =
unsigned int>
24 for (T i = 1; i <= k; ++i)
39 template <
typename T =
unsigned int, T n, T k>
42 std::array<
unsigned int, n - k> result{};
43 unsigned int vpos = 0;
44 unsigned int current = n - 1;
46 for (
unsigned int i = 0; i < n - k; ++i, --current)
48 while (vpos < k && current == combi[vpos])
61 template <
int n,
int k,
typename T =
unsigned int>
70 Combination(
const std::array<T, k>& combi,
const std::array<T, n - k>& comp)
79 T
in(
unsigned int i)
const {
return data[i]; }
84 T
out(
unsigned int i)
const {
return cdata[i]; }
101 std::array<T, k - 1> outdata{};
102 for (
unsigned int j = 0; j < i; ++j)
103 outdata[j] =
data[j];
104 const T tmp =
data[i];
105 for (
unsigned int j = i; j < k - 1; ++j)
106 outdata[j] =
data[j + 1];
108 std::array<T, n - k + 1> outcdata{};
109 unsigned int jj = 0, j = 0;
110 for (; j < n - k; ++j, ++jj)
112 if (jj == j &&
cdata[j] < tmp)
117 outcdata[jj] =
cdata[j];
120 outcdata[n - k] = tmp;
129 template <
int kk = k>
130 constexpr
typename std::enable_if<(kk < n), Combination<n, k + 1, T>>::type
add(
131 unsigned int i)
const
133 std::array<T, k + 1> outdata{};
135 for (; j < k; ++j, ++jj)
137 if (jj == j &&
data[j] <= i)
139 assert(
data[j] != i);
143 outdata[jj] =
data[j];
148 std::array<T, n - k - 1> outcdata{};
151 for (; j < n - k - 1; ++j, ++jj)
155 outcdata[j] =
cdata[jj];
163 template <
int kk = k>
164 constexpr
typename std::enable_if<(kk <= n), Combination<n + 1, k + 1, T>>::type
add_and_expand(
165 unsigned int i)
const
167 std::array<T, n + 1 - k> outcdata{};
178 for (
int i = 0; i < k; ++i)
181 for (
int i = 0; i < n - k; ++i)
194 template <
int n,
int k>
200 static constexpr
unsigned int size();
211 static std::array<unsigned int, k>
value(
unsigned int index);
220 static std::array<
unsigned int, n - k>
dual(
unsigned int index);
225 template <
typename T>
233 template <
unsigned int size,
typename... I>
234 static constexpr std::array<unsigned int, k>
compute_value(
unsigned int index, I... args);
239 template <
int n,
int k>
245 template <
int n,
int k>
246 template <
unsigned int sz,
typename... I>
252 if constexpr (sz == 1)
253 return std::array<unsigned int, k>{ args...,
index };
256 unsigned int cs = sz - 1;
258 for (; cs <= n; ++cs)
266 return compute_value<sz - 1>(
index - bn, args..., cs);
270 template <
int n,
int k>
271 template <
typename T>
274 unsigned int result = 0;
276 for (
unsigned int i = 0; i < k; ++i)
283 template <
int n,
int k>
286 if constexpr (k == 0)
289 return std::array<unsigned int, 0>();
292 return compute_value<k>(
index);
295 template <
int n,
int k>
298 if constexpr (k == 0)
300 auto v = value(
index);
301 return compute_complement<unsigned int, n, k>(v);
304 template <
int n,
int k>
constexpr std::array< T, n - k > compute_complement(std::array< T, k > combi)
Definition: combinations.h:40
constexpr std::enable_if<(kk > 0), Combination< n, k - 1, T > >::type eliminate(unsigned int i) const
The combination obtained by eliminating the ith element.
Definition: combinations.h:98
std::array< T, k > data
The array of members.
Definition: combinations.h:65
index
Definition: check_push_test.py:10
void print_debug(std::ostream &os) const
Print the content of this object for debugging.
Definition: combinations.h:176
and that you are informed that you can do these things To protect your we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it For if you distribute copies of the whether gratis or for a you must give the recipients all the rights that we gave you You must make sure that receive or can get the source code If you link other code with the you must provide complete object files to the so that they can relink them with the library after making changes to the library and recompiling it And you must show them these terms so they know their rights We protect your rights with a two step which gives you legal permission to copy
Definition: License.txt:50
static constexpr unsigned int index(const Combination< n, k, T > &combi)
The index of a combination within the lexicographic enumeration.
Definition: combinations.h:272
Combination(const std::array< T, k > &combi, const std::array< T, n - k > &comp)
Definition: combinations.h:70
std::array< T, n - k > cdata
The array of non-members.
Definition: combinations.h:67
constexpr std::enable_if<(kk< n), Combination< n, k+1, T > >::type add(unsigned int i) const
The combination obtained by adding the element i.
Definition: combinations.h:130
Definition: combinations.h:8
Combination< n, n - k, T > complement() const
Return the complement of this combination.
Definition: combinations.h:89
constexpr std::enable_if<(kk<=n), Combination< n+1, k+1, T > >::type add_and_expand(unsigned int i) const
The combination out of n+1 obtained by adding one element.
Definition: combinations.h:164
static std::array< unsigned int, n - k > dual(unsigned int index)
The array of numbers (of length n-k) of numbers not in the combination of the given index.
Definition: combinations.h:296
static constexpr unsigned int size()
The number of such combinations.
Definition: combinations.h:240
T in(unsigned int i) const
The ith element which is part of the combination in descending order.
Definition: combinations.h:79
Dataset for a combination k out of n.
Definition: combinations.h:62
T out(unsigned int i) const
The ith element which is not part of the combination in descending order.
Definition: combinations.h:84
static std::array< unsigned int, k > value(unsigned int index)
The array of numbers (of length k) in the combination with given index.
Definition: combinations.h:284
static constexpr std::array< unsigned int, k > compute_value(unsigned int index, I... args)
The function template computing the combination in lexicographic ordering recursively.
Definition: combinations.h:247
Definition: combinations.h:195
constexpr T binomial(T n, T k)
Compute the binomial coefficient n over k.
Definition: combinations.h:17
constexpr Combination< n, k > operator[](unsigned int)
A boolean array of length n with a true for each selected value.
Definition: combinations.h:305