forked from ryanhaining/cppitertools
/
filter.hpp
135 lines (108 loc) · 4.38 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
#ifndef FILTER__H__
#define FILTER__H__
#include <utility>
namespace iter {
//Forward declarations of Filter and filter
template <typename FilterFunc, typename Container>
class Filter;
template <typename FilterFunc, typename Container>
Filter<FilterFunc, Container> filter(FilterFunc, Container &);
template <typename FilterFunc, typename Container>
class Filter {
private:
Container & container;
FilterFunc filter_func;
// The filter function is the only thing allowed to create a Filter
friend Filter filter<FilterFunc, Container>(FilterFunc,
Container &);
// Type of the Container::Iterator, but since the name of that
// iterator can be anything, we have to grab it with this
using contained_iter_type = decltype(container.begin());
// The type returned when dereferencing the Container::Iterator
using contained_iter_ret = decltype(container.begin().operator*());
// Value constructor for use only in the filter function
Filter(FilterFunc filter_func, Container & container) :
container(container),
filter_func(filter_func)
{ }
Filter () = delete;
Filter & operator=(const Filter &) = delete;
public:
Filter(const Filter &) = default;
class Iterator {
protected:
contained_iter_type sub_iter;
const contained_iter_type sub_end;
FilterFunc filter_func;
// 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->sub_iter)) {
++this->sub_iter;
}
}
public:
Iterator (contained_iter_type iter,
contained_iter_type end,
FilterFunc filter_func) :
sub_iter(iter),
sub_end(end),
filter_func(filter_func)
{
this->skip_failures();
}
contained_iter_ret operator*() const {
return *this->sub_iter;
}
Iterator & operator++() {
++this->sub_iter;
this->skip_failures();
return *this;
}
bool operator!=(const Iterator & other) const {
return this->sub_iter != other.sub_iter;
}
};
Iterator begin() const {
return Iterator(
this->container.begin(),
this->container.end(),
this->filter_func);
}
Iterator end() const {
return Iterator(
this->container.end(),
this->container.end(),
this->filter_func);
}
};
// Helper function to instantiate a Filter
template <typename FilterFunc, typename Container>
Filter<FilterFunc, Container> filter(
FilterFunc filter_func, Container & container) {
return Filter<FilterFunc, Container>(filter_func, container);
}
namespace detail {
template <typename T>
bool boolean_cast(const T & t) {
return bool(t);
}
template <typename Container>
class BoolTester {
protected:
using contained_iter_ret =
decltype(std::declval<Container>().begin().operator*());
public:
bool operator() (const contained_iter_ret item) const {
return bool(item);
}
};
}
template <typename Container>
auto filter(Container & container) ->
decltype(filter(detail::BoolTester<Container>(), container)) {
return filter(detail::BoolTester<Container>(), container);
}
}
#endif //ifndef FILTER__H__