boyer_moore.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. Copyright (c) Marshall Clow 2010-2012.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. For more information, see http://www.boost.org
  6. */
  7. #ifndef BOOST_ALGORITHM_BOYER_MOORE_SEARCH_HPP
  8. #define BOOST_ALGORITHM_BOYER_MOORE_SEARCH_HPP
  9. #include <iterator> // for std::iterator_traits
  10. #include <boost/assert.hpp>
  11. #include <boost/static_assert.hpp>
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. #include <boost/utility/enable_if.hpp>
  15. #include <boost/type_traits/is_same.hpp>
  16. #include <boost/algorithm/searching/detail/bm_traits.hpp>
  17. #include <boost/algorithm/searching/detail/debugging.hpp>
  18. namespace boost { namespace algorithm {
  19. /*
  20. A templated version of the boyer-moore searching algorithm.
  21. References:
  22. http://www.cs.utexas.edu/users/moore/best-ideas/string-searching/
  23. http://www.cs.utexas.edu/~moore/publications/fstrpos.pdf
  24. Explanations:
  25. http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm
  26. http://www.movsd.com/bm.htm
  27. http://www.cs.ucdavis.edu/~gusfield/cs224f09/bnotes.pdf
  28. The Boyer-Moore search algorithm uses two tables, a "bad character" table
  29. to tell how far to skip ahead when it hits a character that is not in the pattern,
  30. and a "good character" table to tell how far to skip ahead when it hits a
  31. mismatch on a character that _is_ in the pattern.
  32. Requirements:
  33. * Random access iterators
  34. * The two iterator types (patIter and corpusIter) must
  35. "point to" the same underlying type and be comparable.
  36. * Additional requirements may be imposed but the skip table, such as:
  37. ** Numeric type (array-based skip table)
  38. ** Hashable type (map-based skip table)
  39. */
  40. template <typename patIter, typename traits = detail::BM_traits<patIter> >
  41. class boyer_moore {
  42. typedef typename std::iterator_traits<patIter>::difference_type difference_type;
  43. public:
  44. boyer_moore ( patIter first, patIter last )
  45. : pat_first ( first ), pat_last ( last ),
  46. k_pattern_length ( std::distance ( pat_first, pat_last )),
  47. skip_ ( k_pattern_length, -1 ),
  48. suffix_ ( k_pattern_length + 1 )
  49. {
  50. this->build_skip_table ( first, last );
  51. this->build_suffix_table ( first, last );
  52. }
  53. ~boyer_moore () {}
  54. /// \fn operator ( corpusIter corpus_first, corpusIter corpus_last )
  55. /// \brief Searches the corpus for the pattern that was passed into the constructor
  56. ///
  57. /// \param corpus_first The start of the data to search (Random Access Iterator)
  58. /// \param corpus_last One past the end of the data to search
  59. ///
  60. template <typename corpusIter>
  61. std::pair<corpusIter, corpusIter>
  62. operator () ( corpusIter corpus_first, corpusIter corpus_last ) const {
  63. BOOST_STATIC_ASSERT (( boost::is_same<
  64. typename std::iterator_traits<patIter>::value_type,
  65. typename std::iterator_traits<corpusIter>::value_type>::value ));
  66. if ( corpus_first == corpus_last ) return std::make_pair(corpus_last, corpus_last); // if nothing to search, we didn't find it!
  67. if ( pat_first == pat_last ) return std::make_pair(corpus_first, corpus_first); // empty pattern matches at start
  68. const difference_type k_corpus_length = std::distance ( corpus_first, corpus_last );
  69. // If the pattern is larger than the corpus, we can't find it!
  70. if ( k_corpus_length < k_pattern_length )
  71. return std::make_pair(corpus_last, corpus_last);
  72. // Do the search
  73. return this->do_search ( corpus_first, corpus_last );
  74. }
  75. template <typename Range>
  76. std::pair<typename boost::range_iterator<Range>::type, typename boost::range_iterator<Range>::type>
  77. operator () ( Range &r ) const {
  78. return (*this) (boost::begin(r), boost::end(r));
  79. }
  80. private:
  81. /// \cond DOXYGEN_HIDE
  82. patIter pat_first, pat_last;
  83. const difference_type k_pattern_length;
  84. typename traits::skip_table_t skip_;
  85. std::vector <difference_type> suffix_;
  86. /// \fn operator ( corpusIter corpus_first, corpusIter corpus_last, Pred p )
  87. /// \brief Searches the corpus for the pattern that was passed into the constructor
  88. ///
  89. /// \param corpus_first The start of the data to search (Random Access Iterator)
  90. /// \param corpus_last One past the end of the data to search
  91. /// \param p A predicate used for the search comparisons.
  92. ///
  93. template <typename corpusIter>
  94. std::pair<corpusIter, corpusIter>
  95. do_search ( corpusIter corpus_first, corpusIter corpus_last ) const {
  96. /* ---- Do the matching ---- */
  97. corpusIter curPos = corpus_first;
  98. const corpusIter lastPos = corpus_last - k_pattern_length;
  99. difference_type j, k, m;
  100. while ( curPos <= lastPos ) {
  101. /* while ( std::distance ( curPos, corpus_last ) >= k_pattern_length ) { */
  102. // Do we match right where we are?
  103. j = k_pattern_length;
  104. while ( pat_first [j-1] == curPos [j-1] ) {
  105. j--;
  106. // We matched - we're done!
  107. if ( j == 0 )
  108. return std::make_pair(curPos, curPos + k_pattern_length);
  109. }
  110. // Since we didn't match, figure out how far to skip forward
  111. k = skip_ [ curPos [ j - 1 ]];
  112. m = j - k - 1;
  113. if ( k < j && m > suffix_ [ j ] )
  114. curPos += m;
  115. else
  116. curPos += suffix_ [ j ];
  117. }
  118. return std::make_pair(corpus_last, corpus_last); // We didn't find anything
  119. }
  120. void build_skip_table ( patIter first, patIter last ) {
  121. for ( std::size_t i = 0; first != last; ++first, ++i )
  122. skip_.insert ( *first, i );
  123. }
  124. template<typename Iter, typename Container>
  125. void compute_bm_prefix ( Iter first, Iter last, Container &prefix ) {
  126. const std::size_t count = std::distance ( first, last );
  127. BOOST_ASSERT ( count > 0 );
  128. BOOST_ASSERT ( prefix.size () == count );
  129. prefix[0] = 0;
  130. std::size_t k = 0;
  131. for ( std::size_t i = 1; i < count; ++i ) {
  132. BOOST_ASSERT ( k < count );
  133. while ( k > 0 && ( first[k] != first[i] )) {
  134. BOOST_ASSERT ( k < count );
  135. k = prefix [ k - 1 ];
  136. }
  137. if ( first[k] == first[i] )
  138. k++;
  139. prefix [ i ] = k;
  140. }
  141. }
  142. void build_suffix_table ( patIter first, patIter last ) {
  143. const std::size_t count = (std::size_t) std::distance ( first, last );
  144. if ( count > 0 ) { // empty pattern
  145. std::vector<typename std::iterator_traits<patIter>::value_type> reversed(count);
  146. (void) std::reverse_copy ( first, last, reversed.begin ());
  147. std::vector<difference_type> prefix (count);
  148. compute_bm_prefix ( first, last, prefix );
  149. std::vector<difference_type> prefix_reversed (count);
  150. compute_bm_prefix ( reversed.begin (), reversed.end (), prefix_reversed );
  151. for ( std::size_t i = 0; i <= count; i++ )
  152. suffix_[i] = count - prefix [count-1];
  153. for ( std::size_t i = 0; i < count; i++ ) {
  154. const std::size_t j = count - prefix_reversed[i];
  155. const difference_type k = i - prefix_reversed[i] + 1;
  156. if (suffix_[j] > k)
  157. suffix_[j] = k;
  158. }
  159. }
  160. }
  161. /// \endcond
  162. };
  163. /* Two ranges as inputs gives us four possibilities; with 2,3,3,4 parameters
  164. Use a bit of TMP to disambiguate the 3-argument templates */
  165. /// \fn boyer_moore_search ( corpusIter corpus_first, corpusIter corpus_last,
  166. /// patIter pat_first, patIter pat_last )
  167. /// \brief Searches the corpus for the pattern.
  168. ///
  169. /// \param corpus_first The start of the data to search (Random Access Iterator)
  170. /// \param corpus_last One past the end of the data to search
  171. /// \param pat_first The start of the pattern to search for (Random Access Iterator)
  172. /// \param pat_last One past the end of the data to search for
  173. ///
  174. template <typename patIter, typename corpusIter>
  175. std::pair<corpusIter, corpusIter> boyer_moore_search (
  176. corpusIter corpus_first, corpusIter corpus_last,
  177. patIter pat_first, patIter pat_last )
  178. {
  179. boyer_moore<patIter> bm ( pat_first, pat_last );
  180. return bm ( corpus_first, corpus_last );
  181. }
  182. template <typename PatternRange, typename corpusIter>
  183. std::pair<corpusIter, corpusIter> boyer_moore_search (
  184. corpusIter corpus_first, corpusIter corpus_last, const PatternRange &pattern )
  185. {
  186. typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator;
  187. boyer_moore<pattern_iterator> bm ( boost::begin(pattern), boost::end (pattern));
  188. return bm ( corpus_first, corpus_last );
  189. }
  190. template <typename patIter, typename CorpusRange>
  191. typename boost::disable_if_c<
  192. boost::is_same<CorpusRange, patIter>::value,
  193. std::pair<typename boost::range_iterator<CorpusRange>::type, typename boost::range_iterator<CorpusRange>::type> >
  194. ::type
  195. boyer_moore_search ( CorpusRange &corpus, patIter pat_first, patIter pat_last )
  196. {
  197. boyer_moore<patIter> bm ( pat_first, pat_last );
  198. return bm (boost::begin (corpus), boost::end (corpus));
  199. }
  200. template <typename PatternRange, typename CorpusRange>
  201. std::pair<typename boost::range_iterator<CorpusRange>::type, typename boost::range_iterator<CorpusRange>::type>
  202. boyer_moore_search ( CorpusRange &corpus, const PatternRange &pattern )
  203. {
  204. typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator;
  205. boyer_moore<pattern_iterator> bm ( boost::begin(pattern), boost::end (pattern));
  206. return bm (boost::begin (corpus), boost::end (corpus));
  207. }
  208. // Creator functions -- take a pattern range, return an object
  209. template <typename Range>
  210. boost::algorithm::boyer_moore<typename boost::range_iterator<const Range>::type>
  211. make_boyer_moore ( const Range &r ) {
  212. return boost::algorithm::boyer_moore
  213. <typename boost::range_iterator<const Range>::type> (boost::begin(r), boost::end(r));
  214. }
  215. template <typename Range>
  216. boost::algorithm::boyer_moore<typename boost::range_iterator<Range>::type>
  217. make_boyer_moore ( Range &r ) {
  218. return boost::algorithm::boyer_moore
  219. <typename boost::range_iterator<Range>::type> (boost::begin(r), boost::end(r));
  220. }
  221. }}
  222. #endif // BOOST_ALGORITHM_BOYER_MOORE_SEARCH_HPP