count.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_ALGORITHM_COUNT_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_COUNT_HPP
  12. #include <boost/compute/lambda.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/command_queue.hpp>
  15. #include <boost/compute/algorithm/count_if.hpp>
  16. #include <boost/compute/type_traits/vector_size.hpp>
  17. namespace boost {
  18. namespace compute {
  19. /// Returns the number of occurrences of \p value in the range
  20. /// [\p first, \p last).
  21. ///
  22. /// \see count_if()
  23. template<class InputIterator, class T>
  24. inline size_t count(InputIterator first,
  25. InputIterator last,
  26. const T &value,
  27. command_queue &queue = system::default_queue())
  28. {
  29. typedef typename std::iterator_traits<InputIterator>::value_type value_type;
  30. using ::boost::compute::_1;
  31. using ::boost::compute::lambda::all;
  32. if(vector_size<value_type>::value == 1){
  33. return ::boost::compute::count_if(first,
  34. last,
  35. _1 == value,
  36. queue);
  37. }
  38. else {
  39. return ::boost::compute::count_if(first,
  40. last,
  41. all(_1 == value),
  42. queue);
  43. }
  44. }
  45. } // end compute namespace
  46. } // end boost namespace
  47. #endif // BOOST_COMPUTE_ALGORITHM_COUNT_HPP