partys_tallest_guests.cpp 6.3 KB

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