• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Party's tallest guests</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="partys_height_average.html" title="Party's height average">
10<link rel="next" href="time_grids.html" title="Time grids for months and weeks">
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="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>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<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
28      guests</a>
29</h3></div></div></div>
30<p>
31        Defining <code class="computeroutput"><span class="keyword">operator</span> <span class="special">+=</span></code>
32        (and <code class="computeroutput"><span class="special">-=</span></code>) is probably the most
33        important method to implement arbitrary kinds of user defined aggregations.
34        An alternative way to choose a desired aggregation is to instantiate an interval_map
35        class template with an appropriate <span class="emphasis"><em><span class="bold"><strong>aggregation
36        functor</strong></span></em></span>. For the most common kinds of aggregation the
37        <span class="bold"><strong>icl</strong></span> provides such functors as shown in the
38        example.
39      </p>
40<p>
41        Example <code class="computeroutput"><span class="identifier">partys_tallest_guests</span><span class="special">.</span><span class="identifier">cpp</span></code> also
42        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>
43        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>
44        that preserves all borders of inserted intervals.
45      </p>
46<p>
47</p>
48<pre class="programlisting"><span class="comment">// The next line includes &lt;boost/date_time/posix_time/posix_time.hpp&gt;</span>
49<span class="comment">// and a few lines of adapter code.</span>
50<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>
51<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
52<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>
53<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>
54
55<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">std</span><span class="special">;</span>
56<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>
57<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>
58
59
60<span class="comment">// A party's height shall be defined as the maximum height of all guests ;-)</span>
61<span class="comment">// The last parameter 'inplace_max' is a functor template that calls a max </span>
62<span class="comment">// aggregation on overlap.</span>
63<span class="keyword">typedef</span> <span class="identifier">interval_map</span><span class="special">&lt;</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">&gt;</span>
64    <span class="identifier">PartyHeightHistoryT</span><span class="special">;</span>
65
66<span class="comment">// Using a split_interval_map we preserve interval splittings that occurred via insertion.</span>
67<span class="keyword">typedef</span> <span class="identifier">split_interval_map</span><span class="special">&lt;</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">&gt;</span>
68    <span class="identifier">PartyHeightSplitHistoryT</span><span class="special">;</span>
69
70<span class="keyword">void</span> <span class="identifier">partys_height</span><span class="special">()</span>
71<span class="special">{</span>
72    <span class="identifier">PartyHeightHistoryT</span> <span class="identifier">tallest_guest</span><span class="special">;</span>
73
74    <span class="identifier">tallest_guest</span> <span class="special">+=</span>
75      <span class="identifier">make_pair</span><span class="special">(</span>
76        <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>
77          <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 19:30"</span><span class="special">),</span>
78          <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 23:00"</span><span class="special">)),</span>
79        <span class="number">180</span><span class="special">);</span> <span class="comment">// Mary &amp; Harry: Harry is 1,80 m tall.</span>
80
81    <span class="identifier">tallest_guest</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">&lt;</span><span class="identifier">ptime</span><span class="special">&gt;::</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 20:10"</span><span class="special">),</span>
85          <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-21 00:00"</span><span class="special">)),</span>
86        <span class="number">170</span><span class="special">);</span> <span class="comment">// Diana &amp; Susan: Diana is 1,70 m tall.</span>
87
88    <span class="identifier">tallest_guest</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">&lt;</span><span class="identifier">ptime</span><span class="special">&gt;::</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 22:15"</span><span class="special">),</span>
92          <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-21 00:30"</span><span class="special">)),</span>
93        <span class="number">200</span><span class="special">);</span> <span class="comment">// Peters height is 2,00 m</span>
94
95    <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>
96    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"-------------- History of maximum guest height -------------------\n"</span><span class="special">;</span>
97    <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>
98    <span class="special">{</span>
99        <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_</span><span class="special">-&gt;</span><span class="identifier">first</span><span class="special">;</span>
100        <span class="comment">// Of what height are the tallest guests within the time interval 'when' ?</span>
101        <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>
102        <span class="identifier">cout</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>
103             <span class="special">&lt;&lt;</span> <span class="string">": "</span> <span class="special">&lt;&lt;</span> <span class="identifier">height</span> <span class="special">&lt;&lt;</span><span class="string">" cm = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">height</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>
104    <span class="special">}</span>
105
106<span class="special">}</span>
107
108<span class="comment">// Next we are using a split_interval_map instead of a joining interval_map</span>
109<span class="keyword">void</span> <span class="identifier">partys_split_height</span><span class="special">()</span>
110<span class="special">{</span>
111    <span class="identifier">PartyHeightSplitHistoryT</span> <span class="identifier">tallest_guest</span><span class="special">;</span>
112
113    <span class="comment">// adding an element can be done wrt. simple aggregate functions</span>
114    <span class="comment">// like e.g. min, max etc. in their 'inplace' or op= incarnation</span>
115    <span class="identifier">tallest_guest</span> <span class="special">+=</span>
116      <span class="identifier">make_pair</span><span class="special">(</span>
117        <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>
118          <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 19:30"</span><span class="special">),</span>
119          <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 23:00"</span><span class="special">)),</span>
120        <span class="number">180</span><span class="special">);</span> <span class="comment">// Mary &amp; Harry: Harry is 1,80 m tall.</span>
121
122    <span class="identifier">tallest_guest</span> <span class="special">+=</span>
123      <span class="identifier">make_pair</span><span class="special">(</span>
124        <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>
125          <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 20:10"</span><span class="special">),</span>
126          <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-21 00:00"</span><span class="special">)),</span>
127        <span class="number">170</span><span class="special">);</span> <span class="comment">// Diana &amp; Susan: Diana is 1,70 m tall.</span>
128
129    <span class="identifier">tallest_guest</span> <span class="special">+=</span>
130      <span class="identifier">make_pair</span><span class="special">(</span>
131        <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>
132          <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-20 22:15"</span><span class="special">),</span>
133          <span class="identifier">time_from_string</span><span class="special">(</span><span class="string">"2008-05-21 00:30"</span><span class="special">)),</span>
134        <span class="number">200</span><span class="special">);</span> <span class="comment">// Peters height is 2,00 m</span>
135
136    <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>
137    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"\n"</span><span class="special">;</span>
138    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"-------- Split History of maximum guest height -------------------\n"</span><span class="special">;</span>
139    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"--- Same map as above but split for every interval insertion.  ---\n"</span><span class="special">;</span>
140    <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>
141    <span class="special">{</span>
142        <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_</span><span class="special">-&gt;</span><span class="identifier">first</span><span class="special">;</span>
143        <span class="comment">// Of what height are the tallest guests within the time interval 'when' ?</span>
144        <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>
145        <span class="identifier">cout</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>
146             <span class="special">&lt;&lt;</span> <span class="string">": "</span> <span class="special">&lt;&lt;</span> <span class="identifier">height</span> <span class="special">&lt;&lt;</span><span class="string">" cm = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">height</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>
147    <span class="special">}</span>
148
149<span class="special">}</span>
150
151
152<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
153<span class="special">{</span>
154    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"&gt;&gt;Interval Container Library: Sample partys_tallest_guests.cpp  &lt;&lt;\n"</span><span class="special">;</span>
155    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"------------------------------------------------------------------\n"</span><span class="special">;</span>
156    <span class="identifier">partys_height</span><span class="special">();</span>
157    <span class="identifier">partys_split_height</span><span class="special">();</span>
158    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
159<span class="special">}</span>
160
161<span class="comment">// Program output:</span>
162<span class="comment">/*-----------------------------------------------------------------------------
163&gt;&gt;Interval Container Library: Sample partys_tallest_guests.cpp  &lt;&lt;
164------------------------------------------------------------------
165-------------- History of maximum guest height -------------------
166[2008-May-20 19:30:00 - 2008-May-20 22:15:00): 180 cm = 5.90551 ft
167[2008-May-20 22:15:00 - 2008-May-21 00:30:00): 200 cm = 6.56168 ft
168
169-------- Split History of maximum guest height -------------------
170--- Same map as above but split for every interval insertion.  ---
171[2008-May-20 19:30:00 - 2008-May-20 20:10:00): 180 cm = 5.90551 ft
172[2008-May-20 20:10:00 - 2008-May-20 22:15:00): 180 cm = 5.90551 ft
173[2008-May-20 22:15:00 - 2008-May-20 23:00:00): 200 cm = 6.56168 ft
174[2008-May-20 23:00:00 - 2008-May-21 00:00:00): 200 cm = 6.56168 ft
175[2008-May-21 00:00:00 - 2008-May-21 00:30:00): 200 cm = 6.56168 ft
176-----------------------------------------------------------------------------*/</span>
177</pre>
178<p>
179      </p>
180</div>
181<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
182<td align="left"></td>
183<td align="right"><div class="copyright-footer">Copyright © 2007-2010 Joachim
184      Faulhaber<br>Copyright © 1999-2006 Cortex Software
185      GmbH<p>
186        Distributed under the Boost Software License, Version 1.0. (See accompanying
187        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>)
188      </p>
189</div></td>
190</tr></table>
191<hr>
192<div class="spirit-nav">
193<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>
194</div>
195</body>
196</html>
197