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