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