<html>
<head></head>
<body>

<h1>Extending <code>std::search</code> to use Additional Searching Algorithms (Version 3)</h1>

<p>Document #: N3703</p>

<p>Marshall Clow   <a href="mailto:mclow.lists@gmail.com">mclow.lists@gmail.com</a></p>

<p>2013-06-28</p>

<p><em>Note:</em></p>

<ul>
<li><em>This is an update of <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3606.html">n3606</a>, presented in Portland. The major differences are support for comparison predicates in Boyer-Moore and Boyer-Moore-Horspool, and wording for the standard.</em></li>
<li><em><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3606.html">n3606</a> was an update of <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3411.pdf">n3411</a>, presented in Portland. The major difference is a different interface for the search functions.</em></li>
</ul>

<h3>Changes</h3>

<ul>
<li>N3411 - Presented to LWG at Portland</li>
<li>N3606 - Presented to LWG at Bristol; used new iterface for <code>std::search</code></li>
<li>N3703 - Added support for comparison predicates; added standard wording (thanks, Daniel!), and simplified the default interface.</li>
</ul>

<h2>Overview</h2>

<p><code>std::search</code> is a powerful tool for searching sequences, but there are lots of other search algorithms in the literature. For specialized tasks, some of them perform significantly better than <code>std::search</code>. In general, they do this by precomputing statistics about the pattern to be searched for, betting that this time can be made up during the search.</p>

<p>The basic principle is to break the search operation into two parts; the first part creates a "search object", which is specific to the pattern being searched for, and then the search object is passed, along with the data being searched, to <code>std::search</code>.</p>

<p>This is done by adding an additional overload to <code>std::search</code>, and some additional functions to create the search objects.</p>

<p>Two additional search algorithms are proposed for inclusion into the standard: "Boyer-Moore" and "Boyer-Moore-Horspool". Additionally, the interface for the search objects is documented so that library implementers and end users can create their own search objects and use them with <code>std::search</code>.</p>

<p><em>Terminology: I will refer to the sequence that is being searched as the "corpus" and the sequence being searched for as the "pattern".</em></p>

<h2>Search Objects</h2>

<p>A search object is an object that is passed a pattern to search for in its constructor. Then, the <code>operator ()</code> is called to search a sequence for matches.</p>

<h3>Example</h3>

<p>A search object that uses a very simple search algorithm might be implemented as:</p>

<pre><code>    template &lt;typename ForwardIterator&gt;
    class sample_searcher {
    public:
        sample_searcher ( ForwardIterator first, ForwardIterator last ) :
            first_ ( first ), last_ ( last ) {}

        template &lt;typename ForwardIterator2&gt;
        ForwardIterator2 operator () ( ForwardIterator2 cFirst, ForwardIterator2 cLast ) const {
            if ( first_ == last_ ) return cFirst;   // empty pattern
            if ( cFirst == cLast ) return cLast;    // empty corpus
            while ( cFirst != cLast ) {
            //  Advance to the first match
                while ( *cFirst != *first_ ) {
                    ++cFirst;
                    if ( cFirst == cLast )
                        return cLast;
                }
            //  At this point, we know that the first element matches
                Iterator it1 = first_;
                Iterator2 it2 = cFirst;
                while ( it1 != last_ &amp;&amp; it2 != cLast &amp;&amp; *++it1 == *++it2 )
                    ;
                if ( it1 == last_ ) // matched the whole pattern
                    return cFirst;
                ++cFirst;
                }
            return cLast;   // didn't find anything
            }

    private:
        Iterator first_;
        Iterator last_;
        };
</code></pre>

<h2>Algorithms</h2>

<h3>Default Search and Default Search with Predicate</h3>

<p>This is a convienience function that allows users of the new interface to use the existing functionality of <code>std::search</code>. </p>

<p>This searcher requires that both the corpus and the pattern be represented with forward iterators (or better).</p>

<h3>Boyer-Moore</h3>

