find.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2014-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_ALGORITHM_FIND_HPP
  14. #define RANGES_V3_ALGORITHM_FIND_HPP
  15. #include <utility>
  16. #include <range/v3/range_fwd.hpp>
  17. #include <range/v3/functional/identity.hpp>
  18. #include <range/v3/functional/invoke.hpp>
  19. #include <range/v3/iterator/concepts.hpp>
  20. #include <range/v3/iterator/traits.hpp>
  21. #include <range/v3/range/access.hpp>
  22. #include <range/v3/range/concepts.hpp>
  23. #include <range/v3/range/dangling.hpp>
  24. #include <range/v3/range/traits.hpp>
  25. #include <range/v3/utility/static_const.hpp>
  26. #include <range/v3/detail/prologue.hpp>
  27. namespace ranges
  28. {
  29. /// \addtogroup group-algorithms
  30. /// @{
  31. RANGES_FUNC_BEGIN(find)
  32. /// \brief template function \c find
  33. ///
  34. /// range-based version of the \c find std algorithm
  35. ///
  36. /// \pre `Rng` is a model of the `range` concept
  37. /// \pre `I` is a model of the `input_iterator` concept
  38. /// \pre `S` is a model of the `sentinel_for<I>` concept
  39. /// \pre `P` is a model of the `invocable<iter_common_reference_t<I>>` concept
  40. /// \pre The ResultType of `P` is equality_comparable with V
  41. template(typename I, typename S, typename V, typename P = identity)(
  42. requires input_iterator<I> AND sentinel_for<S, I> AND
  43. indirect_relation<equal_to, projected<I, P>, V const *>)
  44. constexpr I RANGES_FUNC(find)(I first, S last, V const & val, P proj = P{})
  45. {
  46. for(; first != last; ++first)
  47. if(invoke(proj, *first) == val)
  48. break;
  49. return first;
  50. }
  51. /// \overload
  52. template(typename Rng, typename V, typename P = identity)(
  53. requires input_range<Rng> AND
  54. indirect_relation<equal_to, projected<iterator_t<Rng>, P>, V const *>)
  55. constexpr borrowed_iterator_t<Rng> //
  56. RANGES_FUNC(find)(Rng && rng, V const & val, P proj = P{})
  57. {
  58. return (*this)(begin(rng), end(rng), val, std::move(proj));
  59. }
  60. RANGES_FUNC_END(find)
  61. namespace cpp20
  62. {
  63. using ranges::find;
  64. }
  65. /// @}
  66. } // namespace ranges
  67. #include <range/v3/detail/epilogue.hpp>
  68. #endif