This repository was archived by the owner on Oct 29, 2020. It is now read-only.
forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowerset.hpp
More file actions
100 lines (80 loc) · 2.29 KB
/
Copy pathpowerset.hpp
File metadata and controls
100 lines (80 loc) · 2.29 KB
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
#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_;
using CombinatorType = decltype(combinations(std::declval<Container&>(), 0));
friend PowersetFn;
Powersetter(Container&& container)
: container_(std::forward<Container>(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& container, std::size_t sz)
: container_p_{&container},
set_size_{sz},
comb_{std::make_shared<CombinatorType>(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>(
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> operator*() {
return *comb_iter_;
}
iterator_arrow<CombinatorType> 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_;
}
};
Iterator begin() {
return {container_, 0};
}
Iterator end() {
return {container_, dumb_size(container_) + 1};
}
};
#endif