]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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_SEARCH_HPP | |
12 | #define BOOST_COMPUTE_ALGORITHM_SEARCH_HPP | |
13 | ||
14 | #include <boost/compute/algorithm/detail/search_all.hpp> | |
15 | #include <boost/compute/algorithm/find.hpp> | |
16 | #include <boost/compute/container/vector.hpp> | |
17 | #include <boost/compute/detail/iterator_range_size.hpp> | |
18 | #include <boost/compute/detail/meta_kernel.hpp> | |
19 | #include <boost/compute/system.hpp> | |
20 | ||
21 | namespace boost { | |
22 | namespace compute { | |
23 | ||
24 | /// | |
25 | /// \brief Substring matching algorithm | |
26 | /// | |
27 | /// Searches for the first match of the pattern [p_first, p_last) | |
28 | /// in text [t_first, t_last). | |
29 | /// \return Iterator pointing to beginning of first occurrence | |
30 | /// | |
31 | /// \param t_first Iterator pointing to start of text | |
32 | /// \param t_last Iterator pointing to end of text | |
33 | /// \param p_first Iterator pointing to start of pattern | |
34 | /// \param p_last Iterator pointing to end of pattern | |
35 | /// \param queue Queue on which to execute | |
36 | /// | |
37 | template<class TextIterator, class PatternIterator> | |
38 | inline TextIterator search(TextIterator t_first, | |
39 | TextIterator t_last, | |
40 | PatternIterator p_first, | |
41 | PatternIterator p_last, | |
42 | command_queue &queue = system::default_queue()) | |
43 | { | |
44 | // there is no need to check if pattern starts at last n - 1 indices | |
45 | vector<uint_> matching_indices( | |
46 | detail::iterator_range_size(t_first, t_last) | |
47 | - detail::iterator_range_size(p_first, p_last) + 1, | |
48 | queue.get_context() | |
49 | ); | |
50 | ||
51 | // search_kernel puts value 1 at every index in vector where pattern starts at | |
52 | detail::search_kernel<PatternIterator, | |
53 | TextIterator, | |
54 | vector<uint_>::iterator> kernel; | |
55 | ||
56 | kernel.set_range(p_first, p_last, t_first, t_last, matching_indices.begin()); | |
57 | kernel.exec(queue); | |
58 | ||
59 | vector<uint_>::iterator index = ::boost::compute::find( | |
60 | matching_indices.begin(), matching_indices.end(), uint_(1), queue | |
61 | ); | |
62 | ||
63 | // pattern was not found | |
64 | if(index == matching_indices.end()) | |
65 | return t_last; | |
66 | ||
67 | return t_first + detail::iterator_range_size(matching_indices.begin(), index); | |
68 | } | |
69 | ||
70 | } //end compute namespace | |
71 | } //end boost namespace | |
72 | ||
73 | #endif // BOOST_COMPUTE_ALGORITHM_SEARCH_HPP |