find.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_HPP_INCLUDED
  10. #define BOOST_RANGE_ALGORITHM_FIND_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
  22. ///
  23. /// range-based version of the find std algorithm
  24. ///
  25. /// \pre SinglePassRange is a model of the SinglePassRangeConcept
  26. template< class SinglePassRange, class Value >
  27. inline BOOST_DEDUCED_TYPENAME disable_if<
  28. is_const<SinglePassRange>,
  29. BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange>::type
  30. >::type
  31. find( SinglePassRange& rng, const Value& val )
  32. {
  33. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange> ));
  34. return std::find(boost::begin(rng), boost::end(rng), val);
  35. }
  36. /// \overload
  37. template< class SinglePassRange, class Value >
  38. inline BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange>::type
  39. find( const SinglePassRange& rng, const Value& val )
  40. {
  41. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> ));
  42. return std::find(boost::begin(rng), boost::end(rng), val);
  43. }
  44. // range_return overloads
  45. /// \overload
  46. template< range_return_value re, class SinglePassRange, class Value >
  47. inline BOOST_DEDUCED_TYPENAME disable_if<
  48. is_const<SinglePassRange>,
  49. BOOST_DEDUCED_TYPENAME range_return<SinglePassRange,re>::type
  50. >::type
  51. find( SinglePassRange& rng, const Value& val )
  52. {
  53. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange> ));
  54. return range_return<SinglePassRange,re>::
  55. pack(std::find(boost::begin(rng), boost::end(rng), val),
  56. rng);
  57. }
  58. /// \overload
  59. template< range_return_value re, class SinglePassRange, class Value >
  60. inline BOOST_DEDUCED_TYPENAME range_return<const SinglePassRange,re>::type
  61. find( const SinglePassRange& rng, const Value& val )
  62. {
  63. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> ));
  64. return range_return<const SinglePassRange,re>::
  65. pack(std::find(boost::begin(rng), boost::end(rng), val),
  66. rng);
  67. }
  68. } // namespace range
  69. using range::find;
  70. }
  71. #endif // include guard