closed_interval.hpp 4.1 KB

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