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 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/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> 22 23 namespace boost{namespace icl 24 { 25 26 template <class DomainT, 27 ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT)> 28 class discrete_interval 29 { 30 public: 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; 35 36 public: 37 //========================================================================== 38 //= Construct, copy, destruct 39 //========================================================================== 40 /** Default constructor; yields an empty interval <tt>[0,0)</tt>. */ discrete_interval()41 discrete_interval() 42 : _lwb(identity_element<DomainT>::value()), _upb(identity_element<DomainT>::value()) 43 , _bounds(interval_bounds::right_open()) 44 { 45 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>)); 46 BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>)); 47 BOOST_STATIC_ASSERT((icl::is_discrete<DomainT>::value)); 48 } 49 50 //NOTE: Compiler generated copy constructor is used 51 52 /** Constructor for a closed singleton interval <tt>[val,val]</tt> */ discrete_interval(const DomainT & val)53 explicit discrete_interval(const DomainT& val) 54 : _lwb(val), _upb(val), _bounds(interval_bounds::closed()) 55 { 56 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>)); 57 BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>)); 58 BOOST_STATIC_ASSERT((icl::is_discrete<DomainT>::value)); 59 } 60 61 /** Interval from <tt>low</tt> to <tt>up</tt> with bounds <tt>bounds</tt> */ discrete_interval(const DomainT & low,const DomainT & up,interval_bounds bounds=interval_bounds::right_open (),discrete_interval * =0)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) 66 { 67 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>)); 68 BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>)); 69 BOOST_STATIC_ASSERT((icl::is_discrete<DomainT>::value)); 70 } 71 lower() const72 domain_type lower()const { return _lwb; } upper() const73 domain_type upper()const { return _upb; } bounds() const74 interval_bounds bounds()const{ return _bounds; } 75 open(const DomainT & lo,const DomainT & up)76 static discrete_interval open (const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::open()); } right_open(const DomainT & lo,const DomainT & up)77 static discrete_interval right_open(const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::right_open());} left_open(const DomainT & lo,const DomainT & up)78 static discrete_interval left_open (const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::left_open()); } closed(const DomainT & lo,const DomainT & up)79 static discrete_interval closed (const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::closed()); } 80 81 private: 82 domain_type _lwb; 83 domain_type _upb; 84 interval_bounds _bounds; 85 }; 86 87 //============================================================================== 88 //=T discrete_interval -> concept intervals 89 //============================================================================== 90 template<class DomainT, ICL_COMPARE Compare> 91 struct interval_traits< icl::discrete_interval<DomainT, Compare> > 92 { 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; 97 constructboost::icl::interval_traits98 static interval_type construct(const domain_type& lo, const domain_type& up) 99 { 100 return interval_type(lo, up); 101 } 102 lowerboost::icl::interval_traits103 static domain_type lower(const interval_type& inter_val){ return inter_val.lower(); } upperboost::icl::interval_traits104 static domain_type upper(const interval_type& inter_val){ return inter_val.upper(); } 105 }; 106 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> > 112 { 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; 117 constructboost::icl::dynamic_interval_traits118 static interval_type construct(const domain_type& lo, const domain_type& up, interval_bounds bounds) 119 { 120 return interval_type(lo, up, bounds, static_cast<interval_type*>(0) ); 121 } 122 construct_boundedboost::icl::dynamic_interval_traits123 static interval_type construct_bounded(const bounded_value<DomainT>& lo, 124 const bounded_value<DomainT>& up) 125 { 126 return interval_type 127 ( 128 lo.value(), up.value(), 129 lo.bound().left() | up.bound().right(), 130 static_cast<interval_type* >(0) 131 ); 132 } 133 }; 134 135 //============================================================================== 136 //= Type traits 137 //============================================================================== 138 template <class DomainT, ICL_COMPARE Compare> 139 struct interval_bound_type< discrete_interval<DomainT,Compare> > 140 { 141 typedef interval_bound_type type; 142 BOOST_STATIC_CONSTANT(bound_type, value = interval_bounds::dynamic); 143 }; 144 145 template <class DomainT, ICL_COMPARE Compare> 146 struct is_discrete_interval<discrete_interval<DomainT,Compare> > 147 { 148 typedef is_discrete_interval<discrete_interval<DomainT,Compare> > type; 149 BOOST_STATIC_CONSTANT(bool, value = is_discrete<DomainT>::value); 150 }; 151 152 template <class DomainT, ICL_COMPARE Compare> 153 struct type_to_string<icl::discrete_interval<DomainT,Compare> > 154 { applyboost::icl::type_to_string155 static std::string apply() 156 { return "dI<"+ type_to_string<DomainT>::apply() +">"; } 157 }; 158 159 template<class DomainT> 160 struct value_size<icl::discrete_interval<DomainT> > 161 { applyboost::icl::value_size162 static std::size_t apply(const icl::discrete_interval<DomainT>&) 163 { return 2; } 164 }; 165 166 }} // namespace icl boost 167 168 #endif 169 170