| 1 |
/// \file |
| 2 |
// 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> |
| 27 |
#include <range/v3/functional/invoke.hpp> |
| 28 |
#include <range/v3/utility/static_const.hpp> |
| 29 |
|
| 30 |
#include <range/v3/detail/prologue.hpp> |
| 31 |
|
| 32 |
namespace ranges |
| 33 |
{ |
| 34 |
/// \addtogroup group-utility |
| 35 |
/// @{ |
| 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 |
|
| 40 |
struct tuple_apply_fn |
| 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 |
|
| 62 |
/// \ingroup group-utility |
| 63 |
/// \sa `tuple_apply_fn` |
| 64 |
RANGES_INLINE_VARIABLE(tuple_apply_fn, tuple_apply) |
| 65 |
|
| 66 |
struct tuple_transform_fn |
| 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 |
|
| 107 |
/// \ingroup group-utility |
| 108 |
/// \sa `tuple_transform_fn` |
| 109 |
RANGES_INLINE_VARIABLE(tuple_transform_fn, tuple_transform) |
| 110 |
|
| 111 |
struct tuple_foldl_fn |
| 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 |
|
| 149 |
/// \ingroup group-utility |
| 150 |
/// \sa `tuple_foldl_fn` |
| 151 |
RANGES_INLINE_VARIABLE(tuple_foldl_fn, tuple_foldl) |
| 152 |
|
| 153 |
struct tuple_for_each_fn |
| 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 |
|
| 173 |
/// \ingroup group-utility |
| 174 |
/// \sa `tuple_for_each_fn` |
| 175 |
RANGES_INLINE_VARIABLE(tuple_for_each_fn, tuple_for_each) |
| 176 |
|
| 177 |
struct make_tuple_fn |
| 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 |
|
| 188 |
/// \ingroup group-utility |
| 189 |
/// \sa `make_tuple_fn` |
| 190 |
RANGES_INLINE_VARIABLE(make_tuple_fn, make_tuple) |
| 191 |
/// @} |
| 192 |
} // namespace ranges |
| 193 |
|
| 194 |
#include <range/v3/detail/epilogue.hpp> |
| 195 |
|
| 196 |
#endif |