forked from alabuzhev/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.hpp
119 lines (95 loc) · 3.06 KB
/
compress.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_COMPRESS_H_
#define ITER_COMPRESS_H_
#include "internal/iterbase.hpp"
#include <utility>
#include <iterator>
namespace iter {
namespace impl {
template <typename Container, typename Selector>
class Compressed;
}
template <typename Container, typename Selector>
impl::Compressed<Container, Selector> compress(Container&&, Selector&&);
}
template <typename Container, typename Selector>
class iter::impl::Compressed {
private:
Container container;
Selector selectors;
friend Compressed iter::compress<Container, Selector>(
Container&&, Selector&&);
// Selector::Iterator type
using selector_iter_type = decltype(std::begin(selectors));
Compressed(Container&& in_container, Selector&& in_selectors)
: container(std::forward<Container>(in_container)),
selectors(std::forward<Selector>(in_selectors)) {}
public:
Compressed(Compressed&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag,
iterator_traits_deref<Container>> {
private:
iterator_type<Container> sub_iter;
iterator_type<Container> sub_end;
selector_iter_type selector_iter;
selector_iter_type selector_end;
void increment_iterators() {
++this->sub_iter;
++this->selector_iter;
}
void skip_failures() {
while (this->sub_iter != this->sub_end
&& this->selector_iter != this->selector_end
&& !*this->selector_iter) {
this->increment_iterators();
}
}
public:
Iterator(iterator_type<Container>&& cont_iter,
iterator_type<Container>&& cont_end, selector_iter_type&& sel_iter,
selector_iter_type&& sel_end)
: sub_iter{std::move(cont_iter)},
sub_end{std::move(cont_end)},
selector_iter{std::move(sel_iter)},
selector_end{std::move(sel_end)} {
this->skip_failures();
}
iterator_deref<Container> operator*() {
return *this->sub_iter;
}
iterator_arrow<Container> operator->() {
return apply_arrow(this->sub_iter);
}
Iterator& operator++() {
this->increment_iterators();
this->skip_failures();
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
bool operator!=(const Iterator& other) const {
return this->sub_iter != other.sub_iter
&& this->selector_iter != other.selector_iter;
}
bool operator==(const Iterator& other) const {
return !(*this != other);
}
};
Iterator begin() {
return {std::begin(this->container), std::end(this->container),
std::begin(this->selectors), std::end(this->selectors)};
}
Iterator end() {
return {std::end(this->container), std::end(this->container),
std::end(this->selectors), std::end(this->selectors)};
}
};
template <typename Container, typename Selector>
iter::impl::Compressed<Container, Selector> iter::compress(
Container&& container, Selector&& selectors) {
return {
std::forward<Container>(container), std::forward<Selector>(selectors)};
}
#endif