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 overlap_counter.cpp \file overlap_counter.cpp
12
13 \brief The most simple application of an interval map:
14 Counting the overlaps of added intervals.
15
16 The most basic application of an interval_map is a counter counting
17 the number of overlaps of intervals inserted into it.
18
19 On could call an interval_map an aggregate on overlap machine. A very basic
20 aggregation is summation of an integer. A interval_map<int,int> maps
21 intervals of int to ints.
22
23 If we insert a value pair (discrete_interval<int>(2,6), 1) into the interval_map, it
24 increases the content of all value pairs in the map by 1, if their interval
25 part overlaps with discrete_interval<int>(2,6).
26
27 \include overlap_counter_/overlap_counter.cpp
28 */
29 //[example_overlap_counter
30 #include <iostream>
31 #include <boost/icl/split_interval_map.hpp>
32
33 using namespace std;
34 using namespace boost::icl;
35
36
37 /* The most simple example of an interval_map is an overlap counter.
38 If intervals are added that are associated with the value 1,
39 all overlaps of added intervals are counted as a result in the
40 associated values.
41 */
42 typedef interval_map<int, int> OverlapCounterT;
43
print_overlaps(const OverlapCounterT & counter)44 void print_overlaps(const OverlapCounterT& counter)
45 {
46 for(OverlapCounterT::const_iterator it = counter.begin(); it != counter.end(); it++)
47 {
48 discrete_interval<int> itv = (*it).first;
49 int overlaps_count = (*it).second;
50 if(overlaps_count == 1)
51 cout << "in interval " << itv << " intervals do not overlap" << endl;
52 else
53 cout << "in interval " << itv << ": "<< overlaps_count << " intervals overlap" << endl;
54 }
55 }
56
overlap_counter()57 void overlap_counter()
58 {
59 OverlapCounterT overlap_counter;
60 discrete_interval<int> inter_val;
61
62 inter_val = discrete_interval<int>::right_open(4,8);
63 cout << "-- adding " << inter_val << " -----------------------------------------" << endl;
64 overlap_counter += make_pair(inter_val, 1);
65 print_overlaps(overlap_counter);
66 cout << "-----------------------------------------------------------" << endl;
67
68 inter_val = discrete_interval<int>::right_open(6,9);
69 cout << "-- adding " << inter_val << " -----------------------------------------" << endl;
70 overlap_counter += make_pair(inter_val, 1);
71 print_overlaps(overlap_counter);
72 cout << "-----------------------------------------------------------" << endl;
73
74 inter_val = discrete_interval<int>::right_open(1,9);
75 cout << "-- adding " << inter_val << " -----------------------------------------" << endl;
76 overlap_counter += make_pair(inter_val, 1);
77 print_overlaps(overlap_counter);
78 cout << "-----------------------------------------------------------" << endl;
79
80 }
81
main()82 int main()
83 {
84 cout << ">>Interval Container Library: Sample overlap_counter.cpp <<\n";
85 cout << "-----------------------------------------------------------\n";
86 overlap_counter();
87 return 0;
88 }
89
90 // Program output:
91
92 // >>Interval Container Library: Sample overlap_counter.cpp <<
93 // -----------------------------------------------------------
94 // -- adding [4,8) -----------------------------------------
95 // in interval [4,8) intervals do not overlap
96 // -----------------------------------------------------------
97 // -- adding [6,9) -----------------------------------------
98 // in interval [4,6) intervals do not overlap
99 // in interval [6,8): 2 intervals overlap
100 // in interval [8,9) intervals do not overlap
101 // -----------------------------------------------------------
102 // -- adding [1,9) -----------------------------------------
103 // in interval [1,4) intervals do not overlap
104 // in interval [4,6): 2 intervals overlap
105 // in interval [6,8): 3 intervals overlap
106 // in interval [8,9): 2 intervals overlap
107 // -----------------------------------------------------------
108 //]
109