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.

74 satır
2.5KB

  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_ADJACENT_FIND_HPP
  14. #define RANGES_V3_ALGORITHM_ADJACENT_FIND_HPP
  15. #include <range/v3/range_fwd.hpp>
  16. #include <range/v3/functional/comparisons.hpp>
  17. #include <range/v3/functional/identity.hpp>
  18. #include <range/v3/functional/invoke.hpp>
  19. #include <range/v3/iterator/traits.hpp>
  20. #include <range/v3/range/access.hpp>
  21. #include <range/v3/range/concepts.hpp>
  22. #include <range/v3/range/dangling.hpp>
  23. #include <range/v3/range/traits.hpp>
  24. #include <range/v3/utility/static_const.hpp>
  25. namespace ranges
  26. {
  27. /// \addtogroup group-algorithms
  28. /// @{
  29. RANGES_BEGIN_NIEBLOID(adjacent_find)
  30. /// \brief function template \c adjacent_find
  31. ///
  32. /// range-based version of the \c adjacent_find std algorithm
  33. ///
  34. /// \pre `Rng` is a model of the `Range` concept
  35. /// \pre `C` is a model of the `BinaryPredicate` concept
  36. template<typename I, typename S, typename C = equal_to, typename P = identity>
  37. auto RANGES_FUN_NIEBLOID(adjacent_find)(
  38. I first, S last, C pred = C{}, P proj = P{}) //
  39. ->CPP_ret(I)( //
  40. requires forward_iterator<I> && sentinel_for<S, I> &&
  41. indirect_relation<C, projected<I, P>>)
  42. {
  43. if(first == last)
  44. return first;
  45. auto inext = first;
  46. for(; ++inext != last; first = inext)
  47. if(invoke(pred, invoke(proj, *first), invoke(proj, *inext)))
  48. return first;
  49. return inext;
  50. }
  51. /// \overload
  52. template<typename Rng, typename C = equal_to, typename P = identity>
  53. auto RANGES_FUN_NIEBLOID(adjacent_find)(Rng && rng, C pred = C{}, P proj = P{}) //
  54. ->CPP_ret(safe_iterator_t<Rng>)( //
  55. requires forward_range<Rng> &&
  56. indirect_relation<C, projected<iterator_t<Rng>, P>>)
  57. {
  58. return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
  59. }
  60. RANGES_END_NIEBLOID(adjacent_find)
  61. namespace cpp20
  62. {
  63. using ranges::adjacent_find;
  64. }
  65. /// @}
  66. } // namespace ranges
  67. #endif // RANGE_ALGORITHM_ADJACENT_FIND_HPP