Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:56

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2014 Roshan <thisisroshansmail@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
0009 //---------------------------------------------------------------------------//
0010 
0011 #ifndef BOOST_COMPUTE_ALGORITHM_IS_PERMUTATION_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_IS_PERMUTATION_HPP
0013 
0014 #include <iterator>
0015 
0016 #include <boost/static_assert.hpp>
0017 
0018 #include <boost/compute/system.hpp>
0019 #include <boost/compute/command_queue.hpp>
0020 #include <boost/compute/container/vector.hpp>
0021 #include <boost/compute/detail/iterator_range_size.hpp>
0022 #include <boost/compute/algorithm/equal.hpp>
0023 #include <boost/compute/algorithm/sort.hpp>
0024 #include <boost/compute/type_traits/is_device_iterator.hpp>
0025 
0026 namespace boost {
0027 namespace compute {
0028 
0029 ///
0030 /// \brief Permutation checking algorithm
0031 ///
0032 /// Checks if the range [first1, last1) can be permuted into the
0033 /// range [first2, last2)
0034 /// \return True, if it can be permuted. False, otherwise.
0035 ///
0036 /// \param first1 Iterator pointing to start of first range
0037 /// \param last1 Iterator pointing to end of first range
0038 /// \param first2 Iterator pointing to start of second range
0039 /// \param last2 Iterator pointing to end of second range
0040 /// \param queue Queue on which to execute
0041 ///
0042 /// Space complexity: \Omega(distance(\p first1, \p last1) + distance(\p first2, \p last2))
0043 template<class InputIterator1, class InputIterator2>
0044 inline bool is_permutation(InputIterator1 first1,
0045                            InputIterator1 last1,
0046                            InputIterator2 first2,
0047                            InputIterator2 last2,
0048                            command_queue &queue = system::default_queue())
0049 {
0050     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
0051     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
0052     typedef typename std::iterator_traits<InputIterator1>::value_type value_type1;
0053     typedef typename std::iterator_traits<InputIterator2>::value_type value_type2;
0054 
0055     size_t count1 = detail::iterator_range_size(first1, last1);
0056     size_t count2 = detail::iterator_range_size(first2, last2);
0057 
0058     if(count1 != count2) return false;
0059 
0060     vector<value_type1> temp1(first1, last1, queue);
0061     vector<value_type2> temp2(first2, last2, queue);
0062 
0063     sort(temp1.begin(), temp1.end(), queue);
0064     sort(temp2.begin(), temp2.end(), queue);
0065 
0066     return equal(temp1.begin(), temp1.end(),
0067                  temp2.begin(), queue);
0068 }
0069 
0070 } // end compute namespace
0071 } // end boost namespace
0072 
0073 #endif // BOOST_COMPUTE_ALGORITHM_IS_PERMUTATION_HPP