1 /*-----------------------------------------------------------------------------+
2 Copyright (c) 2010-2010: Joachim Faulhaber
3 +------------------------------------------------------------------------------+
4    Distributed under the Boost Software License, Version 1.0.
5       (See accompanying file LICENCE.txt or copy at
6            http://www.boost.org/LICENSE_1_0.txt)
7 +-----------------------------------------------------------------------------*/
8 #ifndef BOOST_ICL_OPEN_INTERVAL_HPP_JOFA_100930
9 #define BOOST_ICL_OPEN_INTERVAL_HPP_JOFA_100930
10 
11 #include <functional>
12 #include <boost/concept/assert.hpp>
13 #include <boost/icl/detail/concept_check.hpp>
14 #include <boost/icl/concept/interval.hpp>
15 #include <boost/icl/type_traits/value_size.hpp>
16 #include <boost/icl/type_traits/type_to_string.hpp>
17 
18 namespace boost{namespace icl
19 {
20 
21 template <class DomainT,
22           ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT)>
23 class open_interval
24 {
25 public:
26     typedef open_interval<DomainT,Compare> type;
27     typedef DomainT domain_type;
28     typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
29 
30 public:
31     //==========================================================================
32     //= Construct, copy, destruct
33     //==========================================================================
34     /** Default constructor; yields an empty interval <tt>(0,0)</tt>. */
open_interval()35     open_interval()
36         : _lwb(identity_element<DomainT>::value()), _upb(identity_element<DomainT>::value())
37     {
38         BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
39         BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
40     }
41 
42     //NOTE: Compiler generated copy constructor is used
43 
44     /** Constructor for an open singleton interval <tt>(val-1,val+1)</tt> */
open_interval(const DomainT & val)45     explicit open_interval(const DomainT& val)
46         : _lwb(pred(val)), _upb(succ(val))
47     {
48         BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
49         BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
50         // Only for discrete types this ctor creates an interval containing
51         // a single element only.
52         BOOST_STATIC_ASSERT((icl::is_discrete<DomainT>::value));
53         BOOST_ASSERT((numeric_minimum<DomainT, domain_compare, is_numeric<DomainT>::value >
54                                      ::is_less_than(val) ));
55     }
56 
57     /** Interval from <tt>low</tt> to <tt>up</tt> with bounds <tt>bounds</tt> */
open_interval(const DomainT & low,const DomainT & up)58     open_interval(const DomainT& low, const DomainT& up) :
59         _lwb(low), _upb(up)
60     {
61         BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
62         BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
63     }
64 
lower() const65     DomainT lower()const{ return _lwb; }
upper() const66     DomainT upper()const{ return _upb; }
67 
68 private:
69     DomainT _lwb;
70     DomainT _upb;
71 };
72 
73 //==============================================================================
74 //=T open_interval -> concept intervals
75 //==============================================================================
76 template<class DomainT, ICL_COMPARE Compare>
77 struct interval_traits< icl::open_interval<DomainT, Compare> >
78 {
79     typedef DomainT domain_type;
80     typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
81     typedef icl::open_interval<DomainT, Compare> interval_type;
82 
constructboost::icl::interval_traits83     static interval_type construct(const domain_type& lo, const domain_type& up)
84     {
85         return interval_type(lo, up);
86     }
87 
lowerboost::icl::interval_traits88     static domain_type lower(const interval_type& inter_val){ return inter_val.lower(); };
upperboost::icl::interval_traits89     static domain_type upper(const interval_type& inter_val){ return inter_val.upper(); };
90 };
91 
92 
93 //==============================================================================
94 //= Type traits
95 //==============================================================================
96 template <class DomainT, ICL_COMPARE Compare>
97 struct interval_bound_type< open_interval<DomainT,Compare> >
98 {
99     typedef interval_bound_type type;
100     BOOST_STATIC_CONSTANT(bound_type, value = interval_bounds::static_open);
101 };
102 
103 template <class DomainT, ICL_COMPARE Compare>
104 struct type_to_string<icl::open_interval<DomainT,Compare> >
105 {
applyboost::icl::type_to_string106     static std::string apply()
107     { return "(I)<"+ type_to_string<DomainT>::apply() +">"; }
108 };
109 
110 template<class DomainT, ICL_COMPARE Compare>
111 struct value_size<icl::open_interval<DomainT,Compare> >
112 {
applyboost::icl::value_size113     static std::size_t apply(const icl::open_interval<DomainT>&)
114     { return 2; }
115 };
116 
117 }} // namespace icl boost
118 
119 #endif
120 
121