<p>The Boyer-Moore string search algorithm is a particularly efficient string searching algorithm, and it has been the standard benchmark for the practical string search literature. The Boyer-Moore algorithm was invented by Bob Boyer and J. Strother Moore, and published in the October 1977 issue of the Communications of the ACM, and <a href="http://www.cs.utexas.edu/~moore/publications/fstrpos.pdf">a copy of that article is available</a>; another description is available on <a href="http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm">Wikipedia</a>.</p>

<p>The Boyer-Moore algorithm uses two precomputed tables to give better performance than a naive search. These tables depend on the pattern being searched for, and give the Boyer-Moore algorithm a larger memory footprint and startup costs than a simpler algorithm, but these costs are recovered quickly during the searching process, especially if the pattern is longer than a few elements. Both tables contain only non-negative integers.</p>

<p>In the following discussion, I will refer to <code>pattern_length</code>, the length of the pattern being searched for; in other words, <code>std::distance ( pattern_first, pattern_last )</code>.</p>

<p>The first table contains one entry for each element in the "alphabet" being searched (i.e, the corpus). For searching (narrow) character sequences, a 256-element array can be used for quick lookup. For searching other types of data, a hash table can be used to save space. The contents of the first table are: For each element in the "alphabet" being processed (i.e, the set of all values contained in the corpus) If the element does not appear in the pattern, then <code>pattern_length</code>, otherwise <code>pattern_length - j</code>, where <code>j</code> is the maximum value for which <code>*(pattern_first + j) == element</code>.</p>

<p><em>Note: Even though the table may contain one entry for each element that occurs in the corpus, the contents of the tables only depend on the pattern.</em></p>

<p>The second table contains one entry for each element in the pattern; for example, a <code>std::vector&lt;size_t&gt;(pattern_length)</code>. Each entry in the table is basically the amount that the matching window can be moved when a mismatch is found.
The Boyer-Moore algorithm works by at each position, comparing an element in the pattern to one in the corpus. If it matches, it advances to the next element in both the pattern and the corpus. If the end of the pattern is reached, then a match has been found, and can be returned. If the elements being compared do not match, then the precomputed tables are consulted to determine where to position the pattern in the corpus, and what position in the pattern to resume the matching.</p>

<p>The Boyer-Moore algorithm requires that both the corpus and the pattern be represented with random-access iterators, and that both iterator types "point to" the same type.</p>

<h3>Boyer-Moore-Horspool</h3>

<p>The Boyer-Moore-Horspool search algorithm was published by Nigel Horspool in 1980. It is a refinement of the Boyer-Moore algorithm that trades space for time. It uses less space for internal tables than Boyer-Moore, and has poorer worst-case performance.</p>

<p>Like the Boyer-Moore algorithm, it has a table that (logically) contains one entry for each element in the pattern "alphabet". When a mismatch is found in the comparison between the pattern and the corpus, this table and the mismatched character in the corpus are used to decide how far to advance the pattern to start the new comparison.</p>

<p>A reasonable description (with sample code) is available on <a href="http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm">Wikipedia</a>.</p>

<p>The Boyer-Moore algorithm requires that both the corpus and the pattern be represented with random-access iterators, and that both iterator types "point to" the same type.</p>

<h3>Calls to be added to the standard library</h3>

