mp_count.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef BOOST_MP11_DETAIL_MP_COUNT_HPP_INCLUDED
  2. #define BOOST_MP11_DETAIL_MP_COUNT_HPP_INCLUDED
  3. // Copyright 2015, 2016 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/integral.hpp>
  10. #include <boost/mp11/detail/mp_plus.hpp>
  11. #include <boost/mp11/detail/config.hpp>
  12. namespace boost
  13. {
  14. namespace mp11
  15. {
  16. // mp_count<L, V>
  17. namespace detail
  18. {
  19. #if !defined( BOOST_MP11_NO_CONSTEXPR )
  20. constexpr std::size_t cx_plus()
  21. {
  22. return 0;
  23. }
  24. template<class T1, class... T> constexpr std::size_t cx_plus(T1 t1, T... t)
  25. {
  26. return static_cast<std::size_t>(t1) + cx_plus(t...);
  27. }
  28. template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class... T>
  29. constexpr std::size_t cx_plus(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T... t)
  30. {
  31. return static_cast<std::size_t>(t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9 + t10) + cx_plus(t...);
  32. }
  33. #endif
  34. template<class L, class V> struct mp_count_impl;
  35. #if defined( BOOST_MP11_HAS_CXX14_CONSTEXPR )
  36. template<class V, class... T> constexpr std::size_t cx_count()
  37. {
  38. constexpr bool a[] = { false, std::is_same<T, V>::value... };
  39. std::size_t r = 0;
  40. for( std::size_t i = 1; i < sizeof...(T) + 1; ++i )
  41. {
  42. r += a[ i ];
  43. }
  44. return r;
  45. }
  46. template<template<class...> class L, class... T, class V> struct mp_count_impl<L<T...>, V>
  47. {
  48. using type = mp_size_t<cx_count<V, T...>()>;
  49. };
  50. #elif !defined( BOOST_MP11_NO_CONSTEXPR )
  51. template<template<class...> class L, class... T, class V> struct mp_count_impl<L<T...>, V>
  52. {
  53. using type = mp_size_t<cx_plus(std::is_same<T, V>::value...)>;
  54. };
  55. #else
  56. template<template<class...> class L, class... T, class V> struct mp_count_impl<L<T...>, V>
  57. {
  58. using type = mp_size_t<mp_plus<std::is_same<T, V>...>::value>;
  59. };
  60. #endif
  61. } // namespace detail
  62. template<class L, class V> using mp_count = typename detail::mp_count_impl<L, V>::type;
  63. // mp_count_if<L, P>
  64. namespace detail
  65. {
  66. template<class L, template<class...> class P> struct mp_count_if_impl;
  67. #if defined( BOOST_MP11_HAS_CXX14_CONSTEXPR ) && !BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1930 )
  68. template<template<class...> class P, class... T> constexpr std::size_t cx_count_if()
  69. {
  70. constexpr bool a[] = { false, static_cast<bool>( P<T>::value )... };
  71. std::size_t r = 0;
  72. for( std::size_t i = 1; i < sizeof...(T) + 1; ++i )
  73. {
  74. r += a[ i ];
  75. }
  76. return r;
  77. }
  78. template<template<class...> class L, class... T, template<class...> class P> struct mp_count_if_impl<L<T...>, P>
  79. {
  80. using type = mp_size_t<cx_count_if<P, T...>()>;
  81. };
  82. #elif !defined( BOOST_MP11_NO_CONSTEXPR )
  83. template<template<class...> class L, class... T, template<class...> class P> struct mp_count_if_impl<L<T...>, P>
  84. {
  85. using type = mp_size_t<cx_plus(mp_to_bool<P<T>>::value...)>;
  86. };
  87. #else
  88. template<template<class...> class L, class... T, template<class...> class P> struct mp_count_if_impl<L<T...>, P>
  89. {
  90. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  91. template<class T> struct _f { using type = mp_to_bool<P<T>>; };
  92. using type = mp_size_t<mp_plus<typename _f<T>::type...>::value>;
  93. #else
  94. using type = mp_size_t<mp_plus<mp_to_bool<P<T>>...>::value>;
  95. #endif
  96. };
  97. #endif
  98. } // namespace detail
  99. template<class L, template<class...> class P> using mp_count_if = typename detail::mp_count_if_impl<L, P>::type;
  100. template<class L, class Q> using mp_count_if_q = mp_count_if<L, Q::template fn>;
  101. } // namespace mp11
  102. } // namespace boost
  103. #endif // #ifndef BOOST_MP11_DETAIL_MP_COUNT_HPP_INCLUDED