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_CLOSED_INTERVAL_HPP_JOFA_100324
9 #define BOOST_ICL_CLOSED_INTERVAL_HPP_JOFA_100324
10 
11 #include <boost/icl/detail/concept_check.hpp>
12 #include <boost/icl/concept/interval.hpp>
13 #include <boost/icl/type_traits/value_size.hpp>
14 #include <boost/icl/type_traits/type_to_string.hpp>
15 
16 namespace boost{namespace icl
17 {
18 
19 template <class DomainT,
20           ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT)>
21 class closed_interval
22 {
23 public:
24     typedef closed_interval<DomainT,Compare> type;
25     typedef DomainT domain_type;
26     typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
27 
28 public:
29     //==========================================================================
30     //= Construct, copy, destruct
31     //==========================================================================
32     /** Default constructor; yields an empty interval <tt>[0,0)</tt>. */
closed_interval()33     closed_interval()
34         : _lwb(unit_element<DomainT>::value()), _upb(identity_element<DomainT>::value())
35     {
36         BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
37         BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
38         BOOST_STATIC_ASSERT((icl::is_discrete<DomainT>::value));
39     }
40 
41     //NOTE: Compiler generated copy constructor is used
42 
43     /** Constructor for a closed singleton interval <tt>[val,val]</tt> */
closed_interval(const DomainT & val)44     explicit closed_interval(const DomainT& val)
45         : _lwb(val), _upb(val)
46     {
47         BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
48         BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
49         BOOST_STATIC_ASSERT((!icl::is_continuous<DomainT>::value));
50     }
51 
52     /** Interval from <tt>low</tt> to <tt>up</tt> with bounds <tt>bounds</tt> */
closed_interval(const DomainT & low,const DomainT & up)53     closed_interval(const DomainT& low, const DomainT& up) :
54         _lwb(low), _upb(up)
55     {
56         BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
57         BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
58     }
59 
lower() const60     DomainT lower()const{ return _lwb; }
upper() const61     DomainT upper()const{ return _upb; }
62 
first() const63     DomainT first()const{ return _lwb; }
last() const64     DomainT last() const{ return _upb; }
65 
66 private:
67     DomainT _lwb;
68     DomainT _upb;
69 };
70 
71 
72 //==============================================================================
73 //=T closed_interval -> concept intervals
74 //==============================================================================
75 template<class DomainT, ICL_COMPARE Compare>
76 struct interval_traits< icl::closed_interval<DomainT, Compare> >
77 {
78     typedef DomainT domain_type;
79     typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
80     typedef icl::closed_interval<DomainT, Compare> interval_type;
81 
constructboost::icl::interval_traits82     static interval_type construct(const domain_type& lo, const domain_type& up)
83     {
84         return interval_type(lo, up);
85     }
86 
lowerboost::icl::interval_traits87     static domain_type lower(const interval_type& inter_val){ return inter_val.lower(); }
upperboost::icl::interval_traits88     static domain_type upper(const interval_type& inter_val){ return inter_val.upper(); }
89 };
90 
91 //==============================================================================
92 //= Type traits
93 //==============================================================================
94 template <class DomainT, ICL_COMPARE Compare>
95 struct interval_bound_type< closed_interval<DomainT,Compare> >
96 {
97     typedef interval_bound_type type;
98     BOOST_STATIC_CONSTANT(bound_type, value = interval_bounds::static_closed);
99 };
100 
101 template <class DomainT, ICL_COMPARE Compare>
102 struct type_to_string<icl::closed_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>
109 struct value_size<icl::closed_interval<DomainT> >
110 {
applyboost::icl::value_size111     static std::size_t apply(const icl::closed_interval<DomainT>&)
112     { return 2; }
113 };
114 
115 }} // namespace icl boost
116 
117 #endif
118 
119