Documentation for c26eea874d

This commit is contained in:
realstealthninja
2025-05-19 11:38:33 +00:00
parent c7a9a42397
commit 14583a2f33
2829 changed files with 30266 additions and 21669 deletions

View File

@@ -4,7 +4,7 @@
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.12.0"/>
<meta name="generator" content="Doxygen 1.13.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>TheAlgorithms/C++: strings/rabin_karp.cpp File Reference</title>
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
@@ -60,7 +60,7 @@ window.MathJax = {
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.12.0 -->
<!-- Generated by Doxygen 1.13.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
@@ -167,6 +167,8 @@ Functions</h2></td></tr>
<tr class="separator:a21c673d56cbf67b1d2ee4d869185b7d9"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a840291bc02cba5474a4cb46a9b9566fe" id="r_a840291bc02cba5474a4cb46a9b9566fe"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a840291bc02cba5474a4cb46a9b9566fe">main</a> (void)</td></tr>
<tr class="separator:a840291bc02cba5474a4cb46a9b9566fe"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a21c673d56cbf67b1d2ee4d869185b7d9" id="r_a21c673d56cbf67b1d2ee4d869185b7d9"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a21c673d56cbf67b1d2ee4d869185b7d9">rabin_karp</a> (const std::string &amp;str, const std::string &amp;pat)</td></tr>
<tr class="separator:a21c673d56cbf67b1d2ee4d869185b7d9"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>The <a href="https://en.wikipedia.org/wiki/RabinKarp_algorithm" target="_blank">Rabin-Karp Algorithm</a> for finding a pattern within a piece of text with complexity O(n + m) </p>
@@ -210,12 +212,65 @@ Functions</h2></td></tr>
<p class="definition">Definition at line <a class="el" href="../../d6/dce/rabin__karp_8cpp_source.html#l00105">105</a> of file <a class="el" href="../../d6/dce/rabin__karp_8cpp_source.html">rabin_karp.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 105</span> {</div>
<div class="line"><span class="lineno"> 106</span> assert(rabin_karp(<span class="stringliteral">&quot;helloWorld&quot;</span>, <span class="stringliteral">&quot;world&quot;</span>) == -1);</div>
<div class="line"><span class="lineno"> 107</span> assert(rabin_karp(<span class="stringliteral">&quot;helloWorld&quot;</span>, <span class="stringliteral">&quot;World&quot;</span>) == 5);</div>
<div class="line"><span class="lineno"> 108</span> assert(rabin_karp(<span class="stringliteral">&quot;this_is_c++&quot;</span>, <span class="stringliteral">&quot;c++&quot;</span>) == 8);</div>
<div class="line"><span class="lineno"> 109</span> assert(rabin_karp(<span class="stringliteral">&quot;happy_coding&quot;</span>, <span class="stringliteral">&quot;happy&quot;</span>) == 0);</div>
<div class="line"><span class="lineno"> 106</span> assert(<a class="code hl_function" href="#a21c673d56cbf67b1d2ee4d869185b7d9">rabin_karp</a>(<span class="stringliteral">&quot;helloWorld&quot;</span>, <span class="stringliteral">&quot;world&quot;</span>) == -1);</div>
<div class="line"><span class="lineno"> 107</span> assert(<a class="code hl_function" href="#a21c673d56cbf67b1d2ee4d869185b7d9">rabin_karp</a>(<span class="stringliteral">&quot;helloWorld&quot;</span>, <span class="stringliteral">&quot;World&quot;</span>) == 5);</div>
<div class="line"><span class="lineno"> 108</span> assert(<a class="code hl_function" href="#a21c673d56cbf67b1d2ee4d869185b7d9">rabin_karp</a>(<span class="stringliteral">&quot;this_is_c++&quot;</span>, <span class="stringliteral">&quot;c++&quot;</span>) == 8);</div>
<div class="line"><span class="lineno"> 109</span> assert(<a class="code hl_function" href="#a21c673d56cbf67b1d2ee4d869185b7d9">rabin_karp</a>(<span class="stringliteral">&quot;happy_coding&quot;</span>, <span class="stringliteral">&quot;happy&quot;</span>) == 0);</div>
<div class="line"><span class="lineno"> 110</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 111</span>}</div>
<div class="ttc" id="arabin__karp_8cpp_html_a21c673d56cbf67b1d2ee4d869185b7d9"><div class="ttname"><a href="#a21c673d56cbf67b1d2ee4d869185b7d9">rabin_karp</a></div><div class="ttdeci">int rabin_karp(const std::string &amp;str, const std::string &amp;pat)</div><div class="ttdef"><b>Definition</b> <a href="../../d6/dce/rabin__karp_8cpp_source.html#l00083">rabin_karp.cpp:83</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a21c673d56cbf67b1d2ee4d869185b7d9" name="a21c673d56cbf67b1d2ee4d869185b7d9"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a21c673d56cbf67b1d2ee4d869185b7d9">&#9670;&#160;</a></span>rabin_karp()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int string_search::rabin_karp </td>
<td>(</td>
<td class="paramtype">const std::string &amp;</td> <td class="paramname"><span class="paramname"><em>str</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const std::string &amp;</td> <td class="paramname"><span class="paramname"><em>pat</em></span>&#160;)</td>
</tr>
</table>
</div><div class="memdoc">
<p>Perform string pattern search using Rabin-Karp algorithm </p><dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">str</td><td>string to search in </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">pat</td><td>pattern to search for </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>index of first occurrence of pattern </dd>
<dd>
-1 if pattern not found </dd></dl>
<p class="definition">Definition at line <a class="el" href="../../d6/dce/rabin__karp_8cpp_source.html#l00083">83</a> of file <a class="el" href="../../d6/dce/rabin__karp_8cpp_source.html">rabin_karp.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 83</span> {</div>
<div class="line"><span class="lineno"> 84</span> int64_t pat_hash = <a class="code hl_function" href="../../d9/d03/namespacestring__search.html#a8fb0bc932ba8b582c9f4c71338d050f8">create_hash</a>(pat, pat.size());</div>
<div class="line"><span class="lineno"> 85</span> int64_t str_hash = <a class="code hl_function" href="../../d9/d03/namespacestring__search.html#a8fb0bc932ba8b582c9f4c71338d050f8">create_hash</a>(str, pat.size());</div>
<div class="line"><span class="lineno"> 86</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt;= str.size() - pat.size(); ++i) {</div>
<div class="line"><span class="lineno"> 87</span> <span class="keywordflow">if</span> (pat_hash == str_hash &amp;&amp;</div>
<div class="line"><span class="lineno"> 88</span> <a class="code hl_function" href="../../d9/d03/namespacestring__search.html#aebe07cea289a13142503d98be7df11fd">check_if_equal</a>(str, pat, i, i + pat.size() - 1, 0,</div>
<div class="line"><span class="lineno"> 89</span> pat.size() - 1)) {</div>
<div class="line"><span class="lineno"> 90</span> <span class="keywordflow">return</span> i;</div>
<div class="line"><span class="lineno"> 91</span> }</div>
<div class="line"><span class="lineno"> 92</span> <span class="keywordflow">if</span> (i &lt; str.size() - pat.size()) {</div>
<div class="line"><span class="lineno"> 93</span> str_hash =</div>
<div class="line"><span class="lineno"> 94</span> <a class="code hl_function" href="../../d9/d03/namespacestring__search.html#aed769d565b705a9b3e0eb1ec74088893">recalculate_hash</a>(str, i, i + pat.size(), str_hash, pat.size());</div>
<div class="line"><span class="lineno"> 95</span> }</div>
<div class="line"><span class="lineno"> 96</span> }</div>
<div class="line"><span class="lineno"> 97</span> <span class="keywordflow">return</span> -1; <span class="comment">// return -1 if given pattern not found</span></div>
<div class="line"><span class="lineno"> 98</span>}</div>
<div class="ttc" id="anamespacestring__search_html_a8fb0bc932ba8b582c9f4c71338d050f8"><div class="ttname"><a href="../../d9/d03/namespacestring__search.html#a8fb0bc932ba8b582c9f4c71338d050f8">string_search::create_hash</a></div><div class="ttdeci">int64_t create_hash(const std::string &amp;s, int n)</div><div class="ttdef"><b>Definition</b> <a href="../../d6/dce/rabin__karp_8cpp_source.html#l00025">rabin_karp.cpp:25</a></div></div>
<div class="ttc" id="anamespacestring__search_html_aebe07cea289a13142503d98be7df11fd"><div class="ttname"><a href="../../d9/d03/namespacestring__search.html#aebe07cea289a13142503d98be7df11fd">string_search::check_if_equal</a></div><div class="ttdeci">bool check_if_equal(const std::string &amp;str1, const std::string &amp;str2, int start1, int end1, int start2, int end2)</div><div class="ttdef"><b>Definition</b> <a href="../../d6/dce/rabin__karp_8cpp_source.html#l00060">rabin_karp.cpp:60</a></div></div>
<div class="ttc" id="anamespacestring__search_html_aed769d565b705a9b3e0eb1ec74088893"><div class="ttname"><a href="../../d9/d03/namespacestring__search.html#aed769d565b705a9b3e0eb1ec74088893">string_search::recalculate_hash</a></div><div class="ttdeci">int64_t recalculate_hash(const std::string &amp;s, int old_index, int new_index, int64_t old_hash, int patLength)</div><div class="ttdef"><b>Definition</b> <a href="../../d6/dce/rabin__karp_8cpp_source.html#l00042">rabin_karp.cpp:42</a></div></div>
</div><!-- fragment -->
</div>
</div>
@@ -225,7 +280,7 @@ Functions</h2></td></tr>
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_73a3cc5065b223eb41b02873c0e19f0e.html">strings</a></li><li class="navelem"><a class="el" href="../../d6/dce/rabin__karp_8cpp.html">rabin_karp.cpp</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.12.0 </li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.13.2 </li>
</ul>
</div>
</body>