interval_set.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2007-2010: Joachim Faulhaber
  3. Copyright (c) 1999-2006: Cortex Software GmbH, Kantstrasse 57, Berlin
  4. +------------------------------------------------------------------------------+
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENCE.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. +-----------------------------------------------------------------------------*/
  9. #ifndef BOOST_ICL_INTERVAL_SET_HPP_JOFA_990223
  10. #define BOOST_ICL_INTERVAL_SET_HPP_JOFA_990223
  11. #include <boost/assert.hpp>
  12. #include <boost/icl/type_traits/is_interval_joiner.hpp>
  13. #include <boost/icl/interval_base_set.hpp>
  14. namespace boost{namespace icl
  15. {
  16. /** \brief Implements a set as a set of intervals - merging adjoining intervals */
  17. template
  18. <
  19. typename DomainT,
  20. ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT),
  21. ICL_INTERVAL(ICL_COMPARE) Interval = ICL_INTERVAL_INSTANCE(ICL_INTERVAL_DEFAULT, DomainT, Compare),
  22. ICL_ALLOC Alloc = std::allocator
  23. >
  24. class interval_set:
  25. public interval_base_set<interval_set<DomainT,Compare,Interval,Alloc>,
  26. DomainT,Compare,Interval,Alloc>
  27. {
  28. public:
  29. typedef interval_set<DomainT,Compare,Interval,Alloc> type;
  30. /// The base_type of this class
  31. typedef interval_base_set<type,DomainT,Compare,Interval,Alloc> base_type;
  32. typedef type overloadable_type;
  33. typedef type joint_type;
  34. typedef type key_object_type;
  35. /// The domain type of the set
  36. typedef DomainT domain_type;
  37. /// The codomaintype is the same as domain_type
  38. typedef DomainT codomain_type;
  39. /// The element type of the set
  40. typedef DomainT element_type;
  41. /// The interval type of the set
  42. typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) interval_type;
  43. /// The segment type of the set
  44. typedef interval_type segment_type;
  45. /// Comparison functor for domain values
  46. typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
  47. /// Comparison functor for intervals
  48. typedef exclusive_less_than<interval_type> interval_compare;
  49. /// Comparison functor for keys
  50. typedef exclusive_less_than<interval_type> key_compare;
  51. /// The allocator type of the set
  52. typedef Alloc<interval_type> allocator_type;
  53. /// allocator type of the corresponding element set
  54. typedef Alloc<DomainT> domain_allocator_type;
  55. /// The corresponding atomized type representing this interval container of elements
  56. typedef typename base_type::atomized_type atomized_type;
  57. /// Container type for the implementation
  58. typedef typename base_type::ImplSetT ImplSetT;
  59. /// key type of the implementing container
  60. typedef typename ImplSetT::key_type key_type;
  61. /// data type of the implementing container
  62. typedef typename ImplSetT::value_type data_type;
  63. /// value type of the implementing container
  64. typedef typename ImplSetT::value_type value_type;
  65. /// iterator for iteration over intervals
  66. typedef typename ImplSetT::iterator iterator;
  67. /// const_iterator for iteration over intervals
  68. typedef typename ImplSetT::const_iterator const_iterator;
  69. enum { fineness = 1 };
  70. public:
  71. //==========================================================================
  72. //= Construct, copy, destruct
  73. //==========================================================================
  74. /// Default constructor for the empty object
  75. interval_set(): base_type() {}
  76. /// Copy constructor
  77. interval_set(const interval_set& src): base_type(src) {}
  78. /// Copy constructor for base_type
  79. template<class SubType>
  80. explicit interval_set
  81. (const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  82. {
  83. this->assign(src);
  84. }
  85. /// Constructor for a single element
  86. explicit interval_set(const domain_type& value): base_type()
  87. { this->add(interval_type(value)); }
  88. /// Constructor for a single interval
  89. explicit interval_set(const interval_type& itv): base_type()
  90. {
  91. this->add(itv);
  92. }
  93. /// Assignment from a base interval_set.
  94. template<class SubType>
  95. void assign(const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  96. {
  97. typedef interval_base_set<SubType,DomainT,Compare,Interval,Alloc> base_set_type;
  98. this->clear();
  99. // Has to be implemented via add. there might be touching borders to be joined
  100. iterator prior_ = this->_set.end();
  101. ICL_const_FORALL(typename base_set_type, it_, src)
  102. prior_ = this->add(prior_, *it_);
  103. }
  104. /// Assignment operator for base type
  105. template<class SubType>
  106. interval_set& operator =
  107. (const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  108. {
  109. this->assign(src);
  110. return *this;
  111. }
  112. # ifndef BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  113. //==========================================================================
  114. //= Move semantics
  115. //==========================================================================
  116. /// Move constructor
  117. interval_set(interval_set&& src)
  118. : base_type(boost::move(src))
  119. {}
  120. /// Move assignment operator
  121. interval_set& operator = (interval_set src)
  122. {
  123. base_type::operator=(boost::move(src));
  124. return *this;
  125. }
  126. //==========================================================================
  127. # else
  128. /// Assignment operator
  129. interval_set& operator = (const interval_set& src)
  130. {
  131. base_type::operator=(src);
  132. return *this;
  133. }
  134. # endif // BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  135. private:
  136. // Private functions that shall be accessible by the baseclass:
  137. friend class
  138. interval_base_set <interval_set<DomainT,Compare,Interval,Alloc>,
  139. DomainT,Compare,Interval,Alloc>;
  140. iterator handle_inserted(iterator it_)
  141. {
  142. return segmental::join_neighbours(*this, it_);
  143. }
  144. iterator add_over(const interval_type& addend, iterator last_)
  145. {
  146. iterator joined_ = segmental::join_under(*this, addend, last_);
  147. return segmental::join_neighbours(*this, joined_);
  148. }
  149. iterator add_over(const interval_type& addend)
  150. {
  151. iterator joined_ = segmental::join_under(*this, addend);
  152. return segmental::join_neighbours(*this, joined_);
  153. }
  154. } ;
  155. //-----------------------------------------------------------------------------
  156. // type traits
  157. //-----------------------------------------------------------------------------
  158. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  159. struct is_set<icl::interval_set<DomainT,Compare,Interval,Alloc> >
  160. {
  161. typedef is_set<icl::interval_set<DomainT,Compare,Interval,Alloc> > type;
  162. BOOST_STATIC_CONSTANT(bool, value = true);
  163. };
  164. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  165. struct is_interval_container<icl::interval_set<DomainT,Compare,Interval,Alloc> >
  166. {
  167. typedef is_interval_container<icl::interval_set<DomainT,Compare,Interval,Alloc> > type;
  168. BOOST_STATIC_CONSTANT(bool, value = true);
  169. };
  170. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  171. struct is_interval_joiner<icl::interval_set<DomainT,Compare,Interval,Alloc> >
  172. {
  173. typedef is_interval_joiner<icl::interval_set<DomainT,Compare,Interval,Alloc> > type;
  174. BOOST_STATIC_CONSTANT(bool, value = true);
  175. };
  176. //-----------------------------------------------------------------------------
  177. // type representation
  178. //-----------------------------------------------------------------------------
  179. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  180. struct type_to_string<icl::interval_set<DomainT,Compare,Interval,Alloc> >
  181. {
  182. static std::string apply()
  183. { return "itv_set<"+ type_to_string<DomainT>::apply() +">"; }
  184. };
  185. }} // namespace icl boost
  186. #endif