4a04618b4e
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]
200 lines
22 KiB
HTML
200 lines
22 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Party's tallest guests</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 1. Boost.Icl">
|
|
<link rel="up" href="../examples.html" title="Examples">
|
|
<link rel="prev" href="partys_height_average.html" title="Party's height average">
|
|
<link rel="next" href="time_grids.html" title="Time grids for months and weeks">
|
|
</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="partys_height_average.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="time_grids.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
<div class="section boost_icl_examples_partys_tallest_guests" lang="en">
|
|
<div class="titlepage"><div><div><h3 class="title">
|
|
<a name="boost_icl.examples.partys_tallest_guests"></a><a class="link" href="partys_tallest_guests.html" title="Party's tallest guests"> Party's tallest
|
|
guests</a>
|
|
</h3></div></div></div>
|
|
<p>
|
|
Defining <code class="computeroutput"><span class="keyword">operator</span> <span class="special">+=</span></code>
|
|
(and <code class="computeroutput"><span class="special">-=</span></code>) is probably the most
|
|
important method to implement arbitrary kinds of user defined aggregations.
|
|
An alternative way to choose a desired aggregation is to instantiate an interval_map
|
|
class template with an appropriate <span class="emphasis"><em><span class="bold"><strong>aggregation
|
|
functor</strong></span></em></span>. For the most common kinds of aggregation the
|
|
<span class="bold"><strong>icl</strong></span> provides such functors as shown in the
|
|
example.
|
|
</p>
|
|
<p>
|
|
Example <code class="computeroutput"><span class="identifier">partys_tallest_guests</span><span class="special">.</span><span class="identifier">cpp</span></code> also
|
|
demonstrates the difference between an <code class="computeroutput"><a class="link" href="../../boost/icl/interval_map.html" title="Class template interval_map">interval_map</a></code>
|
|
that joins intervals for equal associated values and a <code class="computeroutput"><a class="link" href="../../boost/icl/split_interval_map.html" title="Class template split_interval_map">split_interval_map</a></code>
|
|
that preserves all borders of inserted intervals.
|
|
</p>
|
|
<p>
|
|
</p>
|
|
<p>
|
|
|
|
</p>
|
|
<pre class="programlisting"><span class="comment">// The next line includes <boost/date_time/posix_time/posix_time.hpp>
|
|
</span><span class="comment">// and a few lines of adapter code.
|
|
</span><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>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span>
|
|
<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>
|
|
<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>
|
|
|
|
<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="comment">// A party's height shall be defined as the maximum height of all guests ;-)
|
|
</span><span class="comment">// The last parameter 'inplace_max' is a functor template that calls a max
|
|
</span><span class="comment">// aggregation on overlap.
|
|
</span><span class="keyword">typedef</span> <span class="identifier">interval_map</span><span class="special"><</span><span class="identifier">ptime</span><span class="special">,</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">partial_absorber</span><span class="special">,</span> <span class="identifier">less</span><span class="special">,</span> <span class="identifier">inplace_max</span><span class="special">></span>
|
|
<span class="identifier">PartyHeightHistoryT</span><span class="special">;</span>
|
|
|
|
<span class="comment">// Using a split_interval_map we preserve interval splittings that occurred via insertion.
|
|
</span><span class="keyword">typedef</span> <span class="identifier">split_interval_map</span><span class="special"><</span><span class="identifier">ptime</span><span class="special">,</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">partial_absorber</span><span class="special">,</span> <span class="identifier">less</span><span class="special">,</span> <span class="identifier">inplace_max</span><span class="special">></span>
|
|
<span class="identifier">PartyHeightSplitHistoryT</span><span class="special">;</span>
|
|
|
|
<span class="keyword">void</span> <span class="identifier">partys_height</span><span class="special">()</span>
|
|
<span class="special">{</span>
|
|
<span class="identifier">PartyHeightHistoryT</span> <span class="identifier">tallest_guest</span><span class="special">;</span>
|
|
|
|
<span class="identifier">tallest_guest</span> <span class="special">+=</span>
|
|
<span class="identifier">make_pair</span><span class="special">(</span>
|
|
<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>
|
|
<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="number">180</span><span class="special">);</span> <span class="comment">// Mary & Harry: Harry is 1,80 m tall.
|
|
</span>
|
|
<span class="identifier">tallest_guest</span> <span class="special">+=</span>
|
|
<span class="identifier">make_pair</span><span class="special">(</span>
|
|
<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>
|
|
<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="number">170</span><span class="special">);</span> <span class="comment">// Diana & Susan: Diana is 1,70 m tall.
|
|
</span>
|
|
<span class="identifier">tallest_guest</span> <span class="special">+=</span>
|
|
<span class="identifier">make_pair</span><span class="special">(</span>
|
|
<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>
|
|
<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="number">200</span><span class="special">);</span> <span class="comment">// Peters height is 2,00 m
|
|
</span>
|
|
<span class="identifier">PartyHeightHistoryT</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">height_</span> <span class="special">=</span> <span class="identifier">tallest_guest</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
|
|
<span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"-------------- History of maximum guest height -------------------\n"</span><span class="special">;</span>
|
|
<span class="keyword">while</span><span class="special">(</span><span class="identifier">height_</span> <span class="special">!=</span> <span class="identifier">tallest_guest</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"><</span><span class="identifier">ptime</span><span class="special">></span> <span class="identifier">when</span> <span class="special">=</span> <span class="identifier">height_</span><span class="special">-></span><span class="identifier">first</span><span class="special">;</span>
|
|
<span class="comment">// Of what height are the tallest guests within the time interval 'when' ?
|
|
</span> <span class="keyword">int</span> <span class="identifier">height</span> <span class="special">=</span> <span class="special">(*</span><span class="identifier">height_</span><span class="special">++).</span><span class="identifier">second</span><span class="special">;</span>
|
|
<span class="identifier">cout</span> <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>
|
|
<span class="special"><<</span> <span class="string">": "</span> <span class="special"><<</span> <span class="identifier">height</span> <span class="special"><<</span><span class="string">" cm = "</span> <span class="special"><<</span> <span class="identifier">height</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>
|
|
<span class="special">}</span>
|
|
|
|
<span class="special">}</span>
|
|
|
|
<span class="comment">// Next we are using a split_interval_map instead of a joining interval_map
|
|
</span><span class="keyword">void</span> <span class="identifier">partys_split_height</span><span class="special">()</span>
|
|
<span class="special">{</span>
|
|
<span class="identifier">PartyHeightSplitHistoryT</span> <span class="identifier">tallest_guest</span><span class="special">;</span>
|
|
|
|
<span class="comment">// adding an element can be done wrt. simple aggregate functions
|
|
</span> <span class="comment">// like e.g. min, max etc. in their 'inplace' or op= incarnation
|
|
</span> <span class="identifier">tallest_guest</span> <span class="special">+=</span>
|
|
<span class="identifier">make_pair</span><span class="special">(</span>
|
|
<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>
|
|
<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="number">180</span><span class="special">);</span> <span class="comment">// Mary & Harry: Harry is 1,80 m tall.
|
|
</span>
|
|
<span class="identifier">tallest_guest</span> <span class="special">+=</span>
|
|
<span class="identifier">make_pair</span><span class="special">(</span>
|
|
<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>
|
|
<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="number">170</span><span class="special">);</span> <span class="comment">// Diana & Susan: Diana is 1,70 m tall.
|
|
</span>
|
|
<span class="identifier">tallest_guest</span> <span class="special">+=</span>
|
|
<span class="identifier">make_pair</span><span class="special">(</span>
|
|
<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>
|
|
<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="number">200</span><span class="special">);</span> <span class="comment">// Peters height is 2,00 m
|
|
</span>
|
|
<span class="identifier">PartyHeightSplitHistoryT</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">height_</span> <span class="special">=</span> <span class="identifier">tallest_guest</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
|
|
<span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span>
|
|
<span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"-------- Split History of maximum guest height -------------------\n"</span><span class="special">;</span>
|
|
<span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"--- Same map as above but split for every interval insertion. ---\n"</span><span class="special">;</span>
|
|
<span class="keyword">while</span><span class="special">(</span><span class="identifier">height_</span> <span class="special">!=</span> <span class="identifier">tallest_guest</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"><</span><span class="identifier">ptime</span><span class="special">></span> <span class="identifier">when</span> <span class="special">=</span> <span class="identifier">height_</span><span class="special">-></span><span class="identifier">first</span><span class="special">;</span>
|
|
<span class="comment">// Of what height are the tallest guests within the time interval 'when' ?
|
|
</span> <span class="keyword">int</span> <span class="identifier">height</span> <span class="special">=</span> <span class="special">(*</span><span class="identifier">height_</span><span class="special">++).</span><span class="identifier">second</span><span class="special">;</span>
|
|
<span class="identifier">cout</span> <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>
|
|
<span class="special"><<</span> <span class="string">": "</span> <span class="special"><<</span> <span class="identifier">height</span> <span class="special"><<</span><span class="string">" cm = "</span> <span class="special"><<</span> <span class="identifier">height</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>
|
|
<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"><<</span> <span class="string">">>Interval Container Library: Sample partys_tallest_guests.cpp <<\n"</span><span class="special">;</span>
|
|
<span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"------------------------------------------------------------------\n"</span><span class="special">;</span>
|
|
<span class="identifier">partys_height</span><span class="special">();</span>
|
|
<span class="identifier">partys_split_height</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">/*-----------------------------------------------------------------------------
|
|
>>Interval Container Library: Sample partys_tallest_guests.cpp <<
|
|
------------------------------------------------------------------
|
|
-------------- History of maximum guest height -------------------
|
|
[2008-May-20 19:30:00 - 2008-May-20 22:15:00): 180 cm = 5.90551 ft
|
|
[2008-May-20 22:15:00 - 2008-May-21 00:30:00): 200 cm = 6.56168 ft
|
|
|
|
-------- Split History of maximum guest height -------------------
|
|
--- Same map as above but split for every interval insertion. ---
|
|
[2008-May-20 19:30:00 - 2008-May-20 20:10:00): 180 cm = 5.90551 ft
|
|
[2008-May-20 20:10:00 - 2008-May-20 22:15:00): 180 cm = 5.90551 ft
|
|
[2008-May-20 22:15:00 - 2008-May-20 23:00:00): 200 cm = 6.56168 ft
|
|
[2008-May-20 23:00:00 - 2008-May-21 00:00:00): 200 cm = 6.56168 ft
|
|
[2008-May-21 00:00:00 - 2008-May-21 00:30:00): 200 cm = 6.56168 ft
|
|
-----------------------------------------------------------------------------*/</span>
|
|
</pre>
|
|
<p>
|
|
</p>
|
|
<p>
|
|
</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 © 2007 -2010 Joachim Faulhaber<br>Copyright © 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="partys_height_average.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="time_grids.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|