left_open_interval.hpp 4.4 KB

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