• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ALL_HPP
12 #define BOOST_COMPUTE_ALGORITHM_DETAIL_SEARCH_ALL_HPP
13 
14 #include <boost/compute/algorithm/copy.hpp>
15 #include <boost/compute/container/vector.hpp>
16 #include <boost/compute/detail/iterator_range_size.hpp>
17 #include <boost/compute/detail/meta_kernel.hpp>
18 #include <boost/compute/lambda.hpp>
19 #include <boost/compute/system.hpp>
20 
21 namespace boost {
22 namespace compute {
23 namespace detail {
24 
25 ///
26 /// \brief Search kernel class
27 ///
28 /// Subclass of meta_kernel which is capable of performing pattern matching
29 ///
30 template<class PatternIterator, class TextIterator, class OutputIterator>
31 class search_kernel : public meta_kernel
32 {
33 public:
search_kernel()34     search_kernel() : meta_kernel("search")
35     {}
36 
set_range(PatternIterator p_first,PatternIterator p_last,TextIterator t_first,TextIterator t_last,OutputIterator result)37     void set_range(PatternIterator p_first,
38                    PatternIterator p_last,
39                    TextIterator t_first,
40                    TextIterator t_last,
41                    OutputIterator result)
42     {
43         m_p_count = iterator_range_size(p_first, p_last);
44         m_p_count_arg = add_arg<uint_>("p_count");
45 
46         m_count = iterator_range_size(t_first, t_last);
47         m_count = m_count + 1 - m_p_count;
48 
49         *this <<
50             "uint i = get_global_id(0);\n" <<
51             "const uint i1 = i;\n" <<
52             "uint j;\n" <<
53             "for(j = 0; j<p_count; j++,i++)\n" <<
54             "{\n" <<
55             "   if(" << p_first[expr<uint_>("j")] << " != " <<
56                     t_first[expr<uint_>("i")] << ")\n" <<
57             "       j = p_count + 1;\n" <<
58             "}\n" <<
59             "if(j == p_count)\n" <<
60             result[expr<uint_>("i1")] << " = 1;\n" <<
61             "else\n" <<
62             result[expr<uint_>("i1")] << " = 0;\n";
63     }
64 
exec(command_queue & queue)65     event exec(command_queue &queue)
66     {
67         if(m_count == 0) {
68             return event();
69         }
70 
71         set_arg(m_p_count_arg, uint_(m_p_count));
72 
73         return exec_1d(queue, 0, m_count);
74     }
75 
76 private:
77     size_t m_p_count;
78     size_t m_p_count_arg;
79     size_t m_count;
80 };
81 
82 } //end detail namespace
83 } //end compute namespace
84 } //end boost namespace
85 
86 #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SEARCH_ALL_HPP
87