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