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