]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | #ifndef BOOST_MP11_DETAIL_MP_COUNT_HPP_INCLUDED |
2 | #define BOOST_MP11_DETAIL_MP_COUNT_HPP_INCLUDED | |
3 | ||
4 | // Copyright 2015, 2016 Peter Dimov. | |
5 | // | |
6 | // Distributed under the Boost Software License, Version 1.0. | |
7 | // | |
8 | // See accompanying file LICENSE_1_0.txt or copy at | |
9 | // http://www.boost.org/LICENSE_1_0.txt | |
10 | ||
11 | #include <boost/mp11/integral.hpp> | |
12 | #include <boost/mp11/detail/mp_plus.hpp> | |
13 | #include <boost/mp11/detail/config.hpp> | |
b32b8144 FG |
14 | |
15 | namespace boost | |
16 | { | |
17 | namespace mp11 | |
18 | { | |
19 | ||
20 | // mp_count<L, V> | |
21 | namespace detail | |
22 | { | |
23 | ||
1e59de90 | 24 | #if !defined( BOOST_MP11_NO_CONSTEXPR ) |
b32b8144 FG |
25 | |
26 | constexpr std::size_t cx_plus() | |
27 | { | |
28 | return 0; | |
29 | } | |
30 | ||
31 | template<class T1, class... T> constexpr std::size_t cx_plus(T1 t1, T... t) | |
32 | { | |
92f5a8d4 | 33 | return static_cast<std::size_t>(t1) + cx_plus(t...); |
b32b8144 FG |
34 | } |
35 | ||
36 | template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class... T> | |
37 | 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) | |
38 | { | |
92f5a8d4 | 39 | return static_cast<std::size_t>(t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9 + t10) + cx_plus(t...); |
b32b8144 FG |
40 | } |
41 | ||
1e59de90 TL |
42 | #endif |
43 | ||
44 | template<class L, class V> struct mp_count_impl; | |
45 | ||
46 | #if defined( BOOST_MP11_HAS_CXX14_CONSTEXPR ) | |
47 | ||
48 | template<class V, class... T> constexpr std::size_t cx_count() | |
49 | { | |
50 | constexpr bool a[] = { false, std::is_same<T, V>::value... }; | |
51 | ||
52 | std::size_t r = 0; | |
53 | ||
54 | for( std::size_t i = 1; i < sizeof...(T) + 1; ++i ) | |
55 | { | |
56 | r += a[ i ]; | |
57 | } | |
58 | ||
59 | return r; | |
60 | } | |
61 | ||
62 | template<template<class...> class L, class... T, class V> struct mp_count_impl<L<T...>, V> | |
63 | { | |
64 | using type = mp_size_t<cx_count<V, T...>()>; | |
65 | }; | |
66 | ||
67 | #elif !defined( BOOST_MP11_NO_CONSTEXPR ) | |
68 | ||
b32b8144 FG |
69 | template<template<class...> class L, class... T, class V> struct mp_count_impl<L<T...>, V> |
70 | { | |
71 | using type = mp_size_t<cx_plus(std::is_same<T, V>::value...)>; | |
72 | }; | |
73 | ||
74 | #else | |
75 | ||
76 | template<template<class...> class L, class... T, class V> struct mp_count_impl<L<T...>, V> | |
77 | { | |
78 | using type = mp_size_t<mp_plus<std::is_same<T, V>...>::value>; | |
79 | }; | |
80 | ||
81 | #endif | |
82 | ||
83 | } // namespace detail | |
84 | ||
85 | template<class L, class V> using mp_count = typename detail::mp_count_impl<L, V>::type; | |
86 | ||
87 | // mp_count_if<L, P> | |
88 | namespace detail | |
89 | { | |
90 | ||
91 | template<class L, template<class...> class P> struct mp_count_if_impl; | |
92 | ||
1e59de90 TL |
93 | #if defined( BOOST_MP11_HAS_CXX14_CONSTEXPR ) && !BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1930 ) |
94 | ||
95 | template<template<class...> class P, class... T> constexpr std::size_t cx_count_if() | |
96 | { | |
97 | constexpr bool a[] = { false, static_cast<bool>( P<T>::value )... }; | |
98 | ||
99 | std::size_t r = 0; | |
100 | ||
101 | for( std::size_t i = 1; i < sizeof...(T) + 1; ++i ) | |
102 | { | |
103 | r += a[ i ]; | |
104 | } | |
105 | ||
106 | return r; | |
107 | } | |
b32b8144 FG |
108 | |
109 | template<template<class...> class L, class... T, template<class...> class P> struct mp_count_if_impl<L<T...>, P> | |
110 | { | |
1e59de90 | 111 | using type = mp_size_t<cx_count_if<P, T...>()>; |
b32b8144 FG |
112 | }; |
113 | ||
114 | #elif !defined( BOOST_MP11_NO_CONSTEXPR ) | |
115 | ||
116 | template<template<class...> class L, class... T, template<class...> class P> struct mp_count_if_impl<L<T...>, P> | |
117 | { | |
118 | using type = mp_size_t<cx_plus(mp_to_bool<P<T>>::value...)>; | |
119 | }; | |
120 | ||
121 | #else | |
122 | ||
123 | template<template<class...> class L, class... T, template<class...> class P> struct mp_count_if_impl<L<T...>, P> | |
124 | { | |
92f5a8d4 | 125 | #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 ) |
b32b8144 FG |
126 | |
127 | template<class T> struct _f { using type = mp_to_bool<P<T>>; }; | |
128 | using type = mp_size_t<mp_plus<typename _f<T>::type...>::value>; | |
129 | ||
130 | #else | |
131 | ||
132 | using type = mp_size_t<mp_plus<mp_to_bool<P<T>>...>::value>; | |
133 | ||
134 | #endif | |
135 | }; | |
136 | ||
137 | #endif | |
138 | ||
139 | } // namespace detail | |
140 | ||
141 | template<class L, template<class...> class P> using mp_count_if = typename detail::mp_count_if_impl<L, P>::type; | |
142 | template<class L, class Q> using mp_count_if_q = mp_count_if<L, Q::template fn>; | |
143 | ||
144 | } // namespace mp11 | |
145 | } // namespace boost | |
146 | ||
147 | #endif // #ifndef BOOST_MP11_DETAIL_MP_COUNT_HPP_INCLUDED |