mismatch.hpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <[email protected]>
  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_MISMATCH_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_MISMATCH_HPP
  12. #include <iterator>
  13. #include <utility>
  14. #include <boost/static_assert.hpp>
  15. #include <boost/compute/system.hpp>
  16. #include <boost/compute/functional.hpp>
  17. #include <boost/compute/command_queue.hpp>
  18. #include <boost/compute/algorithm/find.hpp>
  19. #include <boost/compute/iterator/transform_iterator.hpp>
  20. #include <boost/compute/iterator/zip_iterator.hpp>
  21. #include <boost/compute/functional/detail/unpack.hpp>
  22. #include <boost/compute/type_traits/is_device_iterator.hpp>
  23. namespace boost {
  24. namespace compute {
  25. /// Returns a pair of iterators pointing to the first position where the
  26. /// range [\p first1, \p last1) and the range starting at \p first2
  27. /// differ.
  28. ///
  29. /// Space complexity: \Omega(1)
  30. template<class InputIterator1, class InputIterator2>
  31. inline std::pair<InputIterator1, InputIterator2>
  32. mismatch(InputIterator1 first1,
  33. InputIterator1 last1,
  34. InputIterator2 first2,
  35. command_queue &queue = system::default_queue())
  36. {
  37. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
  38. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
  39. typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
  40. ::boost::compute::equal_to<value_type> op;
  41. InputIterator2 last2 = first2 + std::distance(first1, last1);
  42. InputIterator1 iter =
  43. boost::get<0>(
  44. ::boost::compute::find(
  45. ::boost::compute::make_transform_iterator(
  46. ::boost::compute::make_zip_iterator(
  47. boost::make_tuple(first1, first2)
  48. ),
  49. detail::unpack(op)
  50. ),
  51. ::boost::compute::make_transform_iterator(
  52. ::boost::compute::make_zip_iterator(
  53. boost::make_tuple(last1, last2)
  54. ),
  55. detail::unpack(op)
  56. ),
  57. false,
  58. queue
  59. ).base().get_iterator_tuple()
  60. );
  61. return std::make_pair(iter, first2 + std::distance(first1, iter));
  62. }
  63. /// \overload
  64. template<class InputIterator1, class InputIterator2>
  65. inline std::pair<InputIterator1, InputIterator2>
  66. mismatch(InputIterator1 first1,
  67. InputIterator1 last1,
  68. InputIterator2 first2,
  69. InputIterator2 last2,
  70. command_queue &queue = system::default_queue())
  71. {
  72. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
  73. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
  74. if(std::distance(first1, last1) < std::distance(first2, last2)){
  75. return ::boost::compute::mismatch(first1, last1, first2, queue);
  76. }
  77. else {
  78. return ::boost::compute::mismatch(
  79. first1, first1 + std::distance(first2, last2), first2, queue
  80. );
  81. }
  82. }
  83. } // end compute namespace
  84. } // end boost namespace
  85. #endif // BOOST_COMPUTE_ALGORITHM_MISMATCH_HPP