is_permutation.hpp 2.7 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_IS_PERMUTATION_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_IS_PERMUTATION_HPP
  12. #include <iterator>
  13. #include <boost/static_assert.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/command_queue.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include <boost/compute/detail/iterator_range_size.hpp>
  18. #include <boost/compute/algorithm/equal.hpp>
  19. #include <boost/compute/algorithm/sort.hpp>
  20. #include <boost/compute/type_traits/is_device_iterator.hpp>
  21. namespace boost {
  22. namespace compute {
  23. ///
  24. /// \brief Permutation checking algorithm
  25. ///
  26. /// Checks if the range [first1, last1) can be permuted into the
  27. /// range [first2, last2)
  28. /// \return True, if it can be permuted. False, otherwise.
  29. ///
  30. /// \param first1 Iterator pointing to start of first range
  31. /// \param last1 Iterator pointing to end of first range
  32. /// \param first2 Iterator pointing to start of second range
  33. /// \param last2 Iterator pointing to end of second range
  34. /// \param queue Queue on which to execute
  35. ///
  36. /// Space complexity: \Omega(distance(\p first1, \p last1) + distance(\p first2, \p last2))
  37. template<class InputIterator1, class InputIterator2>
  38. inline bool is_permutation(InputIterator1 first1,
  39. InputIterator1 last1,
  40. InputIterator2 first2,
  41. InputIterator2 last2,
  42. command_queue &queue = system::default_queue())
  43. {
  44. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
  45. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
  46. typedef typename std::iterator_traits<InputIterator1>::value_type value_type1;
  47. typedef typename std::iterator_traits<InputIterator2>::value_type value_type2;
  48. size_t count1 = detail::iterator_range_size(first1, last1);
  49. size_t count2 = detail::iterator_range_size(first2, last2);
  50. if(count1 != count2) return false;
  51. vector<value_type1> temp1(first1, last1, queue);
  52. vector<value_type2> temp2(first2, last2, queue);
  53. sort(temp1.begin(), temp1.end(), queue);
  54. sort(temp2.begin(), temp2.end(), queue);
  55. return equal(temp1.begin(), temp1.end(),
  56. temp2.begin(), queue);
  57. }
  58. } // end compute namespace
  59. } // end boost namespace
  60. #endif // BOOST_COMPUTE_ALGORITHM_IS_PERMUTATION_HPP