File indexing completed on 2025-12-16 10:27:53
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef RANGES_V3_ALGORITHM_FOR_EACH_N_HPP
0015 #define RANGES_V3_ALGORITHM_FOR_EACH_N_HPP
0016
0017 #include <functional>
0018
0019 #include <range/v3/range_fwd.hpp>
0020
0021 #include <range/v3/algorithm/result_types.hpp>
0022 #include <range/v3/functional/identity.hpp>
0023 #include <range/v3/functional/invoke.hpp>
0024 #include <range/v3/iterator/operations.hpp>
0025 #include <range/v3/iterator/traits.hpp>
0026 #include <range/v3/range/concepts.hpp>
0027 #include <range/v3/range/dangling.hpp>
0028 #include <range/v3/range/traits.hpp>
0029 #include <range/v3/utility/static_const.hpp>
0030
0031 #include <range/v3/detail/prologue.hpp>
0032
0033 namespace ranges
0034 {
0035
0036
0037 RANGES_FUNC_BEGIN(for_each_n)
0038
0039
0040 template(typename I, typename F, typename P = identity)(
0041 requires input_iterator<I> AND
0042 indirectly_unary_invocable<F, projected<I, P>>)
0043 constexpr I RANGES_FUNC(for_each_n)(I first, iter_difference_t<I> n, F fun, P proj = P{})
0044 {
0045 RANGES_EXPECT(0 <= n);
0046 auto norig = n;
0047 auto b = uncounted(first);
0048 for(; 0 < n; ++b, --n)
0049 invoke(fun, invoke(proj, *b));
0050 return recounted(first, b, norig);
0051 }
0052
0053
0054 template(typename Rng, typename F, typename P = identity)(
0055 requires input_range<Rng> AND
0056 indirectly_unary_invocable<F, projected<iterator_t<Rng>, P>>)
0057 constexpr borrowed_iterator_t<Rng> RANGES_FUNC(for_each_n)(
0058 Rng && rng, range_difference_t<Rng> n, F fun, P proj = P{})
0059 {
0060 if(sized_range<Rng>)
0061 RANGES_EXPECT(n <= distance(rng));
0062
0063 return (*this)(begin(rng), n, detail::move(fun), detail::move(proj));
0064 }
0065
0066 RANGES_FUNC_END(for_each_n)
0067
0068 namespace cpp20
0069 {
0070 using ranges::for_each_n;
0071 }
0072
0073 }
0074
0075 #include <range/v3/detail/epilogue.hpp>
0076
0077 #endif