forked from alabuzhev/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combinations_with_replacement.hpp
119 lines (98 loc) · 3.05 KB
/
combinations_with_replacement.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
#ifndef ITER_COMBINATIONS_WITH_REPLACEMENT_HPP_
#define ITER_COMBINATIONS_WITH_REPLACEMENT_HPP_
#include "internal/iterbase.hpp"
#include "internal/iteratoriterator.hpp"
#include <iterator>
#include <vector>
#include <type_traits>
namespace iter {
namespace impl {
template <typename Container>
class CombinatorWithReplacement;
}
template <typename Container>
impl::CombinatorWithReplacement<Container> combinations_with_replacement(
Container&&, std::size_t);
}
template <typename Container>
class iter::impl::CombinatorWithReplacement {
private:
Container container;
std::size_t length;
friend CombinatorWithReplacement
iter::combinations_with_replacement<Container>(Container&&, std::size_t);
CombinatorWithReplacement(Container&& in_container, std::size_t n)
: container(std::forward<Container>(in_container)), length{n} {}
using IndexVector = std::vector<iterator_type<Container>>;
using CombIteratorDeref = IterIterWrapper<IndexVector>;
public:
CombinatorWithReplacement(CombinatorWithReplacement&&) = default;
class Iterator
: public std::iterator<std::input_iterator_tag, CombIteratorDeref> {
private:
constexpr static const int COMPLETE = -1;
std::remove_reference_t<Container>* container_p;
CombIteratorDeref indices;
int steps;
public:
Iterator(Container& in_container, std::size_t n)
: container_p{&in_container},
indices(n, std::begin(in_container)),
steps{(std::begin(in_container) != std::end(in_container) && n)
? 0
: COMPLETE} {}
CombIteratorDeref& operator*() {
return this->indices;
}
CombIteratorDeref* operator->() {
return &this->indices;
}
Iterator& operator++() {
for (auto iter = indices.get().rbegin(); iter != indices.get().rend();
++iter) {
++(*iter);
if (!(*iter != std::end(*this->container_p))) {
if ((iter + 1) != indices.get().rend()) {
for (auto down = iter; down != indices.get().rbegin() - 1; --down) {
(*down) = dumb_next(*(iter + 1));
}
} else {
this->steps = COMPLETE;
break;
}
} else {
// we break because none of the rest of the items
// need to be incremented
break;
}
}
if (this->steps != COMPLETE) {
++this->steps;
}
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
bool operator!=(const Iterator& other) const {
return !(*this == other);
}
bool operator==(const Iterator& other) const {
return this->steps == other.steps;
}
};
Iterator begin() {
return {this->container, this->length};
}
Iterator end() {
return {this->container, 0};
}
};
template <typename Container>
iter::impl::CombinatorWithReplacement<Container>
iter::combinations_with_replacement(Container&& container, std::size_t length) {
return {std::forward<Container>(container), length};
}
#endif