forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtakewhile.hpp
More file actions
126 lines (101 loc) · 4.27 KB
/
takewhile.hpp
File metadata and controls
126 lines (101 loc) · 4.27 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef TAKEWHILE__H__
#define TAKEWHILE__H__
#include "iterbase.hpp"
#include <utility>
#include <iterator>
#include <initializer_list>
namespace iter {
//Forward declarations of TakeWhile and takewhile
template <typename FilterFunc, typename Container>
class TakeWhile;
template <typename FilterFunc, typename Container>
TakeWhile<FilterFunc, Container> takewhile(FilterFunc, Container&&);
template <typename FilterFunc, typename T>
TakeWhile<FilterFunc, std::initializer_list<T>> takewhile(
FilterFunc, std::initializer_list<T>);
template <typename FilterFunc, typename Container>
class TakeWhile {
private:
Container container;
FilterFunc filter_func;
friend TakeWhile takewhile<FilterFunc, Container>(
FilterFunc, Container&&);
template <typename FF, typename T>
friend TakeWhile<FF, std::initializer_list<T>> takewhile(
FF, std::initializer_list<T>);
// Value constructor for use only in the takewhile function
TakeWhile(FilterFunc filter_func, Container container)
: container(std::forward<Container>(container)),
filter_func(filter_func)
{ }
TakeWhile () = delete;
TakeWhile& operator=(const TakeWhile&) = delete;
public:
TakeWhile(const TakeWhile&) = default;
class Iterator {
private:
using iter_type = iterator_type<Container>;
iterator_type<Container> sub_iter;
const iterator_type<Container> sub_end;
FilterFunc filter_func;
// check if the current value is true under the predicate
// if it is not, set the sub_iter to the end using
// placement new to avoid the requirement of the iterator
// having an operator=
void check_current() {
if (!this->filter_func(*this->sub_iter)) {
this->sub_iter.~iter_type();
new(&this->sub_iter) iterator_type<Container>(
this->sub_end);
}
}
public:
Iterator (iterator_type<Container> iter,
iterator_type<Container> end,
FilterFunc filter_func)
: sub_iter{iter},
sub_end{end},
filter_func(filter_func)
{
if (this->sub_iter != this->sub_end) {
// only do the check if not already at the end
this->check_current();
}
}
iterator_deref<Container> operator*() const {
return *this->sub_iter;
}
Iterator& operator++() {
++this->sub_iter;
this->check_current();
return *this;
}
bool operator!=(const Iterator& other) const {
return this->sub_iter != other.sub_iter;
}
};
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};
}
};
// Helper function to instantiate a TakeWhile
template <typename FilterFunc, typename Container>
TakeWhile<FilterFunc, Container> takewhile(
FilterFunc filter_func, Container&& container) {
return {filter_func, std::forward<Container>(container)};
}
template <typename FilterFunc, typename T>
TakeWhile<FilterFunc, std::initializer_list<T>> takewhile(
FilterFunc filter_func, std::initializer_list<T> il)
{
return {filter_func, std::move(il)};
}
}
#endif //ifndef TAKEWHILE__H__