<div dir="ltr"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">public</span><span style=""> </span><span style="color:rgb(187,44,162)">extension</span><span style=""> </span><span style="color:rgb(112,61,170)">CollectionType</span><span style=""> </span><span style="color:rgb(187,44,162)">where</span><span style=""> Index : RandomAccessIndexType {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""> </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)"> </span><span style="">@warn_unused_result</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">func</span><span style=""> matchWith<C : </span><span style="color:rgb(112,61,170)">CollectionType</span><span style=""> </span><span style="color:rgb(187,44,162)">where</span><span style=""> </span><span style="color:rgb(79,129,135)">C</span><span style="">.Index : </span><span style="color:rgb(112,61,170)">BidirectionalIndexType</span><span style="">, </span><span style="color:rgb(79,129,135)">C</span><span style="">.Generator.Element == Generator.Element>(pattern: </span><span style="color:rgb(79,129,135)">C</span><span style="">, </span><span style="color:rgb(187,44,162)">@noescape</span><span style=""> isEquivalent: (Generator.Element, Generator.Element) </span><span style="color:rgb(187,44,162)">throws</span><span style=""> -> </span><span style="color:rgb(112,61,170)">Bool</span><span style="">) </span><span style="color:rgb(187,44,162)">rethrows</span><span style=""> -> Index? {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""> </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">let</span><span style=""> pattern_count = pattern.</span><span style="color:rgb(112,61,170)">count</span><span style="">.</span><span style="color:rgb(61,29,129)">toIntMax</span><span style="">()</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">if</span><span style=""> </span><span style="color:rgb(112,61,170)">count</span><span style="">.</span><span style="color:rgb(61,29,129)">toIntMax</span><span style="">() < pattern_count {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">return</span><span style=""> </span><span style="color:rgb(187,44,162)">nil</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">let</span><span style=""> reverse_pattern = pattern.</span><span style="color:rgb(61,29,129)">reverse</span><span style="">()</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">var</span><span style=""> cursor = </span><span style="color:rgb(112,61,170)">startIndex</span><span style="">.</span><span style="color:rgb(61,29,129)">advancedBy</span><span style="">(</span><span style="color:rgb(61,29,129)">numericCast</span><span style="">(pattern_count - </span><span style="color:rgb(39,42,216)">1</span><span style="">))</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">while</span><span style=""> cursor </span><span style="color:rgb(61,29,129)"><</span><span style=""> </span><span style="color:rgb(112,61,170)">endIndex</span><span style=""> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">let</span><span style=""> left = </span><span style="color:rgb(112,61,170)">startIndex</span><span style="">...cursor</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">let</span><span style=""> pair = </span><span style="color:rgb(61,29,129)">zip</span><span style="">(left.</span><span style="color:rgb(61,29,129)">reverse</span><span style="">(), reverse_pattern)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">guard</span><span style=""> </span><span style="color:rgb(187,44,162)">let</span><span style=""> not_match = </span><span style="color:rgb(187,44,162)">try</span><span style=""> pair.</span><span style="color:rgb(49,89,93)">firstOf</span><span style="">({ </span><span style="color:rgb(187,44,162)">try</span><span style=""> !isEquivalent(</span><span style="color:rgb(187,44,162)">self</span><span style="">[$0], $1) }) </span><span style="color:rgb(187,44,162)">else</span><span style=""> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">return</span><span style=""> cursor.</span><span style="color:rgb(61,29,129)">advancedBy</span><span style="">(</span><span style="color:rgb(61,29,129)">numericCast</span><span style="">(</span><span style="color:rgb(39,42,216)">1</span><span style=""> - pattern_count))</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">if</span><span style=""> </span><span style="color:rgb(187,44,162)">let</span><span style=""> pos = </span><span style="color:rgb(187,44,162)">try</span><span style=""> reverse_pattern.</span><span style="color:rgb(61,29,129)">dropFirst</span><span style="">().</span><span style="color:rgb(61,29,129)">indexOf</span><span style="">({ </span><span style="color:rgb(187,44,162)">try</span><span style=""> isEquivalent(</span><span style="color:rgb(187,44,162)">self</span><span style="">[not_match.</span><span style="color:rgb(39,42,216)">0</span><span style="">], $0) }) {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">let</span><span style=""> offset = reverse_pattern.</span><span style="color:rgb(112,61,170)">startIndex</span><span style="">.</span><span style="color:rgb(61,29,129)">distanceTo</span><span style="">(pos).</span><span style="color:rgb(61,29,129)">toIntMax</span><span style="">()</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> cursor = not_match.</span><span style="color:rgb(39,42,216)">0</span><span style="">.</span><span style="color:rgb(61,29,129)">advancedBy</span><span style="">(</span><span style="color:rgb(61,29,129)">numericCast</span><span style="">(offset), limit: </span><span style="color:rgb(112,61,170)">endIndex</span><span style="">)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> } </span><span style="color:rgb(187,44,162)">else</span><span style=""> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> cursor = not_match.</span><span style="color:rgb(39,42,216)">0</span><span style="">.</span><span style="color:rgb(61,29,129)">advancedBy</span><span style="">(</span><span style="color:rgb(61,29,129)">numericCast</span><span style="">(pattern_count), limit: </span><span style="color:rgb(112,61,170)">endIndex</span><span style="">)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">if</span><span style=""> </span><span style="color:rgb(187,44,162)">try</span><span style=""> </span><span style="color:rgb(187,44,162)">self</span><span style="">.</span><span style="color:rgb(61,29,129)">reverse</span><span style="">().</span><span style="color:rgb(61,29,129)">startsWith</span><span style="">(reverse_pattern, isEquivalent: isEquivalent) {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">return</span><span style=""> </span><span style="color:rgb(112,61,170)">endIndex</span><span style="">.</span><span style="color:rgb(61,29,129)">advancedBy</span><span style="">(</span><span style="color:rgb(61,29,129)">numericCast</span><span style="">(-pattern_count))</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">return</span><span style=""> </span><span style="color:rgb(187,44,162)">nil</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">public</span><span style=""> </span><span style="color:rgb(187,44,162)">extension</span><span style=""> </span><span style="color:rgb(112,61,170)">CollectionType</span><span style=""> </span><span style="color:rgb(187,44,162)">where</span><span style=""> Index : RandomAccessIndexType, Generator.Element : Equatable {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""> </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)"> </span><span style="">@warn_unused_result</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">func</span><span style=""> matchWith<C : </span><span style="color:rgb(112,61,170)">CollectionType</span><span style=""> </span><span style="color:rgb(187,44,162)">where</span><span style=""> </span><span style="color:rgb(79,129,135)">C</span><span style="">.Index : </span><span style="color:rgb(112,61,170)">BidirectionalIndexType</span><span style="">, </span><span style="color:rgb(79,129,135)">C</span><span style="">.Generator.Element == Generator.Element>(pattern: </span><span style="color:rgb(79,129,135)">C</span><span style="">) -> Index? {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">return</span><span style=""> </span><span style="color:rgb(187,44,162)">self</span><span style="">.</span><span style="color:rgb(49,89,93)">matchWith</span><span style="">(pattern) { $0 </span><span style="color:rgb(61,29,129)">==</span><span style=""> $1 }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">}</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""><br></span></p><p style="margin:0px;line-height:normal"><font face="Menlo"><span style="font-size:11px">with this simplify version of Horspool algorithm, it can speed up searching in any random access CollectionType (better than brute force searching).</span></font></p></div>