Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mpl): more functors to type_list #86

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 156 additions & 4 deletions include/co_context/mpl/type_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,36 @@ struct tails<type_list<H, Ts...>> {
template<TL in>
using tails_t = typename tails<in>::type;

template<TL in>
requires(in::size > 0)
struct last;

template<typename H>
struct last<type_list<H>> {
using type = H;
};

template<typename H, typename H2, typename... Ts>
struct last<type_list<H, H2, Ts...>> : last<type_list<H2, Ts...>> {};

template<TL in>
using last_t = typename last<in>::type;

template<TL in, typename out = type_list<>>
struct drop_last;

template<typename L, typename out>
struct drop_last<type_list<L>, out> {
using type = out;
};

template<typename H, typename H2, typename... Ts, typename out>
struct drop_last<type_list<H, H2, Ts...>, out>
: drop_last<type_list<H2, Ts...>, typename out::template append<H>> {};

template<TL in>
using drop_last_t = typename drop_last<in>::type;

template<TL in, template<typename> class F>
struct map;

Expand Down Expand Up @@ -122,7 +152,7 @@ template<TL in, template<typename> typename P>
struct remove_if {
private:
template<typename E>
using not_P = detail::negate<P>::template type<E>;
using not_P = typename detail::negate<P>::template type<E>;

public:
using type = filter_t<in, not_P>;
Expand Down Expand Up @@ -194,6 +224,25 @@ namespace detail {
std::is_same_v<E, H>,
std::integral_constant<size_t, offset>,
find_impl<type_list<Ts...>, E, offset + 1>> {};

template<TL in, template<typename> typename P, size_t offset>
struct find_if_impl;

template<template<typename> typename P, size_t offset>
struct find_if_impl<type_list<>, P, offset>
: std::integral_constant<size_t, npos> {};

template<
template<typename>
typename P,
typename H,
typename... Ts,
size_t offset>
struct find_if_impl<type_list<H, Ts...>, P, offset>
: std::conditional_t<
P<H>::value,
std::integral_constant<size_t, offset>,
find_if_impl<type_list<Ts...>, P, offset + 1>> {};
} // namespace detail

template<TL in, typename E>
Expand All @@ -205,6 +254,15 @@ using find_t = typename find<in, E>::type;
template<TL in, typename E>
constexpr size_t find_v = find<in, E>::value;

template<typename in, template<typename> typename P>
struct find_if : detail::find_if_impl<in, P, 0> {};

template<typename in, template<typename> typename P>
using find_if_t = typename find_if<in, P>::type;

template<TL in, template<typename> typename P>
constexpr size_t find_if_v = find_if<in, P>::value;

template<TL in>
class unique {
template<TL acc, typename E>
Expand Down Expand Up @@ -283,6 +341,100 @@ struct replace {
template<typename in, typename Old, typename New>
using replace_t = typename replace<in, Old, New>::type;

template<TL in1, TL in2>
struct union_set {
using type = unique_t<concat_t<in1, in2>>;
};

template<TL in1, TL in2>
using union_set_t = typename union_set<in1, in2>::type;

template<TL in1, TL in2>
class intersection_set {
template<typename E>
using in1_contain = contain<in1, E>;

public:
using type = filter_t<in2, in1_contain>;
};

template<TL in1, TL in2>
using intersection_set_t = typename intersection_set<in1, in2>::type;

template<TL in1, TL in2>
class difference_set {
template<typename E>
struct in2_contain : contain<in2, E> {};

public:
using type = remove_if_t<in1, in2_contain>;
};

template<TL in1, TL in2>
using difference_set_t = typename difference_set<in1, in2>::type;

template<TL in1, TL in2>
struct symmetric_difference_set {
using type =
concat_t<difference_set_t<in1, in2>, difference_set_t<in2, in1>>;
};

template<TL in1, TL in2>
using symmetric_difference_set_t =
typename symmetric_difference_set<in1, in2>::type;

template<
TL in,
template<typename>
typename P,
typename out = type_list<type_list<>>>
struct split_if;

template<template<typename> typename P, typename out>
struct split_if<type_list<>, P, out> {
private:
using no_empty_t = remove_t<out, type_list<>>;
using reverse_outer_t = reverse_t<no_empty_t>;

public:
using type = reverse_outer_t;
};

template<
template<typename>
typename P,
typename H,
typename... Ts,
typename head_list,
typename... lists>
struct split_if<type_list<H, Ts...>, P, type_list<head_list, lists...>>
: std::conditional_t<
P<H>::value,
split_if<
type_list<Ts...>,
P,
type_list<type_list<>, head_list, lists...>>,
split_if<
type_list<Ts...>,
P,
type_list<typename head_list::template append<H>, lists...>>> {};

template<TL in, template<typename> typename P>
using split_if_t = typename split_if<in, P>::type;

template<TL in, typename delimiter>
struct split {
private:
template<typename E>
struct is_delimiter : std::is_same<delimiter, E> {};

public:
using type = split_if_t<in, is_delimiter>;
};

template<TL in, typename delimiter>
using split_t = typename split<in, delimiter>::type;

} // namespace co_context::mpl

namespace co_context::mpl::detail {
Expand All @@ -300,14 +452,14 @@ struct reverse_sequence<std::integer_sequence<T>> {
template<typename T, T idx, T... idxs>
struct reverse_sequence<std::integer_sequence<T, idx, idxs...>> {
template<T... app>
using append = reverse_sequence<
using append = typename reverse_sequence<
std::integer_sequence<T, idxs...>>::template append<idx, app...>;
using type = reverse_sequence<
using type = typename reverse_sequence<
std::integer_sequence<T, idxs...>>::template append<idx>;
};

template<typename T>
using reverse_sequence_t = reverse_sequence<T>::type;
using reverse_sequence_t = typename reverse_sequence<T>::type;

static_assert(std::is_same_v<
reverse_sequence_t<std::integer_sequence<int, 0, 10, 3, 7>>,
Expand Down
44 changes: 43 additions & 1 deletion test/mpl_type_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ struct s {};
using list = mpl::type_list<char, int, float, double>;
using empty = mpl::type_list<>;

void head_tails() {
void head_tails_last() {
static_assert(std::is_same_v<mpl::head_t<list>, char>);
static_assert(std::is_same_v<
mpl::tails_t<list>, mpl::type_list<int, float, double>>);
static_assert(std::is_same_v<mpl::last_t<list>, double>);
}

void drop_last() {
using no_last = mpl::type_list<char, int, float>;
static_assert(std::is_same_v<mpl::drop_last_t<list>, no_last>);
}

void from_to() {
Expand Down Expand Up @@ -81,6 +87,9 @@ void find() {
static_assert(mpl::find_v<list, float> == 2);
static_assert(mpl::find_v<list, double> == 3);
static_assert(mpl::find_v<list, void> == mpl::npos);

static_assert(mpl::find_if_v<list, std::is_floating_point> == 2);
static_assert(mpl::find_if_v<list, std::is_void> == mpl::npos);
}

void reverse() {
Expand Down Expand Up @@ -122,5 +131,38 @@ void replace() {
mpl::replace_t<list, int, void>, int_to_void_list>);
}

void set() {
using a = mpl::type_list<void, char, int>;
using b = mpl::type_list<char, int, float>;

static_assert(std::is_same_v<
mpl::union_set_t<a, b>,
mpl::type_list<void, char, int, float>>);

static_assert(std::is_same_v<
mpl::intersection_set_t<a, b>, mpl::type_list<char, int>>);

static_assert(std::is_same_v<
mpl::difference_set_t<a, b>, mpl::type_list<void>>);

static_assert(std::is_same_v<
mpl::difference_set_t<a, b>, mpl::type_list<void>>);

static_assert(std::is_same_v<
mpl::symmetric_difference_set_t<a, b>,
mpl::type_list<void, float>>);
}

void split() {
using sp_list =
mpl::type_list<mpl::type_list<char, int>, mpl::type_list<double>>;
static_assert(std::is_same_v<mpl::split_t<list, float>, sp_list>);

using no_floating_list = mpl::type_list<mpl::type_list<char, int>>;
static_assert(std::is_same_v<
mpl::split_if_t<list, std::is_floating_point>,
no_floating_list>);
}

int main() {
}