Horizon
tuple_algorithm.hpp
Go to the documentation of this file.
1 // Range v3 library
3 //
4 // Copyright Eric Niebler 2013-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 
14 #ifndef RANGES_V3_UTILITY_TUPLE_ALGORITHM_HPP
15 #define RANGES_V3_UTILITY_TUPLE_ALGORITHM_HPP
16 
17 #include <initializer_list>
18 #include <tuple>
19 #include <type_traits>
20 #include <utility>
21 
22 #include <meta/meta.hpp>
23 
24 #include <range/v3/range_fwd.hpp>
25 
26 #include <range/v3/detail/adl_get.hpp>
28 #include <range/v3/utility/static_const.hpp>
29 
30 #include <range/v3/detail/prologue.hpp>
31 
32 namespace ranges
33 {
36  template<typename Tup>
37  using tuple_indices_t = meta::make_index_sequence<
38  std::tuple_size<typename std::remove_reference<Tup>::type>::value>;
39 
41  {
42  // clang-format off
43  private:
44  template<typename Fun, typename Tup, std::size_t... Is>
45  static constexpr auto //
46  CPP_auto_fun(impl)(Fun &&fun, Tup &&tup, meta::index_sequence<Is...>)
47  (
48  return invoke(static_cast<Fun &&>(fun),
49  detail::adl_get<Is>(static_cast<Tup &&>(tup))...)
50  )
51  public:
52  template<typename Fun, typename Tup>
53  constexpr auto CPP_auto_fun(operator())(Fun &&fun, Tup &&tup)(const)
54  (
55  return tuple_apply_fn::impl(static_cast<Fun &&>(fun),
56  static_cast<Tup &&>(tup),
57  tuple_indices_t<Tup>{})
58  )
59  // clang-format on
60  };
61 
65 
67  {
68  // clang-format off
69  private:
70  template<typename Tup, typename Fun, std::size_t... Is>
71  static constexpr auto //
72  CPP_auto_fun(impl1)(Tup &&tup, Fun &fun, meta::index_sequence<Is...>)
73  (
74  return std::tuple<
75  decltype(fun(detail::adl_get<Is>(static_cast<Tup &&>(tup))))...>{
76  fun(detail::adl_get<Is>(static_cast<Tup &&>(
77  tup)))...}
78  )
79  template<typename Tup0, typename Tup1, typename Fun, std::size_t... Is>
80  static constexpr auto CPP_auto_fun(impl2)(Tup0 &&tup0, Tup1 &&tup1, Fun &fun,
81  meta::index_sequence<Is...>)
82  (
83  return std::tuple<
84  decltype(fun(detail::adl_get<Is>(static_cast<Tup0 &&>(tup0)),
85  detail::adl_get<Is>(static_cast<Tup1 &&>(tup1))))...>{
86  fun(detail::adl_get<Is>(static_cast<Tup0 &&>(tup0)),
87  detail::adl_get<Is>(static_cast<Tup1 &&>(tup1)))...}
88  )
89  public:
90  template<typename Tup, typename Fun>
91  constexpr auto CPP_auto_fun(operator())(Tup &&tup, Fun fun)(const)
92  (
93  return tuple_transform_fn::impl1(
94  static_cast<Tup &&>(tup), fun,
95  tuple_indices_t<Tup>{})
96  )
97  template<typename Tup0, typename Tup1, typename Fun>
98  constexpr auto CPP_auto_fun(operator())(Tup0 &&tup0, Tup1 &&tup1, Fun fun)(const)
99  (
100  return tuple_transform_fn::impl2(static_cast<Tup0 &&>(tup0),
101  static_cast<Tup1 &&>(tup1), fun,
102  tuple_indices_t<Tup0>{})
103  )
104  // clang-format on
105  };
106 
110 
112  {
113  private:
114  template<typename Tup, typename Val, typename Fun>
115  static constexpr Val impl(Tup &&, Val val, Fun &)
116  {
117  return val;
118  }
119  // clang-format off
120  template<std::size_t I0, std::size_t... Is, typename Tup, typename Val,
121  typename Fun, typename Impl = tuple_foldl_fn>
122  static constexpr auto CPP_auto_fun(impl)(Tup &&tup, Val val, Fun &fun)
123  (
124  return Impl::template impl<Is...>(
125  static_cast<Tup &&>(tup),
126  fun(std::move(val), detail::adl_get<I0>(static_cast<Tup &&>(tup))),
127  fun)
128  )
129  template<typename Tup, typename Val, typename Fun, std::size_t... Is>
130  static constexpr auto CPP_auto_fun(impl2)(Tup &&tup, Val val, Fun &fun,
131  meta::index_sequence<Is...>)
132  (
133  return tuple_foldl_fn::impl<Is...>(static_cast<Tup &&>(tup),
134  std::move(val),
135  fun)
136  )
137  public:
138  template<typename Tup, typename Val, typename Fun>
139  constexpr auto CPP_auto_fun(operator())(Tup &&tup, Val val, Fun fun)(const)
140  (
141  return tuple_foldl_fn::impl2(static_cast<Tup &&>(tup),
142  std::move(val),
143  fun,
144  tuple_indices_t<Tup>{})
145  )
146  // clang-format on
147  };
148 
152 
154  {
155  private:
156  template<typename Tup, typename Fun, std::size_t... Is>
157  static constexpr void impl(Tup && tup, Fun & fun, meta::index_sequence<Is...>)
158  {
159  (void)std::initializer_list<int>{
160  ((void)fun(detail::adl_get<Is>(static_cast<Tup &&>(tup))), 42)...};
161  }
162 
163  public:
164  template<typename Tup, typename Fun>
165  constexpr Fun operator()(Tup && tup, Fun fun) const
166  {
167  return tuple_for_each_fn::impl(
168  static_cast<Tup &&>(tup), fun, tuple_indices_t<Tup>{}),
169  fun;
170  }
171  };
172 
176 
178  {
179  // clang-format off
180  template<typename... Ts>
181  constexpr auto CPP_auto_fun(operator())(Ts &&... ts)(const)
182  (
183  return std::make_tuple(static_cast<Ts &&>(ts)...)
184  )
185  // clang-format on
186  };
187 
192 } // namespace ranges
193 
194 #include <range/v3/detail/epilogue.hpp>
195 
196 #endif
template(typename ActionFn, typename Rng)(concept(invocable_action_closure_)(ActionFn
\concept invocable_action_closure_
RANGES_INLINE_VARIABLE(detail::to_container_fn< detail::from_range< std::vector >>, to_vector) template< template< typename... > class ContT > auto to(RANGES_HIDDEN_DETAIL(detail
For initializing a container of the specified type with the elements of an Range.
Definition: conversion.hpp:399
_t< detail::make_indices_< N, index_sequence< 0 >, detail::strategy_(1, N)> > make_index_sequence
Generate index_sequence containing integer constants [0,1,2,...,N-1].
Definition: meta.hpp:473
std::integral_constant< std::size_t, N > size_t
An integral constant wrapper for std::size_t.
Definition: meta.hpp:163
typename Fn::template invoke< Args... > invoke
Evaluate the invocable Fn with the arguments Args.
Definition: meta.hpp:541
Tiny meta-programming library.
A container for a sequence of compile-time integer constants.
Definition: meta.hpp:434
Definition: tuple_algorithm.hpp:178
Definition: tuple_algorithm.hpp:41
Definition: tuple_algorithm.hpp:112
Definition: tuple_algorithm.hpp:154
Definition: tuple_algorithm.hpp:67