count_if.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright Neil Groves 2009. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // For more information, see http://www.boost.org/libs/range/
  8. //
  9. #ifndef BOOST_RANGE_ALGORITHM_COUNT_IF_HPP_INCLUDED
  10. #define BOOST_RANGE_ALGORITHM_COUNT_IF_HPP_INCLUDED
  11. #include <boost/concept_check.hpp>
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. #include <boost/range/concepts.hpp>
  15. #include <boost/range/difference_type.hpp>
  16. #include <algorithm>
  17. namespace boost
  18. {
  19. namespace range
  20. {
  21. /// \brief template function count_if
  22. ///
  23. /// range-based version of the count_if std algorithm
  24. ///
  25. /// \pre SinglePassRange is a model of the SinglePassRangeConcept
  26. /// \pre UnaryPredicate is a model of the UnaryPredicateConcept
  27. template< class SinglePassRange, class UnaryPredicate >
  28. inline BOOST_DEDUCED_TYPENAME boost::range_difference<SinglePassRange>::type
  29. count_if(SinglePassRange& rng, UnaryPredicate pred)
  30. {
  31. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange> ));
  32. return std::count_if(boost::begin(rng), boost::end(rng), pred);
  33. }
  34. /// \overload
  35. template< class SinglePassRange, class UnaryPredicate >
  36. inline BOOST_DEDUCED_TYPENAME boost::range_difference<const SinglePassRange>::type
  37. count_if(const SinglePassRange& rng, UnaryPredicate pred)
  38. {
  39. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> ));
  40. return std::count_if(boost::begin(rng), boost::end(rng), pred);
  41. }
  42. } // namespace range
  43. using range::count_if;
  44. } // namespace boost
  45. #endif // include guard