1 /*-----------------------------------------------------------------------------+
2 Interval Container Library
3 Author: Joachim Faulhaber
4 Copyright (c) 2007-2009: Joachim Faulhaber
5 Copyright (c) 1999-2006: Cortex Software GmbH, Kantstrasse 57, Berlin
6 +------------------------------------------------------------------------------+
7    Distributed under the Boost Software License, Version 1.0.
8       (See accompanying file LICENCE.txt or copy at
9            http://www.boost.org/LICENSE_1_0.txt)
10 +-----------------------------------------------------------------------------*/
11 
12 /** Example partys_tallest_guests.cpp \file partys_tallest_guests.cpp
13     \brief Using <i>aggregate on overlap</i> the heights of the party's tallest
14            guests are computed.
15 
16     In partys_tallest_guests.cpp we use a different instantiation of
17     interval map templates to compute maxima of guest heights.
18 
19     Instead of aggregating groups of people attending the party in time
20     we aggregate the maximum of guest height for the time intervals.
21 
22     Using a joining interval_map results in a smaller map: All interval
23     value pairs are joined if the maximum does not change in time. Using
24     a split_interval_map results in a larger map: All splits of intervals
25     that occur due to entering and leaving of guests are preserved in
26     the split_interval_map.
27 
28     \include partys_tallest_guests_/partys_tallest_guests.cpp
29 */
30 //[example_partys_tallest_guests
31 // The next line includes <boost/date_time/posix_time/posix_time.hpp>
32 // and a few lines of adapter code.
33 #include <boost/icl/ptime.hpp>
34 #include <iostream>
35 #include <boost/icl/interval_map.hpp>
36 #include <boost/icl/split_interval_map.hpp>
37 
38 using namespace std;
39 using namespace boost::posix_time;
40 using namespace boost::icl;
41 
42 
43 // A party's height shall be defined as the maximum height of all guests ;-)
44 // The last parameter 'inplace_max' is a functor template that calls a max
45 // aggregation on overlap.
46 typedef interval_map<ptime, int, partial_absorber, less, inplace_max>
47     PartyHeightHistoryT;
48 
49 // Using a split_interval_map we preserve interval splittings that occurred via insertion.
50 typedef split_interval_map<ptime, int, partial_absorber, less, inplace_max>
51     PartyHeightSplitHistoryT;
52 
partys_height()53 void partys_height()
54 {
55     PartyHeightHistoryT tallest_guest;
56 
57     tallest_guest +=
58       make_pair(
59         discrete_interval<ptime>::right_open(
60           time_from_string("2008-05-20 19:30"),
61           time_from_string("2008-05-20 23:00")),
62         180); // Mary & Harry: Harry is 1,80 m tall.
63 
64     tallest_guest +=
65       make_pair(
66         discrete_interval<ptime>::right_open(
67           time_from_string("2008-05-20 20:10"),
68           time_from_string("2008-05-21 00:00")),
69         170); // Diana & Susan: Diana is 1,70 m tall.
70 
71     tallest_guest +=
72       make_pair(
73         discrete_interval<ptime>::right_open(
74           time_from_string("2008-05-20 22:15"),
75           time_from_string("2008-05-21 00:30")),
76         200); // Peters height is 2,00 m
77 
78     PartyHeightHistoryT::iterator height_ = tallest_guest.begin();
79     cout << "-------------- History of maximum guest height -------------------\n";
80     while(height_ != tallest_guest.end())
81     {
82         discrete_interval<ptime> when = height_->first;
83         // Of what height are the tallest guests within the time interval 'when' ?
84         int height = (*height_++).second;
85         cout << "[" << first(when) << " - " << upper(when) << ")"
86              << ": " << height <<" cm = " << height/30.48 << " ft" << endl;
87     }
88 
89 }
90 
91 // Next we are using a split_interval_map instead of a joining interval_map
partys_split_height()92 void partys_split_height()
93 {
94     PartyHeightSplitHistoryT tallest_guest;
95 
96     // adding an element can be done wrt. simple aggregate functions
97     // like e.g. min, max etc. in their 'inplace' or op= incarnation
98     tallest_guest +=
99       make_pair(
100         discrete_interval<ptime>::right_open(
101           time_from_string("2008-05-20 19:30"),
102           time_from_string("2008-05-20 23:00")),
103         180); // Mary & Harry: Harry is 1,80 m tall.
104 
105     tallest_guest +=
106       make_pair(
107         discrete_interval<ptime>::right_open(
108           time_from_string("2008-05-20 20:10"),
109           time_from_string("2008-05-21 00:00")),
110         170); // Diana & Susan: Diana is 1,70 m tall.
111 
112     tallest_guest +=
113       make_pair(
114         discrete_interval<ptime>::right_open(
115           time_from_string("2008-05-20 22:15"),
116           time_from_string("2008-05-21 00:30")),
117         200); // Peters height is 2,00 m
118 
119     PartyHeightSplitHistoryT::iterator height_ = tallest_guest.begin();
120     cout << "\n";
121     cout << "-------- Split History of maximum guest height -------------------\n";
122     cout << "--- Same map as above but split for every interval insertion.  ---\n";
123     while(height_ != tallest_guest.end())
124     {
125         discrete_interval<ptime> when = height_->first;
126         // Of what height are the tallest guests within the time interval 'when' ?
127         int height = (*height_++).second;
128         cout << "[" << first(when) << " - " << upper(when) << ")"
129              << ": " << height <<" cm = " << height/30.48 << " ft" << endl;
130     }
131 
132 }
133 
134 
main()135 int main()
136 {
137     cout << ">>Interval Container Library: Sample partys_tallest_guests.cpp  <<\n";
138     cout << "------------------------------------------------------------------\n";
139     partys_height();
140     partys_split_height();
141     return 0;
142 }
143 
144 // Program output:
145 /*-----------------------------------------------------------------------------
146 >>Interval Container Library: Sample partys_tallest_guests.cpp  <<
147 ------------------------------------------------------------------
148 -------------- History of maximum guest height -------------------
149 [2008-May-20 19:30:00 - 2008-May-20 22:15:00): 180 cm = 5.90551 ft
150 [2008-May-20 22:15:00 - 2008-May-21 00:30:00): 200 cm = 6.56168 ft
151 
152 -------- Split History of maximum guest height -------------------
153 --- Same map as above but split for every interval insertion.  ---
154 [2008-May-20 19:30:00 - 2008-May-20 20:10:00): 180 cm = 5.90551 ft
155 [2008-May-20 20:10:00 - 2008-May-20 22:15:00): 180 cm = 5.90551 ft
156 [2008-May-20 22:15:00 - 2008-May-20 23:00:00): 200 cm = 6.56168 ft
157 [2008-May-20 23:00:00 - 2008-May-21 00:00:00): 200 cm = 6.56168 ft
158 [2008-May-21 00:00:00 - 2008-May-21 00:30:00): 200 cm = 6.56168 ft
159 -----------------------------------------------------------------------------*/
160 //]
161 
162