set.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #ifndef BOOST_MP11_SET_HPP_INCLUDED
  2. #define BOOST_MP11_SET_HPP_INCLUDED
  3. // Copyright 2015, 2019 Peter Dimov.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. //
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. #include <boost/mp11/utility.hpp>
  10. #include <boost/mp11/function.hpp>
  11. #include <boost/mp11/detail/mp_list.hpp>
  12. #include <boost/mp11/detail/mp_append.hpp>
  13. #include <boost/mp11/detail/mp_copy_if.hpp>
  14. #include <boost/mp11/detail/mp_remove_if.hpp>
  15. #include <boost/mp11/detail/mp_is_list.hpp>
  16. #include <type_traits>
  17. namespace boost
  18. {
  19. namespace mp11
  20. {
  21. // mp_set_contains<S, V>
  22. namespace detail
  23. {
  24. template<class S, class V> struct mp_set_contains_impl
  25. {
  26. };
  27. template<template<class...> class L, class... T, class V> struct mp_set_contains_impl<L<T...>, V>
  28. {
  29. using type = mp_to_bool<std::is_base_of<mp_identity<V>, mp_inherit<mp_identity<T>...> > >;
  30. };
  31. } // namespace detail
  32. template<class S, class V> using mp_set_contains = typename detail::mp_set_contains_impl<S, V>::type;
  33. // mp_set_push_back<S, T...>
  34. namespace detail
  35. {
  36. template<class S, class... T> struct mp_set_push_back_impl
  37. {
  38. };
  39. template<template<class...> class L, class... U> struct mp_set_push_back_impl<L<U...>>
  40. {
  41. using type = L<U...>;
  42. };
  43. template<template<class...> class L, class... U, class T1, class... T> struct mp_set_push_back_impl<L<U...>, T1, T...>
  44. {
  45. using S = mp_if<mp_set_contains<L<U...>, T1>, L<U...>, L<U..., T1>>;
  46. using type = typename mp_set_push_back_impl<S, T...>::type;
  47. };
  48. } // namespace detail
  49. template<class S, class... T> using mp_set_push_back = typename detail::mp_set_push_back_impl<S, T...>::type;
  50. // mp_set_push_front<S, T...>
  51. namespace detail
  52. {
  53. template<class S, class... T> struct mp_set_push_front_impl
  54. {
  55. };
  56. template<template<class...> class L, class... U> struct mp_set_push_front_impl<L<U...>>
  57. {
  58. using type = L<U...>;
  59. };
  60. template<template<class...> class L, class... U, class T1> struct mp_set_push_front_impl<L<U...>, T1>
  61. {
  62. using type = mp_if<mp_set_contains<L<U...>, T1>, L<U...>, L<T1, U...>>;
  63. };
  64. template<template<class...> class L, class... U, class T1, class... T> struct mp_set_push_front_impl<L<U...>, T1, T...>
  65. {
  66. using S = typename mp_set_push_front_impl<L<U...>, T...>::type;
  67. using type = typename mp_set_push_front_impl<S, T1>::type;
  68. };
  69. } // namespace detail
  70. template<class S, class... T> using mp_set_push_front = typename detail::mp_set_push_front_impl<S, T...>::type;
  71. // mp_is_set<S>
  72. namespace detail
  73. {
  74. template<class S> struct mp_is_set_impl
  75. {
  76. using type = mp_false;
  77. };
  78. template<template<class...> class L, class... T> struct mp_is_set_impl<L<T...>>
  79. {
  80. using type = mp_to_bool<std::is_same<mp_list<T...>, mp_set_push_back<mp_list<>, T...> > >;
  81. };
  82. } // namespace detail
  83. template<class S> using mp_is_set = typename detail::mp_is_set_impl<S>::type;
  84. // mp_set_union<L...>
  85. namespace detail
  86. {
  87. template<class... L> struct mp_set_union_impl
  88. {
  89. };
  90. template<> struct mp_set_union_impl<>
  91. {
  92. using type = mp_list<>;
  93. };
  94. template<template<class...> class L, class... T> struct mp_set_union_impl<L<T...>>
  95. {
  96. using type = L<T...>;
  97. };
  98. template<template<class...> class L1, class... T1, template<class...> class L2, class... T2> struct mp_set_union_impl<L1<T1...>, L2<T2...>>
  99. {
  100. using type = mp_set_push_back<L1<T1...>, T2...>;
  101. };
  102. template<class L1, class... L> using mp_set_union_ = typename mp_set_union_impl<L1, mp_append<mp_list<>, L...>>::type;
  103. 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...>
  104. {
  105. };
  106. } // namespace detail
  107. template<class... L> using mp_set_union = typename detail::mp_set_union_impl<L...>::type;
  108. // mp_set_intersection<S...>
  109. namespace detail
  110. {
  111. template<class... S> struct in_all_sets
  112. {
  113. template<class T> using fn = mp_all< mp_set_contains<S, T>... >;
  114. };
  115. template<class L, class... S> using mp_set_intersection_ = mp_if< mp_all<mp_is_list<S>...>, mp_copy_if_q<L, detail::in_all_sets<S...>> >;
  116. template<class... S> struct mp_set_intersection_impl
  117. {
  118. };
  119. template<> struct mp_set_intersection_impl<>
  120. {
  121. using type = mp_list<>;
  122. };
  123. template<class L, class... S> struct mp_set_intersection_impl<L, S...>: mp_defer<mp_set_intersection_, L, S...>
  124. {
  125. };
  126. } // namespace detail
  127. template<class... S> using mp_set_intersection = typename detail::mp_set_intersection_impl<S...>::type;
  128. // mp_set_difference<L, S...>
  129. namespace detail
  130. {
  131. template<class... S> struct in_any_set
  132. {
  133. template<class T> using fn = mp_any< mp_set_contains<S, T>... >;
  134. };
  135. } // namespace detail
  136. template<class L, class... S> using mp_set_difference = mp_if< mp_all<mp_is_list<S>...>, mp_remove_if_q<L, detail::in_any_set<S...>> >;
  137. } // namespace mp11
  138. } // namespace boost
  139. #endif // #ifndef BOOST_MP11_SET_HPP_INCLUDED