forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerset.hpp
96 lines (82 loc) · 2.97 KB
/
powerset.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef POWERSET_HPP_
#define POWERSET_HPP_
#include "iterbase.hpp"
#include "combinations.hpp"
#include "enumerate.hpp"
#include <cassert>
#include <vector>
#include <initializer_list>
#include <utility>
#include <iterator>
namespace iter {
template <typename Container,
typename CombinatorType=
decltype(combinations(std::declval<Container&>(), 0))>
class Powersetter {
private:
Container container;
std::vector<CombinatorType> combinators;
public:
Powersetter(Container in_container)
: container(std::forward<Container>(in_container))
{
combinators.push_back(combinations(this->container, 0));
std::size_t i = 1;
for (auto iter = std::begin(this->container),
end = std::end(this->container);
iter != end;
++iter, ++i) {
combinators.push_back(combinations(this->container, i));
}
}
class Iterator {
private:
std::size_t container_size;
std::size_t list_size = 0;
bool not_done = true;
std::vector<CombinatorType>& combinators;
std::vector<iterator_type<CombinatorType>> inner_iters;
public:
Iterator(std::vector<CombinatorType>& combs)
: container_size{combs.size() - 1},
combinators(combs)
{
for (auto& comb : combinators) {
inner_iters.push_back(std::begin(comb));
}
}
Iterator& operator++() {
++inner_iters[list_size];
if (!(inner_iters[list_size] != inner_iters[list_size])) {
++list_size;
}
if (container_size < list_size) {
not_done = false;
}
return *this;
}
auto operator*() -> decltype(*inner_iters[0]) {
return *(inner_iters[list_size]);
}
bool operator != (const Iterator&) {
return not_done;
}
};
Iterator begin() {
return {this->combinators};
}
Iterator end() {
return {this->combinators};
}
};
template <typename Container>
Powersetter<Container> powerset(Container&& container) {
return {std::forward<Container>(container)};
}
template <typename T>
Powersetter<std::initializer_list<T>> powerset(
std::initializer_list<T> il) {
return {il};
}
}
#endif // #ifndef POWERSET_HPP_