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 (87 loc) · 2.45 KB
/
Copy pathdropwhile.hpp
File metadata and controls
107 lines (87 loc) · 2.45 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 ITER_DROPWHILE_H_
#define ITER_DROPWHILE_H_
#include "filter.hpp"
#include "internal/iterator_wrapper.hpp"
#include "internal/iterbase.hpp"
#include <iterator>
#include <utility>
namespace iter {
namespace impl {
template <typename FilterFunc, typename Container>
class Dropper;
using DropWhileFn = IterToolFnOptionalBindFirst<Dropper, BoolTester>;
}
constexpr impl::DropWhileFn dropwhile{};
}
template <typename FilterFunc, typename Container>
class iter::impl::Dropper {
private:
Container container_;
FilterFunc filter_func_;
friend DropWhileFn;
Dropper(FilterFunc filter_func, Container&& container)
: container_(std::forward<Container>(container)),
filter_func_(filter_func) {}
public:
Dropper(Dropper&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag,
iterator_traits_deref<Container>> {
private:
using Holder = DerefHolder<iterator_deref<Container>>;
IteratorWrapper<Container> sub_iter_;
IteratorWrapper<Container> sub_end_;
Holder item_;
FilterFunc* filter_func_;
void inc_sub_iter() {
++sub_iter_;
if (sub_iter_ != sub_end_) {
item_.reset(*sub_iter_);
}
}
// skip all values for which the predicate is true
void skip_passes() {
while (sub_iter_ != sub_end_ && (*filter_func_)(item_.get())) {
inc_sub_iter();
}
}
public:
Iterator(IteratorWrapper<Container>&& sub_iter,
IteratorWrapper<Container>&& sub_end, FilterFunc& filter_func)
: sub_iter_{std::move(sub_iter)},
sub_end_{std::move(sub_end)},
filter_func_(&filter_func) {
if (sub_iter_ != sub_end_) {
item_.reset(*sub_iter_);
}
skip_passes();
}
typename Holder::reference operator*() {
return item_.get();
}
typename Holder::pointer operator->() {
return item_.get_ptr();
}
Iterator& operator++() {
inc_sub_iter();
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
bool operator!=(const Iterator& other) const {
return sub_iter_ != other.sub_iter_;
}
bool operator==(const Iterator& other) const {
return !(*this != other);
}
};
Iterator begin() {
return {get_begin(container_), get_end(container_), filter_func_};
}
Iterator end() {
return {get_end(container_), get_end(container_), filter_func_};
}
};
#endif