1 | // Boost.Range library |
---|
2 | // |
---|
3 | // Copyright Thorsten Ottosen, Neil Groves 2006 - 2008. Use, modification and |
---|
4 | // distribution is subject to the Boost Software License, Version |
---|
5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
6 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
7 | // |
---|
8 | // For more information, see http://www.boost.org/libs/range/ |
---|
9 | // |
---|
10 | |
---|
11 | #ifndef BOOST_RANGE_ADAPTOR_FILTERED_HPP |
---|
12 | #define BOOST_RANGE_ADAPTOR_FILTERED_HPP |
---|
13 | |
---|
14 | #include <boost/range/adaptor/argument_fwd.hpp> |
---|
15 | #include <boost/range/iterator_range.hpp> |
---|
16 | #include <boost/iterator/filter_iterator.hpp> |
---|
17 | |
---|
18 | namespace boost |
---|
19 | { |
---|
20 | namespace range_detail |
---|
21 | { |
---|
22 | template< class P, class R > |
---|
23 | struct filter_range : |
---|
24 | boost::iterator_range< |
---|
25 | boost::filter_iterator< P, |
---|
26 | BOOST_DEDUCED_TYPENAME range_iterator<R>::type |
---|
27 | > |
---|
28 | > |
---|
29 | { |
---|
30 | private: |
---|
31 | typedef boost::iterator_range< |
---|
32 | boost::filter_iterator< P, |
---|
33 | BOOST_DEDUCED_TYPENAME range_iterator<R>::type |
---|
34 | > |
---|
35 | > base; |
---|
36 | public: |
---|
37 | filter_range( P p, R& r ) |
---|
38 | : base( make_filter_iterator( p, boost::begin(r), boost::end(r) ), |
---|
39 | make_filter_iterator( p, boost::end(r), boost::end(r) ) ) |
---|
40 | { } |
---|
41 | }; |
---|
42 | |
---|
43 | template< class T > |
---|
44 | struct filter_holder : holder<T> |
---|
45 | { |
---|
46 | filter_holder( T r ) : holder<T>(r) |
---|
47 | { } |
---|
48 | }; |
---|
49 | |
---|
50 | template< class InputRng, class Predicate > |
---|
51 | inline filter_range<Predicate, InputRng> |
---|
52 | operator|( InputRng& r, |
---|
53 | const filter_holder<Predicate>& f ) |
---|
54 | { |
---|
55 | return filter_range<Predicate, InputRng>( f.val, r ); |
---|
56 | } |
---|
57 | |
---|
58 | template< class InputRng, class Predicate > |
---|
59 | inline filter_range<Predicate, const InputRng> |
---|
60 | operator|( const InputRng& r, |
---|
61 | const filter_holder<Predicate>& f ) |
---|
62 | { |
---|
63 | return filter_range<Predicate, const InputRng>( f.val, r ); |
---|
64 | } |
---|
65 | |
---|
66 | } // 'range_detail' |
---|
67 | |
---|
68 | // Unusual use of 'using' is intended to bring filter_range into the boost namespace |
---|
69 | // while leaving the mechanics of the '|' operator in range_detail and maintain |
---|
70 | // argument dependent lookup. |
---|
71 | // filter_range logically needs to be in the boost namespace to allow user of |
---|
72 | // the library to define the return type for filter() |
---|
73 | using range_detail::filter_range; |
---|
74 | |
---|
75 | namespace adaptors |
---|
76 | { |
---|
77 | namespace |
---|
78 | { |
---|
79 | const range_detail::forwarder<range_detail::filter_holder> |
---|
80 | filtered = |
---|
81 | range_detail::forwarder<range_detail::filter_holder>(); |
---|
82 | } |
---|
83 | |
---|
84 | template<class InputRange, class Predicate> |
---|
85 | inline filter_range<Predicate, InputRange> |
---|
86 | filter(InputRange& rng, Predicate filter_pred) |
---|
87 | { |
---|
88 | return range_detail::filter_range<Predicate, InputRange>( filter_pred, rng ); |
---|
89 | } |
---|
90 | |
---|
91 | template<class InputRange, class Predicate> |
---|
92 | inline filter_range<Predicate, const InputRange> |
---|
93 | filter(const InputRange& rng, Predicate filter_pred) |
---|
94 | { |
---|
95 | return range_detail::filter_range<Predicate, const InputRange>( filter_pred, rng ); |
---|
96 | } |
---|
97 | } // 'adaptors' |
---|
98 | |
---|
99 | } |
---|
100 | |
---|
101 | #endif |
---|