forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupby.hpp
More file actions
268 lines (217 loc) · 6.57 KB
/
groupby.hpp
File metadata and controls
268 lines (217 loc) · 6.57 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#ifndef ITER_GROUP_BY_HPP_
#define ITER_GROUP_BY_HPP_
// this is easily the most functionally complex itertool
#include "internal/iterbase.hpp"
#include <type_traits>
#include <memory>
#include <utility>
#include <iterator>
namespace iter {
namespace impl {
template <typename Container, typename KeyFunc>
class GroupProducer;
struct Identity {
template <typename T>
const T& operator()(const T& t) const {
return t;
}
};
using GroupByFn = IterToolFnOptionalBindSecond<GroupProducer, Identity>;
}
constexpr impl::GroupByFn groupby{};
}
template <typename Container, typename KeyFunc>
class iter::impl::GroupProducer {
private:
Container container;
KeyFunc key_func;
friend GroupByFn;
using key_func_ret = std::result_of_t<KeyFunc(iterator_deref<Container>)>;
GroupProducer(Container&& in_container, KeyFunc in_key_func)
: container(std::forward<Container>(in_container)),
key_func(in_key_func) {}
public:
GroupProducer(GroupProducer&&) = default;
class Iterator;
class Group;
private:
using KeyGroupPair = std::pair<key_func_ret, Group>;
using Holder = DerefHolder<iterator_deref<Container>>;
public:
class Iterator : public std::iterator<std::input_iterator_tag, KeyGroupPair> {
private:
iterator_type<Container> sub_iter;
iterator_type<Container> sub_end;
Holder item;
KeyFunc* key_func;
std::unique_ptr<KeyGroupPair> current_key_group_pair;
public:
Iterator(iterator_type<Container>&& si, iterator_type<Container>&& end,
KeyFunc& in_key_func)
: sub_iter{std::move(si)},
sub_end{std::move(end)},
key_func(&in_key_func) {
if (this->sub_iter != this->sub_end) {
this->item.reset(*this->sub_iter);
}
}
Iterator(const Iterator& other)
: sub_iter{other.sub_iter},
sub_end{other.sub_end},
item{other.item},
key_func{other.key_func} {}
Iterator& operator=(const Iterator& other) {
if (this == &other) {
return *this;
}
this->sub_iter = other.sub_iter;
this->sub_end = other.sub_end;
this->item = other.item;
this->key_func = other.key_func;
this->current_key_group_pair.reset();
return *this;
}
~Iterator() = default;
// NOTE the implicitly generated move constructor would
// be wrong
KeyGroupPair& operator*() {
set_key_group_pair();
return *this->current_key_group_pair;
}
KeyGroupPair* operator->() {
set_key_group_pair();
return this->current_key_group_pair.get();
}
Iterator& operator++() {
if (!this->current_key_group_pair) {
this->set_key_group_pair();
}
this->current_key_group_pair.reset();
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
bool operator!=(const Iterator& other) const {
return this->sub_iter != other.sub_iter;
}
bool operator==(const Iterator& other) const {
return !(*this != other);
}
void increment_iterator() {
if (this->sub_iter != this->sub_end) {
++this->sub_iter;
if (this->sub_iter != this->sub_end) {
this->item.reset(*this->sub_iter);
}
}
}
bool exhausted() const {
return !(this->sub_iter != this->sub_end);
}
typename Holder::reference get() {
return this->item.get();
}
typename Holder::pointer get_ptr() {
return this->item.get_ptr();
}
key_func_ret next_key() {
return (*this->key_func)(this->item.get());
}
void set_key_group_pair() {
if (!this->current_key_group_pair) {
this->current_key_group_pair =
std::make_unique<KeyGroupPair>((*this->key_func)(this->item.get()),
Group{*this, this->next_key()});
}
}
};
class Group {
private:
friend Iterator;
friend class GroupIterator;
Iterator& owner;
key_func_ret key;
// completed is set if a Group is iterated through
// completely. It is checked in the destructor, and
// if the Group has not been completed, the destructor
// exhausts it. This ensures that the next Group starts
// at the correct position when the user short-circuits
// iteration over a Group.
// The move constructor sets the rvalue's completed
// attribute to true, so its destructor doesn't do anything
// when called.
bool completed = false;
Group(Iterator& in_owner, key_func_ret in_key)
: owner(in_owner), key(in_key) {}
public:
~Group() {
if (!this->completed) {
for (auto iter = this->begin(), end = this->end(); iter != end;
++iter) {
}
}
}
// move-constructible, non-copy-constructible, non-assignable
Group(Group&& other) noexcept : owner(other.owner),
key{other.key},
completed{other.completed} {
other.completed = true;
}
class GroupIterator : public std::iterator<std::input_iterator_tag,
iterator_traits_deref<Container>> {
private:
std::remove_reference_t<key_func_ret>* key;
Group* group_p;
bool not_at_end() {
return !this->group_p->owner.exhausted()
&& this->group_p->owner.next_key() == *this->key;
}
public:
GroupIterator(Group* in_group_p, key_func_ret& in_key)
: key{&in_key}, group_p{in_group_p} {}
bool operator!=(const GroupIterator& other) const {
return !(*this == other);
}
bool operator==(const GroupIterator& other) const {
return this->group_p == other.group_p;
}
GroupIterator& operator++() {
this->group_p->owner.increment_iterator();
if (!this->not_at_end()) {
this->group_p->completed = true;
this->group_p = nullptr;
}
return *this;
}
GroupIterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
iterator_deref<Container> operator*() {
return this->group_p->owner.get();
}
typename Holder::pointer operator->() {
return this->group_p->owner.get_ptr();
}
};
GroupIterator begin() {
return {this, key};
}
GroupIterator end() {
return {nullptr, key};
}
};
Iterator begin() {
return {
std::begin(this->container), std::end(this->container), this->key_func};
}
Iterator end() {
return {
std::end(this->container), std::end(this->container), this->key_func};
}
};
#endif