forked from alabuzhev/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.hpp
139 lines (111 loc) · 3.39 KB
/
filter.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
#ifndef ITER_FILTER_H_
#define ITER_FILTER_H_
#include "internal/iterbase.hpp"
#include <utility>
#include <iterator>
namespace iter {
namespace impl {
template <typename FilterFunc, typename Container>
class Filtered;
}
template <typename FilterFunc, typename Container>
impl::Filtered<FilterFunc, Container> filter(FilterFunc, Container&&);
}
template <typename FilterFunc, typename Container>
class iter::impl::Filtered {
private:
Container container;
FilterFunc filter_func;
// The filter function is the only thing allowed to create a Filtered
friend Filtered iter::filter<FilterFunc, Container>(FilterFunc, Container&&);
// Value constructor for use only in the filter function
Filtered(FilterFunc in_filter_func, Container&& in_container)
: container(std::forward<Container>(in_container)),
filter_func(in_filter_func) {}
public:
Filtered(Filtered&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag,
iterator_traits_deref<Container>> {
protected:
using Holder = DerefHolder<iterator_deref<Container>>;
iterator_type<Container> sub_iter;
iterator_type<Container> sub_end;
Holder item;
FilterFunc* filter_func;
void inc_sub_iter() {
++this->sub_iter;
if (this->sub_iter != this->sub_end) {
this->item.reset(*this->sub_iter);
}
}
// increment until the iterator points to is true on the
// predicate. Called by constructor and operator++
void skip_failures() {
while (this->sub_iter != this->sub_end
&& !(*this->filter_func)(this->item.get())) {
this->inc_sub_iter();
}
}
public:
Iterator(iterator_type<Container> iter, iterator_type<Container> end,
FilterFunc& in_filter_func)
: sub_iter{iter}, sub_end{end}, filter_func(&in_filter_func) {
if (this->sub_iter != this->sub_end) {
this->item.reset(*this->sub_iter);
}
this->skip_failures();
}
typename Holder::reference operator*() {
return this->item.get();
}
typename Holder::pointer operator->() {
return this->item.get_ptr();
}
Iterator& operator++() {
this->inc_sub_iter();
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;
}
bool operator==(const Iterator& other) const {
return !(*this != other);
}
};
Iterator begin() {
return {std::begin(this->container), std::end(this->container),
this->filter_func};
}
Iterator end() {
return {std::end(this->container), std::end(this->container),
this->filter_func};
}
};
template <typename FilterFunc, typename Container>
iter::impl::Filtered<FilterFunc, Container> iter::filter(
FilterFunc filter_func, Container&& container) {
return {filter_func, std::forward<Container>(container)};
}
namespace iter {
namespace detail {
template <typename Container>
class BoolTester {
public:
bool operator()(const impl::iterator_deref<Container> item) const {
return bool(item);
}
};
}
template <typename Container>
auto filter(Container&& container) {
return filter(
detail::BoolTester<Container>(), std::forward<Container>(container));
}
}
#endif