interval.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_INTERVAL_HPP_JOFA_101014
  9. #define BOOST_ICL_INTERVAL_HPP_JOFA_101014
  10. #include <boost/icl/type_traits/interval_type_default.hpp>
  11. namespace boost{ namespace icl
  12. {
  13. template <class IntervalT, bool IsDiscrete, bound_type PretendedBounds, bound_type RepresentedBounds>
  14. struct static_interval;
  15. template <class DomainT, ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT)>
  16. struct interval
  17. {
  18. typedef typename interval_type_default<DomainT,Compare>::type interval_type;
  19. typedef interval_type type;
  20. #ifdef BOOST_ICL_USE_STATIC_BOUNDED_INTERVALS
  21. static inline interval_type open(const DomainT& low, const DomainT& up)
  22. {
  23. return
  24. static_interval
  25. < interval_type // if the domain_type is discrete ...
  26. , is_discrete<typename interval_traits<interval_type>::domain_type>::value
  27. , interval_bounds::static_open // 'pretended' bounds will be transformed to
  28. , interval_bound_type<interval_type>::value // the represented bounds
  29. >
  30. ::construct(low, up);
  31. }
  32. static inline interval_type left_open(const DomainT& low, const DomainT& up)
  33. {
  34. return
  35. static_interval
  36. < interval_type
  37. , is_discrete<typename interval_traits<interval_type>::domain_type>::value
  38. , interval_bounds::static_left_open
  39. , interval_bound_type<interval_type>::value
  40. >
  41. ::construct(low, up);
  42. }
  43. static inline interval_type right_open(const DomainT& low, const DomainT& up)
  44. {
  45. return
  46. static_interval
  47. < interval_type
  48. , is_discrete<typename interval_traits<interval_type>::domain_type>::value
  49. , interval_bounds::static_right_open
  50. , interval_bound_type<interval_type>::value
  51. >
  52. ::construct(low, up);
  53. }
  54. static inline interval_type closed(const DomainT& low, const DomainT& up)
  55. {
  56. return
  57. static_interval
  58. < interval_type
  59. , is_discrete<typename interval_traits<interval_type>::domain_type>::value
  60. , interval_bounds::static_closed
  61. , interval_bound_type<interval_type>::value
  62. >
  63. ::construct(low, up);
  64. }
  65. static inline interval_type construct(const DomainT& low, const DomainT& up)
  66. { return icl::construct<interval_type>(low, up); }
  67. #else // ICL_USE_DYNAMIC_INTERVAL_BORDER_DEFAULTS
  68. static inline interval_type right_open(const DomainT& low, const DomainT& up)
  69. { return icl::construct<interval_type>(low, up, interval_bounds::right_open()); }
  70. static inline interval_type left_open(const DomainT& low, const DomainT& up)
  71. { return icl::construct<interval_type>(low, up, interval_bounds::left_open()); }
  72. static inline interval_type open(const DomainT& low, const DomainT& up)
  73. { return icl::construct<interval_type>(low, up, interval_bounds::open()); }
  74. static inline interval_type closed(const DomainT& low, const DomainT& up)
  75. { return icl::construct<interval_type>(low, up, interval_bounds::closed()); }
  76. static inline interval_type construct(const DomainT& low, const DomainT& up)
  77. { return icl::construct<interval_type>(low, up); }
  78. #endif
  79. };
  80. template <class IntervalT, bound_type PretendedBounds, bound_type RepresentedBounds>
  81. struct static_interval<IntervalT, true, PretendedBounds, RepresentedBounds>
  82. {// is_discrete<domain_type<IntervalT>>
  83. typedef typename interval_traits<IntervalT>::domain_type domain_type;
  84. static inline IntervalT construct(const domain_type& low, const domain_type& up)
  85. {
  86. return icl::construct<IntervalT>(
  87. shift_lower(interval_bounds(PretendedBounds), interval_bounds(RepresentedBounds), low)
  88. , shift_upper(interval_bounds(PretendedBounds), interval_bounds(RepresentedBounds), up )
  89. );
  90. }
  91. };
  92. template <class IntervalT, bound_type PretendedBounds, bound_type RepresentedBounds>
  93. struct static_interval<IntervalT, false, PretendedBounds, RepresentedBounds>
  94. {// !is_discrete<domain_type<IntervalT>>
  95. typedef typename interval_traits<IntervalT>::domain_type domain_type;
  96. static inline IntervalT construct(const domain_type& low, const domain_type& up)
  97. {
  98. BOOST_STATIC_ASSERT((is_discrete<domain_type>::value || PretendedBounds==RepresentedBounds));
  99. // For domain_types that are not discrete, e.g. interval<float>
  100. // one of the following must hold: If you call
  101. // interval<T>::right_open(x,y) then interval<T>::type must be static_right_open
  102. // interval<T>::left_open(x,y) then interval<T>::type must be static_left_open
  103. // interval<T>::open(x,y) then interval<T>::type must be static_open
  104. // interval<T>::closed(x,y) then interval<T>::type must be static_closed
  105. // Conversion between 'PretendedBounds' and 'RepresentedBounds' is only possible
  106. // for discrete domain_types.
  107. return icl::construct<IntervalT>(low, up);
  108. }
  109. };
  110. }} // namespace boost icl
  111. #endif // BOOST_ICL_INTERVAL_HPP_JOFA_101014