1
0
forked from boostorg/mp11

Add mp_set_union

This commit is contained in:
Peter Dimov
2019-01-07 06:50:51 +02:00
parent c61d99db87
commit 08444e4587
6 changed files with 137 additions and 7 deletions

View File

@ -31,10 +31,17 @@ A set is a list whose elements are unique.
template<class S, class... T> using mp_set_push_back = /*...*/;
For each `T1` in `T...`, `mp_set_push_back<S, T...>` appends `T1` to the end of `S` if it's not already an element of `S`.
For each `T1` in `T...`, `mp_set_push_back<S, T...>` appends `T1` to the end of the set `S` if it's not already an element of `S`.
## mp_set_push_front<S, T...>
template<class S, class... T> using mp_set_push_front = /*...*/;
`mp_set_push_front<S, T...>` inserts at the front of `S` those elements of `T...` for which `S` does not already contain the same type.
`mp_set_push_front<S, T...>` inserts at the front of the set `S` those elements of `T...` for which `S` does not already contain the same type.
## mp_set_union<L...>
template<class... L> using mp_set_union = /*...*/;
`mp_set_union<S, L...>` is `mp_set_push_back<S, T...>`, where `T...` are the elements of `mp_append<mp_list<>, L...>`.
`mp_set_union<>` is `mp_list<>`.

View File

@ -1,15 +1,16 @@
#ifndef BOOST_MP11_SET_HPP_INCLUDED
#define BOOST_MP11_SET_HPP_INCLUDED
// Copyright 2015 Peter Dimov.
// Copyright 2015, 2019 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/mp11/utility.hpp>
#include <boost/mp11/detail/mp_list.hpp>
#include <boost/mp11/detail/mp_append.hpp>
#include <type_traits>
namespace boost
@ -97,6 +98,39 @@ template<template<class...> class L, class... T> struct mp_is_set_impl<L<T...>>
template<class S> using mp_is_set = typename detail::mp_is_set_impl<S>::type;
// mp_set_union<L...>
namespace detail
{
template<class... L> struct mp_set_union_impl
{
};
template<> struct mp_set_union_impl<>
{
using type = mp_list<>;
};
template<template<class...> class L, class... T> struct mp_set_union_impl<L<T...>>
{
using type = L<T...>;
};
template<template<class...> class L1, class... T1, template<class...> class L2, class... T2> struct mp_set_union_impl<L1<T1...>, L2<T2...>>
{
using type = mp_set_push_back<L1<T1...>, T2...>;
};
template<class L1, class... L> using mp_set_union_ = typename mp_set_union_impl<L1, mp_append<mp_list<>, L...>>::type;
template<class L1, class L2, class L3, class... L> struct mp_set_union_impl<L1, L2, L3, L...>: mp_defer<mp_set_union_, L1, L2, L3, L...>
{
};
} // namespace detail
template<class... L> using mp_set_union = typename detail::mp_set_union_impl<L...>::type;
} // namespace mp11
} // namespace boost

View File

@ -132,6 +132,8 @@ run mp_set_contains.cpp ;
run mp_set_push_back.cpp ;
run mp_set_push_front.cpp ;
run mp_is_set.cpp ;
run mp_set_union.cpp ;
run mp_set_union_sf.cpp ;
# function
run mp_all.cpp ;

View File

@ -1,5 +1,5 @@
// Copyright 2015 Peter Dimov.
// Copyright 2015 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//

55
test/mp_set_union.cpp Normal file
View File

@ -0,0 +1,55 @@
// Copyright 2019 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/mp11/set.hpp>
#include <boost/mp11/list.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <tuple>
#include <utility>
int main()
{
using boost::mp11::mp_list;
using boost::mp11::mp_set_union;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<>, mp_list<>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<mp_list<>>, mp_list<>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<mp_list<void>>, mp_list<void>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<mp_list<void, int>>, mp_list<void, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<mp_list<void, int, float>>, mp_list<void, int, float>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<std::tuple<>>, std::tuple<>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<std::tuple<void>>, std::tuple<void>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<std::tuple<void, int>>, std::tuple<void, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<std::tuple<void, int, float>>, std::tuple<void, int, float>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<mp_list<>, std::pair<float, int>>, mp_list<float, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<mp_list<void>, std::pair<float, int>>, mp_list<void, float, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<mp_list<void, int>, std::pair<float, int>>, mp_list<void, int, float>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<mp_list<void, int, float>, std::pair<float, int>>, mp_list<void, int, float>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<std::tuple<>, mp_list<>, std::pair<float, int>>, std::tuple<float, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<std::tuple<void>, mp_list<double>, std::pair<float, int>>, std::tuple<void, double, float, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<std::tuple<void, int>, mp_list<double, int>, std::pair<float, int>>, std::tuple<void, int, double, float>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<std::tuple<void, int, float>, mp_list<double, int, float>, std::pair<float, int>>, std::tuple<void, int, float, double>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<mp_list<>, std::pair<float, float>, std::pair<int, int>>, mp_list<float, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<mp_list<void>, std::pair<float, float>, std::pair<int, int>>, mp_list<void, float, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<mp_list<void, int>, std::pair<float, float>, std::pair<int, int>>, mp_list<void, int, float>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<mp_list<void, int, float>, std::pair<float, float>, std::pair<int, int>>, mp_list<void, int, float>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<std::tuple<>, std::pair<float, float>, std::pair<int, int>, std::pair<void, void>>, std::tuple<float, int, void>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<std::tuple<void>, std::pair<float, float>, std::pair<int, int>, std::pair<void, void>>, std::tuple<void, float, int>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<std::tuple<void, int>, std::pair<float, float>, std::pair<int, int>, std::pair<void, void>>, std::tuple<void, int, float>>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_set_union<std::tuple<void, int, float>, std::pair<float, float>, std::pair<int, int>, std::pair<void, void>>, std::tuple<void, int, float>>));
return boost::report_errors();
}

32
test/mp_set_union_sf.cpp Normal file
View File

@ -0,0 +1,32 @@
// Copyright 2019 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/mp11/set.hpp>
#include <boost/mp11/utility.hpp>
#include <boost/core/lightweight_test_trait.hpp>
int main()
{
using boost::mp11::mp_set_union;
using boost::mp11::mp_valid;
using boost::mp11::mp_list;
BOOST_TEST_TRAIT_FALSE((mp_valid<mp_set_union, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<mp_set_union, void, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<mp_set_union, void, void, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<mp_set_union, void, void, void, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<mp_set_union, void, void, void, void, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<mp_set_union, mp_list<>, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<mp_set_union, mp_list<>, mp_list<>, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<mp_set_union, mp_list<>, mp_list<>, mp_list<>, void>));
BOOST_TEST_TRAIT_FALSE((mp_valid<mp_set_union, mp_list<>, mp_list<>, mp_list<>, mp_list<>, void>));
return boost::report_errors();
}