<pre><code>    template &lt;typename ForwardIterator, typename Searcher&gt;
    ForwardIterator search ( ForwardIterator first, ForwardIterator last, const Searcher &amp;searcher );


    template &lt;typename ForwardIterator,
      typename BinaryPredicate = typename std::equal_to&lt;typename std::iterator_traits&lt;ForwardIterator&gt;::value_type&gt;&gt;
    default_searcher&lt;ForwardIterator, BinaryPredicate&gt; make_searcher ( ForwardIterator first, ForwardIterator last, BinaryPredicate pred = BinaryPredicate ());

    template &lt;typename RandomAccessIterator, 
      typename Hash =            typename std::hash    &lt;typename std::iterator_traits&lt;RandomAccessIterator&gt;::value_type&gt;,
      typename BinaryPredicate = typename std::equal_to&lt;typename std::iterator_traits&lt;RandomAccessIterator&gt;::value_type&gt;&gt;
    boyer_moore_searcher&lt;RandomAccessIterator, Hash, BinaryPredicate&gt; make_boyer_moore_searcher ( 
            RandomAccessIterator first, RandomAccessIterator last, Hash hash = Hash (), BinaryPredicate pred = BinaryPredicate ());

    template &lt;typename RandomAccessIterator, 
      typename Hash =            typename std::hash    &lt;typename std::iterator_traits&lt;RandomAccessIterator&gt;::value_type&gt;,
      typename BinaryPredicate = typename std::equal_to&lt;typename std::iterator_traits&lt;RandomAccessIterator&gt;::value_type&gt;&gt;
    boyer_moore_horspool_searcher&lt;RandomAccessIterator, Hash, BinaryPredicate&gt; make_boyer_moore_horspool_searcher ( 
            RandomAccessIterator first, RandomAccessIterator last, Hash hash = Hash (), BinaryPredicate pred = BinaryPredicate ());
</code></pre>

<h3>Example usage</h3>

<pre><code>    // existing code
    iter = search ( corpus_first, corpus_last, pattern_first, pattern_last );

    // same code, new interface
    iter = search ( corpus_first, corpus_last, make_searcher ( pattern_first, pattern_last ));

    // same code, different algorithm
    iter = search ( corpus_first, corpus_last, make_boyer_moore_searcher ( pattern_first, pattern_last ));
</code></pre>

<h3>Restrictions on comparison predicates</h3>

<p>Boyer-Moore and Boyer-Moore-Horspool require a hash function as well as a comparison function. The two functions must compare in the same way. In particular for any two values <code>A</code> and <code>B</code>, if <code>pred(A,B)</code> then <code>hash(A) == hash(B)</code>.</p>

<h3>Performance</h3>

<p>Using the new interface with the existing search algorithms should fulfill all the performance guarantees for the current interface of <code>std::search</code>. No additional comparisons are necessary. However, the creation of the search object may add some additional overhead. Different algorithms will have different amounts of overhead to create the search object. The <code>default_search</code> objects, for example, should be cheap to create - they will typically be a pair of iterators. The Boyer-Moore search object, on the other hand, will contain a pair of tables, and require a significant amount of computation to create.</p>

<p>In <a href="https://github.com/mclow/search-library">my tests</a>, on medium sized search patterns (about 100 entries), the Boyer-Moore and Boyer-Moore-Horspool were about 8-10x faster than <code>std::search</code>. For longer patterns, the advantage increases. For short patterns, they may actually be slower. </p>

<h4>Test timings</h4>

<p>These results were run on a MacBookPro (i5) computer, compiled with <code>clang 3.2 -O3</code>, and compared against libc++.  The corpus was about 2.8MB of base64-encoded text. All timings are normalized against <code>std::search</code>.</p>

<p>The titles of the test indicate where the pattern is located in the corpus being searched; "At Start", etc. "Not found" is the case where the pattern does not exist in the corpus, i.e, the search will fail.</p>

<table border=1>
    <tr><th>Algorithm</th><th>At Start</th><th>Middle</th><th>At End</th><th>Not Found</th></tr>
<tr><td><I>Pattern Length</I></td><td>119</td><td>105</td><td>43</td><td>91</td></tr>
    <tr><td>std::search</td><td>100.0</td><td>100.0</td><td>100.0</td><td>100.0</td></tr>
    <tr><td>default search</td><td>107.1</td><td>92.79</td><td>104.6</td><td>107</td></tr>
    <tr><td>Boyer-Moore</td><td>110.7</td><td>14.34</td><td>23.14</td><td>12.86</td></tr>
    <tr><td>Boyer-Moore Horspool</td><td>82.14</td><td>11.8</td><td>20.04</td><td>10.41</td></tr>
</table>

<h3>Sample Implementation</h3>

<p>An implementation of this proposal, available under the <a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License</a> can be found on <a href="https://github.com/mclow/search-library">GitHub</a>.</p>

<h3>Placement into the standard</h3>

