party.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*-----------------------------------------------------------------------------+
  2. Interval Container Library
  3. Author: Joachim Faulhaber
  4. Copyright (c) 2007-2010: 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. #include <iostream>
  12. #include <boost/icl/interval_map.hpp>
  13. #include "../toytime.hpp"
  14. using namespace std;
  15. using namespace boost::icl;
  16. /**
  17. Party.cpp demonstrates the possibilities of an interval map (interval_map or
  18. split_interval_map). An interval_map maps intervals to a given content. In
  19. this case the content is a set of party guests represented by their name
  20. strings.
  21. As time goes by, groups of people join the party and leave later in the
  22. evening. So we add a time interval and a name set to the interval_map for
  23. the attendance of each group of people, that come together and leave
  24. together.
  25. On every overlap of intervals, the corresponding name sets are accumulated.
  26. At the points of overlap the intervals are split. The accumulation of content
  27. on overlap of intervals is always done via an operator += that has to be
  28. implemented for the content parameter of the interval_map.
  29. Finally the interval_map contains the history of attendance and all points
  30. in time, where the group of party guests changed.
  31. Party.cpp demonstrates a principle that we call aggregate on overlap
  32. (aggovering;) On insertion a value associated to the interval is aggregated
  33. (added) to those values in the interval_map that overlap with the inserted
  34. value.
  35. There are two behavioral aspects to aggovering: a decompositional behavior
  36. and a accumulative behavior.
  37. The decompositional behavior splits up intervals on the time dimension of
  38. the interval_map so that the intervals change whenever associated values
  39. change.
  40. The accumulative behavior accumulates associated values on every overlap of
  41. an insertion for the associated values.
  42. */
  43. // Type set<string> collects the names of party guests. Since std::set is
  44. // a model of the itl's set concept, the concept provides an operator +=
  45. // that performs a set union on overlap of intervals.
  46. typedef std::set<string> GuestSetT;
  47. // 'Time' is the domain type the interval_map. It's key values are therefore
  48. // time intervals. The content is the set of names: GuestSetT.
  49. void party()
  50. {
  51. GuestSetT mary_harry;
  52. mary_harry.insert("Mary");
  53. mary_harry.insert("Harry");
  54. GuestSetT diana_susan;
  55. diana_susan.insert("Diana");
  56. diana_susan.insert("Susan");
  57. GuestSetT peter;
  58. peter.insert("Peter");
  59. interval_map<Time, GuestSetT> party;
  60. party += make_pair( interval<Time>::right_open(Time(19,30), Time(23,00)), mary_harry);
  61. party += make_pair( interval<Time>::right_open(Time(20,10), Time(monday,0,0)), diana_susan);
  62. party += make_pair( interval<Time>::right_open(Time(22,15), Time(monday,0,30)), peter);
  63. interval_map<Time, GuestSetT>::iterator it = party.begin();
  64. while(it != party.end())
  65. {
  66. discrete_interval<Time> when = it->first;
  67. // Who is at the party within the time interval 'when' ?
  68. GuestSetT who = (*it++).second;
  69. cout << when << ": " << who << endl;
  70. }
  71. }
  72. int main()
  73. {
  74. cout << ">>Interval Container Library: Sample party.cpp <<\n";
  75. cout << "-------------------------------------------------------\n";
  76. party();
  77. return 0;
  78. }
  79. // Program output:
  80. // >>Interval Container Library: Sample party.cpp <<
  81. // -------------------------------------------------
  82. // [sun:19:30,sun:20:10): Harry Mary
  83. // [sun:20:10,sun:22:15): Diana Harry Mary Susan
  84. // [sun:22:15,sun:23:00): Diana Harry Mary Peter Susan
  85. // [sun:23:00,mon:00:00): Diana Peter Susan
  86. // [mon:00:00,mon:00:30): Peter