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/static_assert.hpp>
13 #include <boost/concept/assert.hpp>
14 #include <boost/icl/detail/concept_check.hpp>
15 #include <boost/icl/concept/interval.hpp>
16 #include <boost/icl/type_traits/succ_pred.hpp>
17 #include <boost/icl/type_traits/value_size.hpp>
18 #include <boost/icl/type_traits/type_to_string.hpp>
19 
20 namespace boost{namespace icl
21 {
22 
23 template <class DomainT,
24           ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT)>
25 class open_interval
26 {
27 public:
28     typedef open_interval<DomainT,Compare> type;
29     typedef DomainT domain_type;
30     typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
31 
32 public:
33     //==========================================================================
34     //= Construct, copy, destruct
35     //==========================================================================
36     /** Default constructor; yields an empty interval <tt>(0,0)</tt>. */
open_interval()37     open_interval()
38         : _lwb(identity_element<DomainT>::value()), _upb(identity_element<DomainT>::value())
39     {
40         BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
41         BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
42     }
43 
44     //NOTE: Compiler generated copy constructor is used
45 
46     /** Constructor for an open singleton interval <tt>(val-1,val+1)</tt> */
open_interval(const DomainT & val)47     explicit open_interval(const DomainT& val)
48         : _lwb(pred(val)), _upb(succ(val))
49     {
50         BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
51         BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
52         // Only for discrete types this ctor creates an interval containing
53         // a single element only.
54         BOOST_STATIC_ASSERT((icl::is_discrete<DomainT>::value));
55         BOOST_ASSERT((numeric_minimum<DomainT, domain_compare, is_numeric<DomainT>::value >
56                                      ::is_less_than(val) ));
57     }
58 
59     /** Interval from <tt>low</tt> to <tt>up</tt> with bounds <tt>bounds</tt> */
open_interval(const DomainT & low,const DomainT & up)60     open_interval(const DomainT& low, const DomainT& up) :
61         _lwb(low), _upb(up)
62     {
63         BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
64         BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
65     }
66 
lower() const67     DomainT lower()const{ return _lwb; }
upper() const68     DomainT upper()const{ return _upb; }
69 
70 private:
71     DomainT _lwb;
72     DomainT _upb;
73 };
74 
75 //==============================================================================
76 //=T open_interval -> concept intervals
77 //==============================================================================
78 template<class DomainT, ICL_COMPARE Compare>
79 struct interval_traits< icl::open_interval<DomainT, Compare> >
80 {
81     typedef DomainT domain_type;
82     typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
83     typedef icl::open_interval<DomainT, Compare> interval_type;
84 
constructboost::icl::interval_traits85     static interval_type construct(const domain_type& lo, const domain_type& up)
86     {
87         return interval_type(lo, up);
88     }
89 
lowerboost::icl::interval_traits90     static domain_type lower(const interval_type& inter_val){ return inter_val.lower(); }
upperboost::icl::interval_traits91     static domain_type upper(const interval_type& inter_val){ return inter_val.upper(); }
92 };
93 
94 
95 //==============================================================================
96 //= Type traits
97 //==============================================================================
98 template <class DomainT, ICL_COMPARE Compare>
99 struct interval_bound_type< open_interval<DomainT,Compare> >
100 {
101     typedef interval_bound_type type;
102     BOOST_STATIC_CONSTANT(bound_type, value = interval_bounds::static_open);
103 };
104 
105 template <class DomainT, ICL_COMPARE Compare>
106 struct type_to_string<icl::open_interval<DomainT,Compare> >
107 {
applyboost::icl::type_to_string108     static std::string apply()
109     { return "(I)<"+ type_to_string<DomainT>::apply() +">"; }
110 };
111 
112 template<class DomainT, ICL_COMPARE Compare>
113 struct value_size<icl::open_interval<DomainT,Compare> >
114 {
applyboost::icl::value_size115     static std::size_t apply(const icl::open_interval<DomainT>&)
116     { return 2; }
117 };
118 
119 }} // namespace icl boost
120 
121 #endif
122