You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.6KB

  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-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_IF_HPP
  14. #define RANGES_V3_ALGORITHM_FIND_IF_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. namespace ranges
  27. {
  28. /// \addtogroup group-algorithms
  29. /// @{
  30. RANGES_BEGIN_NIEBLOID(find_if)
  31. /// \brief template function \c find
  32. ///
  33. /// range-based version of the \c find std algorithm
  34. ///
  35. /// \pre `Rng` is a model of the `Range` concept
  36. /// \pre `I` is a model of the `input_iterator` concept
  37. /// \pre `S` is a model of the `sentinel_for<I>` concept
  38. /// \pre `P` is a model of the `invocable<V>` concept, where `V` is the
  39. /// value type of I.
  40. /// \pre `F` models `predicate<X>`, where `X` is the result type
  41. /// of `invocable<P, V>`
  42. template<typename I, typename S, typename F, typename P = identity>
  43. auto RANGES_FUN_NIEBLOID(find_if)(I first, S last, F pred, P proj = P{}) //
  44. ->CPP_ret(I)( //
  45. requires input_iterator<I> && sentinel_for<S, I> &&
  46. indirect_unary_predicate<F, projected<I, P>>)
  47. {
  48. for(; first != last; ++first)
  49. if(invoke(pred, invoke(proj, *first)))
  50. break;
  51. return first;
  52. }
  53. /// \overload
  54. template<typename Rng, typename F, typename P = identity>
  55. auto RANGES_FUN_NIEBLOID(find_if)(Rng && rng, F pred, P proj = P{})
  56. ->CPP_ret(safe_iterator_t<Rng>)( //
  57. requires input_range<Rng> &&
  58. indirect_unary_predicate<F, projected<iterator_t<Rng>, P>>)
  59. {
  60. return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
  61. }
  62. RANGES_END_NIEBLOID(find_if)
  63. namespace cpp20
  64. {
  65. using ranges::find_if;
  66. }
  67. /// @}
  68. } // namespace ranges
  69. #endif // include guard