forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerset.hpp
131 lines (107 loc) · 3.18 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef ITER_POWERSET_HPP_
#define ITER_POWERSET_HPP_
#include "combinations.hpp"
#include "internal/iterbase.hpp"
#include <cassert>
#include <initializer_list>
#include <iterator>
#include <memory>
#include <type_traits>
#include <utility>
namespace iter {
namespace impl {
template <typename Container>
class Powersetter;
using PowersetFn = IterToolFn<Powersetter>;
}
constexpr impl::PowersetFn powerset{};
}
template <typename Container>
class iter::impl::Powersetter {
private:
Container container_;
template <typename T>
using CombinatorType = decltype(combinations(std::declval<T&>(), 0));
friend PowersetFn;
Powersetter(Container&& container)
: container_(std::forward<Container>(container)) {}
public:
Powersetter(Powersetter&&) = default;
template <typename ContainerT>
class Iterator {
private:
#if 0
template <typename> friend class Iterator;
#endif
std::remove_reference_t<ContainerT>* container_p_;
std::size_t set_size_{};
std::shared_ptr<CombinatorType<ContainerT>> comb_;
iterator_type<CombinatorType<ContainerT>> comb_iter_;
iterator_type<CombinatorType<ContainerT>> comb_end_;
public:
using iterator_category = std::input_iterator_tag;
using value_type = CombinatorType<ContainerT>;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
Iterator(ContainerT& container, std::size_t sz)
: container_p_{&container},
set_size_{sz},
comb_{std::make_shared<CombinatorType<ContainerT>>(
combinations(container, sz))},
comb_iter_{get_begin(*comb_)},
comb_end_{get_end(*comb_)} {}
Iterator& operator++() {
++comb_iter_;
if (comb_iter_ == comb_end_) {
++set_size_;
comb_ = std::make_shared<CombinatorType<ContainerT>>(
combinations(*container_p_, set_size_));
comb_iter_ = get_begin(*comb_);
comb_end_ = get_end(*comb_);
}
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
iterator_deref<CombinatorType<ContainerT>> operator*() {
return *comb_iter_;
}
iterator_arrow<CombinatorType<ContainerT>> operator->() {
apply_arrow(comb_iter_);
}
bool operator!=(const Iterator& other) const {
return !(*this == other);
}
bool operator==(const Iterator& other) const {
return set_size_ == other.set_size_ && comb_iter_ == other.comb_iter_;
}
#if 0
template <typename T>
bool operator!=(const Iterator<T>& other) const {
return !(*this == other);
}
template <typename T>
bool operator==(const Iterator<T>& other) const {
return set_size_ == other.set_size_ && comb_iter_ == other.comb_iter_;
}
#endif
};
Iterator<Container> begin() {
return {container_, 0};
}
Iterator<Container> end() {
return {container_, dumb_size(container_) + 1};
}
Iterator<AsConst<Container>> begin() const {
return {std::as_const(container_), 0};
}
Iterator<AsConst<Container>> end() const {
return {
std::as_const(container_), dumb_size(std::as_const(container_)) + 1};
}
};
#endif