<p>We (myself and Daniel) believe that the searcher functions should go into <code>&lt;functional&gt;</code>, and the specialization of <code>search</code> should go into <code>&lt;algorithm&gt;</code>. </p>

<p>It would also be possible to put the searchers into <code>&lt;utility&gt;</code>. We think that since there are not function objects in <code>&lt;algorithm&gt;</code>, only algorithms, the searchers do not belong there.</p>

<p>We are not 100% sure that the new search overload is a real algorithm, and such, does it belong in <code>&lt;algorithm&gt;</code>. </p>

<h3>Wording</h3>

<p>The proposed wording changes refer to <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3485.pdf">N3485</a>.</p>

<p/>
<em>Editorial note: The specification of the class templates <tt>boyer_moore_searcher</tt> and <tt>boyer_moore_horspool_searcher</tt> 
are exactly equal except for their names. The editor may merge them into one sub-clause at his discretion.</em>
</p>

<ol>

<li><p>Edit 20.10 [function.objects] p2, header <tt>&lt;functional&gt;</tt> synopsis as indicated:</p>
<blockquote><pre>
namespace std {

  [&hellip;]  

  <ins>// [searchers], searchers:</ins>
  <ins>template&lt;class ForwardIterator, class BinaryPredicate = equal_to&lt;&gt;&gt;</ins>
    <ins>class default_searcher;</ins>
  <ins>template&lt;class RandomAccessIterator, class Hash = typename std::hash&lttypename std::iterator_traits&ltRandomAccessIterator&gt::value_type&gt, class BinaryPredicate = equal_to&lt;&gt;&gt;</ins>
    <ins>boyer_moore_searcher&lt;RandomAccessIterator, Hash, BinaryPredicate&gt;</ins>
  <ins>template&lt;class RandomAccessIterator, class Hash = typename std::hash&lttypename std::iterator_traits&ltRandomAccessIterator&gt::value_type&gt, class BinaryPredicate = equal_to&lt;&gt;&gt;</ins>
    <ins>boyer_moore_horspool_searcher&lt;RandomAccessIterator, Hash, BinaryPredicate&gt;</ins>

  <ins>template&lt;class ForwardIterator, class BinaryPredicate = equal_to&lt;&gt;&gt;</ins>
  <ins>default_searcher&lt;ForwardIterator, BinaryPredicate&gt;</ins>
  <ins>make_searcher(ForwardIterator pat_first, ForwardIterator pat_last, BinaryPredicate pred = BinaryPredicate());</ins>

  <ins>template&lt;class RandomAccessIterator, class Hash = typename std::hash&lttypename std::iterator_traits&ltRandomAccessIterator&gt::value_type&gt, class BinaryPredicate = equal_to&lt;&gt;&gt;</ins>
  <ins>boyer_moore_searcher&lt;RandomAccessIterator, Hash, BinaryPredicate&gt;</ins>
  <ins>make_boyer_moore_searcher(RandomAccessIterator pat_first, RandomAccessIterator pat_last,</ins> 
                        <ins>Hash hash = Hash(), BinaryPredicate pred = BinaryPredicate());</ins>

  <ins>template&lt;class RandomAccessIterator, class Hash = typename std::hash&lttypename std::iterator_traits&ltRandomAccessIterator&gt::value_type&gt, class BinaryPredicate = equal_to&lt;&gt;&gt;</ins>
  <ins>boyer_moore_horspool_searcher&lt;RandomAccessIterator, BinaryPredicate&gt;</ins>
  <ins>make_boyer_moore_horspool_searcher(RandomAccessIterator pat_first, RandomAccessIterator pat_last,</ins> 
                        <ins>Hash hash = Hash(), BinaryPredicate pred = BinaryPredicate());</ins>

  <em>// 20.10.9, bind:</em>
}
</pre></blockquote>
</li>

<li><p>Add a new sub-clause at the end of 20.10 [function.objects] and a series of paragraphs as indicated:</p>

<p>?? Searchers [searchers]</p>

