closed_interval.hpp 4.2 KB

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