icl/doc/html/boost_icl/examples/partys_height_average.html
Joachim Faulhaber 4a04618b4e Merged revision(s) 78975-79009, 79180-79219 from trunk/libs/icl:
Fix for ticket #6924: Now using insert with hint.
........
Fixed ticket #6924 (reporter: John Reid). Improvements of icl/functors.hpp.
........
Documentation tweaks. Ticket #6967, fixed broken links.
........
Added doxygen generated html to complete broken links.
........


[SVN r79220]
2012-07-01 23:35:44 +00:00

193 lines
22 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Party's height average</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.74.0">
<link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Boost.Icl">
<link rel="up" href="../examples.html" title="Examples">
<link rel="prev" href="overlap_counter.html" title="Overlap counter">
<link rel="next" href="partys_tallest_guests.html" title="Party's tallest guests">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
<div class="section boost_icl_examples_partys_height_average" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<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
average</a>
</h3></div></div></div>
<p>
In the example <code class="computeroutput"><span class="identifier">partys_height_average</span><span class="special">.</span><span class="identifier">cpp</span></code> we
compute yet another aggregation: The average height of guests. This is done
by defining a <code class="computeroutput"><span class="keyword">class</span> <span class="identifier">counted_sum</span></code>
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>.
</p>
<p>
Based on the <code class="computeroutput"><span class="keyword">operator</span> <span class="special">+=</span></code>
we can aggregate counted sums on addition of interval value pairs into an
interval_map.
</p>
<p>
</p>
<p>
</p>
<pre class="programlisting"><span class="comment">// The next line includes &lt;boost/date_time/posix_time/posix_time.hpp&gt;
</span><span class="comment">// and a few lines of adapter code.
</span><span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">std</span><span class="special">;</span>
<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>
<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>
<span class="keyword">class</span> <span class="identifier">counted_sum</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
<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>
<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>
<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>
<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>
<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">&lt;</span><span class="keyword">double</span><span class="special">&gt;(</span><span class="identifier">_count</span><span class="special">);</span> <span class="special">}</span>
<span class="identifier">counted_sum</span><span class="special">&amp;</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">&amp;</span> <span class="identifier">right</span><span class="special">)</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="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>
<span class="keyword">private</span><span class="special">:</span>
<span class="keyword">int</span> <span class="identifier">_sum</span><span class="special">;</span>
<span class="keyword">int</span> <span class="identifier">_count</span><span class="special">;</span>
<span class="special">};</span>
<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">&amp;</span> <span class="identifier">left</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">counted_sum</span><span class="special">&amp;</span> <span class="identifier">right</span><span class="special">)</span>
<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">&amp;&amp;</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>
<span class="keyword">void</span> <span class="identifier">partys_height_average</span><span class="special">()</span>
<span class="special">{</span>
<span class="identifier">interval_map</span><span class="special">&lt;</span><span class="identifier">ptime</span><span class="special">,</span> <span class="identifier">counted_sum</span><span class="special">&gt;</span> <span class="identifier">height_sums</span><span class="special">;</span>
<span class="identifier">height_sums</span> <span class="special">+=</span>
<span class="identifier">make_pair</span><span class="special">(</span>
<span class="identifier">discrete_interval</span><span class="special">&lt;</span><span class="identifier">ptime</span><span class="special">&gt;::</span><span class="identifier">right_open</span><span class="special">(</span>
<span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 19:30"</span><span class="special">),</span>
<span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 23:00"</span><span class="special">)),</span>
<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>
<span class="identifier">height_sums</span> <span class="special">+=</span>
<span class="identifier">make_pair</span><span class="special">(</span>
<span class="identifier">discrete_interval</span><span class="special">&lt;</span><span class="identifier">ptime</span><span class="special">&gt;::</span><span class="identifier">right_open</span><span class="special">(</span>
<span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 19:30"</span><span class="special">),</span>
<span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 23:00"</span><span class="special">)),</span>
<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>
<span class="identifier">height_sums</span> <span class="special">+=</span>
<span class="identifier">make_pair</span><span class="special">(</span>
<span class="identifier">discrete_interval</span><span class="special">&lt;</span><span class="identifier">ptime</span><span class="special">&gt;::</span><span class="identifier">right_open</span><span class="special">(</span>
<span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 20:10"</span><span class="special">),</span>
<span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-21 00:00"</span><span class="special">)),</span>
<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>
<span class="identifier">height_sums</span> <span class="special">+=</span>
<span class="identifier">make_pair</span><span class="special">(</span>
<span class="identifier">discrete_interval</span><span class="special">&lt;</span><span class="identifier">ptime</span><span class="special">&gt;::</span><span class="identifier">right_open</span><span class="special">(</span>
<span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 20:10"</span><span class="special">),</span>
<span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-21 00:00"</span><span class="special">)),</span>
<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>
<span class="identifier">height_sums</span> <span class="special">+=</span>
<span class="identifier">make_pair</span><span class="special">(</span>
<span class="identifier">discrete_interval</span><span class="special">&lt;</span><span class="identifier">ptime</span><span class="special">&gt;::</span><span class="identifier">right_open</span><span class="special">(</span>
<span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 22:15"</span><span class="special">),</span>
<span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-21 00:30"</span><span class="special">)),</span>
<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>
<span class="identifier">interval_map</span><span class="special">&lt;</span><span class="identifier">ptime</span><span class="special">,</span> <span class="identifier">counted_sum</span><span class="special">&gt;::</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>
<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"-------------- History of average guest height -------------------\n"</span><span class="special">;</span>
<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>
<span class="special">{</span>
<span class="identifier">discrete_interval</span><span class="special">&lt;</span><span class="identifier">ptime</span><span class="special">&gt;</span> <span class="identifier">when</span> <span class="special">=</span> <span class="identifier">height_sum_</span><span class="special">-&gt;</span><span class="identifier">first</span><span class="special">;</span>
<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>
<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">setprecision</span><span class="special">(</span><span class="number">3</span><span class="special">)</span>
<span class="special">&lt;&lt;</span> <span class="string">"["</span> <span class="special">&lt;&lt;</span> <span class="identifier">first</span><span class="special">(</span><span class="identifier">when</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">" - "</span> <span class="special">&lt;&lt;</span> <span class="identifier">upper</span><span class="special">(</span><span class="identifier">when</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">")"</span>
<span class="special">&lt;&lt;</span> <span class="string">": "</span> <span class="special">&lt;&lt;</span> <span class="identifier">height_average</span> <span class="special">&lt;&lt;</span><span class="string">" cm = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">height_average</span><span class="special">/</span><span class="number">30.48</span> <span class="special">&lt;&lt;</span> <span class="string">" ft"</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
<span class="special">}</span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"&gt;&gt;Interval Container Library: Sample partys_height_average.cpp &lt;&lt;\n"</span><span class="special">;</span>
<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"------------------------------------------------------------------\n"</span><span class="special">;</span>
<span class="identifier">partys_height_average</span><span class="special">();</span>
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
<span class="comment">// Program output:
</span><span class="comment">/*-----------------------------------------------------------------------------
&gt;&gt;Interval Container Library: Sample partys_height_average.cpp &lt;&lt;
------------------------------------------------------------------
-------------- History of average guest height -------------------
[2008-May-20 19:30:00 - 2008-May-20 20:10:00): 173 cm = 5.66 ft
[2008-May-20 20:10:00 - 2008-May-20 22:15:00): 170 cm = 5.58 ft
[2008-May-20 22:15:00 - 2008-May-20 23:00:00): 176 cm = 5.77 ft
[2008-May-20 23:00:00 - 2008-May-21 00:00:00): 178 cm = 5.85 ft
[2008-May-21 00:00:00 - 2008-May-21 00:30:00): 200 cm = 6.56 ft
-----------------------------------------------------------------------------*/</span>
</pre>
<p>
</p>
<p>
</p>
<p>
Required for <code class="computeroutput"><span class="keyword">class</span> <span class="identifier">counted_sum</span></code>
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>
<span class="special">==</span></code> to test equality. To enable additive
aggregation on overlap also an <code class="computeroutput"><span class="keyword">operator</span>
<span class="special">+=</span></code> is needed.
</p>
<p>
Note that no <code class="computeroutput"><span class="keyword">operator</span> <span class="special">-=</span></code>
for a subtraction of <code class="computeroutput"><span class="identifier">counted_sum</span></code>
values is defined. So you can only add to the <code class="computeroutput"><span class="identifier">interval_map</span><span class="special">&lt;</span><span class="identifier">ptime</span><span class="special">,</span> <span class="identifier">counted_sum</span><span class="special">&gt;</span></code> but not subtract from it.
</p>
<p>
In many real world applications only addition is needed and user defined
classes will work fine, if they only implement <code class="computeroutput"><span class="keyword">operator</span>
<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>
or <code class="computeroutput"><span class="special">-</span></code> is called on the interval_map,
the user defined class has to implement it's own <code class="computeroutput"><span class="keyword">operator</span>
<span class="special">-=</span></code> to provide the subtractive aggregation
on overlap.
</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2007 -2010 Joachim Faulhaber<br>Copyright &#169; 1999 -2006 Cortex Software GmbH<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
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>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
</body>
</html>