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)
8 // For more information, see http://www.boost.org/libs/range/
11 #ifndef BOOST_RANGE_ADAPTOR_FILTERED_HPP
12 #define BOOST_RANGE_ADAPTOR_FILTERED_HPP
14 #include <boost/range/adaptor/argument_fwd.hpp>
15 #include <boost/range/detail/default_constructible_unary_fn.hpp>
16 #include <boost/range/iterator_range.hpp>
17 #include <boost/range/concepts.hpp>
18 #include <boost/iterator/filter_iterator.hpp>
22 namespace range_detail
24 template< class P, class R >
25 struct filtered_range :
26 boost::iterator_range<
27 boost::filter_iterator<
28 typename default_constructible_unary_fn_gen<P, bool>::type,
29 typename range_iterator<R>::type
34 typedef boost::iterator_range<
35 boost::filter_iterator<
36 typename default_constructible_unary_fn_gen<P, bool>::type,
37 typename range_iterator<R>::type
41 typedef typename default_constructible_unary_fn_gen<P, bool>::type
44 filtered_range(P p, R& r)
45 : base(make_filter_iterator(pred_t(p),
46 boost::begin(r), boost::end(r)),
47 make_filter_iterator(pred_t(p),
48 boost::end(r), boost::end(r)))
53 struct filter_holder : holder<T>
55 filter_holder( T r ) : holder<T>(r)
59 template< class SinglePassRange, class Predicate >
60 inline filtered_range<Predicate, SinglePassRange>
61 operator|(SinglePassRange& r,
62 const filter_holder<Predicate>& f)
64 BOOST_RANGE_CONCEPT_ASSERT((SinglePassRangeConcept<SinglePassRange>));
65 return filtered_range<Predicate, SinglePassRange>( f.val, r );
68 template< class SinglePassRange, class Predicate >
69 inline filtered_range<Predicate, const SinglePassRange>
70 operator|(const SinglePassRange& r,
71 const filter_holder<Predicate>& f )
73 BOOST_RANGE_CONCEPT_ASSERT((
74 SinglePassRangeConcept<const SinglePassRange>));
75 return filtered_range<Predicate, const SinglePassRange>( f.val, r );
80 // Unusual use of 'using' is intended to bring filter_range into the boost namespace
81 // while leaving the mechanics of the '|' operator in range_detail and maintain
82 // argument dependent lookup.
83 // filter_range logically needs to be in the boost namespace to allow user of
84 // the library to define the return type for filter()
85 using range_detail::filtered_range;
91 const range_detail::forwarder<range_detail::filter_holder>
93 range_detail::forwarder<range_detail::filter_holder>();
96 template<class SinglePassRange, class Predicate>
97 inline filtered_range<Predicate, SinglePassRange>
98 filter(SinglePassRange& rng, Predicate filter_pred)
100 BOOST_RANGE_CONCEPT_ASSERT((
101 SinglePassRangeConcept<SinglePassRange>));
103 return range_detail::filtered_range<
104 Predicate, SinglePassRange>( filter_pred, rng );
107 template<class SinglePassRange, class Predicate>
108 inline filtered_range<Predicate, const SinglePassRange>
109 filter(const SinglePassRange& rng, Predicate filter_pred)
111 BOOST_RANGE_CONCEPT_ASSERT((
112 SinglePassRangeConcept<const SinglePassRange>));
114 return range_detail::filtered_range<
115 Predicate, const SinglePassRange>( filter_pred, rng );