count.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*!
  2. @file
  3. Defines `boost::hana::count`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_COUNT_HPP
  9. #define BOOST_HANA_COUNT_HPP
  10. #include <boost/hana/fwd/count.hpp>
  11. #include <boost/hana/concept/foldable.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/dispatch.hpp>
  14. #include <boost/hana/count_if.hpp>
  15. #include <boost/hana/equal.hpp>
  16. BOOST_HANA_NAMESPACE_BEGIN
  17. //! @cond
  18. template <typename Xs, typename Value>
  19. constexpr auto count_t::operator()(Xs&& xs, Value&& value) const {
  20. using S = typename hana::tag_of<Xs>::type;
  21. using Count = BOOST_HANA_DISPATCH_IF(count_impl<S>,
  22. hana::Foldable<S>::value
  23. );
  24. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  25. static_assert(hana::Foldable<S>::value,
  26. "hana::count(xs, value) requires 'xs' to be Foldable");
  27. #endif
  28. return Count::apply(static_cast<Xs&&>(xs), static_cast<Value&&>(value));
  29. }
  30. //! @endcond
  31. template <typename T, bool condition>
  32. struct count_impl<T, when<condition>> : default_ {
  33. template <typename Xs, typename Value>
  34. static constexpr auto apply(Xs&& xs, Value&& value) {
  35. return hana::count_if(static_cast<Xs&&>(xs),
  36. hana::equal.to(static_cast<Value&&>(value)));
  37. }
  38. };
  39. BOOST_HANA_NAMESPACE_END
  40. #endif // !BOOST_HANA_COUNT_HPP