• 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 
12 /** Example custom_interval.cpp \file custom_interval.cpp
13     \brief Shows how to use interval containers with own interval classes.
14 
15     There may be instances, where we want to use interval container with our
16     own user defined interval classes. Boost interval containers can be adapted
17     to your interval class by partial template specialisation. Only a few lines
18     of code are needed to achieve this.
19 
20     \include custom_interval_/custom_interval.cpp
21 */
22 //[example_custom_interval
23 #include <iostream>
24 #include <boost/icl/interval_set.hpp>
25 
26 using namespace std;
27 using namespace boost::icl;
28 
29 // Here is a typical class that may model intervals in your application.
30 class MyInterval
31 {
32 public:
MyInterval()33     MyInterval(): _first(), _past(){}
MyInterval(int lo,int up)34     MyInterval(int lo, int up): _first(lo), _past(up){}
first() const35     int first()const{ return _first; }
past() const36     int past ()const{ return _past; }
37 private:
38     int _first, _past;
39 };
40 
41 namespace boost{ namespace icl
42 {
43 // Class template interval_traits serves as adapter to register and customize your interval class
44 template<>
45 struct interval_traits< MyInterval >       //1.  Partially specialize interval_traits for
46 {                                          //    your class MyInterval
47                                            //2.  Define associated types
48     typedef MyInterval     interval_type;  //2.1 MyInterval will be the interval_type
49     typedef int            domain_type;    //2.2 The elements of the domain are ints
50     typedef std::less<int> domain_compare; //2.3 This is the way our element shall be ordered.
51                                            //3.  Next we define the essential functions
52                                            //    of the specialisation
53                                            //3.1 Construction of intervals
constructboost::icl::interval_traits54     static interval_type construct(const domain_type& lo, const domain_type& up)
55     { return interval_type(lo, up); }
56                                            //3.2 Selection of values
lowerboost::icl::interval_traits57     static domain_type lower(const interval_type& inter_val){ return inter_val.first(); };
upperboost::icl::interval_traits58     static domain_type upper(const interval_type& inter_val){ return inter_val.past(); };
59 };
60 
61 template<>
62 struct interval_bound_type<MyInterval>     //4.  Finally we define the interval borders.
63 {                                          //    Choose between static_open         (lo..up)
64     typedef interval_bound_type type;      //                   static_left_open    (lo..up]
65     BOOST_STATIC_CONSTANT(bound_type, value = interval_bounds::static_right_open);//[lo..up)
66 };                                         //               and static_closed       [lo..up]
67 
68 }} // namespace boost icl
69 
custom_interval()70 void custom_interval()
71 {
72     // Now we can use class MyInterval with interval containers:
73     typedef interval_set<int, std::less, MyInterval> MyIntervalSet;
74     MyIntervalSet mySet;
75     mySet += MyInterval(1,9);
76     cout << mySet << endl;
77     mySet.subtract(3) -= 6;
78     cout << mySet << "            subtracted 3 and 6\n";
79     mySet ^= MyInterval(2,8);
80     cout << mySet <<      "  flipped between 2 and 7\n";
81 }
82 
83 
main()84 int main()
85 {
86     cout << ">>Interval Container Library: Sample custom_interval.cpp <<\n";
87     cout << "-----------------------------------------------------------\n";
88     cout << "This program uses a user defined interval class:\n";
89     custom_interval();
90     return 0;
91 }
92 
93 // Program output:
94 /*-----------------------------------------------------------------------------
95 >>Interval Container Library: Sample custom_interval.cpp <<
96 -----------------------------------------------------------
97 This program uses a user defined interval class:
98 {[1,                      9)}
99 {[1,  3)   [4,  6)   [7,  9)}       subtracted 3 and 6
100 {[1,2) [3,4)     [6,7) [8,9)}  flipped between 2 and 7
101 -----------------------------------------------------------------------------*/
102 //]
103 
104