-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcombinations_oop.cpp
51 lines (44 loc) · 1.33 KB
/
combinations_oop.cpp
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
#include <vector>
#include <algorithm>
#include <iostream>
/**
* Class for iterating all combinations in a vector.
*
* example:
* Combination c(vector);
* while (c.hasNext()) {
* print(c.next());
* }
*/
template <typename T>
class Combinations {
private:
enum class State { begin, running, end };
std::vector<T> arr;
std::vector<bool> inset;
std::vector<T> nextComb;
int R;
State state;
void fillComb() {
int j = 0;
for (int i = 0; i < inset.size(); ++i) if (inset[i]) { nextComb[j++] = arr[i]; } }
public:
Combinations<T>(std::vector<T>& arr, int r)
: arr{arr}, R{r}, inset{std::vector<bool>(arr.size())}, nextComb{std::vector<T>(r)}, state{State::begin} {
std::fill(inset.begin()+std::max(0, (int)arr.size()-r), inset.end(), 1);
fillComb();
}
std::vector<T>& curr() { return nextComb; }
std::vector<T>& next() {
switch (state) {
case State::begin:
state = (std::next_permutation(inset.begin(), inset.end())) ? State::running : State::end;
break;
case State::running:
fillComb();
state = (std::next_permutation(inset.begin(), inset.end())) ? State::running : State::end;
}
return nextComb;
}
bool hasNext() { return state != State::end; }
};