unique.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2014 Roshan <thisisroshansmail@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_UNIQUE_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_UNIQUE_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/command_queue.hpp>
  15. #include <boost/compute/algorithm/unique_copy.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include <boost/compute/functional/operator.hpp>
  18. #include <boost/compute/type_traits/is_device_iterator.hpp>
  19. namespace boost {
  20. namespace compute {
  21. /// Removes all consecutive duplicate elements (determined by \p op) from the
  22. /// range [first, last). If \p op is not provided, the equality operator is
  23. /// used.
  24. ///
  25. /// \param first first element in the input range
  26. /// \param last last element in the input range
  27. /// \param op binary operator used to check for uniqueness
  28. /// \param queue command queue to perform the operation
  29. ///
  30. /// \return \c InputIterator to the new logical end of the range
  31. ///
  32. /// Space complexity: \Omega(4n)
  33. ///
  34. /// \see unique_copy()
  35. template<class InputIterator, class BinaryPredicate>
  36. inline InputIterator unique(InputIterator first,
  37. InputIterator last,
  38. BinaryPredicate op,
  39. command_queue &queue = system::default_queue())
  40. {
  41. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  42. typedef typename std::iterator_traits<InputIterator>::value_type value_type;
  43. vector<value_type> temp(first, last, queue);
  44. return ::boost::compute::unique_copy(
  45. temp.begin(), temp.end(), first, op, queue
  46. );
  47. }
  48. /// \overload
  49. template<class InputIterator>
  50. inline InputIterator unique(InputIterator first,
  51. InputIterator last,
  52. command_queue &queue = system::default_queue())
  53. {
  54. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  55. typedef typename std::iterator_traits<InputIterator>::value_type value_type;
  56. return ::boost::compute::unique(
  57. first, last, ::boost::compute::equal_to<value_type>(), queue
  58. );
  59. }
  60. } // end compute namespace
  61. } // end boost namespace
  62. #endif // BOOST_COMPUTE_ALGORITHM_UNIQUE_HPP