1 /*! 2 @file 3 Defines `boost::hana::count_if`. 4 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) 8 */ 9 10 #ifndef BOOST_HANA_COUNT_IF_HPP 11 #define BOOST_HANA_COUNT_IF_HPP 12 13 #include <boost/hana/fwd/count_if.hpp> 14 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> 22 23 #include <cstddef> 24 #include <type_traits> 25 #include <utility> 26 27 28 BOOST_HANA_NAMESPACE_BEGIN 29 //! @cond 30 template <typename Xs, typename Pred> operator ()(Xs && xs,Pred && pred) const31 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 35 ); 36 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"); 40 #endif 41 42 return CountIf::apply(static_cast<Xs&&>(xs), 43 static_cast<Pred&&>(pred)); 44 } 45 //! @endcond 46 47 namespace detail { 48 template <typename Pred> 49 struct count_pred { 50 Pred pred; 51 template <typename ...Xs, typename = typename std::enable_if< 52 detail::fast_and< 53 Constant<decltype((*pred)(std::declval<Xs&&>()))>::value... 54 >::value 55 >::type> operator ()detail::count_pred56 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)))>())... 59 }; 60 constexpr std::size_t total = detail::count( 61 results, results + sizeof(results), true 62 ); 63 return hana::size_c<total>; 64 } 65 66 template <typename ...Xs, typename = void, typename = typename std::enable_if< 67 !detail::fast_and< 68 Constant<decltype((*pred)(std::declval<Xs&&>()))>::value... 69 >::value 70 >::type> operator ()detail::count_pred71 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)...}; 75 return total; 76 } 77 }; 78 } 79 80 template <typename T, bool condition> 81 struct count_if_impl<T, when<condition>> : default_ { 82 template <typename Xs, typename Pred> applycount_if_impl83 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} 87 ); 88 } 89 }; 90 BOOST_HANA_NAMESPACE_END 91 92 #endif // !BOOST_HANA_COUNT_IF_HPP 93