<p>This sub-clause provides function object types ([function.objects]) for operations that search a pattern range 
<tt>[pat_first, pat_last)</tt> in another sequence denoted by a range <tt>[first, last)</tt> that is provided to its 
function call operator. Each type instantiated from a class template specified in this sub-clause [searchers] meets the 
<tt>CopyConstructible</tt> and <tt>CopyAssignable</tt> requirements. Template parameters named <tt>ForwardIterator</tt>, 
<tt>ForwardIterator2</tt>, <tt>RandomAccessIterator</tt>, <tt>RandomAccessIterator2</tt>, and <tt>BinaryPredicate</tt> 
of templates specified in this sub-clause [searchers] shall meet the same requirements and semantics as specified in 
[algorithms.general].
</p>

<p>??.1 Class template <tt>default_searcher</tt> [searcher.default]</p>

<blockquote><pre>
namespace std {
  template&lt;class ForwardIterator, class BinaryPredicate = equal_to&lt;&gt;&gt;
  class default_searcher {
  public:
    default_searcher(ForwardIterator pat_first, ForwardIterator pat_last, 
                     BinaryPredicate pred = BinaryPredicate());

    template&lt;class ForwardIterator2&gt;
    ForwardIterator2 
    operator()(ForwardIterator2 first, ForwardIterator2 last) const;

  private:
    BinaryPredicate pred_; <i>// exposition only</i>
    ForwardIterator pat_first_; <i>// exposition only</i>
    ForwardIterator pat_last_; <i>// exposition only</i>
  };

  template&lt;class ForwardIterator, class BinaryPredicate = equal_to&lt;&gt;&gt;
  default_searcher&lt;ForwardIterator, BinaryPredicate&gt; 
  make_searcher(ForwardIterator pat_first, ForwardIterator pat_last, 
                BinaryPredicate pred = BinaryPredicate());
}
</pre></blockquote>

<blockquote>
<pre>
default_searcher(ForwardIterator pat_first, ForwardIterator pat_last, 
                 BinaryPredicate pred = BinaryPredicate());
</pre><blockquote>
<p>
-?- <em>Effects:</em> Constructs a <tt>default_searcher</tt> object, initializing <tt>pat_first_</tt> with <tt>pat_first</tt>,
<tt>pat_last_</tt> with <tt>pat_last</tt>, and <tt>pred_</tt> with <tt>pred</tt>.
</p>
</blockquote>

<pre>
template&lt;class ForwardIterator2&gt;
ForwardIterator2 
operator()(ForwardIterator2 first, ForwardIterator2 last) const;
</pre>
<blockquote>
<p>
-?- <em>Effects:</em> Equivalent to <tt>return std::search(first, last, pat_first_, pat_last_, pred_);</tt>.
</p>
</blockquote>
</blockquote>

<p>??.1.1 <tt>default_searcher</tt> creation functions [searcher.default.creation]</p>

<blockquote>
<pre>
template&lt;class ForwardIterator, class BinaryPredicate = equal_to&lt;&gt;&gt;
default_searcher&lt;ForwardIterator, BinaryPredicate&gt; 
make_searcher(ForwardIterator pat_first, ForwardIterator pat_last, 
              BinaryPredicate pred = BinaryPredicate());
</pre>
<blockquote>
<p>
-?- <em>Returns:</em> <tt>default_searcher&lt;ForwardIterator, BinaryPredicate&gt;(pat_first, pat_last, pred);</tt>.
</p>
</blockquote>
</blockquote>

<p>??.1 Class template <tt>boyer_moore_searcher</tt> [searcher.boyer_moore_searcher]</p>

<blockquote><pre>
namespace std {
  template&lt;class RandomAccessIterator, class Hash = typename std::hash&lttypename std::iterator_traits&ltRandomAccessIterator&gt::value_type&gt, class BinaryPredicate = equal_to&lt;&gt;&gt;
  class boyer_moore_searcher {
  public:
    boyer_moore_searcher(RandomAccessIterator pat_first, RandomAccessIterator pat_last, 
                         Hash hash = Hash(), BinaryPredicate pred = BinaryPredicate());

