TheAlgorithms-C/da/da0/segment__tree_8c.html
2023-01-20 04:47:59 +00:00

671 lines
52 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/binary_trees/segment_tree.c 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('da/da0/segment__tree_8c.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">Data Structures</a> &#124;
<a href="#typedef-members">Typedefs</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">segment_tree.c File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>segment trees with only point updates
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;assert.h&gt;</code><br />
<code>#include &lt;inttypes.h&gt;</code><br />
<code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;stdlib.h&gt;</code><br />
<code>#include &lt;string.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for segment_tree.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d6/ddc/segment__tree_8c__incl.svg" width="448" 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>
Data Structures</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="../../dd/d06/structsegment__tree.html">segment_tree</a></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight">This structures holds all the data that is required by a segment tree. <a href="../../dd/d06/structsegment__tree.html#details">More...</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="typedef-members" name="typedef-members"></a>
Typedefs</h2></td></tr>
<tr class="memitem:aca549b5311d32ab1a703b4a4605821d8"><td class="memItemLeft" align="right" valign="top">typedef void(*&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/da0/segment__tree_8c.html#aca549b5311d32ab1a703b4a4605821d8">combine_function</a>) (const void *a, const void *b, void *result)</td></tr>
<tr class="memdesc:aca549b5311d32ab1a703b4a4605821d8"><td class="mdescLeft">&#160;</td><td class="mdescRight">Function that combines two data to generate a new one The name of function might be misleading actually combine here signifies the fact that in segment trees we take partial result from two ranges and using partial results we derive the result for joint range of those two ranges For Example: array(1,2,3,4,5,6) sum of range [0,2] = 6 and sum of range [3,5] = 15 the combined sum of two range is 6+15=21. <a href="../../da/da0/segment__tree_8c.html#aca549b5311d32ab1a703b4a4605821d8">More...</a><br /></td></tr>
<tr class="separator:aca549b5311d32ab1a703b4a4605821d8"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ac206721972f739510cb11f7c0a6a8f63"><td class="memItemLeft" align="right" valign="top"><a id="ac206721972f739510cb11f7c0a6a8f63" name="ac206721972f739510cb11f7c0a6a8f63"></a>
typedef struct <a class="el" href="../../dd/d06/structsegment__tree.html">segment_tree</a>&#160;</td><td class="memItemRight" valign="bottom"><b>segment_tree</b></td></tr>
<tr class="memdesc:ac206721972f739510cb11f7c0a6a8f63"><td class="mdescLeft">&#160;</td><td class="mdescRight">This structures holds all the data that is required by a segment tree. <br /></td></tr>
<tr class="separator:ac206721972f739510cb11f7c0a6a8f63"><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:aae59daf9a0dc33f8cbc7a525a616ee75"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/da0/segment__tree_8c.html#aae59daf9a0dc33f8cbc7a525a616ee75">segment_tree_build</a> (<a class="el" href="../../dd/d06/structsegment__tree.html">segment_tree</a> *tree)</td></tr>
<tr class="memdesc:aae59daf9a0dc33f8cbc7a525a616ee75"><td class="mdescLeft">&#160;</td><td class="mdescRight">Builds a Segment tree It is assumed that leaves of tree already contains data. <a href="../../da/da0/segment__tree_8c.html#aae59daf9a0dc33f8cbc7a525a616ee75">More...</a><br /></td></tr>
<tr class="separator:aae59daf9a0dc33f8cbc7a525a616ee75"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a1e81a9bbf01716f1b4fb27ef36a9098c"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/da0/segment__tree_8c.html#a1e81a9bbf01716f1b4fb27ef36a9098c">segment_tree_update</a> (<a class="el" href="../../dd/d06/structsegment__tree.html">segment_tree</a> *tree, size_t index, void *val)</td></tr>
<tr class="memdesc:a1e81a9bbf01716f1b4fb27ef36a9098c"><td class="mdescLeft">&#160;</td><td class="mdescRight">For point updates This function updates the element at given index and also updates segment tree accordingly. <a href="../../da/da0/segment__tree_8c.html#a1e81a9bbf01716f1b4fb27ef36a9098c">More...</a><br /></td></tr>
<tr class="separator:a1e81a9bbf01716f1b4fb27ef36a9098c"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:af61bd96660cb53f49f28d60a5f1d0c91"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/da0/segment__tree_8c.html#af61bd96660cb53f49f28d60a5f1d0c91">segment_tree_query</a> (<a class="el" href="../../dd/d06/structsegment__tree.html">segment_tree</a> *tree, long long l, long long r, void *res)</td></tr>
<tr class="memdesc:af61bd96660cb53f49f28d60a5f1d0c91"><td class="mdescLeft">&#160;</td><td class="mdescRight">Query the segment tree This function helps in range query of segment tree This function assumes that the given range is valid Performs the query in range [l,r]. <a href="../../da/da0/segment__tree_8c.html#af61bd96660cb53f49f28d60a5f1d0c91">More...</a><br /></td></tr>
<tr class="separator:af61bd96660cb53f49f28d60a5f1d0c91"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3513a2b7c3f1d15aa3d019af1f834cde"><td class="memItemLeft" align="right" valign="top"><a class="el" href="../../dd/d06/structsegment__tree.html">segment_tree</a> *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/da0/segment__tree_8c.html#a3513a2b7c3f1d15aa3d019af1f834cde">segment_tree_init</a> (void *arr, size_t elem_size, size_t len, void *identity, <a class="el" href="../../da/da0/segment__tree_8c.html#aca549b5311d32ab1a703b4a4605821d8">combine_function</a> <a class="el" href="../../dd/d93/client_8c.html#ac17020a38607ab29ce18939d5194a32a">func</a>)</td></tr>
<tr class="memdesc:a3513a2b7c3f1d15aa3d019af1f834cde"><td class="mdescLeft">&#160;</td><td class="mdescRight">Initializes Segment Tree Accquires memory for segment tree and fill the leaves of segment tree with data from array. <a href="../../da/da0/segment__tree_8c.html#a3513a2b7c3f1d15aa3d019af1f834cde">More...</a><br /></td></tr>
<tr class="separator:a3513a2b7c3f1d15aa3d019af1f834cde"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:af20a9f373083d3f701e1cd92560cef01"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/da0/segment__tree_8c.html#af20a9f373083d3f701e1cd92560cef01">segment_tree_dispose</a> (<a class="el" href="../../dd/d06/structsegment__tree.html">segment_tree</a> *tree)</td></tr>
<tr class="memdesc:af20a9f373083d3f701e1cd92560cef01"><td class="mdescLeft">&#160;</td><td class="mdescRight">Dispose Segment Tree Frees all heap memory accquired by segment tree. <a href="../../da/da0/segment__tree_8c.html#af20a9f373083d3f701e1cd92560cef01">More...</a><br /></td></tr>
<tr class="separator:af20a9f373083d3f701e1cd92560cef01"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a776abfa81cde9016a2885dca7cfc05ab"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/da0/segment__tree_8c.html#a776abfa81cde9016a2885dca7cfc05ab">segment_tree_print_int</a> (<a class="el" href="../../dd/d06/structsegment__tree.html">segment_tree</a> *tree)</td></tr>
<tr class="memdesc:a776abfa81cde9016a2885dca7cfc05ab"><td class="mdescLeft">&#160;</td><td class="mdescRight">Prints the data in segment tree The data should be of int type A utility to print segment tree with data type of int. <a href="../../da/da0/segment__tree_8c.html#a776abfa81cde9016a2885dca7cfc05ab">More...</a><br /></td></tr>
<tr class="separator:a776abfa81cde9016a2885dca7cfc05ab"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a93bfab032ce9dbc0c1feaeee32a885fb"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/da0/segment__tree_8c.html#a93bfab032ce9dbc0c1feaeee32a885fb">minimum</a> (const void *a, const void *b, void *c)</td></tr>
<tr class="memdesc:a93bfab032ce9dbc0c1feaeee32a885fb"><td class="mdescLeft">&#160;</td><td class="mdescRight">Utility for test A function compare for minimum between two integers This function is used as combine_function for RMQ. <a href="../../da/da0/segment__tree_8c.html#a93bfab032ce9dbc0c1feaeee32a885fb">More...</a><br /></td></tr>
<tr class="separator:a93bfab032ce9dbc0c1feaeee32a885fb"><td class="memSeparator" colspan="2">&#160;</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="../../da/da0/segment__tree_8c.html#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
<tr class="memdesc:aa8dca7b867074164d5f45b0f3851269d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Test RMQ Testing Segment tree using Range Minimum Queries. <a href="../../da/da0/segment__tree_8c.html#aa8dca7b867074164d5f45b0f3851269d">More...</a><br /></td></tr>
<tr class="separator:aa8dca7b867074164d5f45b0f3851269d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/da0/segment__tree_8c.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main Function. <a href="../../da/da0/segment__tree_8c.html#ae66f6b31b5ad750f1fe042a706a4e3d4">More...</a><br /></td></tr>
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><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 >segment trees with only point updates </p>
<p >This code implements segment trees. Segment trees are general structures which allow range based queries in a given array in logN time. Segment tree with point updates allow update of single element in the array in logN time. <a href="https://codeforces.com/blog/entry/18051" target="_blank">Learn more about segment trees here</a> </p><dl class="section author"><dt>Author</dt><dd><a href="https://github.com/Lakhan-Nad" target="_blank">Lakhan Nad</a> </dd></dl>
</div><h2 class="groupheader">Typedef Documentation</h2>
<a id="aca549b5311d32ab1a703b4a4605821d8" name="aca549b5311d32ab1a703b4a4605821d8"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aca549b5311d32ab1a703b4a4605821d8">&#9670;&#160;</a></span>combine_function</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">typedef void(* combine_function) (const void *a, const void *b, void *result)</td>
</tr>
</table>
</div><div class="memdoc">
<p>Function that combines two data to generate a new one The name of function might be misleading actually combine here signifies the fact that in segment trees we take partial result from two ranges and using partial results we derive the result for joint range of those two ranges For Example: array(1,2,3,4,5,6) sum of range [0,2] = 6 and sum of range [3,5] = 15 the combined sum of two range is 6+15=21. </p>
<dl class="section note"><dt>Note</dt><dd>The function is same to binary function in Discrete Mathematics </dd></dl>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">a</td><td>pointer to first data </td></tr>
<tr><td class="paramname">b</td><td>pointer to second data </td></tr>
<tr><td class="paramname">result</td><td>pointer to memory location where result of combining a and b is to be stored </td></tr>
</table>
</dd>
</dl>
</div>
</div>
<h2 class="groupheader">Function Documentation</h2>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#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">void&#160;</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main Function. </p>
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 232</span>{</div>
<div class="line"><span class="lineno"> 233</span> <a class="code hl_function" href="../../da/da0/segment__tree_8c.html#aa8dca7b867074164d5f45b0f3851269d">test</a>();</div>
<div class="line"><span class="lineno"> 234</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 235</span>}</div>
<div class="ttc" id="asegment__tree_8c_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="../../da/da0/segment__tree_8c.html#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Test RMQ Testing Segment tree using Range Minimum Queries.</div><div class="ttdef"><b>Definition:</b> segment_tree.c:205</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="../../da/da0/segment__tree_8c_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="450" height="283"><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="a93bfab032ce9dbc0c1feaeee32a885fb" name="a93bfab032ce9dbc0c1feaeee32a885fb"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a93bfab032ce9dbc0c1feaeee32a885fb">&#9670;&#160;</a></span>minimum()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void minimum </td>
<td>(</td>
<td class="paramtype">const void *&#160;</td>
<td class="paramname"><em>a</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const void *&#160;</td>
<td class="paramname"><em>b</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">void *&#160;</td>
<td class="paramname"><em>c</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Utility for test A function compare for minimum between two integers This function is used as combine_function for RMQ. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">a</td><td>pointer to integer a </td></tr>
<tr><td class="paramname">b</td><td>pointer to integer b </td></tr>
<tr><td class="paramname">c</td><td>pointer where minimum of a and b is tored as result </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 195</span>{</div>
<div class="line"><span class="lineno"> 196</span> *(<span class="keywordtype">int</span> *)c = *(<span class="keywordtype">int</span> *)a &lt; *(<span class="keywordtype">int</span> *)b ? *(<span class="keywordtype">int</span> *)a : *(<span class="keywordtype">int</span> *)b;</div>
<div class="line"><span class="lineno"> 197</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="aae59daf9a0dc33f8cbc7a525a616ee75" name="aae59daf9a0dc33f8cbc7a525a616ee75"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aae59daf9a0dc33f8cbc7a525a616ee75">&#9670;&#160;</a></span>segment_tree_build()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void segment_tree_build </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../dd/d06/structsegment__tree.html">segment_tree</a> *&#160;</td>
<td class="paramname"><em>tree</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Builds a Segment tree It is assumed that leaves of tree already contains data. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">tree</td><td>pointer to segment tree to be build </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 56</span>{</div>
<div class="line"><span class="lineno"> 57</span> <span class="keywordtype">size_t</span> elem_size = tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#aa9dc376b5b219c4cec6546483527b853">elem_size</a>;</div>
<div class="line"><span class="lineno"> 58</span> <span class="keywordtype">int</span> index = (tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a5ad61abcbd2c25a4a71416281dba8f1e">length</a> - 2);</div>
<div class="line"><span class="lineno"> 59</span> <span class="keywordtype">size_t</span> b, l, r;</div>
<div class="line"><span class="lineno"> 60</span> <span class="keywordtype">char</span> *ptr = (<span class="keywordtype">char</span> *)tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#aa18d7cb422873a807707b26448dce7cd">root</a>;</div>
<div class="line"><span class="lineno"> 61</span> for (; index &gt;= 0; index--)</div>
<div class="line"><span class="lineno"> 62</span> {</div>
<div class="line"><span class="lineno"> 63</span> b = index * elem_size;</div>
<div class="line"><span class="lineno"> 64</span> l = (2 * index + 1) * elem_size;</div>
<div class="line"><span class="lineno"> 65</span> r = (2 * index + 2) * elem_size;</div>
<div class="line"><span class="lineno"> 66</span> tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a973ab017a97678fdc6774543585897df">combine</a>(ptr + l, ptr + r, ptr + b);</div>
<div class="line"><span class="lineno"> 67</span> }</div>
<div class="line"><span class="lineno"> 68</span>}</div>
<div class="ttc" id="astructsegment__tree_html_a5ad61abcbd2c25a4a71416281dba8f1e"><div class="ttname"><a href="../../dd/d06/structsegment__tree.html#a5ad61abcbd2c25a4a71416281dba8f1e">segment_tree::length</a></div><div class="ttdeci">size_t length</div><div class="ttdoc">total size of array which segment tree represents</div><div class="ttdef"><b>Definition:</b> segment_tree.c:43</div></div>
<div class="ttc" id="astructsegment__tree_html_a973ab017a97678fdc6774543585897df"><div class="ttname"><a href="../../dd/d06/structsegment__tree.html#a973ab017a97678fdc6774543585897df">segment_tree::combine</a></div><div class="ttdeci">combine_function combine</div><div class="ttdoc">the function to be used to combine two node's data to form parent's data</div><div class="ttdef"><b>Definition:</b> segment_tree.c:47</div></div>
<div class="ttc" id="astructsegment__tree_html_aa18d7cb422873a807707b26448dce7cd"><div class="ttname"><a href="../../dd/d06/structsegment__tree.html#aa18d7cb422873a807707b26448dce7cd">segment_tree::root</a></div><div class="ttdeci">void * root</div><div class="ttdoc">the root of formed segment tree</div><div class="ttdef"><b>Definition:</b> segment_tree.c:40</div></div>
<div class="ttc" id="astructsegment__tree_html_aa9dc376b5b219c4cec6546483527b853"><div class="ttname"><a href="../../dd/d06/structsegment__tree.html#aa9dc376b5b219c4cec6546483527b853">segment_tree::elem_size</a></div><div class="ttdeci">size_t elem_size</div><div class="ttdoc">size in bytes of each data element</div><div class="ttdef"><b>Definition:</b> segment_tree.c:42</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="af20a9f373083d3f701e1cd92560cef01" name="af20a9f373083d3f701e1cd92560cef01"></a>
<h2 class="memtitle"><span class="permalink"><a href="#af20a9f373083d3f701e1cd92560cef01">&#9670;&#160;</a></span>segment_tree_dispose()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void segment_tree_dispose </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../dd/d06/structsegment__tree.html">segment_tree</a> *&#160;</td>
<td class="paramname"><em>tree</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Dispose Segment Tree Frees all heap memory accquired by segment tree. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">tree</td><td>pointer to segment tree </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 163</span>{</div>
<div class="line"><span class="lineno"> 164</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#aa18d7cb422873a807707b26448dce7cd">root</a>);</div>
<div class="line"><span class="lineno"> 165</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a5373ee53a5ac1cd7a9dcb89a4c23a04a">identity</a>);</div>
<div class="line"><span class="lineno"> 166</span>}</div>
<div class="ttc" id="amalloc__dbg_8h_html_a9cc854374299a1dd933bf62029761768"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a></div><div class="ttdeci">#define free(ptr)</div><div class="ttdoc">This macro replace the standard free function with free_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:26</div></div>
<div class="ttc" id="astructsegment__tree_html_a5373ee53a5ac1cd7a9dcb89a4c23a04a"><div class="ttname"><a href="../../dd/d06/structsegment__tree.html#a5373ee53a5ac1cd7a9dcb89a4c23a04a">segment_tree::identity</a></div><div class="ttdeci">void * identity</div><div class="ttdoc">identity element for combine function</div><div class="ttdef"><b>Definition:</b> segment_tree.c:41</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a3513a2b7c3f1d15aa3d019af1f834cde" name="a3513a2b7c3f1d15aa3d019af1f834cde"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a3513a2b7c3f1d15aa3d019af1f834cde">&#9670;&#160;</a></span>segment_tree_init()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="el" href="../../dd/d06/structsegment__tree.html">segment_tree</a> * segment_tree_init </td>
<td>(</td>
<td class="paramtype">void *&#160;</td>
<td class="paramname"><em>arr</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">size_t&#160;</td>
<td class="paramname"><em>elem_size</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">size_t&#160;</td>
<td class="paramname"><em>len</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">void *&#160;</td>
<td class="paramname"><em>identity</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype"><a class="el" href="../../da/da0/segment__tree_8c.html#aca549b5311d32ab1a703b4a4605821d8">combine_function</a>&#160;</td>
<td class="paramname"><em>func</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Initializes Segment Tree Accquires memory for segment tree and fill the leaves of segment tree with data from array. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">arr</td><td>the array data upon which segment tree is build </td></tr>
<tr><td class="paramname">elem_size</td><td>size of each element in segment tree </td></tr>
<tr><td class="paramname">len</td><td>total no of elements in array </td></tr>
<tr><td class="paramname">identity</td><td>the identity element for combine_function </td></tr>
<tr><td class="paramname">func</td><td>the combine_function used to build segment tree</td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>pointer to sgement tree build </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 142</span>{</div>
<div class="line"><span class="lineno"> 143</span> <a class="code hl_struct" href="../../dd/d06/structsegment__tree.html">segment_tree</a> *tree = <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<a class="code hl_struct" href="../../dd/d06/structsegment__tree.html">segment_tree</a>));</div>
<div class="line"><span class="lineno"> 144</span> tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#aa9dc376b5b219c4cec6546483527b853">elem_size</a> = elem_size;</div>
<div class="line"><span class="lineno"> 145</span> tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a5ad61abcbd2c25a4a71416281dba8f1e">length</a> = len;</div>
<div class="line"><span class="lineno"> 146</span> tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a973ab017a97678fdc6774543585897df">combine</a> = <a class="code hl_function" href="../../dd/d93/client_8c.html#ac17020a38607ab29ce18939d5194a32a">func</a>;</div>
<div class="line"><span class="lineno"> 147</span> tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#aa18d7cb422873a807707b26448dce7cd">root</a> = <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<span class="keywordtype">char</span>) * elem_size * (2 * len - 1));</div>
<div class="line"><span class="lineno"> 148</span> tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a5373ee53a5ac1cd7a9dcb89a4c23a04a">identity</a> = <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<span class="keyword">sizeof</span>(<span class="keywordtype">char</span>) * elem_size);</div>
<div class="line"><span class="lineno"> 149</span> <span class="keywordtype">char</span> *ptr = (<span class="keywordtype">char</span> *)tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#aa18d7cb422873a807707b26448dce7cd">root</a>;</div>
<div class="line"><span class="lineno"> 150</span> memset(ptr, 0, (len - 1) * elem_size); <span class="comment">// Initializing memory</span></div>
<div class="line"><span class="lineno"> 151</span> ptr = ptr + (len - 1) * elem_size;</div>
<div class="line"><span class="lineno"> 152</span> memcpy(ptr, arr, elem_size * len); <span class="comment">// copy the leaf nodes i.e. array data</span></div>
<div class="line"><span class="lineno"> 153</span> memcpy(tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a5373ee53a5ac1cd7a9dcb89a4c23a04a">identity</a>, identity, elem_size); <span class="comment">// copy identity element</span></div>
<div class="line"><span class="lineno"> 154</span> <span class="keywordflow">return</span> tree;</div>
<div class="line"><span class="lineno"> 155</span>}</div>
<div class="ttc" id="aclient_8c_html_ac17020a38607ab29ce18939d5194a32a"><div class="ttname"><a href="../../dd/d93/client_8c.html#ac17020a38607ab29ce18939d5194a32a">func</a></div><div class="ttdeci">void func(int sockfd)</div><div class="ttdoc">Continuous loop to send and receive over the socket.</div><div class="ttdef"><b>Definition:</b> client.c:37</div></div>
<div class="ttc" id="amalloc__dbg_8h_html_a725f50ecaf1959d96de79b36b4788fee"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a></div><div class="ttdeci">#define malloc(bytes)</div><div class="ttdoc">This macro replace the standard malloc function with malloc_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:18</div></div>
<div class="ttc" id="astructsegment__tree_html"><div class="ttname"><a href="../../dd/d06/structsegment__tree.html">segment_tree</a></div><div class="ttdoc">This structures holds all the data that is required by a segment tree.</div><div class="ttdef"><b>Definition:</b> segment_tree.c:39</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="../../da/da0/segment__tree_8c_a3513a2b7c3f1d15aa3d019af1f834cde_cgraph.svg" width="231" height="36"><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="a776abfa81cde9016a2885dca7cfc05ab" name="a776abfa81cde9016a2885dca7cfc05ab"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a776abfa81cde9016a2885dca7cfc05ab">&#9670;&#160;</a></span>segment_tree_print_int()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void segment_tree_print_int </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../dd/d06/structsegment__tree.html">segment_tree</a> *&#160;</td>
<td class="paramname"><em>tree</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Prints the data in segment tree The data should be of int type A utility to print segment tree with data type of int. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">tree</td><td>pointer to segment tree </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 176</span>{</div>
<div class="line"><span class="lineno"> 177</span> <span class="keywordtype">char</span> *base = (<span class="keywordtype">char</span> *)tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#aa18d7cb422873a807707b26448dce7cd">root</a>;</div>
<div class="line"><span class="lineno"> 178</span> <span class="keywordtype">size_t</span> i = 0;</div>
<div class="line"><span class="lineno"> 179</span> for (; i &lt; 2 * tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a5ad61abcbd2c25a4a71416281dba8f1e">length</a> - 1; i++)</div>
<div class="line"><span class="lineno"> 180</span> {</div>
<div class="line"><span class="lineno"> 181</span> printf(<span class="stringliteral">&quot;%d &quot;</span>, *(<span class="keywordtype">int</span> *)(base + i * tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#aa9dc376b5b219c4cec6546483527b853">elem_size</a>));</div>
<div class="line"><span class="lineno"> 182</span> }</div>
<div class="line"><span class="lineno"> 183</span> printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 184</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="af61bd96660cb53f49f28d60a5f1d0c91" name="af61bd96660cb53f49f28d60a5f1d0c91"></a>
<h2 class="memtitle"><span class="permalink"><a href="#af61bd96660cb53f49f28d60a5f1d0c91">&#9670;&#160;</a></span>segment_tree_query()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void segment_tree_query </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../dd/d06/structsegment__tree.html">segment_tree</a> *&#160;</td>
<td class="paramname"><em>tree</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">long long&#160;</td>
<td class="paramname"><em>l</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">long long&#160;</td>
<td class="paramname"><em>r</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">void *&#160;</td>
<td class="paramname"><em>res</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Query the segment tree This function helps in range query of segment tree This function assumes that the given range is valid Performs the query in range [l,r]. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">tree</td><td>pointer to segment tree </td></tr>
<tr><td class="paramname">l</td><td>the start of range </td></tr>
<tr><td class="paramname">r</td><td>the end of range </td></tr>
<tr><td class="paramname">res</td><td>the pointer to memory where result of query is stored </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 106</span>{</div>
<div class="line"><span class="lineno"> 107</span> <span class="keywordtype">size_t</span> elem_size = tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#aa9dc376b5b219c4cec6546483527b853">elem_size</a>;</div>
<div class="line"><span class="lineno"> 108</span> memcpy(res, tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a5373ee53a5ac1cd7a9dcb89a4c23a04a">identity</a>, elem_size);</div>
<div class="line"><span class="lineno"> 109</span> elem_size = tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#aa9dc376b5b219c4cec6546483527b853">elem_size</a>;</div>
<div class="line"><span class="lineno"> 110</span> <span class="keywordtype">char</span> *root = (<span class="keywordtype">char</span> *)tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#aa18d7cb422873a807707b26448dce7cd">root</a>;</div>
<div class="line"><span class="lineno"> 111</span> l += tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a5ad61abcbd2c25a4a71416281dba8f1e">length</a> - 1;</div>
<div class="line"><span class="lineno"> 112</span> r += tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a5ad61abcbd2c25a4a71416281dba8f1e">length</a> - 1;</div>
<div class="line"><span class="lineno"> 113</span> while (l &lt;= r)</div>
<div class="line"><span class="lineno"> 114</span> {</div>
<div class="line"><span class="lineno"> 115</span> <span class="keywordflow">if</span> (!(l &amp; 1))</div>
<div class="line"><span class="lineno"> 116</span> {</div>
<div class="line"><span class="lineno"> 117</span> tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a973ab017a97678fdc6774543585897df">combine</a>(res, root + l * elem_size, res);</div>
<div class="line"><span class="lineno"> 118</span> }</div>
<div class="line"><span class="lineno"> 119</span> <span class="keywordflow">if</span> (r &amp; 1)</div>
<div class="line"><span class="lineno"> 120</span> {</div>
<div class="line"><span class="lineno"> 121</span> tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a973ab017a97678fdc6774543585897df">combine</a>(res, root + r * elem_size, res);</div>
<div class="line"><span class="lineno"> 122</span> }</div>
<div class="line"><span class="lineno"> 123</span> r = (r &gt;&gt; 1) - 1;</div>
<div class="line"><span class="lineno"> 124</span> l = (l &gt;&gt; 1);</div>
<div class="line"><span class="lineno"> 125</span> }</div>
<div class="line"><span class="lineno"> 126</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a1e81a9bbf01716f1b4fb27ef36a9098c" name="a1e81a9bbf01716f1b4fb27ef36a9098c"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a1e81a9bbf01716f1b4fb27ef36a9098c">&#9670;&#160;</a></span>segment_tree_update()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void segment_tree_update </td>
<td>(</td>
<td class="paramtype"><a class="el" href="../../dd/d06/structsegment__tree.html">segment_tree</a> *&#160;</td>
<td class="paramname"><em>tree</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">size_t&#160;</td>
<td class="paramname"><em>index</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">void *&#160;</td>
<td class="paramname"><em>val</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>For point updates This function updates the element at given index and also updates segment tree accordingly. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">tree</td><td>pointer to segment tree </td></tr>
<tr><td class="paramname">index</td><td>the index whose element is to be updated (0 based indexing used) </td></tr>
<tr><td class="paramname">val</td><td>pointer to value that is to be replaced at given index </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 80</span>{</div>
<div class="line"><span class="lineno"> 81</span> <span class="keywordtype">size_t</span> elem_size = tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#aa9dc376b5b219c4cec6546483527b853">elem_size</a>;</div>
<div class="line"><span class="lineno"> 82</span> index = index + tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a5ad61abcbd2c25a4a71416281dba8f1e">length</a> - 1;</div>
<div class="line"><span class="lineno"> 83</span> <span class="keywordtype">char</span> *base = (<span class="keywordtype">char</span> *)tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#aa18d7cb422873a807707b26448dce7cd">root</a>;</div>
<div class="line"><span class="lineno"> 84</span> <span class="keywordtype">char</span> *t = base + index * elem_size;</div>
<div class="line"><span class="lineno"> 85</span> memcpy(t, val, elem_size);</div>
<div class="line"><span class="lineno"> 86</span> <span class="keywordflow">while</span> (index &gt; 0)</div>
<div class="line"><span class="lineno"> 87</span> {</div>
<div class="line"><span class="lineno"> 88</span> index = ((index - 1) &gt;&gt; 1);</div>
<div class="line"><span class="lineno"> 89</span> tree-&gt;<a class="code hl_variable" href="../../dd/d06/structsegment__tree.html#a973ab017a97678fdc6774543585897df">combine</a>(base + (2 * index + 1) * elem_size,</div>
<div class="line"><span class="lineno"> 90</span> base + (2 * index + 2) * elem_size,</div>
<div class="line"><span class="lineno"> 91</span> base + index * elem_size);</div>
<div class="line"><span class="lineno"> 92</span> }</div>
<div class="line"><span class="lineno"> 93</span>}</div>
</div><!-- fragment -->
</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="paramtype">void&#160;</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>Test RMQ Testing Segment tree using Range Minimum Queries. </p>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 206</span>{</div>
<div class="line"><span class="lineno"> 207</span> int32_t arr[10] = {1, 0, 3, 5, 7, 2, 11, 6, -2, 8};</div>
<div class="line"><span class="lineno"> 208</span> int32_t identity = __INT32_MAX__;</div>
<div class="line"><span class="lineno"> 209</span> <a class="code hl_struct" href="../../dd/d06/structsegment__tree.html">segment_tree</a> *tree =</div>
<div class="line"><span class="lineno"> 210</span> <a class="code hl_function" href="../../da/da0/segment__tree_8c.html#a3513a2b7c3f1d15aa3d019af1f834cde">segment_tree_init</a>(arr, <span class="keyword">sizeof</span>(*arr), 10, &amp;identity, <a class="code hl_function" href="../../da/da0/segment__tree_8c.html#a93bfab032ce9dbc0c1feaeee32a885fb">minimum</a>);</div>
<div class="line"><span class="lineno"> 211</span> <a class="code hl_function" href="../../da/da0/segment__tree_8c.html#aae59daf9a0dc33f8cbc7a525a616ee75">segment_tree_build</a>(tree);</div>
<div class="line"><span class="lineno"> 212</span> int32_t result;</div>
<div class="line"><span class="lineno"> 213</span> <a class="code hl_function" href="../../da/da0/segment__tree_8c.html#af61bd96660cb53f49f28d60a5f1d0c91">segment_tree_query</a>(tree, 3, 6, &amp;result);</div>
<div class="line"><span class="lineno"> 214</span> assert(result == 2);</div>
<div class="line"><span class="lineno"> 215</span> <a class="code hl_function" href="../../da/da0/segment__tree_8c.html#af61bd96660cb53f49f28d60a5f1d0c91">segment_tree_query</a>(tree, 8, 9, &amp;result);</div>
<div class="line"><span class="lineno"> 216</span> assert(result == -2);</div>
<div class="line"><span class="lineno"> 217</span> result = 12;</div>
<div class="line"><span class="lineno"> 218</span> <a class="code hl_function" href="../../da/da0/segment__tree_8c.html#a1e81a9bbf01716f1b4fb27ef36a9098c">segment_tree_update</a>(tree, 5, &amp;result);</div>
<div class="line"><span class="lineno"> 219</span> <a class="code hl_function" href="../../da/da0/segment__tree_8c.html#a1e81a9bbf01716f1b4fb27ef36a9098c">segment_tree_update</a>(tree, 8, &amp;result);</div>
<div class="line"><span class="lineno"> 220</span> <a class="code hl_function" href="../../da/da0/segment__tree_8c.html#af61bd96660cb53f49f28d60a5f1d0c91">segment_tree_query</a>(tree, 0, 3, &amp;result);</div>
<div class="line"><span class="lineno"> 221</span> assert(result == 0);</div>
<div class="line"><span class="lineno"> 222</span> <a class="code hl_function" href="../../da/da0/segment__tree_8c.html#af61bd96660cb53f49f28d60a5f1d0c91">segment_tree_query</a>(tree, 8, 9, &amp;result);</div>
<div class="line"><span class="lineno"> 223</span> assert(result == 8);</div>
<div class="line"><span class="lineno"> 224</span> <a class="code hl_function" href="../../da/da0/segment__tree_8c.html#af20a9f373083d3f701e1cd92560cef01">segment_tree_dispose</a>(tree);</div>
<div class="line"><span class="lineno"> 225</span>}</div>
<div class="ttc" id="asegment__tree_8c_html_a1e81a9bbf01716f1b4fb27ef36a9098c"><div class="ttname"><a href="../../da/da0/segment__tree_8c.html#a1e81a9bbf01716f1b4fb27ef36a9098c">segment_tree_update</a></div><div class="ttdeci">void segment_tree_update(segment_tree *tree, size_t index, void *val)</div><div class="ttdoc">For point updates This function updates the element at given index and also updates segment tree acco...</div><div class="ttdef"><b>Definition:</b> segment_tree.c:79</div></div>
<div class="ttc" id="asegment__tree_8c_html_a3513a2b7c3f1d15aa3d019af1f834cde"><div class="ttname"><a href="../../da/da0/segment__tree_8c.html#a3513a2b7c3f1d15aa3d019af1f834cde">segment_tree_init</a></div><div class="ttdeci">segment_tree * segment_tree_init(void *arr, size_t elem_size, size_t len, void *identity, combine_function func)</div><div class="ttdoc">Initializes Segment Tree Accquires memory for segment tree and fill the leaves of segment tree with d...</div><div class="ttdef"><b>Definition:</b> segment_tree.c:140</div></div>
<div class="ttc" id="asegment__tree_8c_html_a93bfab032ce9dbc0c1feaeee32a885fb"><div class="ttname"><a href="../../da/da0/segment__tree_8c.html#a93bfab032ce9dbc0c1feaeee32a885fb">minimum</a></div><div class="ttdeci">void minimum(const void *a, const void *b, void *c)</div><div class="ttdoc">Utility for test A function compare for minimum between two integers This function is used as combine...</div><div class="ttdef"><b>Definition:</b> segment_tree.c:194</div></div>
<div class="ttc" id="asegment__tree_8c_html_aae59daf9a0dc33f8cbc7a525a616ee75"><div class="ttname"><a href="../../da/da0/segment__tree_8c.html#aae59daf9a0dc33f8cbc7a525a616ee75">segment_tree_build</a></div><div class="ttdeci">void segment_tree_build(segment_tree *tree)</div><div class="ttdoc">Builds a Segment tree It is assumed that leaves of tree already contains data.</div><div class="ttdef"><b>Definition:</b> segment_tree.c:55</div></div>
<div class="ttc" id="asegment__tree_8c_html_af20a9f373083d3f701e1cd92560cef01"><div class="ttname"><a href="../../da/da0/segment__tree_8c.html#af20a9f373083d3f701e1cd92560cef01">segment_tree_dispose</a></div><div class="ttdeci">void segment_tree_dispose(segment_tree *tree)</div><div class="ttdoc">Dispose Segment Tree Frees all heap memory accquired by segment tree.</div><div class="ttdef"><b>Definition:</b> segment_tree.c:162</div></div>
<div class="ttc" id="asegment__tree_8c_html_af61bd96660cb53f49f28d60a5f1d0c91"><div class="ttname"><a href="../../da/da0/segment__tree_8c.html#af61bd96660cb53f49f28d60a5f1d0c91">segment_tree_query</a></div><div class="ttdeci">void segment_tree_query(segment_tree *tree, long long l, long long r, void *res)</div><div class="ttdoc">Query the segment tree This function helps in range query of segment tree This function assumes that ...</div><div class="ttdef"><b>Definition:</b> segment_tree.c:105</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="../../da/da0/segment__tree_8c_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg" width="351" height="283"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</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="../../dir_e39df94f82619fa5256f1786519937be.html">binary_trees</a></li><li class="navelem"><a class="el" href="../../da/da0/segment__tree_8c.html">segment_tree.c</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>