/*! @file Defines `boost::hana::count_if`. @copyright Louis Dionne 2013-2016 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_HANA_COUNT_IF_HPP #define BOOST_HANA_COUNT_IF_HPP #include #include #include #include #include #include #include #include #include #include #include BOOST_HANA_NAMESPACE_BEGIN //! @cond template constexpr auto count_if_t::operator()(Xs&& xs, Pred&& pred) const { using S = typename hana::tag_of::type; using CountIf = BOOST_HANA_DISPATCH_IF(count_if_impl, hana::Foldable::value ); #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS static_assert(hana::Foldable::value, "hana::count_if(xs, pred) requires 'xs' to be Foldable"); #endif return CountIf::apply(static_cast(xs), static_cast(pred)); } //! @endcond namespace detail { template struct count_pred { Pred pred; template ()))>::value... >::value >::type> constexpr auto operator()(Xs&& ...xs) const { constexpr bool results[] = {false, // <-- avoid empty array static_cast(hana::value(xs)))>())... }; constexpr std::size_t total = detail::count( results, results + sizeof(results), true ); return hana::size_c; } template ()))>::value... >::value >::type> constexpr auto operator()(Xs&& ...xs) const { std::size_t total = 0; using Swallow = std::size_t[]; (void)Swallow{0, ((*pred)(static_cast(xs)) ? ++total : 0)...}; return total; } }; } template struct count_if_impl> : default_ { template static constexpr decltype(auto) apply(Xs&& xs, Pred&& pred) { // We use a pointer instead of a reference to avoid a Clang ICE. return hana::unpack(static_cast(xs), detail::count_pred{&pred} ); } }; BOOST_HANA_NAMESPACE_END #endif // !BOOST_HANA_COUNT_IF_HPP