forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdropwhile.hpp
More file actions
107 lines (86 loc) · 3.54 KB
/
Copy pathdropwhile.hpp
File metadata and controls
107 lines (86 loc) · 3.54 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
#ifndef DROPWHILE__H__
#define DROPWHILE__H__
#include <utility>
namespace iter {
//Forward declarations of DropWhile and dropwhile
template <typename FilterFunc, typename Container>
class DropWhile;
template <typename FilterFunc, typename Container>
DropWhile<FilterFunc, Container> dropwhile(FilterFunc, Container &);
template <typename FilterFunc, typename Container>
class DropWhile {
friend DropWhile dropwhile<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(std::declval<Container>().begin());
// The type returned when dereferencing the Container::Iterator
using contained_iter_ret =
decltype(std::declval<contained_iter_type>().operator*());
private:
Container & container;
FilterFunc filter_func;
// Value constructor for use only in the dropwhile function
DropWhile(FilterFunc filter_func, Container & container) :
container(container),
filter_func(filter_func)
{ }
DropWhile () = delete;
DropWhile & operator=(const DropWhile &) = delete;
// Default copy constructor used
public:
class Iterator {
private:
contained_iter_type sub_iter;
const contained_iter_type sub_end;
FilterFunc filter_func;
// skip all values for which the predicate is true
void skip_passes() {
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_passes();
}
contained_iter_ret operator*() const {
return *this->sub_iter;
}
Iterator & operator++() {
++this->sub_iter;
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 DropWhile
template <typename FilterFunc, typename Container>
DropWhile<FilterFunc, Container> dropwhile(
FilterFunc filter_func, Container & container) {
return DropWhile<FilterFunc, Container>(filter_func, container);
}
}
#endif //ifndef DROPWHILE__H__