]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/compute/algorithm/is_permutation.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / compute / algorithm / is_permutation.hpp
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/static_assert.hpp>
17
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>
25
26 namespace boost {
27 namespace compute {
28
29 ///
30 /// \brief Permutation checking algorithm
31 ///
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.
35 ///
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
41 ///
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,
45 InputIterator1 last1,
46 InputIterator2 first2,
47 InputIterator2 last2,
48 command_queue &queue = system::default_queue())
49 {
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;
54
55 size_t count1 = detail::iterator_range_size(first1, last1);
56 size_t count2 = detail::iterator_range_size(first2, last2);
57
58 if(count1 != count2) return false;
59
60 vector<value_type1> temp1(first1, last1, queue);
61 vector<value_type2> temp2(first2, last2, queue);
62
63 sort(temp1.begin(), temp1.end(), queue);
64 sort(temp2.begin(), temp2.end(), queue);
65
66 return equal(temp1.begin(), temp1.end(),
67 temp2.begin(), queue);
68 }
69
70 } // end compute namespace
71 } // end boost namespace
72
73 #endif // BOOST_COMPUTE_ALGORITHM_IS_PERMUTATION_HPP