-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpredicated_range.h
231 lines (161 loc) · 8.26 KB
/
predicated_range.h
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#ifndef RO_PREDICATED_RANGE_H
#define RO_PREDICATED_RANGE_H
#include <ro/basic_range.h>
#include <cassert>
namespace {
namespace ro {
// forward dcl
template< class Rg, class F = void*> struct predicated_range;
///////////////////////////////////////////////////////////////////////////////////////// PREDICATED_RANGE_ITERATOR
template <class Rg, class F, bool RO>
struct predicated_range_iterator : basic_range_iterator<Rg,RO> {
typedef basic_range_iterator<Rg,RO> b;
typedef SEL <
RO,
predicated_range<Rg,F> const,
predicated_range<Rg,F>
> parent_t;
//parent_t * const & parent_p = (parent_t*)(b::parent_p);
// STL ITERATOR TYPES
typedef typename parent_t::iterator iterator;
typedef typename parent_t::const_iterator const_iterator;
typedef rm_ref<predicated_range_iterator> self_type;
typedef typename b::iterator_category iterator_category;
typedef typename b::org_iterator org_iterator;
typedef typename parent_t::value_type value_type;
using typename b::size_type;
using typename b::difference_type;
using typename b::pointer;
typedef rg_const_reference<Rg> const_reference;
typedef typename std::conditional <
std::is_const<Rg>::value || RO,
rg_const_reference<Rg>,
rg_reference<Rg>
>::type reference;
// non-STL
using typename b::elem_type;
////// MEMBERS
////// CTOR
predicated_range_iterator (const self_type& rhs) : b(rhs) {}; // copy
predicated_range_iterator (parent_t* parent_p, const org_iterator current) : b(parent_p, current) {};
////// CONVERSION non-const --> const
//operator predicated_range_iterator<Rg&&,F,true>() { return predicated_range_iterator<Rg&&,F,true>(parent_p, b::current); };
operator predicated_range_iterator<Rg&&,F,true>() { return predicated_range_iterator<Rg&&,F,true>((parent_t*)b::parent_p, b::current); };
////// IFACE
reference operator*() { return *b::current; };
const_reference operator*() const { return *b::current; };
pointer operator->() { return &(operator*()); }
pointer const operator->() const { return &(operator*()); }
// ++ It
self_type& operator++() { move_pos(); return *this; }
// ++ It
self_type operator++(int) {
org_iterator e = endz(b::parent_p->rg); assert(b::current !=e);
self_type tmp=*this;
move_pos();
return std::move(tmp);
}
bool operator==(const_iterator rhs) const { return b::current == rhs.current; }
bool operator!=(const_iterator rhs) const { return b::current != rhs.current; }
///////////////////////////////////////////////////////////////////// INPORT ORG_ITERATOR METHODS
/*
// bidiractional
using b::operator--; // TOFIX
// random access
using b::operator+=; // TOFIX
using b::operator-=; // TOFIX
using b::operator[]; // TOFIX
using b::operator<;
using b::operator<=;
using b::operator>;
using b::operator>=;
*/
void move_pos() { // move current pos
org_iterator e = endz(b::parent_p->rg);
assert(b::current !=e);
b::current = std::find_if(++b::current, e, ((parent_t*)b::parent_p)->f);
}
};
///////////////////////////////////////////////////////////////////////////////////////// PREDICATED_RANGE
template<class Rg, class F>
struct predicated_range : basic_range<Rg> {
typedef basic_range<Rg> b;
// STL IFACE
typedef rg_elem_type<Rg> value_type;
typedef predicated_range_iterator<Rg,F,false> iterator;
typedef predicated_range_iterator<Rg,F,true> const_iterator;
typedef size_t size_type;
typedef ptrdiff_t difference_type ;
typedef rm_ref<value_type>* pointer;
typedef rg_const_reference<Rg> const_reference; // non-STL
typedef typename std::conditional <
std::is_const<Rg>::value /* || RO*/, // <-- different from interator
rg_const_reference<Rg>,
rg_reference<Rg>
>::type reference;
// non-STL
typedef rg_elem_type<Rg> elem_type;
typedef predicated_range self_type;
// MEMBERS
F f;
//// CTOR
explicit predicated_range (Rg&& rg, F f) : b(std::forward<Rg>(rg)), f(f) {};
//// ASSIGNMENT
self_type& operator= (elem_type x) { std::fill(begin(), end(), x); return *this; };
/*
template<class Rg2> // TODO specialize for seq containers self-assignemet
eIF <have_same_elems<Rg,Rg2>::value, self_type>
operator= (const Rg2& rhs) { //std:: cout << " SSIGNMENT \n";
//ro::clear(rg);
auto e = endz(rhs);
for (auto it = std::begin(rhs); it != e; ++it) {
detail::append_elem(std::forward<Rg>(rg), *it);
}
return *this;
};
*/
//// ITERATOR
iterator end() { return iterator(this, endz(b::rg)); }
const_iterator end() const { return const_iterator(this, endz(b::rg)); }
iterator begin() { return iterator(this, std::find_if(std::begin(b::rg), endz(b::rg), f)); };
const_iterator begin() const { return const_iterator(this, std::find_if(std::begin(b::rg), endz(b::rg), f)); };
//// RG PROPERTIES
size_type size () const { return std::count_if(std::begin(b::rg), endz(b::rg), f); }
bool empty () const { return size()==0; }
explicit operator bool() const { return !empty(); }
//// ELEM ACCESS
reference const front() const { return *begin(); }
reference front() { return *begin(); }
reference const back() const { return *std::prev(ro::endz(b::rg)); } // TOFIX
reference back() { return *std::prev(ro::endz(b::rg)); } // TOFIX
//// INPORTED RG METHODS
/* filtered should not have (?) these methods
using b::push_front;
using b::push_back;
using b::insert;
using b::pop_front;
using b::pop_back;
using b::operator[];
*/
// conversion to stl-containers
#include <ro/conversion.h>
};
//////////////////////////////////////////////////////////////// TRAITS
template<class Rg, class F> struct is_range_t<predicated_range<Rg,F>> : std::true_type {};
template<class Rg, class F, bool RO> struct is_range_t<predicated_range_iterator<Rg,F,RO>> : std::false_type {};
template<class Rg, class F> struct is_ro_range<predicated_range<Rg,F>> : std::true_type {};
template<class Rg, class F, bool RO> struct is_ro_range_iterator <predicated_range_iterator<Rg,F,RO>> : std::true_type {};
//////////////////////////////////////////////////////////////// PREDICATED
// Rg1 | Pred --> range
template<class Rg> eIF<is_range<Rg>::value, predicated_range<Rg&&,std::function<bool(const rg_elem_type<Rg>&)> >> operator| (Rg&& rg, std::function<bool(const rg_elem_type<Rg>&)> pred){ return predicated_range<Rg&&,std::function<bool(const rg_elem_type<Rg>&)>> (std::forward<Rg>(rg), pred); };
template<class Rg> eIF<is_range<Rg>::value, predicated_range<Rg&&,std::function<bool(rg_elem_type<Rg>)> >> operator| (Rg&& rg, std::function<bool(rg_elem_type<Rg>)> pred){ return predicated_range<Rg&&,std::function<bool(rg_elem_type<Rg>)> > (std::forward<Rg>(rg), pred); };
template<class Rg> eIF<is_range<Rg>::value, predicated_range<Rg&&,bool(*)(const rg_elem_type<Rg>&) >> operator| (Rg&& rg, bool(pred)(const rg_elem_type<Rg>&) ){ return predicated_range<Rg&&,bool(*)(const rg_elem_type<Rg>&) > (std::forward<Rg>(rg), pred); };
template<class Rg> eIF<is_range<Rg>::value, predicated_range<Rg&&,bool(*)(rg_elem_type<Rg>) >> operator| (Rg&& rg, bool(pred)(rg_elem_type<Rg>) ){ return predicated_range<Rg&&,bool(*)(rg_elem_type<Rg>) > (std::forward<Rg>(rg), pred); };
template<class Rg, class F> eIF<is_range<Rg>::value && is_callable<F, bool(rg_elem_type<Rg>)>::value,
predicated_range<Rg&&, F>> operator| (Rg&& rg, F pred) { return predicated_range<Rg&&,F> (std::forward<Rg>(rg), pred); };
// Rg1 | elem_type --> range
template<class Rg> eIF<is_range<Rg>::value, predicated_range<Rg&&, std::function<bool(rg_elem_type<Rg>)> >> operator| (Rg&& rg, const rg_elem_type<Rg>& value) { return predicated_range<Rg&&, std::function<bool(rg_elem_type<Rg>)>> (std::forward<Rg>(rg), [value](const rg_elem_type<Rg>& el){return el==value;}); };
// Overload is better than SFINAE selection. With OL we do not need to specify functor template arguments
};
};
#endif // RO_MAPPED_RANGE_H