2018-03-15 23:48:21 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-03-15 23:48:21 +11:00
|
|
|
*
|
|
|
|
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CRUFT_UTIL_TUPLE_VALUE_HPP
|
|
|
|
#define CRUFT_UTIL_TUPLE_VALUE_HPP
|
|
|
|
|
2018-04-05 16:06:09 +10:00
|
|
|
#include "index.hpp"
|
2018-03-15 23:48:21 +11:00
|
|
|
#include "../types.hpp"
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <functional>
|
|
|
|
#include <tuple>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::tuple::value {
|
2018-03-15 23:48:21 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Call a provided functor of type FunctionT with each value in a
|
|
|
|
/// provided tuple-like object TupleT
|
|
|
|
template<
|
|
|
|
typename FunctionT,
|
|
|
|
typename TupleT,
|
|
|
|
std::size_t S = 0
|
|
|
|
>
|
|
|
|
void
|
|
|
|
each (FunctionT &&func, TupleT &&value)
|
|
|
|
{
|
|
|
|
using tuple_t = std::decay_t<TupleT>;
|
|
|
|
static_assert (S < std::tuple_size_v<tuple_t>);
|
|
|
|
|
2018-04-05 12:22:44 +10:00
|
|
|
std::invoke (func, std::get<S> (value));
|
2018-03-15 23:48:21 +11:00
|
|
|
|
|
|
|
if constexpr (S + 1 < std::tuple_size_v<tuple_t>) {
|
|
|
|
each<FunctionT,TupleT,S+1> (std::forward<FunctionT> (func), std::forward<TupleT> (value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
namespace detail {
|
|
|
|
template <typename FuncT, typename ArgT, std::size_t ...Indices>
|
|
|
|
auto
|
|
|
|
map (std::index_sequence<Indices...>, FuncT &&func, ArgT &&arg)
|
|
|
|
{
|
|
|
|
return std::tuple (std::invoke (func, std::get<Indices> (arg))...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-23 16:39:35 +11:00
|
|
|
|
|
|
|
/// returns a tuple of the result of applying the provided function to
|
|
|
|
/// each value of the supplied tuple.
|
2018-03-15 23:48:21 +11:00
|
|
|
template <
|
|
|
|
typename FuncT,
|
|
|
|
typename ArgT,
|
|
|
|
typename IndicesV = std::make_index_sequence<
|
|
|
|
std::tuple_size_v<std::decay_t<ArgT>>
|
|
|
|
>
|
|
|
|
>
|
|
|
|
auto map (FuncT &&func, ArgT &&arg)
|
|
|
|
{
|
|
|
|
return detail::map (IndicesV{}, func, arg);
|
|
|
|
}
|
2018-04-05 12:23:12 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// return a tuple that contains the 'nth' element from each of the supplied
|
|
|
|
// tuple-like arguments
|
|
|
|
template <size_t N, typename ...TupleT>
|
|
|
|
auto
|
|
|
|
nth (TupleT&&...args)
|
|
|
|
{
|
|
|
|
return std::make_tuple (
|
|
|
|
std::get<N> (args)...
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
namespace detail {
|
|
|
|
template <typename ...TupleT, size_t ...Elements>
|
|
|
|
auto
|
|
|
|
zip (std::index_sequence<Elements...>, TupleT &&...args)
|
|
|
|
{
|
|
|
|
return std::make_tuple (
|
|
|
|
nth<Elements> (args...)...
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// converts m arguments of n-tuples into one m-tuple of n-tuples by taking
|
|
|
|
/// successive elements from each argument tuple.
|
|
|
|
///
|
|
|
|
/// eg, zip (('a', 1.f), (0, nullptr)) becomes (('a', 0), (1.f, nullptr))
|
|
|
|
template <typename ...Args >
|
|
|
|
auto
|
|
|
|
zip (Args &&...args)
|
|
|
|
{
|
|
|
|
static_assert (sizeof...(args) > 0);
|
|
|
|
// ensure all the types have the same static size
|
|
|
|
using all_t = std::tuple<std::decay_t<Args>...>;
|
|
|
|
using first_t = std::tuple_element_t<0,all_t>;
|
|
|
|
|
|
|
|
static constexpr auto arity = std::tuple_size_v<first_t>;
|
|
|
|
static_assert ((
|
|
|
|
(arity == std::tuple_size_v<std::decay_t<Args>>) && ...
|
|
|
|
));
|
|
|
|
|
|
|
|
return detail::zip (
|
|
|
|
std::make_index_sequence<arity> {},
|
|
|
|
std::forward<Args> (args)...
|
|
|
|
);
|
|
|
|
}
|
2018-04-05 16:06:09 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
namespace detail {
|
|
|
|
template <typename TupleT, size_t ...Indices>
|
|
|
|
auto
|
|
|
|
reverse (index::indices<Indices...>, TupleT &&val)
|
|
|
|
{
|
|
|
|
return std::make_tuple (
|
|
|
|
std::get<Indices> (val)...
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename TupleT>
|
|
|
|
auto
|
|
|
|
reverse (TupleT &&val)
|
|
|
|
{
|
|
|
|
return detail::reverse (
|
|
|
|
index::make_reverse_t<
|
|
|
|
std::tuple_size_v<std::decay_t<TupleT>>
|
|
|
|
> {},
|
|
|
|
std::forward<TupleT> (val)
|
|
|
|
);
|
|
|
|
}
|
2018-04-05 12:23:12 +10:00
|
|
|
}
|
2018-03-15 23:48:21 +11:00
|
|
|
|
|
|
|
|
|
|
|
#endif
|