Files
C-Plus-Plus/d6/d42/data__structures_2sparse__table_8cpp.html
2022-09-13 16:11:32 +00:00

324 lines
22 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<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.9.5"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C++: data_structures/sparse_table.cpp File Reference</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">Algorithms_in_C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C++.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.5 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('d6/d42/data__structures_2sparse__table_8cpp.html','../../'); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="summary">
<a href="#nested-classes">Classes</a> &#124;
<a href="#namespaces">Namespaces</a> &#124;
<a href="#func-members">Functions</a> &#124;
<a href="#var-members">Variables</a> </div>
<div class="headertitle"><div class="title">sparse_table.cpp File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>Implementation of <a href="https://brilliant.org/wiki/sparse-table/" target="_blank">Sparse Table</a> for <code>min()</code> function.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;array&gt;</code><br />
<code>#include &lt;cassert&gt;</code><br />
<code>#include &lt;iostream&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for sparse_table.cpp:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../df/d88/data__structures_2sparse__table_8cpp__incl.svg" width="248" height="124"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="nested-classes" name="nested-classes"></a>
Classes</h2></td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">struct &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d37/structdata__structures_1_1sparse__table_1_1_sparse__table.html">data_structures::sparse_table::Sparse_table</a></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="namespaces" name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:d5/d3c/namespacedata__structures"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d3c/namespacedata__structures.html">data_structures</a></td></tr>
<tr class="memdesc:d5/d3c/namespacedata__structures"><td class="mdescLeft">&#160;</td><td class="mdescRight">for IO operations <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:d9/d55/namespacesparse__table"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d9/d55/namespacesparse__table.html">sparse_table</a></td></tr>
<tr class="memdesc:d9/d55/namespacesparse__table"><td class="mdescLeft">&#160;</td><td class="mdescRight">Functions for Implementation of <a href="https://brilliant.org/wiki/sparse-table/" target="_blank">Sparse Table</a> <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d6/d42/data__structures_2sparse__table_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
<tr class="memdesc:aa8dca7b867074164d5f45b0f3851269d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Self-test implementations. <a href="../../d6/d42/data__structures_2sparse__table_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">More...</a><br /></td></tr>
<tr class="separator:aa8dca7b867074164d5f45b0f3851269d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a0ddf1224851353fc92bfbff6f499fa97"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d6/d42/data__structures_2sparse__table_8cpp.html#a0ddf1224851353fc92bfbff6f499fa97">main</a> (int argc, char *argv[])</td></tr>
<tr class="memdesc:a0ddf1224851353fc92bfbff6f499fa97"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <a href="../../d6/d42/data__structures_2sparse__table_8cpp.html#a0ddf1224851353fc92bfbff6f499fa97">More...</a><br /></td></tr>
<tr class="separator:a0ddf1224851353fc92bfbff6f499fa97"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="var-members" name="var-members"></a>
Variables</h2></td></tr>
<tr class="memitem:a10f3ffb3f6f7e1b83d556b9c8de89a5d"><td class="memItemLeft" align="right" valign="top">constexpr uint32_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d6/d42/data__structures_2sparse__table_8cpp.html#a10f3ffb3f6f7e1b83d556b9c8de89a5d">data_structures::sparse_table::N</a> = 12345</td></tr>
<tr class="memdesc:a10f3ffb3f6f7e1b83d556b9c8de89a5d"><td class="mdescLeft">&#160;</td><td class="mdescRight">A struct to represent sparse table for <code>min()</code> as their invariant function, for the given array <code>A</code>. The answer to queries are stored in the array ST. <a href="../../d6/d42/data__structures_2sparse__table_8cpp.html#a10f3ffb3f6f7e1b83d556b9c8de89a5d">More...</a><br /></td></tr>
<tr class="separator:a10f3ffb3f6f7e1b83d556b9c8de89a5d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:af7db62f21983565c64d5d42d2a49888e"><td class="memItemLeft" align="right" valign="top"><a id="af7db62f21983565c64d5d42d2a49888e" name="af7db62f21983565c64d5d42d2a49888e"></a>
constexpr uint8_t&#160;</td><td class="memItemRight" valign="bottom"><b>data_structures::sparse_table::M</b> = 14</td></tr>
<tr class="memdesc:af7db62f21983565c64d5d42d2a49888e"><td class="mdescLeft">&#160;</td><td class="mdescRight">ceil(log2(N)). <br /></td></tr>
<tr class="separator:af7db62f21983565c64d5d42d2a49888e"><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 >Implementation of <a href="https://brilliant.org/wiki/sparse-table/" target="_blank">Sparse Table</a> for <code>min()</code> function. </p>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/manncodes" target="_blank">Mann Patel</a></dd></dl>
<p>Sparse Table is a data structure, that allows answering range queries. It can answer most range queries in O(logn), but its true power is answering range minimum queries (or equivalent range maximum queries). For those queries it can compute the answer in O(1) time. The only drawback of this data structure is, that it can only be used on immutable arrays. This means, that the array cannot be changed between two queries.</p>
<p >If any element in the array changes, the complete data structure has to be recomputed.</p>
<dl class="todo"><dt><b><a class="el" href="../../dd/da0/todo.html#_todo000004">Todo:</a></b></dt><dd>make stress tests.</dd></dl>
<dl class="section warning"><dt>Warning</dt><dd>This sparse table is made for <code>min(a1,a2,...an)</code> duplicate invariant function. This implementation can be changed to other functions like <code><a class="el" href="../../d4/da0/gcd__iterative__euclidean_8cpp.html#ae48807fa2b7000afae599e67f327545e">gcd()</a></code>, <code><a class="el" href="../../d4/d21/least__common__multiple_8cpp.html#aa8d0e241db517813725e721a152e3a25">lcm()</a></code>, and <code>max()</code> by changing a few lines of code. </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a0ddf1224851353fc92bfbff6f499fa97" name="a0ddf1224851353fc92bfbff6f499fa97"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0ddf1224851353fc92bfbff6f499fa97">&#9670;&#160;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>argc</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char *&#160;</td>
<td class="paramname"><em>argv</em>[]&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">argc</td><td>commandline argument count (ignored) </td></tr>
<tr><td class="paramname">argv</td><td>commandline array of arguments (ignored) </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 161</span> {</div>
<div class="line"><span class="lineno"> 162</span> <a class="code hl_function" href="../../d6/d42/data__structures_2sparse__table_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// run self-test implementations</span></div>
<div class="line"><span class="lineno"> 163</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 164</span>}</div>
<div class="ttc" id="adata__structures_2sparse__table_8cpp_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="../../d6/d42/data__structures_2sparse__table_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Self-test implementations.</div><div class="ttdef"><b>Definition:</b> sparse_table.cpp:128</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d6/d42/data__structures_2sparse__table_8cpp_a0ddf1224851353fc92bfbff6f499fa97_cgraph.svg" width="280" height="184"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="aa8dca7b867074164d5f45b0f3851269d" name="aa8dca7b867074164d5f45b0f3851269d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aa8dca7b867074164d5f45b0f3851269d">&#9670;&#160;</a></span>test()</h2>
<div class="memitem">
<div class="memproto">
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname">static void test </td>
<td>(</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel">static</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>Self-test implementations. </p>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<p >&lt; array on which RMQ will be performed.</p>
<p >&lt; size of self test's array</p>
<p >&lt; declaring sparse tree</p>
<p >&lt; copying array to the struct</p>
<p >&lt; passing the array's size to the struct</p>
<p >&lt; precomputing sparse tree</p>
<p >&lt; as 1 is smallest from 1..9</p>
<p >&lt; as 2 is smallest from 2..6</p>
<p >&lt; as 3 is smallest from 3..8</p>
<div class="fragment"><div class="line"><span class="lineno"> 128</span> {</div>
<div class="line"><span class="lineno"> 129</span> <span class="comment">/* We take an array as an input on which we need to perform the ranged</span></div>
<div class="line"><span class="lineno"> 130</span><span class="comment"> * minimum queries[RMQ](https://en.wikipedia.org/wiki/Range_minimum_query).</span></div>
<div class="line"><span class="lineno"> 131</span><span class="comment"> */</span></div>
<div class="line"><span class="lineno"> 132</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array&lt;int64_t, 10&gt;</a> testcase = {</div>
<div class="line"><span class="lineno"> 133</span> 1, 2, 3, 4, 5,</div>
<div class="line"><span class="lineno"> 134</span> 6, 7, 8, 9, 10}; <span class="comment">///&lt; array on which RMQ will be performed.</span></div>
<div class="line"><span class="lineno"> 135</span> <span class="keywordtype">size_t</span> testcase_size =</div>
<div class="line"><span class="lineno"> 136</span> <span class="keyword">sizeof</span>(testcase) / <span class="keyword">sizeof</span>(testcase[0]); <span class="comment">///&lt; size of self test&#39;s array</span></div>
<div class="line"><span class="lineno"> 137</span> </div>
<div class="line"><span class="lineno"> 138</span> <a class="code hl_struct" href="../../da/d37/structdata__structures_1_1sparse__table_1_1_sparse__table.html">data_structures::sparse_table::Sparse_table</a></div>
<div class="line"><span class="lineno"> 139</span> st{}; <span class="comment">///&lt; declaring sparse tree</span></div>
<div class="line"><span class="lineno"> 140</span> </div>
<div class="line"><span class="lineno"> 141</span> <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/algorithm/copy.html">std::copy</a>(<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/iterator/begin.html">std::begin</a>(testcase), <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/iterator/end.html">std::end</a>(testcase),</div>
<div class="line"><span class="lineno"> 142</span> <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/iterator/begin.html">std::begin</a>(st.A)); <span class="comment">///&lt; copying array to the struct</span></div>
<div class="line"><span class="lineno"> 143</span> st.n = testcase_size; <span class="comment">///&lt; passing the array&#39;s size to the struct</span></div>
<div class="line"><span class="lineno"> 144</span> </div>
<div class="line"><span class="lineno"> 145</span> st.buildST(); <span class="comment">///&lt; precomputing sparse tree</span></div>
<div class="line"><span class="lineno"> 146</span> </div>
<div class="line"><span class="lineno"> 147</span> <span class="comment">// pass queries of the form: [l,r]</span></div>
<div class="line"><span class="lineno"> 148</span> assert(st.query(1, 9) == 1); <span class="comment">///&lt; as 1 is smallest from 1..9</span></div>
<div class="line"><span class="lineno"> 149</span> assert(st.query(2, 6) == 2); <span class="comment">///&lt; as 2 is smallest from 2..6</span></div>
<div class="line"><span class="lineno"> 150</span> assert(st.query(3, 8) == 3); <span class="comment">///&lt; as 3 is smallest from 3..8</span></div>
<div class="line"><span class="lineno"> 151</span> </div>
<div class="line"><span class="lineno"> 152</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Self-test implementations passed!&quot;</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 153</span>}</div>
<div class="ttc" id="aarray_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a></div></div>
<div class="ttc" id="abasic_ostream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div></div>
<div class="ttc" id="abegin_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/iterator/begin.html">std::begin</a></div><div class="ttdeci">T begin(T... args)</div></div>
<div class="ttc" id="acopy_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/algorithm/copy.html">std::copy</a></div><div class="ttdeci">T copy(T... args)</div></div>
<div class="ttc" id="aend_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/iterator/end.html">std::end</a></div><div class="ttdeci">T end(T... args)</div></div>
<div class="ttc" id="aendl_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a></div><div class="ttdeci">T endl(T... args)</div></div>
<div class="ttc" id="astructdata__structures_1_1sparse__table_1_1_sparse__table_html"><div class="ttname"><a href="../../da/d37/structdata__structures_1_1sparse__table_1_1_sparse__table.html">data_structures::sparse_table::Sparse_table</a></div><div class="ttdef"><b>Definition:</b> sparse_table.cpp:50</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d6/d42/data__structures_2sparse__table_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg" width="182" height="184"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<h2 class="groupheader">Variable Documentation</h2>
<a id="a10f3ffb3f6f7e1b83d556b9c8de89a5d" name="a10f3ffb3f6f7e1b83d556b9c8de89a5d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a10f3ffb3f6f7e1b83d556b9c8de89a5d">&#9670;&#160;</a></span>N</h2>
<div class="memitem">
<div class="memproto">
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname">constexpr uint32_t data_structures::sparse_table::N = 12345</td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel">constexpr</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>A struct to represent sparse table for <code>min()</code> as their invariant function, for the given array <code>A</code>. The answer to queries are stored in the array ST. </p>
<p >the maximum size of the array. </p>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_2e746e9d06bf2d8ff842208bcc6ebcfc.html">data_structures</a></li><li class="navelem"><a class="el" href="../../d6/d42/data__structures_2sparse__table_8cpp.html">sparse_table.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.9.5 </li>
</ul>
</div>
</body>
</html>