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_DETAIL_SEARCH_N_HPP
12 #define BOOST_COMPUTE_ALGORITHM_DETAIL_SEARCH_N_HPP
13 
14 #include <iterator>
15 
16 #include <boost/static_assert.hpp>
17 
18 #include <boost/compute/algorithm/find.hpp>
19 #include <boost/compute/container/vector.hpp>
20 #include <boost/compute/detail/iterator_range_size.hpp>
21 #include <boost/compute/detail/meta_kernel.hpp>
22 #include <boost/compute/system.hpp>
23 #include <boost/compute/type_traits/is_device_iterator.hpp>
24 
25 namespace boost {
26 namespace compute {
27 namespace detail {
28 
29 ///
30 /// \brief Search kernel class
31 ///
32 /// Subclass of meta_kernel which is capable of performing search_n
33 ///
34 template<class TextIterator, class OutputIterator>
35 class search_n_kernel : public meta_kernel
36 {
37 public:
38     typedef typename std::iterator_traits<TextIterator>::value_type value_type;
39 
search_n_kernel()40     search_n_kernel() : meta_kernel("search_n")
41     {}
42 
set_range(TextIterator t_first,TextIterator t_last,value_type value,size_t n,OutputIterator result)43     void set_range(TextIterator t_first,
44                    TextIterator t_last,
45                    value_type value,
46                    size_t n,
47                    OutputIterator result)
48     {
49         m_n = n;
50         m_n_arg = add_arg<uint_>("n");
51 
52         m_value = value;
53         m_value_arg = add_arg<value_type>("value");
54 
55         m_count = iterator_range_size(t_first, t_last);
56         m_count = m_count + 1 - m_n;
57 
58         *this <<
59             "uint i = get_global_id(0);\n" <<
60             "uint i1 = i;\n" <<
61             "uint j;\n" <<
62             "for(j = 0; j<n; j++,i++)\n" <<
63             "{\n" <<
64             "   if(value != " << t_first[expr<uint_>("i")] << ")\n" <<
65             "       j = n + 1;\n" <<
66             "}\n" <<
67             "if(j == n)\n" <<
68             result[expr<uint_>("i1")] << " = 1;\n" <<
69             "else\n" <<
70             result[expr<uint_>("i1")] << " = 0;\n";
71     }
72 
exec(command_queue & queue)73     event exec(command_queue &queue)
74     {
75         if(m_count == 0) {
76             return event();
77         }
78 
79         set_arg(m_n_arg, uint_(m_n));
80         set_arg(m_value_arg, m_value);
81 
82         return exec_1d(queue, 0, m_count);
83     }
84 
85 private:
86     size_t m_n;
87     size_t m_n_arg;
88     size_t m_count;
89     value_type m_value;
90     size_t m_value_arg;
91 };
92 
93 } //end detail namespace
94 
95 ///
96 /// \brief Substring matching algorithm
97 ///
98 /// Searches for the first occurrence of n consecutive occurrences of
99 /// value in text [t_first, t_last).
100 /// \return Iterator pointing to beginning of first occurrence
101 ///
102 /// \param t_first Iterator pointing to start of text
103 /// \param t_last Iterator pointing to end of text
104 /// \param n Number of times value repeats
105 /// \param value Value which repeats
106 /// \param queue Queue on which to execute
107 ///
108 /// Space complexity: \Omega(distance(\p t_first, \p t_last))
109 template<class TextIterator, class ValueType>
search_n(TextIterator t_first,TextIterator t_last,size_t n,ValueType value,command_queue & queue=system::default_queue ())110 inline TextIterator search_n(TextIterator t_first,
111                              TextIterator t_last,
112                              size_t n,
113                              ValueType value,
114                              command_queue &queue = system::default_queue())
115 {
116     BOOST_STATIC_ASSERT(is_device_iterator<TextIterator>::value);
117 
118     // there is no need to check if pattern starts at last n - 1 indices
119     vector<uint_> matching_indices(
120         detail::iterator_range_size(t_first, t_last) + 1 - n,
121         queue.get_context()
122     );
123 
124     // search_n_kernel puts value 1 at every index in vector where pattern
125     // of n values starts at
126     detail::search_n_kernel<TextIterator,
127                             vector<uint_>::iterator> kernel;
128 
129     kernel.set_range(t_first, t_last, value, n, matching_indices.begin());
130     kernel.exec(queue);
131 
132     vector<uint_>::iterator index = ::boost::compute::find(
133         matching_indices.begin(), matching_indices.end(), uint_(1), queue
134     );
135 
136     // pattern was not found
137     if(index == matching_indices.end())
138         return t_last;
139 
140     return t_first + detail::iterator_range_size(matching_indices.begin(), index);
141 }
142 
143 } //end compute namespace
144 } //end boost namespace
145 
146 #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SEARCH_N_HPP
147