interval_set.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 operator
  94. interval_set& operator = (const interval_set& src)
  95. {
  96. base_type::operator=(src);
  97. return *this;
  98. }
  99. /// Assignment operator for base type
  100. template<class SubType>
  101. interval_set& operator =
  102. (const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  103. {
  104. this->assign(src);
  105. return *this;
  106. }
  107. /// Assignment from a base interval_set.
  108. template<class SubType>
  109. void assign(const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  110. {
  111. typedef interval_base_set<SubType,DomainT,Compare,Interval,Alloc> base_set_type;
  112. this->clear();
  113. // Has to be implemented via add. there might be touching borders to be joined
  114. iterator prior_ = this->_set.end();
  115. ICL_const_FORALL(typename base_set_type, it_, src)
  116. prior_ = this->add(prior_, *it_);
  117. }
  118. # ifndef BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  119. //==========================================================================
  120. //= Move semantics
  121. //==========================================================================
  122. /// Move constructor
  123. interval_set(interval_set&& src)
  124. : base_type(boost::move(src))
  125. {}
  126. /// Move assignment operator
  127. interval_set& operator = (interval_set&& src)
  128. {
  129. base_type::operator=(boost::move(src));
  130. return *this;
  131. }
  132. //==========================================================================
  133. # endif // BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  134. private:
  135. // Private functions that shall be accessible by the baseclass:
  136. friend class
  137. interval_base_set <interval_set<DomainT,Compare,Interval,Alloc>,
  138. DomainT,Compare,Interval,Alloc>;
  139. iterator handle_inserted(iterator it_)
  140. {
  141. return segmental::join_neighbours(*this, it_);
  142. }
  143. iterator add_over(const interval_type& addend, iterator last_)
  144. {
  145. iterator joined_ = segmental::join_under(*this, addend, last_);
  146. return segmental::join_neighbours(*this, joined_);
  147. }
  148. iterator add_over(const interval_type& addend)
  149. {
  150. iterator joined_ = segmental::join_under(*this, addend);
  151. return segmental::join_neighbours(*this, joined_);
  152. }
  153. } ;
  154. //-----------------------------------------------------------------------------
  155. // type traits
  156. //-----------------------------------------------------------------------------
  157. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  158. struct is_set<icl::interval_set<DomainT,Compare,Interval,Alloc> >
  159. {
  160. typedef is_set<icl::interval_set<DomainT,Compare,Interval,Alloc> > type;
  161. BOOST_STATIC_CONSTANT(bool, value = true);
  162. };
  163. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  164. struct is_interval_container<icl::interval_set<DomainT,Compare,Interval,Alloc> >
  165. {
  166. typedef is_interval_container<icl::interval_set<DomainT,Compare,Interval,Alloc> > type;
  167. BOOST_STATIC_CONSTANT(bool, value = true);
  168. };
  169. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  170. struct is_interval_joiner<icl::interval_set<DomainT,Compare,Interval,Alloc> >
  171. {
  172. typedef is_interval_joiner<icl::interval_set<DomainT,Compare,Interval,Alloc> > type;
  173. BOOST_STATIC_CONSTANT(bool, value = true);
  174. };
  175. //-----------------------------------------------------------------------------
  176. // type representation
  177. //-----------------------------------------------------------------------------
  178. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  179. struct type_to_string<icl::interval_set<DomainT,Compare,Interval,Alloc> >
  180. {
  181. static std::string apply()
  182. { return "itv_set<"+ type_to_string<DomainT>::apply() +">"; }
  183. };
  184. }} // namespace icl boost
  185. #endif