mp_filter.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2019 Peter Dimov.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. //
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. #include <boost/mp11/algorithm.hpp>
  8. #include <boost/mp11/list.hpp>
  9. #include <boost/mp11/integral.hpp>
  10. #include <boost/core/lightweight_test_trait.hpp>
  11. using boost::mp11::mp_int;
  12. template<class N> using mod_2 = mp_int<N::value % 2>;
  13. template<class N> using mod_3 = mp_int<N::value % 3>;
  14. template<class N> using mod_6 = mp_int<N::value % 6>;
  15. using boost::mp11::mp_not;
  16. using boost::mp11::mp_plus;
  17. template<class T> using P1 = mp_not<mod_6<T>>;
  18. template<class T1, class... T> using P2 = mp_not<mp_plus<T...>>;
  19. using boost::mp11::mp_bool;
  20. template<std::size_t N> struct second_is
  21. {
  22. template<class T1, class T2> using fn = mp_bool< T2::value == N >;
  23. };
  24. using boost::mp11::mp_first;
  25. using boost::mp11::mp_filter_q;
  26. using boost::mp11::mp_iota;
  27. using boost::mp11::mp_size;
  28. template<class L, std::size_t N> using at_c = mp_first< mp_filter_q< second_is<N>, L, mp_iota<mp_size<L>> > >;
  29. int main()
  30. {
  31. using boost::mp11::mp_iota_c;
  32. using boost::mp11::mp_filter;
  33. using boost::mp11::mp_list;
  34. using boost::mp11::mp_size_t;
  35. using boost::mp11::mp_transform;
  36. {
  37. int const N = 12;
  38. using L1 = mp_iota_c<N>;
  39. using R1 = mp_filter<P1, L1>;
  40. BOOST_TEST_TRAIT_TRUE((std::is_same<R1, mp_list<mp_size_t<0>, mp_size_t<6>>>));
  41. using L2 = mp_transform<mod_2, L1>;
  42. using L3 = mp_transform<mod_3, L1>;
  43. using L6 = mp_transform<mod_6, L1>;
  44. using R2 = mp_filter<P2, L1, L6>;
  45. BOOST_TEST_TRAIT_TRUE((std::is_same<R2, mp_list<mp_size_t<0>, mp_size_t<6>>>));
  46. using R3 = mp_filter<P2, L1, L2, L3>;
  47. BOOST_TEST_TRAIT_TRUE((std::is_same<R3, mp_list<mp_size_t<0>, mp_size_t<6>>>));
  48. }
  49. {
  50. int const N = 64;
  51. int const M = 17;
  52. using L1 = mp_iota_c<N>;
  53. using R1 = at_c<L1, M>;
  54. BOOST_TEST_TRAIT_TRUE((std::is_same<R1, mp_size_t<M>>));
  55. }
  56. return boost::report_errors();
  57. }