From 08444e45870b1cb6907b97b7d13fa638c25a1b30 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Mon, 7 Jan 2019 06:50:51 +0200 Subject: [PATCH] Add mp_set_union --- doc/mp11/set.adoc | 11 ++++++-- include/boost/mp11/set.hpp | 42 ++++++++++++++++++++++++++--- test/Jamfile | 2 ++ test/mp_set_push_back.cpp | 2 +- test/mp_set_union.cpp | 55 ++++++++++++++++++++++++++++++++++++++ test/mp_set_union_sf.cpp | 32 ++++++++++++++++++++++ 6 files changed, 137 insertions(+), 7 deletions(-) create mode 100644 test/mp_set_union.cpp create mode 100644 test/mp_set_union_sf.cpp diff --git a/doc/mp11/set.adoc b/doc/mp11/set.adoc index d57ec73..65b36ce 100644 --- a/doc/mp11/set.adoc +++ b/doc/mp11/set.adoc @@ -31,10 +31,17 @@ A set is a list whose elements are unique. template using mp_set_push_back = /*...*/; -For each `T1` in `T...`, `mp_set_push_back` 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` appends `T1` to the end of the set `S` if it's not already an element of `S`. ## mp_set_push_front template using mp_set_push_front = /*...*/; -`mp_set_push_front` inserts at the front of `S` those elements of `T...` for which `S` does not already contain the same type. +`mp_set_push_front` 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 + + template using mp_set_union = /*...*/; + +`mp_set_union` is `mp_set_push_back`, where `T...` are the elements of `mp_append, L...>`. +`mp_set_union<>` is `mp_list<>`. diff --git a/include/boost/mp11/set.hpp b/include/boost/mp11/set.hpp index fe9c1b2..413af7a 100644 --- a/include/boost/mp11/set.hpp +++ b/include/boost/mp11/set.hpp @@ -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 #include +#include #include namespace boost @@ -97,6 +98,39 @@ template class L, class... T> struct mp_is_set_impl> template using mp_is_set = typename detail::mp_is_set_impl::type; +// mp_set_union +namespace detail +{ + +template struct mp_set_union_impl +{ +}; + +template<> struct mp_set_union_impl<> +{ + using type = mp_list<>; +}; + +template class L, class... T> struct mp_set_union_impl> +{ + using type = L; +}; + +template class L1, class... T1, template class L2, class... T2> struct mp_set_union_impl, L2> +{ + using type = mp_set_push_back, T2...>; +}; + +template using mp_set_union_ = typename mp_set_union_impl, L...>>::type; + +template struct mp_set_union_impl: mp_defer +{ +}; + +} // namespace detail + +template using mp_set_union = typename detail::mp_set_union_impl::type; + } // namespace mp11 } // namespace boost diff --git a/test/Jamfile b/test/Jamfile index 5c9d59a..545a567 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -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 ; diff --git a/test/mp_set_push_back.cpp b/test/mp_set_push_back.cpp index 0855536..279140e 100644 --- a/test/mp_set_push_back.cpp +++ b/test/mp_set_push_back.cpp @@ -1,5 +1,5 @@ -// Copyright 2015 Peter Dimov. +// Copyright 2015 Peter Dimov. // // Distributed under the Boost Software License, Version 1.0. // diff --git a/test/mp_set_union.cpp b/test/mp_set_union.cpp new file mode 100644 index 0000000..0811d29 --- /dev/null +++ b/test/mp_set_union.cpp @@ -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 +#include +#include +#include +#include +#include + +int main() +{ + using boost::mp11::mp_list; + using boost::mp11::mp_set_union; + + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_list<>>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same>, mp_list<>>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>, mp_list>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>, mp_list>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>, mp_list>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same>, std::tuple<>>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>, std::tuple>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>, std::tuple>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>, std::tuple>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same, std::pair>, mp_list>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, std::pair>, mp_list>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, std::pair>, mp_list>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, std::pair>, mp_list>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_list<>, std::pair>, std::tuple>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_list, std::pair>, std::tuple>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_list, std::pair>, std::tuple>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, mp_list, std::pair>, std::tuple>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same, std::pair, std::pair>, mp_list>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, std::pair, std::pair>, mp_list>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, std::pair, std::pair>, mp_list>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, std::pair, std::pair>, mp_list>)); + + BOOST_TEST_TRAIT_TRUE((std::is_same, std::pair, std::pair, std::pair>, std::tuple>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, std::pair, std::pair, std::pair>, std::tuple>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, std::pair, std::pair, std::pair>, std::tuple>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, std::pair, std::pair, std::pair>, std::tuple>)); + + return boost::report_errors(); +} diff --git a/test/mp_set_union_sf.cpp b/test/mp_set_union_sf.cpp new file mode 100644 index 0000000..badf793 --- /dev/null +++ b/test/mp_set_union_sf.cpp @@ -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 +#include +#include + +int main() +{ + using boost::mp11::mp_set_union; + using boost::mp11::mp_valid; + using boost::mp11::mp_list; + + BOOST_TEST_TRAIT_FALSE((mp_valid)); + BOOST_TEST_TRAIT_FALSE((mp_valid)); + BOOST_TEST_TRAIT_FALSE((mp_valid)); + BOOST_TEST_TRAIT_FALSE((mp_valid)); + BOOST_TEST_TRAIT_FALSE((mp_valid)); + + BOOST_TEST_TRAIT_FALSE((mp_valid, void>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, mp_list<>, void>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, mp_list<>, mp_list<>, void>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, mp_list<>, mp_list<>, mp_list<>, void>)); + + return boost::report_errors(); +}