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_DISCRETE_INTERVAL_HPP_JOFA_100403
9 #define BOOST_ICL_DISCRETE_INTERVAL_HPP_JOFA_100403
12 #include <boost/static_assert.hpp>
13 #include <boost/concept/assert.hpp>
14 #include <boost/icl/detail/concept_check.hpp>
15 #include <boost/icl/type_traits/succ_pred.hpp>
16 #include <boost/icl/concept/interval.hpp>
17 #include <boost/icl/type_traits/value_size.hpp>
18 #include <boost/icl/type_traits/type_to_string.hpp>
19 #include <boost/icl/type_traits/is_continuous.hpp>
20 #include <boost/icl/type_traits/is_discrete_interval.hpp>
21 #include <boost/icl/interval_bounds.hpp>
23 namespace boost{namespace icl
26 template <class DomainT,
27 ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT)>
28 class discrete_interval
31 typedef discrete_interval<DomainT,Compare> type;
32 typedef DomainT domain_type;
33 typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
34 typedef typename bounded_value<DomainT>::type bounded_domain_type;
37 //==========================================================================
38 //= Construct, copy, destruct
39 //==========================================================================
40 /** Default constructor; yields an empty interval <tt>[0,0)</tt>. */
42 : _lwb(identity_element<DomainT>::value()), _upb(identity_element<DomainT>::value())
43 , _bounds(interval_bounds::right_open())
45 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
46 BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
47 BOOST_STATIC_ASSERT((icl::is_discrete<DomainT>::value));
50 //NOTE: Compiler generated copy constructor is used
52 /** Constructor for a closed singleton interval <tt>[val,val]</tt> */
53 explicit discrete_interval(const DomainT& val)
54 : _lwb(val), _upb(val), _bounds(interval_bounds::closed())
56 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
57 BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
58 BOOST_STATIC_ASSERT((icl::is_discrete<DomainT>::value));
61 /** Interval from <tt>low</tt> to <tt>up</tt> with bounds <tt>bounds</tt> */
62 discrete_interval(const DomainT& low, const DomainT& up,
63 interval_bounds bounds = interval_bounds::right_open(),
64 discrete_interval* = 0)
65 : _lwb(low), _upb(up), _bounds(bounds)
67 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
68 BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
69 BOOST_STATIC_ASSERT((icl::is_discrete<DomainT>::value));
72 domain_type lower()const { return _lwb; }
73 domain_type upper()const { return _upb; }
74 interval_bounds bounds()const{ return _bounds; }
76 static discrete_interval open (const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::open()); }
77 static discrete_interval right_open(const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::right_open());}
78 static discrete_interval left_open (const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::left_open()); }
79 static discrete_interval closed (const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::closed()); }
84 interval_bounds _bounds;
87 //==============================================================================
88 //=T discrete_interval -> concept intervals
89 //==============================================================================
90 template<class DomainT, ICL_COMPARE Compare>
91 struct interval_traits< icl::discrete_interval<DomainT, Compare> >
93 typedef interval_traits type;
94 typedef DomainT domain_type;
95 typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
96 typedef icl::discrete_interval<DomainT, Compare> interval_type;
98 static interval_type construct(const domain_type& lo, const domain_type& up)
100 return interval_type(lo, up);
103 static domain_type lower(const interval_type& inter_val){ return inter_val.lower(); }
104 static domain_type upper(const interval_type& inter_val){ return inter_val.upper(); }
107 //==============================================================================
108 //=T discrete_interval -> concept dynamic_interval_traits
109 //==============================================================================
110 template<class DomainT, ICL_COMPARE Compare>
111 struct dynamic_interval_traits<boost::icl::discrete_interval<DomainT,Compare> >
113 typedef dynamic_interval_traits type;
114 typedef boost::icl::discrete_interval<DomainT,Compare> interval_type;
115 typedef DomainT domain_type;
116 typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
118 static interval_type construct(const domain_type& lo, const domain_type& up, interval_bounds bounds)
120 return interval_type(lo, up, bounds, static_cast<interval_type*>(0) );
123 static interval_type construct_bounded(const bounded_value<DomainT>& lo,
124 const bounded_value<DomainT>& up)
128 lo.value(), up.value(),
129 lo.bound().left() | up.bound().right(),
130 static_cast<interval_type* >(0)
135 //==============================================================================
137 //==============================================================================
138 template <class DomainT, ICL_COMPARE Compare>
139 struct interval_bound_type< discrete_interval<DomainT,Compare> >
141 typedef interval_bound_type type;
142 BOOST_STATIC_CONSTANT(bound_type, value = interval_bounds::dynamic);
145 template <class DomainT, ICL_COMPARE Compare>
146 struct is_discrete_interval<discrete_interval<DomainT,Compare> >
148 typedef is_discrete_interval<discrete_interval<DomainT,Compare> > type;
149 BOOST_STATIC_CONSTANT(bool, value = is_discrete<DomainT>::value);
152 template <class DomainT, ICL_COMPARE Compare>
153 struct type_to_string<icl::discrete_interval<DomainT,Compare> >
155 static std::string apply()
156 { return "dI<"+ type_to_string<DomainT>::apply() +">"; }
159 template<class DomainT>
160 struct value_size<icl::discrete_interval<DomainT> >
162 static std::size_t apply(const icl::discrete_interval<DomainT>&)
166 }} // namespace icl boost