    template&lt;class RandomAccessIterator2&gt;
    RandomAccessIterator2 
    operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const;

  private:
    BinaryPredicate pred_; <i>// exposition only</i>
    Hash hash_; <i>// exposition only</i>
    RandomAccessIterator pat_first_; <i>// exposition only</i>
    RandomAccessIterator pat_last_; <i>// exposition only</i>
  };

  template&lt;class RandomAccessIterator, class Hash = typename std::hash&lttypename std::iterator_traits&ltRandomAccessIterator&gt::value_type&gt, class BinaryPredicate = equal_to&lt;&gt;&gt;
  boyer_moore_searcher&lt;RandomAccessIterator, Hash, BinaryPredicate&gt; 
  make_boyer_moore_searcher(RandomAccessIterator pat_first, RandomAccessIterator pat_last, 
                Hash hash = Hash(), BinaryPredicate pred = BinaryPredicate());
}
</pre></blockquote>

<blockquote>
<pre>
boyer_moore_searcher(RandomAccessIterator pat_first, RandomAccessIterator pat_last, 
                     Hash hash = Hash(), BinaryPredicate pred = BinaryPredicate());
</pre><blockquote>
<p>
-?- <em>Requires:</em> The value type of <tt>RandomAccessIterator</tt> shall meet the <tt>CopyConstructible</tt>,
<tt>DefaultConstructible</tt>, and <tt>CopyAssignable</tt> requirements.
<p/>
-?- <em>Requires:</em> The comparison function and the hash function shall give equivalent results; i.e, for any two values <tt>A</tt> and <tt>B</tt> of the type <tt>std::iterator_traits&ltRandomAccessIterator&gt::value_type</tt>,<tt> hash(A) == hash(B)</tt> shall be true iff <tt>pred(A,B) == true</tt>.
</p>
-?- <em>Effects:</em> Constructs a <tt>boyer_moore_searcher</tt> object, initializing <tt>pat_first_</tt> with <tt>pat_first</tt>,
<tt>pat_last_</tt> with <tt>pat_last</tt>, <tt>hash_</tt> with <tt>hash</tt>, and <tt>pred_</tt> with <tt>pred</tt>.
<p/>
-?- <em>Throws:</em> Any exception thrown by the copy constructor of <tt>BinaryPredicate</tt> or <tt>RandomAccessIterator</tt>, by
the copy constructor, default constructor, or the copy assignment operator of the value type of <tt>RandomAccessIterator</tt>. 
May throw <tt>bad_alloc</tt> if the system cannot allocate additional memory that may be required for internal data
structures needed.
</p>
</blockquote>

<pre>
template&lt;class RandomAccessIterator2&gt;
RandomAccessIterator2 
operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const;
</pre>
<blockquote>
<p>
-?- <em>Requires:</em> <tt>RandomAccessIterator</tt> and <tt>RandomAccessIterator2</tt> shall have the same value type. 
<p/>
-?- <em>Effects:</em> Finds a subsequence of equal values in a sequence.
<p/>
-?- <em>Returns:</em> The first iterator <tt>i</tt> in the range <tt>[first, last - (pat_last_ - pat_first_))</tt> such that for 
any nonnegative integer <tt>n</tt> less than <tt>pat_last_ - pat_first_</tt> the following corresponding conditions hold: 
<tt>pred(*(i + n), *(pat_first_ + n)) != false</tt>. Returns <tt>first</tt> if <tt>[pat_first_, pat_last_)</tt>
is empty, otherwise returns <tt>last</tt> if no such iterator is found.
<p/>
-?- <em>Complexity:</em> At most <tt>distance(first, last) * distance(pat_first_, pat_last_)</tt> applications of the 
corresponding predicate.
</p>
</blockquote>
</blockquote>

<p>??.1.1 <tt>boyer_moore_searcher</tt> creation functions [searcher.boyer_moore.creation]</p>

