continuous_interval.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_CONTINUOUS_INTERVAL_HPP_JOFA_100327
  9. #define BOOST_ICL_CONTINUOUS_INTERVAL_HPP_JOFA_100327
  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/concept/container.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_continuous_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 continuous_interval
  26. {
  27. public:
  28. typedef continuous_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. continuous_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_continuous<DomainT>::value));
  44. }
  45. //NOTE: Compiler generated copy constructor is used
  46. /** Constructor for a closed singleton interval <tt>[val,val]</tt> */
  47. explicit continuous_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_continuous<DomainT>::value));
  53. }
  54. /** Interval from <tt>low</tt> to <tt>up</tt> with bounds <tt>bounds</tt> */
  55. continuous_interval(const DomainT& low, const DomainT& up,
  56. interval_bounds bounds = interval_bounds::right_open(),
  57. continuous_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_continuous<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 continuous_interval open (const DomainT& lo, const DomainT& up){ return continuous_interval(lo, up, interval_bounds::open()); }
  68. static continuous_interval right_open(const DomainT& lo, const DomainT& up){ return continuous_interval(lo, up, interval_bounds::right_open());}
  69. static continuous_interval left_open (const DomainT& lo, const DomainT& up){ return continuous_interval(lo, up, interval_bounds::left_open()); }
  70. static continuous_interval closed (const DomainT& lo, const DomainT& up){ return continuous_interval(lo, up, interval_bounds::closed()); }
  71. private:
  72. domain_type _lwb;
  73. domain_type _upb;
  74. interval_bounds _bounds;
  75. };
  76. //==============================================================================
  77. //=T continuous_interval -> concept interval
  78. //==============================================================================
  79. template<class DomainT, ICL_COMPARE Compare>
  80. struct interval_traits< icl::continuous_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::continuous_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 continuous_interval -> concept dynamic_interval
  95. //==============================================================================
  96. template<class DomainT, ICL_COMPARE Compare>
  97. struct dynamic_interval_traits<boost::icl::continuous_interval<DomainT,Compare> >
  98. {
  99. typedef dynamic_interval_traits type;
  100. typedef boost::icl::continuous_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 icl::continuous_interval<DomainT,Compare>(lo, up, bounds,
  106. static_cast<icl::continuous_interval<DomainT,Compare>* >(0) );
  107. }
  108. static interval_type construct_bounded(const bounded_value<DomainT>& lo,
  109. const bounded_value<DomainT>& up)
  110. {
  111. return icl::continuous_interval<DomainT,Compare>
  112. (
  113. lo.value(), up.value(),
  114. lo.bound().left() | up.bound().right(),
  115. static_cast<icl::continuous_interval<DomainT,Compare>* >(0)
  116. );
  117. }
  118. };
  119. //==============================================================================
  120. //= Type traits
  121. //==============================================================================
  122. template <class DomainT, ICL_COMPARE Compare>
  123. struct interval_bound_type< continuous_interval<DomainT,Compare> >
  124. {
  125. typedef interval_bound_type type;
  126. BOOST_STATIC_CONSTANT(bound_type, value = interval_bounds::dynamic);
  127. };
  128. template <class DomainT, ICL_COMPARE Compare>
  129. struct is_continuous_interval<continuous_interval<DomainT,Compare> >
  130. {
  131. typedef is_continuous_interval<continuous_interval<DomainT,Compare> > type;
  132. BOOST_STATIC_CONSTANT(bool, value = true);
  133. };
  134. template <class DomainT, ICL_COMPARE Compare>
  135. struct type_to_string<icl::continuous_interval<DomainT,Compare> >
  136. {
  137. static std::string apply()
  138. { return "cI<"+ type_to_string<DomainT>::apply() +">"; }
  139. };
  140. template<class DomainT>
  141. struct value_size<icl::continuous_interval<DomainT> >
  142. {
  143. static std::size_t apply(const icl::continuous_interval<DomainT>&)
  144. { return 2; }
  145. };
  146. }} // namespace icl boost
  147. #endif