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