forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinations_with_replacement.hpp
More file actions
148 lines (126 loc) · 3.81 KB
/
Copy pathcombinations_with_replacement.hpp
File metadata and controls
148 lines (126 loc) · 3.81 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef ITER_COMBINATIONS_WITH_REPLACEMENT_HPP_
#define ITER_COMBINATIONS_WITH_REPLACEMENT_HPP_
#include "internal/iteratoriterator.hpp"
#include "internal/iterbase.hpp"
#include <iterator>
#include <type_traits>
#include <vector>
namespace iter {
namespace impl {
template <typename Container>
class CombinatorWithReplacement;
using CombinationsWithReplacementFn =
IterToolFnBindSizeTSecond<CombinatorWithReplacement>;
}
inline constexpr impl::CombinationsWithReplacementFn combinations_with_replacement{};
}
template <typename Container>
class iter::impl::CombinatorWithReplacement {
private:
Container container_;
std::size_t length_;
friend CombinationsWithReplacementFn;
CombinatorWithReplacement(Container&& container, std::size_t n)
: container_(std::forward<Container>(container)), length_{n} {}
template <typename T>
using IndexVector = std::vector<iterator_type<T>>;
template <typename T>
using CombIteratorDeref = IterIterWrapper<IndexVector<T>>;
public:
CombinatorWithReplacement(CombinatorWithReplacement&&) = default;
template <typename ContainerT>
class Iterator {
private:
template <typename>
friend class Iterator;
constexpr static const int COMPLETE = -1;
std::remove_reference_t<ContainerT>* container_p_;
CombIteratorDeref<ContainerT> indices_;
int steps_;
public:
using iterator_category = std::input_iterator_tag;
using value_type = CombIteratorDeref<ContainerT>;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
Iterator(ContainerT& in_container, std::size_t n)
: container_p_{&in_container},
indices_(n, get_begin(in_container)),
steps_{(get_begin(in_container) != get_end(in_container) && n)
? 0
: COMPLETE} {}
static Iterator zero_length_end(ContainerT& container) {
Iterator it{container, 0};
it.steps_ = 0;
return it;
}
CombIteratorDeref<ContainerT>& operator*() {
return indices_;
}
CombIteratorDeref<ContainerT>* operator->() {
return &indices_;
}
Iterator& operator++() {
if (indices_.get().empty()) {
// zero-length case.
++steps_;
return *this;
}
for (auto iter = indices_.get().rbegin(); iter != indices_.get().rend();
++iter) {
++(*iter);
if (!(*iter != get_end(*container_p_))) {
if ((iter + 1) != indices_.get().rend()) {
for (auto down = iter;; --down) {
(*down) = dumb_next(*(iter + 1));
if (down == indices_.get().rbegin()) break;
}
} else {
steps_ = COMPLETE;
break;
}
} else {
// we break because none of the rest of the items
// need to be incremented
break;
}
}
if (steps_ != COMPLETE) {
++steps_;
}
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
template <typename T>
bool operator!=(const Iterator<T>& other) const {
return !(*this == other);
}
template <typename T>
bool operator==(const Iterator<T>& other) const {
return steps_ == other.steps_;
}
};
Iterator<Container> begin() {
return {container_, length_};
}
Iterator<Container> end() {
if (length_ == 0) {
return Iterator<Container>::zero_length_end(container_);
}
return {container_, 0};
}
Iterator<AsConst<Container>> begin() const {
return {std::as_const(container_), length_};
}
Iterator<AsConst<Container>> end() const {
if (length_ == 0) {
return Iterator<AsConst<Container>>::zero_length_end(container_);
}
return {std::as_const(container_), 0};
}
};
#endif