VCCC  2024.05
VisualCamp Common C++ library
for_each_n.hpp
Go to the documentation of this file.
1 //
2 // Created by yonggyulee on 1/26/24.
3 //
4 
5 #ifndef VCCC_ALGORITHM_RANGES_IN_FOR_EACH_N_HPP
6 #define VCCC_ALGORITHM_RANGES_IN_FOR_EACH_N_HPP
7 
8 #include <functional>
9 #include <type_traits>
10 #include <utility>
11 
21 
22 namespace vccc {
23 namespace ranges {
24 
27 
28 template<typename I, typename F>
30 
31 namespace detail {
32 
33 struct for_each_n_niebloid {
34  private:
36  struct check : std::false_type {};
37  template<typename I, typename Proj, typename Fun>
38  struct check<I, Proj, Fun, true> : indirectly_unary_invocable<Fun, projected<I, Proj>> {};
39 
40  public:
41  template<typename I, typename Proj = identity, typename Fun, std::enable_if_t<conjunction<
43  check<I, Proj, Fun>
44  >::value, int> = 0>
46  operator()(I first, iter_difference_t<I> n, Fun f, Proj proj = {}) const {
47  for (; n-- > 0; ++first) {
48  vccc::invoke(f, vccc::invoke(proj, *first));
49  }
50  return {std::move(first), std::move(f)};
51  }
52 };
53 
54 } // namespace detail
55 
56 VCCC_INLINE_OR_STATIC constexpr detail::for_each_n_niebloid for_each_n{};
57 
59 
60 } // namespace ranges
61 } // namespace vccc
62 
63 #endif // VCCC_ALGORITHM_RANGES_IN_FOR_EACH_N_HPP
constexpr VCCC_INLINE_OR_STATIC detail::for_each_n_niebloid for_each_n
Definition: for_each_n.hpp:56
constexpr invoke_result_t< F, Args... > invoke(F &&f, Args &&... args) noexcept(is_nothrow_invocable< F, Args... >::value)
Definition: invoke.hpp:38
typename iter_difference< T >::type iter_difference_t
Computes the difference type of T
Definition: iter_difference_t.hpp:49
#define VCCC_INLINE_OR_STATIC
Definition: inline_or_static.hpp:9
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 0 > first
Definition: key_value.hpp:34
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35
Definition: conjunction.hpp:22
function object that returns its argument unchanged
Definition: identity.hpp:25
specifies that a callable type can be invoked with the result of dereferencing an indirectly_readable...
Definition: indirectly_unary_invocable.hpp:73
specifies that a type is an input iterator, that is, its referenced values can be read and it can be ...
Definition: input_iterator.hpp:55
Definition: in_fun_result.hpp:22