partys_height_average.cpp 4.8 KB

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