VCCC  2024.05
VisualCamp Common C++ library
tuple_fold.hpp
Go to the documentation of this file.
1 //
2 // Created by YongGyu Lee on 2/13/24.
3 //
4 
5 #ifndef VCCC_TUPLE_TUPLE_FOLD_HPP_
6 #define VCCC_TUPLE_TUPLE_FOLD_HPP_
7 
8 #include <cstddef>
9 #include <tuple>
10 #include <utility>
11 
16 
17 namespace vccc {
18 namespace detail {
19 
20 template<std::size_t N, typename Tuple, typename T, typename F>
21 constexpr auto tuple_fold_left_impl(Tuple&& tuple, T&& x, F&& f, std::integral_constant<std::size_t, N - 1>) {
22  return vccc::invoke(
23  std::forward<F>(f),
24  std::forward<T>(x),
25  std::get<N - 1>(std::forward<Tuple>(tuple))
26  );
27 }
28 
29 template<std::size_t N, typename Tuple, typename T, typename F, std::size_t I, std::enable_if_t<(I + 1) != N, int> = 0>
30 constexpr auto tuple_fold_left_impl(Tuple&& tuple, T&& x, F&& f, std::integral_constant<std::size_t, I>) {
31  return tuple_fold_left_impl<N>(
32  std::forward<Tuple>(tuple),
34  std::forward<F>(f),
35  std::forward<T>(x),
36  std::get<I>(std::forward<Tuple>(tuple))
37  ),
38  std::forward<F>(f),
39  std::integral_constant<std::size_t, I + 1>{}
40  );
41 }
42 
43 } // namespace detail
44 
47 
49 template<typename Tuple, typename T, typename F, std::enable_if_t<conjunction<
50  tuple_like<Tuple>
51 >::value, int> = 0>
52 constexpr auto tuple_fold_left(Tuple&& tuple, T&& init, F&& f) {
53  return detail::tuple_fold_left_impl<std::tuple_size<remove_cvref_t<Tuple>>::value>(
54  std::forward<Tuple>(tuple),
55  std::forward<T>(init),
56  std::forward<F>(f),
57  std::integral_constant<std::size_t, 0>{}
58  );
59 }
60 
62 
63 } // namespace vccc
64 
65 #endif // VCCC_TUPLE_TUPLE_FOLD_HPP_
constexpr invoke_result_t< F, Args... > invoke(F &&f, Args &&... args) noexcept(is_nothrow_invocable< F, Args... >::value)
Definition: invoke.hpp:38
constexpr auto tuple_fold_left(Tuple &&tuple, T &&init, F &&f)
Left fold operation for each tuple elements.
Definition: tuple_fold.hpp:52
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35