forked from alabuzhev/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerset.hpp
106 lines (85 loc) · 2.56 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
97
98
99
100
101
102
103
104
105
106
#ifndef ITER_POWERSET_HPP_
#define ITER_POWERSET_HPP_
#include "internal/iterbase.hpp"
#include "combinations.hpp"
#include <cassert>
#include <memory>
#include <utility>
#include <iterator>
#include <type_traits>
namespace iter {
namespace impl {
template <typename Container>
class Powersetter;
}
template <typename Container>
impl::Powersetter<Container> powerset(Container&&);
}
template <typename Container>
class iter::impl::Powersetter {
private:
Container container;
using CombinatorType = decltype(combinations(std::declval<Container&>(), 0));
friend Powersetter iter::powerset<Container>(Container&&);
Powersetter(Container&& in_container)
: container(std::forward<Container>(in_container)) {}
public:
Powersetter(Powersetter&&) = default;
class Iterator
: public std::iterator<std::input_iterator_tag, CombinatorType> {
private:
std::remove_reference_t<Container>* container_p;
std::size_t set_size;
std::shared_ptr<CombinatorType> comb;
iterator_type<CombinatorType> comb_iter;
iterator_type<CombinatorType> comb_end;
public:
Iterator(Container& in_container, std::size_t sz)
: container_p{&in_container},
set_size{sz},
comb{
std::make_shared<CombinatorType>(combinations(in_container, sz))},
comb_iter{std::begin(*comb)},
comb_end{std::end(*comb)} {}
Iterator& operator++() {
++this->comb_iter;
if (this->comb_iter == this->comb_end) {
++this->set_size;
this->comb = std::make_shared<CombinatorType>(
combinations(*this->container_p, this->set_size));
this->comb_iter = std::begin(*this->comb);
this->comb_end = std::end(*this->comb);
}
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
iterator_deref<CombinatorType> operator*() {
return *this->comb_iter;
}
iterator_arrow<CombinatorType> operator->() {
apply_arrow(this->comb_iter);
}
bool operator!=(const Iterator& other) const {
return !(*this == other);
}
bool operator==(const Iterator& other) const {
return this->set_size == other.set_size
&& this->comb_iter == other.comb_iter;
}
};
Iterator begin() {
return {this->container, 0};
}
Iterator end() {
return {this->container, dumb_size(this->container) + 1};
}
};
template <typename Container>
iter::impl::Powersetter<Container> iter::powerset(Container&& container) {
return {std::forward<Container>(container)};
}
#endif