summaryrefslogtreecommitdiff
path: root/boost/spirit/home/phoenix/stl/algorithm/querying.hpp
blob: 87456fd7ff6f3d3af3a76d510ecb7e34b2acfb4b (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Copyright 2005 Daniel Wallin. 
// Copyright 2005 Joel de Guzman.
// Copyright 2005 Dan Marsden. 
// Copyright 2008 Hartmut Kaiser. 
//
// Use, modification and distribution is subject to the Boost Software 
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Modeled after range_ex, Copyright 2004 Eric Niebler

#ifndef PHOENIX_ALGORITHM_QUERYING_HPP
#define PHOENIX_ALGORITHM_QUERYING_HPP

#include <algorithm>

#include <boost/spirit/home/phoenix/stl/algorithm/detail/has_find.hpp>
#include <boost/spirit/home/phoenix/stl/algorithm/detail/has_lower_bound.hpp>
#include <boost/spirit/home/phoenix/stl/algorithm/detail/has_upper_bound.hpp>
#include <boost/spirit/home/phoenix/stl/algorithm/detail/has_equal_range.hpp>

#include <boost/spirit/home/phoenix/stl/algorithm/detail/begin.hpp>
#include <boost/spirit/home/phoenix/stl/algorithm/detail/end.hpp>
#include <boost/spirit/home/phoenix/stl/algorithm/detail/decay_array.hpp>

#include <boost/spirit/home/phoenix/function/function.hpp>

#include <boost/range/result_iterator.hpp>
#include <boost/range/difference_type.hpp>

namespace boost { namespace phoenix {
namespace impl
{
    struct find
    {
        template<class R, class T>
        struct result : range_result_iterator<R>
        {};

        template<class R, class T>
        typename result<R, T>::type execute(R& r, T const& x, mpl::true_) const
        {
            return r.find(x);
        }

        template<class R, class T>
        typename result<R, T>::type execute(R& r, T const& x, mpl::false_) const
        {
            return std::find(detail::begin_(r), detail::end_(r), x);
        }

        template<class R, class T>
        typename result<R, T>::type operator()(R& r, T const& x) const
        {
            return execute(r, x, has_find<R>());
        }
    };

    struct find_if
    {
        template<class R, class P>
        struct result : range_result_iterator<R>
        {};

        template<class R, class P>
        typename result<R, P>::type operator()(R& r, P p) const
        {
            return std::find_if(detail::begin_(r), detail::end_(r), p);
        }
    };

    struct find_end
    {
        template<class R, class R2, class P = void>
        struct result : range_result_iterator<R>
        {};

        template<class R, class R2>
        typename result<R, R2>::type operator()(R& r, R2& r2) const
        {
            return std::find_end(
                detail::begin_(r)
                , detail::end_(r)
                , detail::begin_(r2)
                , detail::end_(r2)
                );
        }

        template<class R, class R2, class P>
        typename result<R, R2, P>::type operator()(R& r, R2& r2, P p) const
        {
            return std::find_end(
                detail::begin_(r)
                , detail::end_(r)
                , detail::begin_(r2)
                , detail::end_(r2)
                , p
                );
        }
    };

    struct find_first_of
    {
        template<class R, class R2, class P = void>
        struct result : range_result_iterator<R>
        {};

        template<class R, class R2>
        typename result<R, R2>::type operator()(R& r, R2& r2) const
        {
            return std::find_first_of(
                detail::begin_(r)
                , detail::end_(r)
                , detail::begin_(r2)
                , detail::end_(r2)
                );
        }

        template<class R, class R2, class P>
        typename result<R, R2, P>::type operator()(R& r, R2& r2, P p) const
        {
            return std::find_first_of(
                detail::begin_(r)
                , detail::end_(r)
                , detail::begin_(r2)
                , detail::end_(r2)
                , p
                );
        }
    };

    struct adjacent_find
    {
        template<class R, class P = void>
        struct result : range_result_iterator<R>
        {};

        template<class R>
        typename result<R>::type operator()(R& r) const
        {
            return std::adjacent_find(detail::begin_(r), detail::end_(r));
        }

        template<class R, class P>
        typename result<R, P>::type operator()(R& r, P p) const
        {
            return std::adjacent_find(detail::begin_(r), detail::end_(r), p);
        }
    };

    struct count
    {
        template<class R, class T>
        struct result : range_difference<R>
        {};

        template<class R, class T>
        typename result<R, T>::type operator()(R& r, T const& x) const
        {
            return std::count(detail::begin_(r), detail::end_(r), x);
        }
    };

    struct count_if
    {
        template<class R, class P>
        struct result : range_difference<R>
        {};

        template<class R, class P>
        typename result<R, P>::type operator()(R& r, P p) const
        {
            return std::count_if(detail::begin_(r), detail::end_(r), p);
        }
    };

    struct distance
    {
        template<class R>
        struct result : range_difference<R>
        {};

        template<class R>
        typename result<R>::type operator()(R& r) const
        {
            return std::distance(detail::begin_(r), detail::end_(r));
        }
    };

    struct equal
    {
        template<class R, class I, class P = void>
        struct result
        {
            typedef bool type;
        };

        template<class R, class I>
        bool operator()(R& r, I i) const
        {
            return std::equal(detail::begin_(r), detail::end_(r), i);
        }

        template<class R, class I, class P>
        bool operator()(R& r, I i, P p) const
        {
            return std::equal(detail::begin_(r), detail::end_(r), i, p);
        }
    };

    struct search
    {
        template<class R, class R2, class P = void>
        struct result : range_result_iterator<R>
        {};

        template<class R, class R2>
        typename result<R, R2>::type operator()(R& r, R2& r2) const
        {
            return std::search(
                detail::begin_(r)
                , detail::end_(r)
                , detail::begin_(r2)
                , detail::end_(r2)
                );
        }

        template<class R, class R2, class P>
        typename result<R, R2, P>::type operator()(R& r, R2& r2, P p) const
        {
            return std::search(
                detail::begin_(r)
                , detail::end_(r)
                , detail::begin_(r2)
                , detail::end_(r2)
                , p
                );
        }
    };

    struct lower_bound 
    {
        template<class R, class T, class C = void>
        struct result : range_result_iterator<R>
        {};

        template<class R, class T>
        typename result<R, T>::type execute(R& r, T const& val, mpl::true_) const
        {
            return r.lower_bound(val);
        }

        template<class R, class T>
        typename result<R, T>::type execute(R& r, T const& val, mpl::false_) const
        {
            return std::lower_bound(detail::begin_(r), detail::end_(r), val);
        }

        template<class R, class T>
        typename result<R, T>::type operator()(R& r, T const& val) const
        {
            return execute(r, val, has_lower_bound<R>());
        }

        template<class R, class T, class C>
        typename result<R, T>::type operator()(R& r, T const& val, C c) const
        {
            return std::lower_bound(detail::begin_(r), detail::end_(r), val, c);
        }
    };

    struct upper_bound 
    {
        template<class R, class T, class C = void>
        struct result : range_result_iterator<R>
        {};

        template<class R, class T>
        typename result<R, T>::type execute(R& r, T const& val, mpl::true_) const
        {
            return r.upper_bound(val);
        }

        template<class R, class T>
        typename result<R, T>::type execute(R& r, T const& val, mpl::false_) const
        {
            return std::upper_bound(detail::begin_(r), detail::end_(r), val);
        }

        template<class R, class T>
        typename result<R, T>::type operator()(R& r, T const& val) const
        {
            return execute(r, val, has_upper_bound<R>());
        }

        template<class R, class T, class C>
        typename result<R, T>::type operator()(R& r, T const& val, C c) const
        {
            return std::upper_bound(detail::begin_(r), detail::end_(r), val, c);
        }
    };

    struct equal_range 
    {
        template<class R, class T, class C = void>
        struct result
        {
            typedef std::pair<
                typename range_result_iterator<R>::type
                , typename range_result_iterator<R>::type
            > type;
        };

        template<class R, class T>
        typename result<R, T>::type execute(R& r, T const& val, mpl::true_) const
        {
            return r.equal_range(val);
        }

        template<class R, class T>
        typename result<R, T>::type execute(R& r, T const& val, mpl::false_) const
        {
            return std::equal_range(detail::begin_(r), detail::end_(r), val);
        }

        template<class R, class T>
        typename result<R, T>::type operator()(R& r, T const& val) const
        {
            return execute(r, val, has_equal_range<R>());
        }

        template<class R, class T, class C>
        typename result<R, T>::type operator()(R& r, T const& val, C c) const
        {
            return std::equal_range(detail::begin_(r), detail::end_(r), val, c);
        }
    };

    struct mismatch
    {
        template<class R, class I, class P = void>
        struct result
        {
            typedef std::pair<
                typename range_result_iterator<R>::type
                , typename detail::decay_array<I>::type
            > type;
        };

        template<class R, class I>
        typename result<R, I>::type operator()(R& r, I i) const
        {
            return std::mismatch(detail::begin_(r), detail::end_(r), i);
        }

        template<class R, class I, class P>
        typename result<R, I, P>::type operator()(R& r, I i, P p) const
        {
            return std::mismatch(detail::begin_(r), detail::end_(r), i, p);
        }
    };

    struct binary_search 
    {
        template<class R, class T, class C = void>
        struct result
        {
            typedef bool type;
        };

        template<class R, class T>
        bool operator()(R& r, T const& val) const
        {
            return std::binary_search(detail::begin_(r), detail::end_(r), val);
        }

        template<class R, class T, class C>
        bool operator()(R& r, T const& val, C c) const
        {
            return std::binary_search(detail::begin_(r), detail::end_(r), val, c);
        }
    };

    struct includes 
    {
        template<class R1, class R2, class C = void>
        struct result
        {
            typedef bool type;
        };

        template<class R1, class R2>
        bool operator()(R1& r1, R2& r2) const
        {
            return std::includes(
                detail::begin_(r1), detail::end_(r1)
                , detail::begin_(r2), detail::end_(r2)
                );
        }

        template<class R1, class R2, class C>
        bool operator()(R1& r1, R2& r2, C c) const
        {
            return std::includes(
                detail::begin_(r1), detail::end_(r1)
                , detail::begin_(r2), detail::end_(r2)
                , c
                );
        }
    };

    struct min_element
    {
        template<class R, class P = void>
        struct result : range_result_iterator<R>
        {};

        template<class R>
        typename result<R>::type operator()(R& r) const
        {
            return std::min_element(detail::begin_(r), detail::end_(r));
        }
    
        template<class R, class P>
        typename result<R, P>::type operator()(R& r, P p) const
        {
            return std::min_element(detail::begin_(r), detail::end_(r), p);
        }
    };

    struct max_element
    {
        template<class R, class P = void>
        struct result : range_result_iterator<R>
        {};

        template<class R>
        typename result<R>::type operator()(R& r) const
        {
            return std::max_element(detail::begin_(r), detail::end_(r));
        }
    
        template<class R, class P>
        typename result<R, P>::type operator()(R& r, P p) const
        {
            return std::max_element(detail::begin_(r), detail::end_(r), p);
        }
    };

    struct lexicographical_compare
    {
        template<class R1, class R2, class P = void>
        struct result
        {
            typedef bool type;
        };

        template<class R1, class R2>
        typename result<R1, R2>::type operator()(R1& r1, R2& r2) const
        {
            return std::lexicographical_compare(
                detail::begin_(r1), detail::end_(r1)
                , detail::begin_(r2), detail::end_(r2)
                );
        }
    
        template<class R1, class R2, class P>
        typename result<R1, R2>::type operator()(R1& r1, R2& r2, P p) const
        {
            return std::lexicographical_compare(
                detail::begin_(r1), detail::end_(r1)
                , detail::begin_(r2), detail::end_(r2)
                , p
                );
        }
    };

}

function<impl::find> const find = impl::find();
function<impl::find_if> const find_if = impl::find_if();
function<impl::find_end> const find_end = impl::find_end();
function<impl::find_first_of> const find_first_of = impl::find_first_of();
function<impl::adjacent_find> const adjacent_find = impl::adjacent_find();
function<impl::count> const count = impl::count();
function<impl::count_if> const count_if = impl::count_if();
function<impl::distance> const distance = impl::distance();
function<impl::equal> const equal = impl::equal();
function<impl::search> const search = impl::search();
function<impl::lower_bound> const lower_bound = impl::lower_bound();
function<impl::upper_bound> const upper_bound = impl::upper_bound();
function<impl::equal_range> const equal_range = impl::equal_range();
function<impl::mismatch> const mismatch = impl::mismatch();
function<impl::binary_search> const binary_search = impl::binary_search();
function<impl::includes> const includes = impl::includes();
function<impl::min_element> const min_element = impl::min_element();
function<impl::max_element> const max_element = impl::max_element();
function<impl::lexicographical_compare> const lexicographical_compare = impl::lexicographical_compare();

}}

#endif