1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Party's height average</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="overlap_counter.html" title="Overlap counter"> 10<link rel="next" href="partys_tallest_guests.html" title="Party's tallest guests"> 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="overlap_counter.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_tallest_guests.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.partys_height_average"></a><a class="link" href="partys_height_average.html" title="Party's height average">Party's height 28 average</a> 29</h3></div></div></div> 30<p> 31 In the example <code class="computeroutput"><span class="identifier">partys_height_average</span><span class="special">.</span><span class="identifier">cpp</span></code> we 32 compute yet another aggregation: The average height of guests. This is done 33 by defining a <code class="computeroutput"><span class="keyword">class</span> <span class="identifier">counted_sum</span></code> 34 that sums up heights and counts the number of guests via an <code class="computeroutput"><span class="keyword">operator</span> <span class="special">+=</span></code>. 35 </p> 36<p> 37 Based on the <code class="computeroutput"><span class="keyword">operator</span> <span class="special">+=</span></code> 38 we can aggregate counted sums on addition of interval value pairs into an 39 interval_map. 40 </p> 41<p> 42</p> 43<pre class="programlisting"><span class="comment">// The next line includes <boost/date_time/posix_time/posix_time.hpp></span> 44<span class="comment">// and a few lines of adapter code.</span> 45<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">ptime</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 46<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 47<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">interval_map</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 48<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> 49 50<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">std</span><span class="special">;</span> 51<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">posix_time</span><span class="special">;</span> 52<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> 53 54 55<span class="keyword">class</span> <span class="identifier">counted_sum</span> 56<span class="special">{</span> 57<span class="keyword">public</span><span class="special">:</span> 58 <span class="identifier">counted_sum</span><span class="special">():</span><span class="identifier">_sum</span><span class="special">(</span><span class="number">0</span><span class="special">),</span><span class="identifier">_count</span><span class="special">(</span><span class="number">0</span><span class="special">){}</span> 59 <span class="identifier">counted_sum</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">sum</span><span class="special">):</span><span class="identifier">_sum</span><span class="special">(</span><span class="identifier">sum</span><span class="special">),</span><span class="identifier">_count</span><span class="special">(</span><span class="number">1</span><span class="special">){}</span> 60 61 <span class="keyword">int</span> <span class="identifier">sum</span><span class="special">()</span><span class="keyword">const</span> <span class="special">{</span><span class="keyword">return</span> <span class="identifier">_sum</span><span class="special">;}</span> 62 <span class="keyword">int</span> <span class="identifier">count</span><span class="special">()</span><span class="keyword">const</span><span class="special">{</span><span class="keyword">return</span> <span class="identifier">_count</span><span class="special">;}</span> 63 <span class="keyword">double</span> <span class="identifier">average</span><span class="special">()</span><span class="keyword">const</span><span class="special">{</span> <span class="keyword">return</span> <span class="identifier">_count</span><span class="special">==</span><span class="number">0</span> <span class="special">?</span> <span class="number">0.0</span> <span class="special">:</span> <span class="identifier">_sum</span><span class="special">/</span><span class="keyword">static_cast</span><span class="special"><</span><span class="keyword">double</span><span class="special">>(</span><span class="identifier">_count</span><span class="special">);</span> <span class="special">}</span> 64 65 <span class="identifier">counted_sum</span><span class="special">&</span> <span class="keyword">operator</span> <span class="special">+=</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">counted_sum</span><span class="special">&</span> <span class="identifier">right</span><span class="special">)</span> 66 <span class="special">{</span> <span class="identifier">_sum</span> <span class="special">+=</span> <span class="identifier">right</span><span class="special">.</span><span class="identifier">sum</span><span class="special">();</span> <span class="identifier">_count</span> <span class="special">+=</span> <span class="identifier">right</span><span class="special">.</span><span class="identifier">count</span><span class="special">();</span> <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span> <span class="special">}</span> 67 68<span class="keyword">private</span><span class="special">:</span> 69 <span class="keyword">int</span> <span class="identifier">_sum</span><span class="special">;</span> 70 <span class="keyword">int</span> <span class="identifier">_count</span><span class="special">;</span> 71<span class="special">};</span> 72 73<span class="keyword">bool</span> <span class="keyword">operator</span> <span class="special">==</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">counted_sum</span><span class="special">&</span> <span class="identifier">left</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">counted_sum</span><span class="special">&</span> <span class="identifier">right</span><span class="special">)</span> 74<span class="special">{</span> <span class="keyword">return</span> <span class="identifier">left</span><span class="special">.</span><span class="identifier">sum</span><span class="special">()==</span><span class="identifier">right</span><span class="special">.</span><span class="identifier">sum</span><span class="special">()</span> <span class="special">&&</span> <span class="identifier">left</span><span class="special">.</span><span class="identifier">count</span><span class="special">()==</span><span class="identifier">right</span><span class="special">.</span><span class="identifier">count</span><span class="special">();</span> <span class="special">}</span> 75 76 77<span class="keyword">void</span> <span class="identifier">partys_height_average</span><span class="special">()</span> 78<span class="special">{</span> 79 <span class="identifier">interval_map</span><span class="special"><</span><span class="identifier">ptime</span><span class="special">,</span> <span class="identifier">counted_sum</span><span class="special">></span> <span class="identifier">height_sums</span><span class="special">;</span> 80 81 <span class="identifier">height_sums</span> <span class="special">+=</span> 82 <span class="identifier">make_pair</span><span class="special">(</span> 83 <span class="identifier">discrete_interval</span><span class="special"><</span><span class="identifier">ptime</span><span class="special">>::</span><span class="identifier">right_open</span><span class="special">(</span> 84 <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 19:30"</span><span class="special">),</span> 85 <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 23:00"</span><span class="special">)),</span> 86 <span class="identifier">counted_sum</span><span class="special">(</span><span class="number">165</span><span class="special">));</span> <span class="comment">// Mary is 1,65 m tall.</span> 87 88 <span class="identifier">height_sums</span> <span class="special">+=</span> 89 <span class="identifier">make_pair</span><span class="special">(</span> 90 <span class="identifier">discrete_interval</span><span class="special"><</span><span class="identifier">ptime</span><span class="special">>::</span><span class="identifier">right_open</span><span class="special">(</span> 91 <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 19:30"</span><span class="special">),</span> 92 <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 23:00"</span><span class="special">)),</span> 93 <span class="identifier">counted_sum</span><span class="special">(</span><span class="number">180</span><span class="special">));</span> <span class="comment">// Harry is 1,80 m tall.</span> 94 95 <span class="identifier">height_sums</span> <span class="special">+=</span> 96 <span class="identifier">make_pair</span><span class="special">(</span> 97 <span class="identifier">discrete_interval</span><span class="special"><</span><span class="identifier">ptime</span><span class="special">>::</span><span class="identifier">right_open</span><span class="special">(</span> 98 <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 20:10"</span><span class="special">),</span> 99 <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-21 00:00"</span><span class="special">)),</span> 100 <span class="identifier">counted_sum</span><span class="special">(</span><span class="number">170</span><span class="special">));</span> <span class="comment">// Diana is 1,70 m tall.</span> 101 102 <span class="identifier">height_sums</span> <span class="special">+=</span> 103 <span class="identifier">make_pair</span><span class="special">(</span> 104 <span class="identifier">discrete_interval</span><span class="special"><</span><span class="identifier">ptime</span><span class="special">>::</span><span class="identifier">right_open</span><span class="special">(</span> 105 <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 20:10"</span><span class="special">),</span> 106 <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-21 00:00"</span><span class="special">)),</span> 107 <span class="identifier">counted_sum</span><span class="special">(</span><span class="number">165</span><span class="special">));</span> <span class="comment">// Susan is 1,65 m tall.</span> 108 109 <span class="identifier">height_sums</span> <span class="special">+=</span> 110 <span class="identifier">make_pair</span><span class="special">(</span> 111 <span class="identifier">discrete_interval</span><span class="special"><</span><span class="identifier">ptime</span><span class="special">>::</span><span class="identifier">right_open</span><span class="special">(</span> 112 <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 22:15"</span><span class="special">),</span> 113 <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-21 00:30"</span><span class="special">)),</span> 114 <span class="identifier">counted_sum</span><span class="special">(</span><span class="number">200</span><span class="special">));</span> <span class="comment">// Peters height is 2,00 m</span> 115 116 <span class="identifier">interval_map</span><span class="special"><</span><span class="identifier">ptime</span><span class="special">,</span> <span class="identifier">counted_sum</span><span class="special">>::</span><span class="identifier">iterator</span> <span class="identifier">height_sum_</span> <span class="special">=</span> <span class="identifier">height_sums</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span> 117 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"-------------- History of average guest height -------------------\n"</span><span class="special">;</span> 118 <span class="keyword">while</span><span class="special">(</span><span class="identifier">height_sum_</span> <span class="special">!=</span> <span class="identifier">height_sums</span><span class="special">.</span><span class="identifier">end</span><span class="special">())</span> 119 <span class="special">{</span> 120 <span class="identifier">discrete_interval</span><span class="special"><</span><span class="identifier">ptime</span><span class="special">></span> <span class="identifier">when</span> <span class="special">=</span> <span class="identifier">height_sum_</span><span class="special">-></span><span class="identifier">first</span><span class="special">;</span> 121 122 <span class="keyword">double</span> <span class="identifier">height_average</span> <span class="special">=</span> <span class="special">(*</span><span class="identifier">height_sum_</span><span class="special">++).</span><span class="identifier">second</span><span class="special">.</span><span class="identifier">average</span><span class="special">();</span> 123 <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">setprecision</span><span class="special">(</span><span class="number">3</span><span class="special">)</span> 124 <span class="special"><<</span> <span class="string">"["</span> <span class="special"><<</span> <span class="identifier">first</span><span class="special">(</span><span class="identifier">when</span><span class="special">)</span> <span class="special"><<</span> <span class="string">" - "</span> <span class="special"><<</span> <span class="identifier">upper</span><span class="special">(</span><span class="identifier">when</span><span class="special">)</span> <span class="special"><<</span> <span class="string">")"</span> 125 <span class="special"><<</span> <span class="string">": "</span> <span class="special"><<</span> <span class="identifier">height_average</span> <span class="special"><<</span><span class="string">" cm = "</span> <span class="special"><<</span> <span class="identifier">height_average</span><span class="special">/</span><span class="number">30.48</span> <span class="special"><<</span> <span class="string">" ft"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 126 <span class="special">}</span> 127<span class="special">}</span> 128 129 130<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 131<span class="special">{</span> 132 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">">>Interval Container Library: Sample partys_height_average.cpp <<\n"</span><span class="special">;</span> 133 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"------------------------------------------------------------------\n"</span><span class="special">;</span> 134 <span class="identifier">partys_height_average</span><span class="special">();</span> 135 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 136<span class="special">}</span> 137 138<span class="comment">// Program output:</span> 139<span class="comment">/*----------------------------------------------------------------------------- 140>>Interval Container Library: Sample partys_height_average.cpp << 141------------------------------------------------------------------ 142-------------- History of average guest height ------------------- 143[2008-May-20 19:30:00 - 2008-May-20 20:10:00): 173 cm = 5.66 ft 144[2008-May-20 20:10:00 - 2008-May-20 22:15:00): 170 cm = 5.58 ft 145[2008-May-20 22:15:00 - 2008-May-20 23:00:00): 176 cm = 5.77 ft 146[2008-May-20 23:00:00 - 2008-May-21 00:00:00): 178 cm = 5.85 ft 147[2008-May-21 00:00:00 - 2008-May-21 00:30:00): 200 cm = 6.56 ft 148-----------------------------------------------------------------------------*/</span> 149</pre> 150<p> 151 </p> 152<p> 153 Required for <code class="computeroutput"><span class="keyword">class</span> <span class="identifier">counted_sum</span></code> 154 is a default constructor <code class="computeroutput"><span class="identifier">counted_sum</span><span class="special">()</span></code>and an <code class="computeroutput"><span class="keyword">operator</span> 155 <span class="special">==</span></code> to test equality. To enable additive 156 aggregation on overlap also an <code class="computeroutput"><span class="keyword">operator</span> 157 <span class="special">+=</span></code> is needed. 158 </p> 159<p> 160 Note that no <code class="computeroutput"><span class="keyword">operator</span> <span class="special">-=</span></code> 161 for a subtraction of <code class="computeroutput"><span class="identifier">counted_sum</span></code> 162 values is defined. So you can only add to the <code class="computeroutput"><span class="identifier">interval_map</span><span class="special"><</span><span class="identifier">ptime</span><span class="special">,</span> <span class="identifier">counted_sum</span><span class="special">></span></code> but not subtract from it. 163 </p> 164<p> 165 In many real world applications only addition is needed and user defined 166 classes will work fine, if they only implement <code class="computeroutput"><span class="keyword">operator</span> 167 <span class="special">+=</span></code>. Only if any of the <code class="computeroutput"><span class="keyword">operator</span></code>s <code class="computeroutput"><span class="special">-=</span></code> 168 or <code class="computeroutput"><span class="special">-</span></code> is called on the interval_map, 169 the user defined class has to implement it's own <code class="computeroutput"><span class="keyword">operator</span> 170 <span class="special">-=</span></code> to provide the subtractive aggregation 171 on overlap. 172 </p> 173</div> 174<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 175<td align="left"></td> 176<td align="right"><div class="copyright-footer">Copyright © 2007-2010 Joachim 177 Faulhaber<br>Copyright © 1999-2006 Cortex Software 178 GmbH<p> 179 Distributed under the Boost Software License, Version 1.0. (See accompanying 180 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>) 181 </p> 182</div></td> 183</tr></table> 184<hr> 185<div class="spirit-nav"> 186<a accesskey="p" href="overlap_counter.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_tallest_guests.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 187</div> 188</body> 189</html> 190