1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2014 Roshan <thisisroshansmail@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ALGORITHM_IS_PERMUTATION_HPP
12 #define BOOST_COMPUTE_ALGORITHM_IS_PERMUTATION_HPP
16 #include <boost/static_assert.hpp>
18 #include <boost/compute/system.hpp>
19 #include <boost/compute/command_queue.hpp>
20 #include <boost/compute/container/vector.hpp>
21 #include <boost/compute/detail/iterator_range_size.hpp>
22 #include <boost/compute/algorithm/equal.hpp>
23 #include <boost/compute/algorithm/sort.hpp>
24 #include <boost/compute/type_traits/is_device_iterator.hpp>
30 /// \brief Permutation checking algorithm
32 /// Checks if the range [first1, last1) can be permuted into the
33 /// range [first2, last2)
34 /// \return True, if it can be permuted. False, otherwise.
36 /// \param first1 Iterator pointing to start of first range
37 /// \param last1 Iterator pointing to end of first range
38 /// \param first2 Iterator pointing to start of second range
39 /// \param last2 Iterator pointing to end of second range
40 /// \param queue Queue on which to execute
42 /// Space complexity: \Omega(distance(\p first1, \p last1) + distance(\p first2, \p last2))
43 template<class InputIterator1, class InputIterator2>
44 inline bool is_permutation(InputIterator1 first1,
46 InputIterator2 first2,
48 command_queue &queue = system::default_queue())
50 BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
51 BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
52 typedef typename std::iterator_traits<InputIterator1>::value_type value_type1;
53 typedef typename std::iterator_traits<InputIterator2>::value_type value_type2;
55 size_t count1 = detail::iterator_range_size(first1, last1);
56 size_t count2 = detail::iterator_range_size(first2, last2);
58 if(count1 != count2) return false;
60 vector<value_type1> temp1(first1, last1, queue);
61 vector<value_type2> temp2(first2, last2, queue);
63 sort(temp1.begin(), temp1.end(), queue);
64 sort(temp2.begin(), temp2.end(), queue);
66 return equal(temp1.begin(), temp1.end(),
67 temp2.begin(), queue);
70 } // end compute namespace
71 } // end boost namespace
73 #endif // BOOST_COMPUTE_ALGORITHM_IS_PERMUTATION_HPP