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 /** Example dynamic_interval.cpp \file dynamic_interval.cpp
12 \brief Intervals with dynamic interval bounds that can be changed at runtime.
13
14 Intervals types with dynamic interval bounds can represent closed and
15 open interval borders. Interval borders are not static or fixed for
16 the type but may change due to computations in interval containers.
17 Dynamically bounded intervals are the library default for interval
18 parameters in interval containers.
19
20 \include dynamic_interval_/dynamic_interval.cpp
21 */
22 //[example_dynamic_interval
23 #include <iostream>
24 #include <string>
25 #include <math.h>
26 #include <boost/type_traits/is_same.hpp>
27
28 #include <boost/icl/interval_set.hpp>
29 #include <boost/icl/split_interval_set.hpp>
30 // Dynamically bounded intervals 'discrete_interval' and 'continuous_interval'
31 // are indirectly included via interval containers as library defaults.
32 #include "../toytime.hpp"
33 #include <boost/icl/rational.hpp>
34
35 using namespace std;
36 using namespace boost;
37 using namespace boost::icl;
38
main()39 int main()
40 {
41 cout << ">>Interval Container Library: Sample interval.cpp <<\n";
42 cout << "----------------------------------------------------\n";
43
44 // Dynamically bounded intervals are the library default for
45 // interval parameters in interval containers.
46 BOOST_STATIC_ASSERT((
47 boost::is_same< interval_set<int>::interval_type
48 , discrete_interval<int> >::value
49 ));
50
51
52 BOOST_STATIC_ASSERT((
53 boost::is_same< interval_set<float>::interval_type
54 , continuous_interval<float> >::value
55 ));
56
57 // As we can see the library default chooses the appropriate
58 // class template instance discrete_interval<T> or continuous_interval<T>
59 // dependent on the domain_type T. The library default for intervals
60 // is also available via the template 'interval':
61 BOOST_STATIC_ASSERT((
62 boost::is_same< interval<int>::type
63 , discrete_interval<int> >::value
64 ));
65
66 BOOST_STATIC_ASSERT((
67 boost::is_same< interval<float>::type
68 , continuous_interval<float> >::value
69 ));
70
71 // template interval also provides static functions for the four border types
72
73 interval<int>::type int_interval = interval<int>::closed(3, 7);
74 interval<double>::type sqrt_interval = interval<double>::right_open(1/sqrt(2.0), sqrt(2.0));
75 interval<string>::type city_interval = interval<string>::left_open("Barcelona", "Boston");
76 interval<Time>::type time_interval = interval<Time>::open(Time(monday,8,30), Time(monday,17,20));
77
78 cout << "----- Dynamically bounded intervals ----------------------------------------\n";
79 cout << " discrete_interval<int> : " << int_interval << endl;
80 cout << "continuous_interval<double>: " << sqrt_interval << " does "
81 << string(contains(sqrt_interval, sqrt(2.0))?"":"NOT")
82 << " contain sqrt(2)" << endl;
83 cout << "continuous_interval<string>: " << city_interval << " does "
84 << string(contains(city_interval,"Barcelona")?"":"NOT")
85 << " contain 'Barcelona'" << endl;
86 cout << "continuous_interval<string>: " << city_interval << " does "
87 << string(contains(city_interval, "Berlin")?"":"NOT")
88 << " contain 'Berlin'" << endl;
89 cout << " discrete_interval<Time> : " << time_interval << "\n\n";
90
91 // Using dynamically bounded intervals allows to apply operations
92 // with intervals and also with elements on all interval containers
93 // including interval containers of continuous domain types:
94
95 interval<rational<int> >::type unit_interval
96 = interval<rational<int> >::right_open(rational<int>(0), rational<int>(1));
97 interval_set<rational<int> > unit_set(unit_interval);
98 interval_set<rational<int> > ratio_set(unit_set);
99 ratio_set -= rational<int>(1,3); // Subtract 1/3 from the set
100
101 cout << "----- Manipulation of single values in continuous sets ---------------------\n";
102 cout << "1/3 subtracted from [0..1) : " << ratio_set << endl;
103 cout << "The set does " << string(contains(ratio_set, rational<int>(1,3))?"":"NOT")
104 << " contain '1/3'" << endl;
105 ratio_set ^= unit_set;
106 cout << "Flipping the holey set : " << ratio_set << endl;
107 cout << "yields the subtracted : 1/3\n\n";
108
109 // Of course we can use interval types that are different from the
110 // library default by explicit instantiation:
111 split_interval_set<int, std::less, closed_interval<Time> > intuitive_times;
112 // Interval set 'intuitive_times' uses statically bounded closed intervals
113 intuitive_times += closed_interval<Time>(Time(monday, 9,00), Time(monday, 10,59));
114 intuitive_times += closed_interval<Time>(Time(monday, 10,00), Time(monday, 11,59));
115 cout << "----- Here we are NOT using the library default for intervals --------------\n";
116 cout << intuitive_times << endl;
117
118 return 0;
119 }
120
121 // Program output:
122 //>>Interval Container Library: Sample interval.cpp <<
123 //----------------------------------------------------
124 //----- Dynamically bounded intervals ----------------------------------------
125 // discrete_interval<int> : [3,7]
126 //continuous_interval<double>: [0.707107,1.41421) does NOT contain sqrt(2)
127 //continuous_interval<string>: (Barcelona,Boston] does NOT contain 'Barcelona'
128 //continuous_interval<string>: (Barcelona,Boston] does contain 'Berlin'
129 // discrete_interval<Time> : (mon:08:30,mon:17:20)
130 //
131 //----- Manipulation of single values in continuous sets ---------------------
132 //1/3 subtracted from [0..1) : {[0/1,1/3)(1/3,1/1)}
133 //The set does NOT contain '1/3'
134 //Flipping the holey set : {[1/3,1/3]}
135 //yields the subtracted : 1/3
136 //
137 //----- Here we are NOT using the library default for intervals --------------
138 //{[mon:09:00,mon:09:59][mon:10:00,mon:10:59][mon:11:00,mon:11:59]}
139 //]
140
141