filtered.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifndef BOOST_RANGE_ADAPTOR_FILTERED_HPP
  11. #define BOOST_RANGE_ADAPTOR_FILTERED_HPP
  12. #include <boost/range/adaptor/argument_fwd.hpp>
  13. #include <boost/range/detail/default_constructible_unary_fn.hpp>
  14. #include <boost/range/iterator_range.hpp>
  15. #include <boost/range/concepts.hpp>
  16. #include <boost/iterator/filter_iterator.hpp>
  17. namespace boost
  18. {
  19. namespace range_detail
  20. {
  21. template< class P, class R >
  22. struct filtered_range :
  23. boost::iterator_range<
  24. boost::filter_iterator<
  25. typename default_constructible_unary_fn_gen<P, bool>::type,
  26. typename range_iterator<R>::type
  27. >
  28. >
  29. {
  30. private:
  31. typedef boost::iterator_range<
  32. boost::filter_iterator<
  33. typename default_constructible_unary_fn_gen<P, bool>::type,
  34. typename range_iterator<R>::type
  35. >
  36. > base;
  37. public:
  38. typedef typename default_constructible_unary_fn_gen<P, bool>::type
  39. pred_t;
  40. filtered_range(P p, R& r)
  41. : base(make_filter_iterator(pred_t(p),
  42. boost::begin(r), boost::end(r)),
  43. make_filter_iterator(pred_t(p),
  44. boost::end(r), boost::end(r)))
  45. { }
  46. };
  47. template< class T >
  48. struct filter_holder : holder<T>
  49. {
  50. filter_holder( T r ) : holder<T>(r)
  51. { }
  52. };
  53. template< class SinglePassRange, class Predicate >
  54. inline filtered_range<Predicate, SinglePassRange>
  55. operator|(SinglePassRange& r,
  56. const filter_holder<Predicate>& f)
  57. {
  58. BOOST_RANGE_CONCEPT_ASSERT((SinglePassRangeConcept<SinglePassRange>));
  59. return filtered_range<Predicate, SinglePassRange>( f.val, r );
  60. }
  61. template< class SinglePassRange, class Predicate >
  62. inline filtered_range<Predicate, const SinglePassRange>
  63. operator|(const SinglePassRange& r,
  64. const filter_holder<Predicate>& f )
  65. {
  66. BOOST_RANGE_CONCEPT_ASSERT((
  67. SinglePassRangeConcept<const SinglePassRange>));
  68. return filtered_range<Predicate, const SinglePassRange>( f.val, r );
  69. }
  70. } // 'range_detail'
  71. // Unusual use of 'using' is intended to bring filter_range into the boost namespace
  72. // while leaving the mechanics of the '|' operator in range_detail and maintain
  73. // argument dependent lookup.
  74. // filter_range logically needs to be in the boost namespace to allow user of
  75. // the library to define the return type for filter()
  76. using range_detail::filtered_range;
  77. namespace adaptors
  78. {
  79. namespace
  80. {
  81. const range_detail::forwarder<range_detail::filter_holder>
  82. filtered =
  83. range_detail::forwarder<range_detail::filter_holder>();
  84. }
  85. template<class SinglePassRange, class Predicate>
  86. inline filtered_range<Predicate, SinglePassRange>
  87. filter(SinglePassRange& rng, Predicate filter_pred)
  88. {
  89. BOOST_RANGE_CONCEPT_ASSERT((
  90. SinglePassRangeConcept<SinglePassRange>));
  91. return range_detail::filtered_range<
  92. Predicate, SinglePassRange>( filter_pred, rng );
  93. }
  94. template<class SinglePassRange, class Predicate>
  95. inline filtered_range<Predicate, const SinglePassRange>
  96. filter(const SinglePassRange& rng, Predicate filter_pred)
  97. {
  98. BOOST_RANGE_CONCEPT_ASSERT((
  99. SinglePassRangeConcept<const SinglePassRange>));
  100. return range_detail::filtered_range<
  101. Predicate, const SinglePassRange>( filter_pred, rng );
  102. }
  103. } // 'adaptors'
  104. }
  105. #endif