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_LEFT_OPEN_INTERVAL_HPP_JOFA_100930 9 #define BOOST_ICL_LEFT_OPEN_INTERVAL_HPP_JOFA_100930 10 11 #include <functional> 12 #include <boost/concept/assert.hpp> 13 #include <boost/icl/concept/interval.hpp> 14 #include <boost/icl/type_traits/value_size.hpp> 15 #include <boost/icl/type_traits/type_to_string.hpp> 16 17 namespace boost{namespace icl 18 { 19 20 template <class DomainT, 21 ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT)> 22 class left_open_interval 23 { 24 public: 25 typedef left_open_interval<DomainT,Compare> type; 26 typedef DomainT domain_type; 27 typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare; 28 29 public: 30 //========================================================================== 31 //= Construct, copy, destruct 32 //========================================================================== 33 /** Default constructor; yields an empty interval <tt>(0,0]</tt>. */ left_open_interval()34 left_open_interval() 35 : _lwb(identity_element<DomainT>::value()), _upb(identity_element<DomainT>::value()) 36 { 37 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>)); 38 BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>)); 39 } 40 41 //NOTE: Compiler generated copy constructor is used 42 43 /** Constructor for a left-open singleton interval <tt>(val-1,val]</tt> */ left_open_interval(const DomainT & val)44 explicit left_open_interval(const DomainT& val) 45 : _lwb(predecessor<DomainT,domain_compare>::apply(val)), _upb(val) 46 { 47 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>)); 48 BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>)); 49 // Only for discrete types this ctor creates an interval containing 50 // a single element only. 51 BOOST_STATIC_ASSERT((icl::is_discrete<DomainT>::value)); 52 BOOST_ASSERT((numeric_minimum<DomainT, domain_compare, is_numeric<DomainT>::value > 53 ::is_less_than(val) )); 54 } 55 56 /** Interval from <tt>low</tt> to <tt>up</tt> with bounds <tt>bounds</tt> */ left_open_interval(const DomainT & low,const DomainT & up)57 left_open_interval(const DomainT& low, const DomainT& up) : 58 _lwb(low), _upb(up) 59 { 60 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>)); 61 BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>)); 62 } 63 lower() const64 DomainT lower()const{ return _lwb; } upper() const65 DomainT upper()const{ return _upb; } 66 67 private: 68 DomainT _lwb; 69 DomainT _upb; 70 }; 71 72 //============================================================================== 73 //=T left_open_interval -> concept intervals 74 //============================================================================== 75 template<class DomainT, ICL_COMPARE Compare> 76 struct interval_traits< icl::left_open_interval<DomainT, Compare> > 77 { 78 typedef DomainT domain_type; 79 typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare; 80 typedef icl::left_open_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 //============================================================================== 93 //= Type traits 94 //============================================================================== 95 template <class DomainT, ICL_COMPARE Compare> 96 struct interval_bound_type< left_open_interval<DomainT,Compare> > 97 { 98 typedef interval_bound_type type; 99 BOOST_STATIC_CONSTANT(bound_type, value = interval_bounds::static_left_open); 100 }; 101 102 template <class DomainT, ICL_COMPARE Compare> 103 struct type_to_string<icl::left_open_interval<DomainT,Compare> > 104 { applyboost::icl::type_to_string105 static std::string apply() 106 { return "(I]<"+ type_to_string<DomainT>::apply() +">"; } 107 }; 108 109 template<class DomainT, ICL_COMPARE Compare> 110 struct value_size<icl::left_open_interval<DomainT,Compare> > 111 { applyboost::icl::value_size112 static std::size_t apply(const icl::left_open_interval<DomainT>&) 113 { return 2; } 114 }; 115 116 }} // namespace icl boost 117 118 #endif 119 120