Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:57

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2014 Roshan <thisisroshansmail@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
0009 //---------------------------------------------------------------------------//
0010 
0011 #ifndef BOOST_COMPUTE_ALGORITHM_SEARCH_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_SEARCH_HPP
0013 
0014 #include <boost/static_assert.hpp>
0015 
0016 #include <boost/compute/algorithm/detail/search_all.hpp>
0017 #include <boost/compute/algorithm/find.hpp>
0018 #include <boost/compute/container/vector.hpp>
0019 #include <boost/compute/detail/iterator_range_size.hpp>
0020 #include <boost/compute/detail/meta_kernel.hpp>
0021 #include <boost/compute/system.hpp>
0022 #include <boost/compute/type_traits/is_device_iterator.hpp>
0023 
0024 namespace boost {
0025 namespace compute {
0026 
0027 ///
0028 /// \brief Substring matching algorithm
0029 ///
0030 /// Searches for the first match of the pattern [p_first, p_last)
0031 /// in text [t_first, t_last).
0032 /// \return Iterator pointing to beginning of first occurrence
0033 ///
0034 /// \param t_first Iterator pointing to start of text
0035 /// \param t_last Iterator pointing to end of text
0036 /// \param p_first Iterator pointing to start of pattern
0037 /// \param p_last Iterator pointing to end of pattern
0038 /// \param queue Queue on which to execute
0039 ///
0040 /// Space complexity: \Omega(distance(\p t_first, \p t_last))
0041 template<class TextIterator, class PatternIterator>
0042 inline TextIterator search(TextIterator t_first,
0043                            TextIterator t_last,
0044                            PatternIterator p_first,
0045                            PatternIterator p_last,
0046                            command_queue &queue = system::default_queue())
0047 {
0048     BOOST_STATIC_ASSERT(is_device_iterator<TextIterator>::value);
0049     BOOST_STATIC_ASSERT(is_device_iterator<PatternIterator>::value);
0050 
0051     // there is no need to check if pattern starts at last n - 1 indices
0052     vector<uint_> matching_indices(
0053         detail::iterator_range_size(t_first, t_last)
0054             - detail::iterator_range_size(p_first, p_last) + 1,
0055         queue.get_context()
0056     );
0057 
0058     // search_kernel puts value 1 at every index in vector where pattern starts at
0059     detail::search_kernel<PatternIterator,
0060                           TextIterator,
0061                           vector<uint_>::iterator> kernel;
0062 
0063     kernel.set_range(p_first, p_last, t_first, t_last, matching_indices.begin());
0064     kernel.exec(queue);
0065 
0066     vector<uint_>::iterator index = ::boost::compute::find(
0067         matching_indices.begin(), matching_indices.end(), uint_(1), queue
0068     );
0069 
0070     // pattern was not found
0071     if(index == matching_indices.end())
0072         return t_last;
0073 
0074     return t_first + detail::iterator_range_size(matching_indices.begin(), index);
0075 }
0076 
0077 } //end compute namespace
0078 } //end boost namespace
0079 
0080 #endif // BOOST_COMPUTE_ALGORITHM_SEARCH_HPP