search.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #ifndef BOOST_COMPUTE_ALGORITHM_SEARCH_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_SEARCH_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/algorithm/detail/search_all.hpp>
  14. #include <boost/compute/algorithm/find.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/system.hpp>
  19. #include <boost/compute/type_traits/is_device_iterator.hpp>
  20. namespace boost {
  21. namespace compute {
  22. ///
  23. /// \brief Substring matching algorithm
  24. ///
  25. /// Searches for the first match of the pattern [p_first, p_last)
  26. /// in text [t_first, t_last).
  27. /// \return Iterator pointing to beginning of first occurrence
  28. ///
  29. /// \param t_first Iterator pointing to start of text
  30. /// \param t_last Iterator pointing to end of text
  31. /// \param p_first Iterator pointing to start of pattern
  32. /// \param p_last Iterator pointing to end of pattern
  33. /// \param queue Queue on which to execute
  34. ///
  35. /// Space complexity: \Omega(distance(\p t_first, \p t_last))
  36. template<class TextIterator, class PatternIterator>
  37. inline TextIterator search(TextIterator t_first,
  38. TextIterator t_last,
  39. PatternIterator p_first,
  40. PatternIterator p_last,
  41. command_queue &queue = system::default_queue())
  42. {
  43. BOOST_STATIC_ASSERT(is_device_iterator<TextIterator>::value);
  44. BOOST_STATIC_ASSERT(is_device_iterator<PatternIterator>::value);
  45. // there is no need to check if pattern starts at last n - 1 indices
  46. vector<uint_> matching_indices(
  47. detail::iterator_range_size(t_first, t_last)
  48. - detail::iterator_range_size(p_first, p_last) + 1,
  49. queue.get_context()
  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. kernel.set_range(p_first, p_last, t_first, t_last, matching_indices.begin());
  56. kernel.exec(queue);
  57. vector<uint_>::iterator index = ::boost::compute::find(
  58. matching_indices.begin(), matching_indices.end(), uint_(1), queue
  59. );
  60. // pattern was not found
  61. if(index == matching_indices.end())
  62. return t_last;
  63. return t_first + detail::iterator_range_size(matching_indices.begin(), index);
  64. }
  65. } //end compute namespace
  66. } //end boost namespace
  67. #endif // BOOST_COMPUTE_ALGORITHM_SEARCH_HPP