Debian Codegraph @

3rd_party/range-v3/range/v3/algorithm/for_each_n.hpp

raw
1 /// \file
2 // Range v3 library
3 //
4 // Copyright Eric Niebler 2014-present
5 // Copyright Rostislav Khlebnikov 2017
6 //
7 // Use, modification and distribution is subject to the
8 // Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
11 //
12 // Project home: https://github.com/ericniebler/range-v3
13 //
14 #ifndef RANGES_V3_ALGORITHM_FOR_EACH_N_HPP
15 #define RANGES_V3_ALGORITHM_FOR_EACH_N_HPP
16
17 #include <functional>
18
19 #include <range/v3/range_fwd.hpp>
20
21 #include <range/v3/algorithm/result_types.hpp>
22 #include <range/v3/functional/identity.hpp>
23 #include <range/v3/functional/invoke.hpp>
24 #include <range/v3/iterator/operations.hpp>
25 #include <range/v3/iterator/traits.hpp>
26 #include <range/v3/range/concepts.hpp>
27 #include <range/v3/range/dangling.hpp>
28 #include <range/v3/range/traits.hpp>
29 #include <range/v3/utility/static_const.hpp>
30
31 #include <range/v3/detail/prologue.hpp>
32
33 namespace ranges
34 {
35 /// \addtogroup group-algorithms
36 /// @{
37 RANGES_FUNC_BEGIN(for_each_n)
38
39 /// \brief function template \c for_each_n
40 template(typename I, typename F, typename P = identity)(
41 requires input_iterator<I> AND
42 indirectly_unary_invocable<F, projected<I, P>>)
43 constexpr I RANGES_FUNC(for_each_n)(I first, iter_difference_t<I> n, F fun, P proj = P{})
44 {
45 RANGES_EXPECT(0 <= n);
46 auto norig = n;
47 auto b = uncounted(first);
48 for(; 0 < n; ++b, --n)
49 invoke(fun, invoke(proj, *b));
50 return recounted(first, b, norig);
51 }
52
53 /// \overload
54 template(typename Rng, typename F, typename P = identity)(
55 requires input_range<Rng> AND
56 indirectly_unary_invocable<F, projected<iterator_t<Rng>, P>>)
57 constexpr borrowed_iterator_t<Rng> RANGES_FUNC(for_each_n)(
58 Rng && rng, range_difference_t<Rng> n, F fun, P proj = P{})
59 {
60 if(sized_range<Rng>)
61 RANGES_EXPECT(n <= distance(rng));
62
63 return (*this)(begin(rng), n, detail::move(fun), detail::move(proj));
64 }
65
66 RANGES_FUNC_END(for_each_n)
67
68 namespace cpp20
69 {
70 using ranges::for_each_n;
71 }
72 /// @}
73 } // namespace ranges
74
75 #include <range/v3/detail/epilogue.hpp>
76
77 #endif