forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.hpp
140 lines (115 loc) · 3.79 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifndef ITER_COMPRESS_H_
#define ITER_COMPRESS_H_
#include "internal/iterator_wrapper.hpp"
#include "internal/iterbase.hpp"
#include <iterator>
#include <utility>
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&&);
Compressed(Container&& in_container, Selector&& in_selectors)
: container_(std::forward<Container>(in_container)),
selectors_(std::forward<Selector>(in_selectors)) {}
public:
Compressed(Compressed&&) = default;
template <typename ContainerT, typename SelectorT>
class Iterator {
private:
template <typename, typename>
friend class Iterator;
IteratorWrapper<ContainerT> sub_iter_;
IteratorWrapper<ContainerT> sub_end_;
IteratorWrapper<SelectorT> selector_iter_;
IteratorWrapper<SelectorT> selector_end_;
void increment_iterators() {
++sub_iter_;
++selector_iter_;
}
void skip_failures() {
while (sub_iter_ != sub_end_ && selector_iter_ != selector_end_
&& !*selector_iter_) {
increment_iterators();
}
}
public:
using iterator_category = std::input_iterator_tag;
using value_type = iterator_traits_deref<ContainerT>;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
Iterator(IteratorWrapper<ContainerT>&& cont_iter,
IteratorWrapper<ContainerT>&& cont_end,
IteratorWrapper<SelectorT>&& sel_iter,
IteratorWrapper<SelectorT>&& 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)} {
skip_failures();
}
iterator_deref<ContainerT> operator*() {
return *sub_iter_;
}
iterator_arrow<ContainerT> operator->() {
return apply_arrow(sub_iter_);
}
Iterator& operator++() {
increment_iterators();
skip_failures();
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
template <typename T, typename U>
bool operator!=(const Iterator<T, U>& other) const {
return sub_iter_ != other.sub_iter_
&& selector_iter_ != other.selector_iter_;
}
template <typename T, typename U>
bool operator==(const Iterator<T, U>& other) const {
return !(*this != other);
}
};
Iterator<Container, Selector> begin() {
return {get_begin(container_), get_end(container_), get_begin(selectors_),
get_end(selectors_)};
}
Iterator<Container, Selector> end() {
return {get_end(container_), get_end(container_), get_end(selectors_),
get_end(selectors_)};
}
Iterator<AsConst<Container>, AsConst<Selector>> begin() const {
return {get_begin(std::as_const(container_)),
get_end(std::as_const(container_)),
get_begin(std::as_const(selectors_)),
get_end(std::as_const(selectors_))};
}
Iterator<AsConst<Container>, AsConst<Selector>> end() const {
return {get_end(std::as_const(container_)),
get_end(std::as_const(container_)),
get_end(std::as_const(selectors_)),
get_end(std::as_const(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