• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_height_average.cpp \file partys_height_average.cpp
13     \brief Using <i>aggregate on overlap</i> a history of height averages of
14            party guests is computed.
15 
16     In partys_height_average.cpp we compute yet another aggregation:
17     The average height of guests as it changes over time. This is done by
18     defining a class counted_sum that sums up heights and counts the number
19     of guests via an operator +=.
20 
21     Based on the operator += we can aggregate counted sums on addition
22     of interval value pairs into an interval_map.
23 
24     \include partys_height_average_/partys_height_average.cpp
25 */
26 //[example_partys_height_average
27 // The next line includes <boost/date_time/posix_time/posix_time.hpp>
28 // and a few lines of adapter code.
29 #include <boost/icl/ptime.hpp>
30 #include <iostream>
31 #include <boost/icl/interval_map.hpp>
32 #include <boost/icl/split_interval_map.hpp>
33 
34 using namespace std;
35 using namespace boost::posix_time;
36 using namespace boost::icl;
37 
38 
39 class counted_sum
40 {
41 public:
counted_sum()42     counted_sum():_sum(0),_count(0){}
counted_sum(int sum)43     counted_sum(int sum):_sum(sum),_count(1){}
44 
sum() const45     int sum()const  {return _sum;}
count() const46     int count()const{return _count;}
average() const47     double average()const{ return _count==0 ? 0.0 : _sum/static_cast<double>(_count); }
48 
operator +=(const counted_sum & right)49     counted_sum& operator += (const counted_sum& right)
50     { _sum += right.sum(); _count += right.count(); return *this; }
51 
52 private:
53     int _sum;
54     int _count;
55 };
56 
operator ==(const counted_sum & left,const counted_sum & right)57 bool operator == (const counted_sum& left, const counted_sum& right)
58 { return left.sum()==right.sum() && left.count()==right.count(); }
59 
60 
partys_height_average()61 void partys_height_average()
62 {
63     interval_map<ptime, counted_sum> height_sums;
64 
65     height_sums +=
66       make_pair(
67         discrete_interval<ptime>::right_open(
68           time_from_string("2008-05-20 19:30"),
69           time_from_string("2008-05-20 23:00")),
70         counted_sum(165)); // Mary is 1,65 m tall.
71 
72     height_sums +=
73       make_pair(
74         discrete_interval<ptime>::right_open(
75           time_from_string("2008-05-20 19:30"),
76           time_from_string("2008-05-20 23:00")),
77         counted_sum(180)); // Harry is 1,80 m tall.
78 
79     height_sums +=
80       make_pair(
81         discrete_interval<ptime>::right_open(
82           time_from_string("2008-05-20 20:10"),
83           time_from_string("2008-05-21 00:00")),
84         counted_sum(170)); // Diana is 1,70 m tall.
85 
86     height_sums +=
87       make_pair(
88         discrete_interval<ptime>::right_open(
89           time_from_string("2008-05-20 20:10"),
90           time_from_string("2008-05-21 00:00")),
91         counted_sum(165)); // Susan is 1,65 m tall.
92 
93     height_sums +=
94       make_pair(
95         discrete_interval<ptime>::right_open(
96           time_from_string("2008-05-20 22:15"),
97           time_from_string("2008-05-21 00:30")),
98         counted_sum(200)); // Peters height is 2,00 m
99 
100     interval_map<ptime, counted_sum>::iterator height_sum_ = height_sums.begin();
101     cout << "-------------- History of average guest height -------------------\n";
102     while(height_sum_ != height_sums.end())
103     {
104         discrete_interval<ptime> when = height_sum_->first;
105 
106         double height_average = (*height_sum_++).second.average();
107         cout << setprecision(3)
108              << "[" << first(when) << " - " << upper(when) << ")"
109              << ": " << height_average <<" cm = " << height_average/30.48 << " ft" << endl;
110     }
111 }
112 
113 
main()114 int main()
115 {
116     cout << ">>Interval Container Library: Sample partys_height_average.cpp  <<\n";
117     cout << "------------------------------------------------------------------\n";
118     partys_height_average();
119     return 0;
120 }
121 
122 // Program output:
123 /*-----------------------------------------------------------------------------
124 >>Interval Container Library: Sample partys_height_average.cpp  <<
125 ------------------------------------------------------------------
126 -------------- History of average guest height -------------------
127 [2008-May-20 19:30:00 - 2008-May-20 20:10:00): 173 cm = 5.66 ft
128 [2008-May-20 20:10:00 - 2008-May-20 22:15:00): 170 cm = 5.58 ft
129 [2008-May-20 22:15:00 - 2008-May-20 23:00:00): 176 cm = 5.77 ft
130 [2008-May-20 23:00:00 - 2008-May-21 00:00:00): 178 cm = 5.85 ft
131 [2008-May-21 00:00:00 - 2008-May-21 00:30:00): 200 cm = 6.56 ft
132 -----------------------------------------------------------------------------*/
133 //]
134 
135