find_if.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright Neil Groves 2009. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // For more information, see http://www.boost.org/libs/range/
  8. //
  9. #ifndef BOOST_RANGE_ALGORITHM_FIND_IF_HPP_INCLUDED
  10. #define BOOST_RANGE_ALGORITHM_FIND_IF_HPP_INCLUDED
  11. #include <boost/concept_check.hpp>
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. #include <boost/range/concepts.hpp>
  15. #include <boost/range/detail/range_return.hpp>
  16. #include <algorithm>
  17. namespace boost
  18. {
  19. namespace range
  20. {
  21. /// \brief template function find_if
  22. ///
  23. /// range-based version of the find_if std algorithm
  24. ///
  25. /// \pre SinglePassRange is a model of the SinglePassRangeConcept
  26. /// \pre UnaryPredicate is a model of the UnaryPredicateConcept
  27. template< class SinglePassRange, class UnaryPredicate >
  28. inline BOOST_DEDUCED_TYPENAME disable_if<
  29. is_const<SinglePassRange>,
  30. BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange>::type
  31. >::type
  32. find_if( SinglePassRange& rng, UnaryPredicate pred )
  33. {
  34. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange> ));
  35. return std::find_if(boost::begin(rng), boost::end(rng), pred);
  36. }
  37. /// \overload
  38. template< class SinglePassRange, class UnaryPredicate >
  39. inline BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange>::type
  40. find_if( const SinglePassRange& rng, UnaryPredicate pred )
  41. {
  42. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> ));
  43. return std::find_if(boost::begin(rng), boost::end(rng), pred);
  44. }
  45. // range_return overloads
  46. /// \overload
  47. template< range_return_value re, class SinglePassRange, class UnaryPredicate >
  48. inline BOOST_DEDUCED_TYPENAME disable_if<
  49. is_const<SinglePassRange>,
  50. BOOST_DEDUCED_TYPENAME range_return<SinglePassRange,re>::type
  51. >::type
  52. find_if( SinglePassRange& rng, UnaryPredicate pred )
  53. {
  54. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange> ));
  55. return range_return<SinglePassRange,re>::
  56. pack(std::find_if(boost::begin(rng), boost::end(rng), pred),
  57. rng);
  58. }
  59. /// \overload
  60. template< range_return_value re, class SinglePassRange, class UnaryPredicate >
  61. inline BOOST_DEDUCED_TYPENAME range_return<const SinglePassRange,re>::type
  62. find_if( const SinglePassRange& rng, UnaryPredicate pred )
  63. {
  64. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> ));
  65. return range_return<const SinglePassRange,re>::
  66. pack(std::find_if(boost::begin(rng), boost::end(rng), pred),
  67. rng);
  68. }
  69. } // namespace range
  70. using range::find_if;
  71. } // namespace boost
  72. #endif // include guard