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