<blockquote>
<pre>
template&lt;class RandomAccessIterator, class Hash = typename std::hash&lttypename std::iterator_traits&ltRandomAccessIterator&gt::value_type&gt, class BinaryPredicate = equal_to&lt;&gt;&gt;
boyer_moore_searcher&lt;RandomAccessIterator, BinaryPredicate&gt; 
make_boyer_moore_searcher(RandomAccessIterator pat_first, RandomAccessIterator pat_last, 
                          Hash hash = Hash(), BinaryPredicate pred = BinaryPredicate());
</pre>
<blockquote>
<p>
-?- <em>Returns:</em> <tt>boyer_moore_searcher&lt;RandomAccessIterator, Hash, BinaryPredicate&gt;(pat_first, pat_last, hash, pred);</tt>.
</p>
</blockquote>
</blockquote>

<p>??.1 Class template <tt>boyer_moore_horspool_searcher</tt> [searcher.boyer_moore_horspool]</p>

<blockquote><pre>
namespace std {
  template&lt;class RandomAccessIterator, class Hash = typename std::hash&lttypename std::iterator_traits&ltRandomAccessIterator&gt::value_type&gt, class BinaryPredicate = equal_to&lt;&gt;&gt;
  class boyer_moore_horspool_searcher {
  public:
    boyer_moore_horspool_searcher(RandomAccessIterator pat_first, RandomAccessIterator pat_last, 
                                  BinaryPredicate pred = BinaryPredicate());

    template&lt;class RandomAccessIterator2&gt;
    RandomAccessIterator2 
    operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const;

  private:
    BinaryPredicate pred_; <i>// exposition only</i>
    Hash hash_; <i>// exposition only</i>
    RandomAccessIterator pat_first_; <i>// exposition only</i>
    RandomAccessIterator pat_last_; <i>// exposition only</i>
  };

  template&lt;class RandomAccessIterator, class Hash = typename std::hash&lttypename std::iterator_traits&ltRandomAccessIterator&gt::value_type&gt, class BinaryPredicate = equal_to&lt;&gt;&gt;
  boyer_moore_horspool_searcher&lt;RandomAccessIterator, Hash, BinaryPredicate&gt; 
  make_boyer_moore_horspool_searcher(RandomAccessIterator pat_first, RandomAccessIterator pat_last, 
                                     Hash hash = Hash(), BinaryPredicate pred = BinaryPredicate());
}
</pre></blockquote>

<blockquote>
<pre>
boyer_moore_horspool_searcher(RandomAccessIterator pat_first, RandomAccessIterator pat_last, 
                              Hash hash = Hash(), BinaryPredicate pred = BinaryPredicate());
</pre><blockquote>
<p>
-?- <em>Requires:</em> The value type of <tt>RandomAccessIterator</tt> shall meet the <tt>CopyConstructible</tt>,
<tt>DefaultConstructible</tt>, and <tt>CopyAssignable</tt> requirements.
<p/>
-?- <em>Requires:</em> The comparison function and the hash function shall give equivalent results; i.e, for any two values <tt>A</tt> and <tt>B</tt> of the type <tt>std::iterator_traits&ltRandomAccessIterator&gt::value_type</tt>,<tt> hash(A) == hash(B)</tt> shall be true iff <tt>pred(A,B) == true</tt>.
</p>
-?- <em>Effects:</em> Constructs a <tt>boyer_moore_horspool_searcher</tt> object, initializing <tt>pat_first_</tt> with <tt>pat_first</tt>,
<tt>pat_last_</tt> with <tt>pat_last</tt>, <tt>hash_</tt> with <tt>hash</tt>, and <tt>pred_</tt> with <tt>pred</tt>.
<p/>
-?- <em>Throws:</em> Any exception thrown by the copy constructor of <tt>BinaryPredicate</tt> or <tt>RandomAccessIterator</tt>, by
the copy constructor, default constructor, or the copy assignment operator of the value type of <tt>RandomAccessIterator</tt>. 
May throw <tt>bad_alloc</tt> if the system cannot allocate additional memory that may be required for internal data
structures needed.
</p>
</blockquote>

