-
Notifications
You must be signed in to change notification settings - Fork 5
/
combinations_generator.cpp
53 lines (52 loc) · 1.58 KB
/
combinations_generator.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
52
53
#include <vector>
#include <algorithm>
template <typename T>
class Combinations {
public:
class Iterator {
const std::vector<T> values;
std::vector<bool> inactive;
bool finished;
public:
Iterator (const std::vector<T> values, size_t k, bool finished = false)
: values{values}, inactive(values.size(), 1), finished{finished} {
std::fill(inactive.begin(), inactive.begin() + k, 0);
}
std::vector<T> operator*() {
std::vector<T> combination;
for (size_t i = 0; i < values.size(); ++i)
if (!inactive[i])
combination.push_back(values[i]);
return combination;
}
Iterator &operator++() {
if (!finished)
finished = !std::next_permutation(inactive.begin(), inactive.end());
return *this;
};
bool operator!=(const Iterator& other) { return other.finished != finished; }
};
Combinations(std::vector<T> values, size_t k) : values{values}, k{k} {}
Iterator begin() { return Iterator(values, std::min(values.size(), k)); }
Iterator end() { return Combinations::endingIterator; }
private:
size_t k;
std::vector<T> values;
static Iterator endingIterator;
};
template <typename T>
typename Combinations<T>::Iterator Combinations<T>::endingIterator({}, 0, true);
// #include <iostream>
// int main() {
// for (auto comb : Combinations<int>({1, 2, 3}, 2)) {
// for(auto x : comb)
// std::cout << x << " ";
// std::cout << std::endl;
// }
// auto combinations = Combinations<int>({1, 2, 3}, 2);
// for (auto it = combinations.begin(); it != combinations.end(); ++it) {
// for(auto x : *it)
// std::cout << x << " ";
// std::cout << std::endl;
// }
// }