2 Copyright (c) Marshall Clow 2010-2012.
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 For more information, see http://www.boost.org
10 #ifndef BOOST_ALGORITHM_BOYER_MOORE_HORSPOOOL_SEARCH_HPP
11 #define BOOST_ALGORITHM_BOYER_MOORE_HORSPOOOL_SEARCH_HPP
13 #include <iterator> // for std::iterator_traits
15 #include <boost/assert.hpp>
16 #include <boost/static_assert.hpp>
18 #include <boost/range/begin.hpp>
19 #include <boost/range/end.hpp>
21 #include <boost/utility/enable_if.hpp>
22 #include <boost/type_traits/is_same.hpp>
24 #include <boost/algorithm/searching/detail/bm_traits.hpp>
25 #include <boost/algorithm/searching/detail/debugging.hpp>
27 // #define BOOST_ALGORITHM_BOYER_MOORE_HORSPOOL_DEBUG_HPP
29 namespace boost { namespace algorithm {
32 A templated version of the boyer-moore-horspool searching algorithm.
35 * Random access iterators
36 * The two iterator types (patIter and corpusIter) must
37 "point to" the same underlying type.
38 * Additional requirements may be imposed buy the skip table, such as:
39 ** Numeric type (array-based skip table)
40 ** Hashable type (map-based skip table)
42 http://www-igm.univ-mlv.fr/%7Elecroq/string/node18.html
46 template <typename patIter, typename traits = detail::BM_traits<patIter> >
47 class boyer_moore_horspool {
48 typedef typename std::iterator_traits<patIter>::difference_type difference_type;
50 boyer_moore_horspool ( patIter first, patIter last )
51 : pat_first ( first ), pat_last ( last ),
52 k_pattern_length ( std::distance ( pat_first, pat_last )),
53 skip_ ( k_pattern_length, k_pattern_length ) {
55 // Build the skip table
57 if ( first != last ) // empty pattern?
58 for ( patIter iter = first; iter != last-1; ++iter, ++i )
59 skip_.insert ( *iter, k_pattern_length - 1 - i );
60 #ifdef BOOST_ALGORITHM_BOYER_MOORE_HORSPOOL_DEBUG_HPP
61 skip_.PrintSkipTable ();
65 ~boyer_moore_horspool () {}
67 /// \fn operator ( corpusIter corpus_first, corpusIter corpus_last)
68 /// \brief Searches the corpus for the pattern that was passed into the constructor
70 /// \param corpus_first The start of the data to search (Random Access Iterator)
71 /// \param corpus_last One past the end of the data to search
73 template <typename corpusIter>
74 std::pair<corpusIter, corpusIter>
75 operator () ( corpusIter corpus_first, corpusIter corpus_last ) const {
76 BOOST_STATIC_ASSERT (( boost::is_same<
77 typename std::iterator_traits<patIter>::value_type,
78 typename std::iterator_traits<corpusIter>::value_type>::value ));
80 if ( corpus_first == corpus_last ) return std::make_pair(corpus_last, corpus_last); // if nothing to search, we didn't find it!
81 if ( pat_first == pat_last ) return std::make_pair(corpus_first, corpus_first); // empty pattern matches at start
83 const difference_type k_corpus_length = std::distance ( corpus_first, corpus_last );
84 // If the pattern is larger than the corpus, we can't find it!
85 if ( k_corpus_length < k_pattern_length )
86 return std::make_pair(corpus_last, corpus_last);
89 return this->do_search ( corpus_first, corpus_last );
92 template <typename Range>
93 std::pair<typename boost::range_iterator<Range>::type, typename boost::range_iterator<Range>::type>
94 operator () ( Range &r ) const {
95 return (*this) (boost::begin(r), boost::end(r));
99 /// \cond DOXYGEN_HIDE
100 patIter pat_first, pat_last;
101 const difference_type k_pattern_length;
102 typename traits::skip_table_t skip_;
104 /// \fn do_search ( corpusIter corpus_first, corpusIter corpus_last )
105 /// \brief Searches the corpus for the pattern that was passed into the constructor
107 /// \param corpus_first The start of the data to search (Random Access Iterator)
108 /// \param corpus_last One past the end of the data to search
109 /// \param k_corpus_length The length of the corpus to search
111 template <typename corpusIter>
112 std::pair<corpusIter, corpusIter>
113 do_search ( corpusIter corpus_first, corpusIter corpus_last ) const {
114 corpusIter curPos = corpus_first;
115 const corpusIter lastPos = corpus_last - k_pattern_length;
116 while ( curPos <= lastPos ) {
117 // Do we match right where we are?
118 std::size_t j = k_pattern_length - 1;
119 while ( pat_first [j] == curPos [j] ) {
120 // We matched - we're done!
122 return std::make_pair(curPos, curPos + k_pattern_length);
126 curPos += skip_ [ curPos [ k_pattern_length - 1 ]];
129 return std::make_pair(corpus_last, corpus_last);
134 /* Two ranges as inputs gives us four possibilities; with 2,3,3,4 parameters
135 Use a bit of TMP to disambiguate the 3-argument templates */
137 /// \fn boyer_moore_horspool_search ( corpusIter corpus_first, corpusIter corpus_last,
138 /// patIter pat_first, patIter pat_last )
139 /// \brief Searches the corpus for the pattern.
141 /// \param corpus_first The start of the data to search (Random Access Iterator)
142 /// \param corpus_last One past the end of the data to search
143 /// \param pat_first The start of the pattern to search for (Random Access Iterator)
144 /// \param pat_last One past the end of the data to search for
146 template <typename patIter, typename corpusIter>
147 std::pair<corpusIter, corpusIter> boyer_moore_horspool_search (
148 corpusIter corpus_first, corpusIter corpus_last,
149 patIter pat_first, patIter pat_last )
151 boyer_moore_horspool<patIter> bmh ( pat_first, pat_last );
152 return bmh ( corpus_first, corpus_last );
155 template <typename PatternRange, typename corpusIter>
156 std::pair<corpusIter, corpusIter> boyer_moore_horspool_search (
157 corpusIter corpus_first, corpusIter corpus_last, const PatternRange &pattern )
159 typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator;
160 boyer_moore_horspool<pattern_iterator> bmh ( boost::begin(pattern), boost::end (pattern));
161 return bmh ( corpus_first, corpus_last );
164 template <typename patIter, typename CorpusRange>
165 typename boost::disable_if_c<
166 boost::is_same<CorpusRange, patIter>::value,
167 std::pair<typename boost::range_iterator<CorpusRange>::type, typename boost::range_iterator<CorpusRange>::type> >
169 boyer_moore_horspool_search ( CorpusRange &corpus, patIter pat_first, patIter pat_last )
171 boyer_moore_horspool<patIter> bmh ( pat_first, pat_last );
172 return bm (boost::begin (corpus), boost::end (corpus));
175 template <typename PatternRange, typename CorpusRange>
176 std::pair<typename boost::range_iterator<CorpusRange>::type, typename boost::range_iterator<CorpusRange>::type>
177 boyer_moore_horspool_search ( CorpusRange &corpus, const PatternRange &pattern )
179 typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator;
180 boyer_moore_horspool<pattern_iterator> bmh ( boost::begin(pattern), boost::end (pattern));
181 return bmh (boost::begin (corpus), boost::end (corpus));
185 // Creator functions -- take a pattern range, return an object
186 template <typename Range>
187 boost::algorithm::boyer_moore_horspool<typename boost::range_iterator<const Range>::type>
188 make_boyer_moore_horspool ( const Range &r ) {
189 return boost::algorithm::boyer_moore_horspool
190 <typename boost::range_iterator<const Range>::type> (boost::begin(r), boost::end(r));
193 template <typename Range>
194 boost::algorithm::boyer_moore_horspool<typename boost::range_iterator<Range>::type>
195 make_boyer_moore_horspool ( Range &r ) {
196 return boost::algorithm::boyer_moore_horspool
197 <typename boost::range_iterator<Range>::type> (boost::begin(r), boost::end(r));
202 #endif // BOOST_ALGORITHM_BOYER_MOORE_HORSPOOOL_SEARCH_HPP