count.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <[email protected]>
  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/static_assert.hpp>
  13. #include <boost/compute/lambda.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/command_queue.hpp>
  16. #include <boost/compute/algorithm/count_if.hpp>
  17. #include <boost/compute/type_traits/vector_size.hpp>
  18. #include <boost/compute/type_traits/is_device_iterator.hpp>
  19. namespace boost {
  20. namespace compute {
  21. /// Returns the number of occurrences of \p value in the range
  22. /// [\p first, \p last).
  23. ///
  24. /// Space complexity on CPUs: \Omega(1)<br>
  25. /// Space complexity on GPUs: \Omega(n)
  26. ///
  27. /// \see count_if()
  28. template<class InputIterator, class T>
  29. inline size_t count(InputIterator first,
  30. InputIterator last,
  31. const T &value,
  32. command_queue &queue = system::default_queue())
  33. {
  34. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  35. typedef typename std::iterator_traits<InputIterator>::value_type value_type;
  36. using ::boost::compute::_1;
  37. using ::boost::compute::lambda::all;
  38. if(vector_size<value_type>::value == 1){
  39. return ::boost::compute::count_if(first,
  40. last,
  41. _1 == value,
  42. queue);
  43. }
  44. else {
  45. return ::boost::compute::count_if(first,
  46. last,
  47. all(_1 == value),
  48. queue);
  49. }
  50. }
  51. } // end compute namespace
  52. } // end boost namespace
  53. #endif // BOOST_COMPUTE_ALGORITHM_COUNT_HPP