open_interval.hpp 4.4 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_OPEN_INTERVAL_HPP_JOFA_100930
  9. #define BOOST_ICL_OPEN_INTERVAL_HPP_JOFA_100930
  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 open_interval
  23. {
  24. public:
  25. typedef open_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. open_interval()
  34. : _lwb(identity_element<DomainT>::value()), _upb(identity_element<DomainT>::value())
  35. {
  36. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  37. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  38. }
  39. //NOTE: Compiler generated copy constructor is used
  40. /** Constructor for an open singleton interval <tt>(val-1,val+1)</tt> */
  41. explicit open_interval(const DomainT& val)
  42. : _lwb(pred(val)), _upb(succ(val))
  43. {
  44. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  45. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  46. // Only for discrete types this ctor creates an interval containing
  47. // a single element only.
  48. BOOST_STATIC_ASSERT((icl::is_discrete<DomainT>::value));
  49. BOOST_ASSERT((numeric_minimum<DomainT, domain_compare, is_numeric<DomainT>::value >
  50. ::is_less_than(val) ));
  51. }
  52. /** Interval from <tt>low</tt> to <tt>up</tt> with bounds <tt>bounds</tt> */
  53. open_interval(const DomainT& low, const DomainT& up) :
  54. _lwb(low), _upb(up)
  55. {
  56. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  57. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  58. }
  59. DomainT lower()const{ return _lwb; }
  60. DomainT upper()const{ return _upb; }
  61. private:
  62. DomainT _lwb;
  63. DomainT _upb;
  64. };
  65. //==============================================================================
  66. //=T open_interval -> concept intervals
  67. //==============================================================================
  68. template<class DomainT, ICL_COMPARE Compare>
  69. struct interval_traits< icl::open_interval<DomainT, Compare> >
  70. {
  71. typedef DomainT domain_type;
  72. typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
  73. typedef icl::open_interval<DomainT, Compare> interval_type;
  74. static interval_type construct(const domain_type& lo, const domain_type& up)
  75. {
  76. return interval_type(lo, up);
  77. }
  78. static domain_type lower(const interval_type& inter_val){ return inter_val.lower(); }
  79. static domain_type upper(const interval_type& inter_val){ return inter_val.upper(); }
  80. };
  81. //==============================================================================
  82. //= Type traits
  83. //==============================================================================
  84. template <class DomainT, ICL_COMPARE Compare>
  85. struct interval_bound_type< open_interval<DomainT,Compare> >
  86. {
  87. typedef interval_bound_type type;
  88. BOOST_STATIC_CONSTANT(bound_type, value = interval_bounds::static_open);
  89. };
  90. template <class DomainT, ICL_COMPARE Compare>
  91. struct type_to_string<icl::open_interval<DomainT,Compare> >
  92. {
  93. static std::string apply()
  94. { return "(I)<"+ type_to_string<DomainT>::apply() +">"; }
  95. };
  96. template<class DomainT, ICL_COMPARE Compare>
  97. struct value_size<icl::open_interval<DomainT,Compare> >
  98. {
  99. static std::size_t apply(const icl::open_interval<DomainT>&)
  100. { return 2; }
  101. };
  102. }} // namespace icl boost
  103. #endif