boost_party.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 boost_party.cpp \file boost_party.cpp
  12. \brief Generates an attendance history of a party by inserting into an interval_map.
  13. Demonstrating <i>aggregate on overlap</i>.
  14. boost_party.cpp demonstrates the possibilities of an interval map
  15. (interval_map or split_interval_map). Boost::posix_time::ptime is used as time
  16. parameter. An interval_map maps intervals to a given content. In this case the
  17. content is a set of party guests represented by their name strings.
  18. As time goes by, groups of people join the party and leave later in the evening.
  19. So we add a time interval and a name set to the interval_map for the attendance
  20. of each group of people, that come together and leave together.
  21. On every overlap of intervals, the corresponding name sets are accumulated. At
  22. the points of overlap the intervals are split. The accumulation of content on
  23. overlap of intervals is done via an operator += that has to be implemented
  24. for the content parameter of the interval_map.
  25. Finally the interval_map contains the history of attendance and all points in
  26. time, where the group of party guests changed.
  27. boost_party.cpp demonstrates a principle that we call
  28. <b><em>aggregate on overlap</em></b>:
  29. On insertion a value associated to the interval is aggregated (added) to those
  30. values in the interval_map that overlap with the inserted value.
  31. There are two behavioral aspects to <b>aggregate on overlap</b>: a <em>decompositional
  32. behavior</em> and a <em>accumulative behavior</em>.
  33. The <em>decompositional behavior</em> splits up intervals on the time dimension of the
  34. interval_map so that the intervals change whenever associated values
  35. change.
  36. The <em>accumulative behavior</em> accumulates associated values on every overlap of
  37. an insertion for the associated values.
  38. \include boost_party_/boost_party.cpp
  39. */
  40. //[example_boost_party
  41. // The next line includes <boost/date_time/posix_time/posix_time.hpp>
  42. // and a few lines of adapter code.
  43. #include <boost/icl/ptime.hpp>
  44. #include <iostream>
  45. #include <boost/icl/interval_map.hpp>
  46. using namespace std;
  47. using namespace boost::posix_time;
  48. using namespace boost::icl;
  49. // Type set<string> collects the names of party guests. Since std::set is
  50. // a model of the itl's set concept, the concept provides an operator +=
  51. // that performs a set union on overlap of intervals.
  52. typedef std::set<string> GuestSetT;
  53. void boost_party()
  54. {
  55. GuestSetT mary_harry;
  56. mary_harry.insert("Mary");
  57. mary_harry.insert("Harry");
  58. GuestSetT diana_susan;
  59. diana_susan.insert("Diana");
  60. diana_susan.insert("Susan");
  61. GuestSetT peter;
  62. peter.insert("Peter");
  63. // A party is an interval map that maps time intervals to sets of guests
  64. interval_map<ptime, GuestSetT> party;
  65. party.add( // add and element
  66. make_pair(
  67. interval<ptime>::right_open(
  68. time_from_string("2008-05-20 19:30"),
  69. time_from_string("2008-05-20 23:00")),
  70. mary_harry));
  71. party += // element addition can also be done via operator +=
  72. make_pair(
  73. interval<ptime>::right_open(
  74. time_from_string("2008-05-20 20:10"),
  75. time_from_string("2008-05-21 00:00")),
  76. diana_susan);
  77. party +=
  78. make_pair(
  79. interval<ptime>::right_open(
  80. time_from_string("2008-05-20 22:15"),
  81. time_from_string("2008-05-21 00:30")),
  82. peter);
  83. interval_map<ptime, GuestSetT>::iterator it = party.begin();
  84. cout << "----- History of party guests -------------------------\n";
  85. while(it != party.end())
  86. {
  87. interval<ptime>::type when = it->first;
  88. // Who is at the party within the time interval 'when' ?
  89. GuestSetT who = (*it++).second;
  90. cout << when << ": " << who << endl;
  91. }
  92. }
  93. int main()
  94. {
  95. cout << ">>Interval Container Library: Sample boost_party.cpp <<\n";
  96. cout << "-------------------------------------------------------\n";
  97. boost_party();
  98. return 0;
  99. }
  100. // Program output:
  101. /*-----------------------------------------------------------------------------
  102. >>Interval Container Library: Sample boost_party.cpp <<
  103. -------------------------------------------------------
  104. ----- History of party guests -------------------------
  105. [2008-May-20 19:30:00, 2008-May-20 20:10:00): Harry Mary
  106. [2008-May-20 20:10:00, 2008-May-20 22:15:00): Diana Harry Mary Susan
  107. [2008-May-20 22:15:00, 2008-May-20 23:00:00): Diana Harry Mary Peter Susan
  108. [2008-May-20 23:00:00, 2008-May-21 00:00:00): Diana Peter Susan
  109. [2008-May-21 00:00:00, 2008-May-21 00:30:00): Peter
  110. -----------------------------------------------------------------------------*/
  111. //]