3 Defines `boost::hana::count_if`.
5 @copyright Louis Dionne 2013-2017
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_HANA_COUNT_IF_HPP
11 #define BOOST_HANA_COUNT_IF_HPP
13 #include <boost/hana/fwd/count_if.hpp>
15 #include <boost/hana/concept/foldable.hpp>
16 #include <boost/hana/config.hpp>
17 #include <boost/hana/core/dispatch.hpp>
18 #include <boost/hana/detail/algorithm.hpp>
19 #include <boost/hana/detail/fast_and.hpp>
20 #include <boost/hana/integral_constant.hpp>
21 #include <boost/hana/unpack.hpp>
24 #include <type_traits>
28 namespace boost { namespace hana {
30 template <typename Xs, typename Pred>
31 constexpr auto count_if_t::operator()(Xs&& xs, Pred&& pred) const {
32 using S = typename hana::tag_of<Xs>::type;
33 using CountIf = BOOST_HANA_DISPATCH_IF(count_if_impl<S>,
34 hana::Foldable<S>::value
37 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
38 static_assert(hana::Foldable<S>::value,
39 "hana::count_if(xs, pred) requires 'xs' to be Foldable");
42 return CountIf::apply(static_cast<Xs&&>(xs),
43 static_cast<Pred&&>(pred));
48 template <typename Pred>
51 template <typename ...Xs, typename = typename std::enable_if<
53 Constant<decltype((*pred)(std::declval<Xs&&>()))>::value...
56 constexpr auto operator()(Xs&& ...xs) const {
57 constexpr bool results[] = {false, // <-- avoid empty array
58 static_cast<bool>(hana::value<decltype((*pred)(static_cast<Xs&&>(xs)))>())...
60 constexpr std::size_t total = detail::count(
61 results, results + sizeof(results), true
63 return hana::size_c<total>;
66 template <typename ...Xs, typename = void, typename = typename std::enable_if<
68 Constant<decltype((*pred)(std::declval<Xs&&>()))>::value...
71 constexpr auto operator()(Xs&& ...xs) const {
72 std::size_t total = 0;
73 using Swallow = std::size_t[];
74 (void)Swallow{0, ((*pred)(static_cast<Xs&&>(xs)) ? ++total : 0)...};
80 template <typename T, bool condition>
81 struct count_if_impl<T, when<condition>> : default_ {
82 template <typename Xs, typename Pred>
83 static constexpr decltype(auto) apply(Xs&& xs, Pred&& pred) {
84 // We use a pointer instead of a reference to avoid a Clang ICE.
85 return hana::unpack(static_cast<Xs&&>(xs),
86 detail::count_pred<decltype(&pred)>{&pred}
90 }} // end namespace boost::hana
92 #endif // !BOOST_HANA_COUNT_IF_HPP