<pre>
template&lt;class RandomAccessIterator2&gt;
RandomAccessIterator2 
operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const;
</pre>
<blockquote>
<p>
-?- <em>Requires:</em> <tt>RandomAccessIterator</tt> and <tt>RandomAccessIterator2</tt> shall have the same value type. 
<p/>
-?- <em>Effects:</em> Finds a subsequence of equal values in a sequence.
<p/>
-?- <em>Returns:</em> The first iterator <tt>i</tt> in the range <tt>[first, last - (pat_last_ - pat_first_))</tt> such that for 
any nonnegative integer <tt>n</tt> less than <tt>pat_last_ - pat_first_</tt> the following corresponding conditions hold: 
<tt>pred(*(i + n), *(pat_first_ + n)) != false</tt>. Returns <tt>first</tt> if <tt>[pat_first_, pat_last_)</tt>
is empty, otherwise returns <tt>last</tt> if no such iterator is found.
<p/>
-?- <em>Complexity:</em> At most <tt>(last - first) * (pat_last_ - pat_first_)</tt> applications of the corresponding
predicate.
</p>
</blockquote>
</blockquote>

<p>??.1.1 <tt>boyer_moore_horspool_searcher</tt> creation functions [searcher.boyer_moore_horspool.creation]</p>

<blockquote>
<pre>
template&lt;class RandomAccessIterator, class Hash = typename std::hash&lttypename std::iterator_traits&ltRandomAccessIterator&gt::value_type&gt, class BinaryPredicate = equal_to&lt;&gt;&gt;
boyer_moore_searcher_horspool&lt;RandomAccessIterator, Hash, BinaryPredicate&gt; 
make_boyer_moore_horspool_searcher(RandomAccessIterator pat_first, RandomAccessIterator pat_last, 
                                   Hash hash = Hash(), BinaryPredicate pred = BinaryPredicate());
</pre>
<blockquote>
<p>
-?- <em>Returns:</em> <tt>boyer_moore_horspool_searcher&lt;RandomAccessIterator, BinaryPredicate&gt;(pat_first, pat_last, hash, pred);</tt>.
</p>
</blockquote>
</blockquote>

</li>

<li><p>Edit 25.1 [algorithms.general] p2, header <tt>&lt;algorithm&gt;</tt> synopsis as indicated:</p>
<blockquote><pre>
namespace std {

  [&hellip;]
  template&lt;class ForwardIterator1, class ForwardIterator2&gt;
  ForwardIterator1 search(
    ForwardIterator1 first1, ForwardIterator1 last1,
    ForwardIterator2 first2, ForwardIterator2 last2);
  template&lt;class ForwardIterator1, class ForwardIterator2,
           class BinaryPredicate&gt;
  ForwardIterator1 search(
    ForwardIterator1 first1, ForwardIterator1 last1,
    ForwardIterator2 first2, ForwardIterator2 last2,
    BinaryPredicate pred);
  <ins>template&lt;class ForwardIterator, class Searcher&gt;</ins>
  <ins>ForwardIterator search(ForwardIterator first, ForwardIterator last,</ins> 
    <ins>const Searcher&amp; searcher);</ins>
  [&hellip;]
}
</pre></blockquote>
</li>

<li><p>Add the following sequence of declarations and paragraphs after 25.2.13 [alg.search] p. 3:</p>

<blockquote>
<pre>
template&lt;class ForwardIterator, class Searcher&gt;
ForwardIterator search(ForwardIterator first, ForwardIterator last, 
                       const Searcher&amp; searcher);
</pre>
<blockquote>
<p>
-?- <em>Effects:</em> Equivalent to <tt>returns searcher(first, last);</tt>.
<p/>
-?- <em>Remarks:</em> <tt>Searcher</tt> is not required to satisfy the <tt>CopyConstructible</tt> requirements.
</p>
</blockquote>
</blockquote>

</li>
</ol>

<h3>Acknowledgments</h3>

<p>Thanks to LWG, which reviewed an earlier version of this document, Matt Austern, who suggested specializing <code>std::search</code>, and especially Daniel Krügler, who wrote most of the wording for the standard.</p>

</body>
</html>