• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 interval_container.cpp \file interval_container.cpp
12 
13     \brief Demonstrates basic characteristics of interval container objects.
14 
15     \include interval_container_/interval_container.cpp
16 */
17 //[example_interval_container
18 #include <iostream>
19 #include <boost/icl/interval_set.hpp>
20 #include <boost/icl/separate_interval_set.hpp>
21 #include <boost/icl/split_interval_set.hpp>
22 #include <boost/icl/split_interval_map.hpp>
23 #include "../toytime.hpp"
24 
25 using namespace std;
26 using namespace boost::icl;
27 
interval_container_basics()28 void interval_container_basics()
29 {
30     interval<Time>::type night_and_day(Time(monday,   20,00), Time(tuesday,  20,00));
31     interval<Time>::type day_and_night(Time(tuesday,   7,00), Time(wednesday, 7,00));
32     interval<Time>::type  next_morning(Time(wednesday, 7,00), Time(wednesday,10,00));
33     interval<Time>::type  next_evening(Time(wednesday,18,00), Time(wednesday,21,00));
34 
35     // An interval set of type interval_set joins intervals that that overlap or touch each other.
36     interval_set<Time> joinedTimes;
37     joinedTimes.insert(night_and_day);
38     joinedTimes.insert(day_and_night); //overlapping in 'day' [07:00, 20.00)
39     joinedTimes.insert(next_morning);  //touching
40     joinedTimes.insert(next_evening);  //disjoint
41 
42     cout << "Joined times  :" << joinedTimes << endl;
43 
44     // A separate interval set of type separate_interval_set joins intervals that that
45     // overlap but it preserves interval borders that just touch each other. You may
46     // represent time grids like the months of a year as a split_interval_set.
47     separate_interval_set<Time> separateTimes;
48     separateTimes.insert(night_and_day);
49     separateTimes.insert(day_and_night); //overlapping in 'day' [07:00, 20.00)
50     separateTimes.insert(next_morning);  //touching
51     separateTimes.insert(next_evening);  //disjoint
52 
53     cout << "Separate times:" << separateTimes << endl;
54 
55     // A split interval set of type split_interval_set preserves all interval
56     // borders. On insertion of overlapping intervals the intervals in the
57     // set are split up at the interval borders of the inserted interval.
58     split_interval_set<Time> splitTimes;
59     splitTimes += night_and_day;
60     splitTimes += day_and_night; //overlapping in 'day' [07:00, 20:00)
61     splitTimes += next_morning;  //touching
62     splitTimes += next_evening;  //disjoint
63 
64     cout << "Split times   :\n" << splitTimes << endl;
65 
66     // A split interval map splits up inserted intervals on overlap and aggregates the
67     // associated quantities via the operator +=
68     split_interval_map<Time, int> overlapCounter;
69     overlapCounter += make_pair(night_and_day,1);
70     overlapCounter += make_pair(day_and_night,1); //overlapping in 'day' [07:00, 20.00)
71     overlapCounter += make_pair(next_morning, 1); //touching
72     overlapCounter += make_pair(next_evening, 1); //disjoint
73 
74     cout << "Split times overlap counted:\n" << overlapCounter << endl;
75 
76     // An interval map joins touching intervals, if associated values are equal
77     interval_map<Time, int> joiningOverlapCounter;
78     joiningOverlapCounter = overlapCounter;
79     cout << "Times overlap counted:\n" << joiningOverlapCounter << endl;
80 }
81 
main()82 int main()
83 {
84     cout << ">>Interval Container Library: Sample interval_container.cpp <<\n";
85     cout << "--------------------------------------------------------------\n";
86     interval_container_basics();
87     return 0;
88 }
89 
90 
91 // Program output:
92 /* ----------------------------------------------------------------------------
93 >>Interval Container Library: Sample interval_container.cpp <<
94 --------------------------------------------------------------
95 Joined times  :[mon:20:00,wed:10:00)[wed:18:00,wed:21:00)
96 Separate times:[mon:20:00,wed:07:00)[wed:07:00,wed:10:00)[wed:18:00,wed:21:00)
97 Split times   :
98 [mon:20:00,tue:07:00)[tue:07:00,tue:20:00)[tue:20:00,wed:07:00)
99 [wed:07:00,wed:10:00)[wed:18:00,wed:21:00)
100 Split times overlap counted:
101 {([mon:20:00,tue:07:00)->1)([tue:07:00,tue:20:00)->2)([tue:20:00,wed:07:00)->1)
102 ([wed:07:00,wed:10:00)->1)([wed:18:00,wed:21:00)->1)}
103 Times overlap counted:
104 {([mon:20:00,tue:07:00)->1)([tue:07:00,tue:20:00)->2)([tue:20:00,wed:10:00)->1)
105 ([wed:18:00,wed:21:00)->1)}
106 -----------------------------------------------------------------------------*/
107 //]
108