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