closed_interval.hpp 4.3 KB

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