• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static_interval.cpp \file static_interval.cpp
12     \brief Intervals with static interval bounds.
13 
14     Intervals types with static or fixed interval bounds. Statically
15     bounded intervals use up to 33% less memory than dynamically
16     bounded ones. Of the four possible statically bounded intervals types
17     right_open_intervals are the most important ones. We can switch the
18     library default to statically bounded intervals by defining
19     BOOST_ICL_USE_STATIC_BOUNDED_INTERVALS.
20 
21     \include static_interval_/static_interval.cpp
22 */
23 //[example_static_interval
24 #include <iostream>
25 #include <string>
26 #include <math.h>
27 #include <boost/type_traits/is_same.hpp>
28 
29 // We can change the library default for the interval types by defining
30 #define BOOST_ICL_USE_STATIC_BOUNDED_INTERVALS
31 // prior to other inluces from the icl.
32 // The interval type that is automatically used with interval
33 // containers then is the statically bounded right_open_interval.
34 
35 #include <boost/icl/interval_set.hpp>
36 #include <boost/icl/split_interval_set.hpp>
37 // The statically bounded interval type 'right_open_interval'
38 // is indirectly included via interval containers.
39 
40 
41 #include "../toytime.hpp"
42 #include <boost/icl/rational.hpp>
43 
44 using namespace std;
45 using namespace boost;
46 using namespace boost::icl;
47 
main()48 int main()
49 {
50     cout << ">> Interval Container Library: Sample static_interval.cpp <<\n";
51     cout << "------------------------------------------------------------\n";
52 
53     // Statically bounded intervals are the user defined library default for
54     // interval parameters in interval containers now.
55     BOOST_STATIC_ASSERT((
56         boost::is_same< interval_set<int>::interval_type
57                       , right_open_interval<int> >::value
58                       ));
59 
60     BOOST_STATIC_ASSERT((
61         boost::is_same< interval_set<float>::interval_type
62                       , right_open_interval<float> >::value
63                       ));
64 
65     // As we can see the library default both for discrete and continuous
66     // domain_types T is 'right_open_interval<T>'.
67     // The user defined library default for intervals is also available via
68     // the template 'interval':
69     BOOST_STATIC_ASSERT((
70         boost::is_same< interval<int>::type
71                       , right_open_interval<int> >::value
72                       ));
73 
74     // Again we are declaring and initializing the four test intervals that have been used
75     // in the example 'interval' and 'dynamic_interval'
76     interval<int>::type    int_interval  = interval<int>::right_open(3, 8); // shifted the upper bound
77     interval<double>::type sqrt_interval = interval<double>::right_open(1/sqrt(2.0), sqrt(2.0));
78 
79     // Interval ("Barcelona", "Boston"] can not be represented because there is no 'steppable next' on
80     // lower bound "Barcelona". Ok. this is a different interval:
81     interval<string>::type city_interval = interval<string>::right_open("Barcelona", "Boston");
82 
83     // Toy Time is discrete again so we can transfrom open(Time(monday,8,30), Time(monday,17,20))
84     //                                       to right_open(Time(monday,8,31), Time(monday,17,20))
85     interval<Time>::type   time_interval = interval<Time>::right_open(Time(monday,8,31), Time(monday,17,20));
86 
87     cout << "----- Statically bounded intervals ----------------------------------------\n";
88     cout << "right_open_interval<int>   : " << int_interval  << endl;
89     cout << "right_open_interval<double>: " << sqrt_interval << " does "
90                                             << string(contains(sqrt_interval, sqrt(2.0))?"":"NOT")
91                                             << " contain sqrt(2)" << endl;
92     cout << "right_open_interval<string>: " << city_interval << " does "
93                                             << string(contains(city_interval,"Barcelona")?"":"NOT")
94                                             << " contain 'Barcelona'" << endl;
95     cout << "right_open_interval<string>: " << city_interval << " does "
96                                             << string(contains(city_interval, "Boston")?"":"NOT")
97                                             << " contain 'Boston'" << endl;
98     cout << "right_open_interval<Time>  : " << time_interval << "\n\n";
99 
100     // Using statically bounded intervals does not allows to apply operations
101     // with elements on all interval containers, if their domain_type is continuous.
102     // The code that follows is identical to example 'dynamic_interval'. Only 'internally'
103     // the library default for the interval template now is 'right_open_interval'
104     interval<rational<int> >::type unit_interval
105         = interval<rational<int> >::right_open(rational<int>(0), rational<int>(1));
106     interval_set<rational<int> > unit_set(unit_interval);
107     interval_set<rational<int> > ratio_set(unit_set);
108     // ratio_set -= rational<int>(1,3); // This line will not compile, because we can not
109                                         // represent a singleton interval as right_open_interval.
110     return 0;
111 }
112 
113 // Program output:
114 //>> Interval Container Library: Sample static_interval.cpp <<
115 //------------------------------------------------------------
116 //----- Statically bounded intervals ----------------------------------------
117 //right_open_interval<int>   : [3,8)
118 //right_open_interval<double>: [0.707107,1.41421) does NOT contain sqrt(2)
119 //right_open_interval<string>: [Barcelona,Boston) does  contain 'Barcelona'
120 //right_open_interval<string>: [Barcelona,Boston) does NOT contain 'Boston'
121 //right_open_interval<Time>  : [mon:08:31,mon:17:20)
122 //]
123 
124