discrete_interval.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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_DISCRETE_INTERVAL_HPP_JOFA_100403
  9. #define BOOST_ICL_DISCRETE_INTERVAL_HPP_JOFA_100403
  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/type_traits/succ_pred.hpp>
  15. #include <boost/icl/concept/interval.hpp>
  16. #include <boost/icl/type_traits/value_size.hpp>
  17. #include <boost/icl/type_traits/type_to_string.hpp>
  18. #include <boost/icl/type_traits/is_continuous.hpp>
  19. #include <boost/icl/type_traits/is_discrete_interval.hpp>
  20. #include <boost/icl/interval_bounds.hpp>
  21. namespace boost{namespace icl
  22. {
  23. template <class DomainT,
  24. ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT)>
  25. class discrete_interval
  26. {
  27. public:
  28. typedef discrete_interval<DomainT,Compare> type;
  29. typedef DomainT domain_type;
  30. typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
  31. typedef typename bounded_value<DomainT>::type bounded_domain_type;
  32. public:
  33. //==========================================================================
  34. //= Construct, copy, destruct
  35. //==========================================================================
  36. /** Default constructor; yields an empty interval <tt>[0,0)</tt>. */
  37. discrete_interval()
  38. : _lwb(identity_element<DomainT>::value()), _upb(identity_element<DomainT>::value())
  39. , _bounds(interval_bounds::right_open())
  40. {
  41. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  42. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  43. BOOST_STATIC_ASSERT((icl::is_discrete<DomainT>::value));
  44. }
  45. //NOTE: Compiler generated copy constructor is used
  46. /** Constructor for a closed singleton interval <tt>[val,val]</tt> */
  47. explicit discrete_interval(const DomainT& val)
  48. : _lwb(val), _upb(val), _bounds(interval_bounds::closed())
  49. {
  50. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  51. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  52. BOOST_STATIC_ASSERT((icl::is_discrete<DomainT>::value));
  53. }
  54. /** Interval from <tt>low</tt> to <tt>up</tt> with bounds <tt>bounds</tt> */
  55. discrete_interval(const DomainT& low, const DomainT& up,
  56. interval_bounds bounds = interval_bounds::right_open(),
  57. discrete_interval* = 0)
  58. : _lwb(low), _upb(up), _bounds(bounds)
  59. {
  60. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  61. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  62. BOOST_STATIC_ASSERT((icl::is_discrete<DomainT>::value));
  63. }
  64. domain_type lower()const { return _lwb; }
  65. domain_type upper()const { return _upb; }
  66. interval_bounds bounds()const{ return _bounds; }
  67. static discrete_interval open (const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::open()); }
  68. static discrete_interval right_open(const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::right_open());}
  69. static discrete_interval left_open (const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::left_open()); }
  70. static discrete_interval closed (const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::closed()); }
  71. private:
  72. domain_type _lwb;
  73. domain_type _upb;
  74. interval_bounds _bounds;
  75. };
  76. //==============================================================================
  77. //=T discrete_interval -> concept intervals
  78. //==============================================================================
  79. template<class DomainT, ICL_COMPARE Compare>
  80. struct interval_traits< icl::discrete_interval<DomainT, Compare> >
  81. {
  82. typedef interval_traits type;
  83. typedef DomainT domain_type;
  84. typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
  85. typedef icl::discrete_interval<DomainT, Compare> interval_type;
  86. static interval_type construct(const domain_type& lo, const domain_type& up)
  87. {
  88. return interval_type(lo, up);
  89. }
  90. static domain_type lower(const interval_type& inter_val){ return inter_val.lower(); }
  91. static domain_type upper(const interval_type& inter_val){ return inter_val.upper(); }
  92. };
  93. //==============================================================================
  94. //=T discrete_interval -> concept dynamic_interval_traits
  95. //==============================================================================
  96. template<class DomainT, ICL_COMPARE Compare>
  97. struct dynamic_interval_traits<boost::icl::discrete_interval<DomainT,Compare> >
  98. {
  99. typedef dynamic_interval_traits type;
  100. typedef boost::icl::discrete_interval<DomainT,Compare> interval_type;
  101. typedef DomainT domain_type;
  102. typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
  103. static interval_type construct(const domain_type& lo, const domain_type& up, interval_bounds bounds)
  104. {
  105. return interval_type(lo, up, bounds, static_cast<interval_type*>(0) );
  106. }
  107. static interval_type construct_bounded(const bounded_value<DomainT>& lo,
  108. const bounded_value<DomainT>& up)
  109. {
  110. return interval_type
  111. (
  112. lo.value(), up.value(),
  113. lo.bound().left() | up.bound().right(),
  114. static_cast<interval_type* >(0)
  115. );
  116. }
  117. };
  118. //==============================================================================
  119. //= Type traits
  120. //==============================================================================
  121. template <class DomainT, ICL_COMPARE Compare>
  122. struct interval_bound_type< discrete_interval<DomainT,Compare> >
  123. {
  124. typedef interval_bound_type type;
  125. BOOST_STATIC_CONSTANT(bound_type, value = interval_bounds::dynamic);
  126. };
  127. template <class DomainT, ICL_COMPARE Compare>
  128. struct is_discrete_interval<discrete_interval<DomainT,Compare> >
  129. {
  130. typedef is_discrete_interval<discrete_interval<DomainT,Compare> > type;
  131. BOOST_STATIC_CONSTANT(bool, value = is_discrete<DomainT>::value);
  132. };
  133. template <class DomainT, ICL_COMPARE Compare>
  134. struct type_to_string<icl::discrete_interval<DomainT,Compare> >
  135. {
  136. static std::string apply()
  137. { return "dI<"+ type_to_string<DomainT>::apply() +">"; }
  138. };
  139. template<class DomainT>
  140. struct value_size<icl::discrete_interval<DomainT> >
  141. {
  142. static std::size_t apply(const icl::discrete_interval<DomainT>&)
  143. { return 2; }
  144. };
  145. }} // namespace icl boost
  146. #endif