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_RIGHT_OPEN_INTERVAL_HPP_JOFA_100323
9 #define BOOST_ICL_RIGHT_OPEN_INTERVAL_HPP_JOFA_100323
10 
11 #include <functional>
12 #include <boost/concept/assert.hpp>
13 #include <boost/icl/concept/interval.hpp>
14 #include <boost/icl/type_traits/succ_pred.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 right_open_interval
24 {
25 public:
26     typedef right_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>. */
right_open_interval()35     right_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 a singleton interval <tt>[val,val+1)</tt> */
right_open_interval(const DomainT & val)45     explicit right_open_interval(const DomainT& val)
46         : _lwb(val), _upb(icl::successor<DomainT,domain_compare>::apply(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     }
54 
55     /** Interval from <tt>low</tt> to <tt>up</tt> with bounds <tt>bounds</tt> */
right_open_interval(const DomainT & low,const DomainT & up)56     right_open_interval(const DomainT& low, const DomainT& up) :
57         _lwb(low), _upb(up)
58     {
59         BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
60         BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
61     }
62 
lower() const63     domain_type lower()const{ return _lwb; }
upper() const64     domain_type upper()const{ return _upb; }
65 
66 private:
67     domain_type _lwb;
68     domain_type _upb;
69 };
70 
71 //==============================================================================
72 //=T right_open_interval -> concept intervals
73 //==============================================================================
74 template<class DomainT, ICL_COMPARE Compare>
75 struct interval_traits< icl::right_open_interval<DomainT, Compare> >
76 {
77     typedef DomainT domain_type;
78     typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
79     typedef icl::right_open_interval<DomainT, Compare> interval_type;
80 
constructboost::icl::interval_traits81     static interval_type construct(const domain_type& lo, const domain_type& up)
82     {
83         return interval_type(lo, up);
84     }
85 
lowerboost::icl::interval_traits86     static domain_type lower(const interval_type& inter_val){ return inter_val.lower(); };
upperboost::icl::interval_traits87     static domain_type upper(const interval_type& inter_val){ return inter_val.upper(); };
88 };
89 
90 
91 //==============================================================================
92 //= Type traits
93 //==============================================================================
94 template <class DomainT, ICL_COMPARE Compare>
95 struct interval_bound_type< right_open_interval<DomainT,Compare> >
96 {
97     typedef interval_bound_type type;
98     BOOST_STATIC_CONSTANT(bound_type, value = interval_bounds::static_right_open);
99 };
100 
101 template <class DomainT, ICL_COMPARE Compare>
102 struct type_to_string<icl::right_open_interval<DomainT,Compare> >
103 {
applyboost::icl::type_to_string104     static std::string apply()
105     { return "[I)<"+ type_to_string<DomainT>::apply() +">"; }
106 };
107 
108 template<class DomainT, ICL_COMPARE Compare>
109 struct value_size<icl::right_open_interval<DomainT,Compare> >
110 {
applyboost::icl::value_size111     static std::size_t apply(const icl::right_open_interval<DomainT>&)
112     { return 2; }
113 };
114 
115 }} // namespace icl boost
116 
117 #endif
118 
119