1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Overlap counter</title> 5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Chapter 1. Boost.Icl"> 8<link rel="up" href="../examples.html" title="Examples"> 9<link rel="prev" href="interval_container.html" title="Interval container"> 10<link rel="next" href="partys_height_average.html" title="Party's height average"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="interval_container.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="partys_height_average.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="boost_icl.examples.overlap_counter"></a><a class="link" href="overlap_counter.html" title="Overlap counter">Overlap counter</a> 28</h3></div></div></div> 29<p> 30 Example <span class="bold"><strong>overlap counter</strong></span> provides the simplest 31 application of an interval_map that maps intervals to integers. An interval_map<int,int> 32 serves as an overlap counter if we only add interval value pairs that carry 33 1 as associated value. 34 </p> 35<p> 36 Doing so, the associated values that are accumulated in the interval_map 37 are just the number of overlaps of all added intervals. 38 </p> 39<p> 40</p> 41<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 42<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">icl</span><span class="special">/</span><span class="identifier">split_interval_map</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 43 44<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">std</span><span class="special">;</span> 45<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">icl</span><span class="special">;</span> 46 47 48<span class="comment">/* The most simple example of an interval_map is an overlap counter. 49 If intervals are added that are associated with the value 1, 50 all overlaps of added intervals are counted as a result in the 51 associated values. 52*/</span> 53<span class="keyword">typedef</span> <span class="identifier">interval_map</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">int</span><span class="special">></span> <span class="identifier">OverlapCounterT</span><span class="special">;</span> 54 55<span class="keyword">void</span> <span class="identifier">print_overlaps</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">OverlapCounterT</span><span class="special">&</span> <span class="identifier">counter</span><span class="special">)</span> 56<span class="special">{</span> 57 <span class="keyword">for</span><span class="special">(</span><span class="identifier">OverlapCounterT</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">counter</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span> <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">counter</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span> <span class="identifier">it</span><span class="special">++)</span> 58 <span class="special">{</span> 59 <span class="identifier">discrete_interval</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">itv</span> <span class="special">=</span> <span class="special">(*</span><span class="identifier">it</span><span class="special">).</span><span class="identifier">first</span><span class="special">;</span> 60 <span class="keyword">int</span> <span class="identifier">overlaps_count</span> <span class="special">=</span> <span class="special">(*</span><span class="identifier">it</span><span class="special">).</span><span class="identifier">second</span><span class="special">;</span> 61 <span class="keyword">if</span><span class="special">(</span><span class="identifier">overlaps_count</span> <span class="special">==</span> <span class="number">1</span><span class="special">)</span> 62 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"in interval "</span> <span class="special"><<</span> <span class="identifier">itv</span> <span class="special"><<</span> <span class="string">" intervals do not overlap"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 63 <span class="keyword">else</span> 64 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"in interval "</span> <span class="special"><<</span> <span class="identifier">itv</span> <span class="special"><<</span> <span class="string">": "</span><span class="special"><<</span> <span class="identifier">overlaps_count</span> <span class="special"><<</span> <span class="string">" intervals overlap"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 65 <span class="special">}</span> 66<span class="special">}</span> 67 68<span class="keyword">void</span> <span class="identifier">overlap_counter</span><span class="special">()</span> 69<span class="special">{</span> 70 <span class="identifier">OverlapCounterT</span> <span class="identifier">overlap_counter</span><span class="special">;</span> 71 <span class="identifier">discrete_interval</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">inter_val</span><span class="special">;</span> 72 73 <span class="identifier">inter_val</span> <span class="special">=</span> <span class="identifier">discrete_interval</span><span class="special"><</span><span class="keyword">int</span><span class="special">>::</span><span class="identifier">right_open</span><span class="special">(</span><span class="number">4</span><span class="special">,</span><span class="number">8</span><span class="special">);</span> 74 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"-- adding "</span> <span class="special"><<</span> <span class="identifier">inter_val</span> <span class="special"><<</span> <span class="string">" -----------------------------------------"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 75 <span class="identifier">overlap_counter</span> <span class="special">+=</span> <span class="identifier">make_pair</span><span class="special">(</span><span class="identifier">inter_val</span><span class="special">,</span> <span class="number">1</span><span class="special">);</span> 76 <span class="identifier">print_overlaps</span><span class="special">(</span><span class="identifier">overlap_counter</span><span class="special">);</span> 77 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"-----------------------------------------------------------"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 78 79 <span class="identifier">inter_val</span> <span class="special">=</span> <span class="identifier">discrete_interval</span><span class="special"><</span><span class="keyword">int</span><span class="special">>::</span><span class="identifier">right_open</span><span class="special">(</span><span class="number">6</span><span class="special">,</span><span class="number">9</span><span class="special">);</span> 80 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"-- adding "</span> <span class="special"><<</span> <span class="identifier">inter_val</span> <span class="special"><<</span> <span class="string">" -----------------------------------------"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 81 <span class="identifier">overlap_counter</span> <span class="special">+=</span> <span class="identifier">make_pair</span><span class="special">(</span><span class="identifier">inter_val</span><span class="special">,</span> <span class="number">1</span><span class="special">);</span> 82 <span class="identifier">print_overlaps</span><span class="special">(</span><span class="identifier">overlap_counter</span><span class="special">);</span> 83 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"-----------------------------------------------------------"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 84 85 <span class="identifier">inter_val</span> <span class="special">=</span> <span class="identifier">discrete_interval</span><span class="special"><</span><span class="keyword">int</span><span class="special">>::</span><span class="identifier">right_open</span><span class="special">(</span><span class="number">1</span><span class="special">,</span><span class="number">9</span><span class="special">);</span> 86 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"-- adding "</span> <span class="special"><<</span> <span class="identifier">inter_val</span> <span class="special"><<</span> <span class="string">" -----------------------------------------"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 87 <span class="identifier">overlap_counter</span> <span class="special">+=</span> <span class="identifier">make_pair</span><span class="special">(</span><span class="identifier">inter_val</span><span class="special">,</span> <span class="number">1</span><span class="special">);</span> 88 <span class="identifier">print_overlaps</span><span class="special">(</span><span class="identifier">overlap_counter</span><span class="special">);</span> 89 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"-----------------------------------------------------------"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 90 91<span class="special">}</span> 92 93<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 94<span class="special">{</span> 95 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">">>Interval Container Library: Sample overlap_counter.cpp <<\n"</span><span class="special">;</span> 96 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"-----------------------------------------------------------\n"</span><span class="special">;</span> 97 <span class="identifier">overlap_counter</span><span class="special">();</span> 98 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 99<span class="special">}</span> 100 101<span class="comment">// Program output:</span> 102 103<span class="comment">// >>Interval Container Library: Sample overlap_counter.cpp <<</span> 104<span class="comment">// -----------------------------------------------------------</span> 105<span class="comment">// -- adding [4,8) -----------------------------------------</span> 106<span class="comment">// in interval [4,8) intervals do not overlap</span> 107<span class="comment">// -----------------------------------------------------------</span> 108<span class="comment">// -- adding [6,9) -----------------------------------------</span> 109<span class="comment">// in interval [4,6) intervals do not overlap</span> 110<span class="comment">// in interval [6,8): 2 intervals overlap</span> 111<span class="comment">// in interval [8,9) intervals do not overlap</span> 112<span class="comment">// -----------------------------------------------------------</span> 113<span class="comment">// -- adding [1,9) -----------------------------------------</span> 114<span class="comment">// in interval [1,4) intervals do not overlap</span> 115<span class="comment">// in interval [4,6): 2 intervals overlap</span> 116<span class="comment">// in interval [6,8): 3 intervals overlap</span> 117<span class="comment">// in interval [8,9): 2 intervals overlap</span> 118<span class="comment">// -----------------------------------------------------------</span> 119</pre> 120<p> 121 </p> 122</div> 123<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 124<td align="left"></td> 125<td align="right"><div class="copyright-footer">Copyright © 2007-2010 Joachim 126 Faulhaber<br>Copyright © 1999-2006 Cortex Software 127 GmbH<p> 128 Distributed under the Boost Software License, Version 1.0. (See accompanying 129 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 130 </p> 131</div></td> 132</tr></table> 133<hr> 134<div class="spirit-nav"> 135<a accesskey="p" href="interval_container.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="partys_height_average.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 136</div> 137</body> 138</html> 139