tuple/type: add entuple, cat, remove, and unique types

This commit is contained in:
Danny Robson 2018-06-06 19:13:17 +10:00
parent 8cd33ae5e5
commit d70d06468e
2 changed files with 179 additions and 1 deletions

View File

@ -42,6 +42,44 @@ main (void)
#endif
}
{
using pair_t = std::pair<int,float>;
using tuple_t = std::tuple<int,float>;
tap.expect (std::is_same_v<util::tuple::type::entuple_t<pair_t>, tuple_t>, "entuple a pair");
}
{
using a = std::pair<int,float>;
using b = std::tuple<char>;
using c = std::tuple<int,float,char>;
tap.expect (std::is_same_v<
util::tuple::type::cat_t<a,b>, c
>, "concatenate pair and tuple");
}
{
using original_t = std::tuple<int,char,float,int,double,int>;
using removed_t = std::tuple<char,float,double>;
tap.expect (std::is_same_v<
util::tuple::type::remove_t<int,original_t>,
removed_t
>, "removed int from tuple");
}
{
using original_t = std::tuple<int,float,float,char,double,char>;
using unique_t = std::tuple<int,float,char,double>;
tap.expect (std::is_same_v<
util::tuple::type::unique_t<original_t>,
unique_t
>, "removed duplicates from tuple");
}
{
using src_t = std::tuple<std::string>;

View File

@ -156,7 +156,147 @@ namespace util::tuple::type {
std::index_sequence<Indices...>
>::value;
};
};
///////////////////////////////////////////////////////////////////////////
// transform a tuple-like type (ie, something that responds to
// tuple_element) into a tuple
template <
typename TupleT,
typename Indices = std::make_index_sequence<
std::tuple_size_v<TupleT>
>
> struct entuple;
//-------------------------------------------------------------------------
template <
typename TupleT,
size_t ...Indices
> struct entuple<
TupleT,
std::index_sequence<Indices...>
> {
using type = std::tuple<
std::tuple_element_t<Indices,TupleT>...
>;
};
//-------------------------------------------------------------------------
template <typename T>
using entuple_t = typename entuple<T>::type;
///////////////////////////////////////////////////////////////////////////
// concatenate a variadic collection of tuple-like types
template <typename ...Args>
struct cat;
//-------------------------------------------------------------------------
template <>
struct cat<> { using type = std::tuple<>; };
//-------------------------------------------------------------------------
template <typename HeadT, typename ...TailT>
struct cat<HeadT,TailT...> {
using type = decltype(
std::tuple_cat (
std::declval<
entuple_t<HeadT>
> (),
std::declval<
typename cat<TailT...>::type
> ()
)
);
};
//-------------------------------------------------------------------------
template <typename ...TupleT>
using cat_t = typename cat<TupleT...>::type;
///////////////////////////////////////////////////////////////////////////
// remove a type from a tuple-like type
template <
typename DoomedT,
typename TupleT
>
struct remove;
//-------------------------------------------------------------------------
template <
typename DoomedT
>
struct remove<
DoomedT,
std::tuple<>
> {
using type = std::tuple<>;
};
//-------------------------------------------------------------------------
template <
typename DoomedT,
typename HeadT,
typename ...TailT
>
struct remove<
DoomedT,
std::tuple<HeadT,TailT...>
> {
using type = cat_t<
std::conditional_t<
std::is_same_v<DoomedT,HeadT>,
std::tuple< >,
std::tuple<HeadT>
>,
typename remove<DoomedT,std::tuple<TailT...>>::type
>;
};
//-------------------------------------------------------------------------
template <typename DoomedT, typename TupleT>
using remove_t = typename remove<DoomedT,TupleT>::type;
///////////////////////////////////////////////////////////////////////////
template <typename TupleT>
struct unique;
//-------------------------------------------------------------------------
template <>
struct unique<std::tuple<>> { using type = std::tuple<>; };
//-------------------------------------------------------------------------
template <typename HeadT, typename ...TailT>
struct unique<
std::tuple<HeadT, TailT...>
> {
using type = cat_t<
std::tuple<HeadT>,
typename unique<
typename remove<
HeadT,
std::tuple<TailT...>
>::type
>::type
>;
};
//-------------------------------------------------------------------------
template <typename T>
using unique_t = typename unique<T>::type;
}
#endif