-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcombinations.hpp
57 lines (45 loc) · 1.04 KB
/
combinations.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once
#include <vector>
#include <cstddef>
// http://stackoverflow.com/questions/9430568/generating-combinations-in-c
namespace sweet {
template<typename RAC = typename std::vector<int>>
struct Combinations {
//typedef std::vector<int> Combination;
typedef RAC Combination;
// initialize status
inline Combinations(int nN, int nR) : completed(nN < 1 || nR > nN), generated(0),
N(nN), R(nR)
{
for(int c = 0; c < nR; ++c) {
curr.push_back(c);
}
}
// true while there are more solutions
bool completed;
// count how many generated
int generated;
// get current and compute next combination
inline Combination next() {
Combination ret = curr;
// find what to increment
completed = true;
for(int i = R - 1; i >= 0; --i) {
if(curr[static_cast<size_t>(i)] < N - R + i) {
int j = curr[static_cast<size_t>(i)] + 1;
while (i <= R) {
curr[static_cast<size_t>(i++)] = j++;
}
completed = false;
++generated;
break;
}
}
return ret;
}
private:
int N;
int R;
Combination curr;
};
}