split_interval_set.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2007-2009: 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_SPLIT_INTERVAL_SET_HPP_JOFA_990223
  10. #define BOOST_ICL_SPLIT_INTERVAL_SET_HPP_JOFA_990223
  11. #include <boost/icl/type_traits/is_interval_splitter.hpp>
  12. #include <boost/icl/interval_base_set.hpp>
  13. #include <boost/icl/interval_set.hpp>
  14. namespace boost{namespace icl
  15. {
  16. /** \brief implements a set as a set of intervals - on insertion
  17. overlapping intervals are split */
  18. template
  19. <
  20. typename DomainT,
  21. ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT),
  22. ICL_INTERVAL(ICL_COMPARE) Interval = ICL_INTERVAL_INSTANCE(ICL_INTERVAL_DEFAULT, DomainT, Compare),
  23. ICL_ALLOC Alloc = std::allocator
  24. >
  25. class split_interval_set:
  26. public interval_base_set<split_interval_set<DomainT,Compare,Interval,Alloc>,
  27. DomainT,Compare,Interval,Alloc>
  28. {
  29. public:
  30. typedef split_interval_set<DomainT,Compare,Interval,Alloc> type;
  31. typedef interval_base_set<type,DomainT,Compare,Interval,Alloc> base_type;
  32. typedef interval_set<DomainT,Compare,Interval,Alloc> joint_type;
  33. typedef type overloadable_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 = 3 };
  70. public:
  71. //==========================================================================
  72. //= Construct, copy, destruct
  73. //==========================================================================
  74. /// Default constructor for the empty object
  75. split_interval_set(): base_type() {}
  76. /// Copy constructor
  77. split_interval_set(const split_interval_set& src): base_type(src) {}
  78. /// Copy constructor for base_type
  79. template<class SubType>
  80. split_interval_set
  81. (const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  82. { this->assign(src); }
  83. /// Constructor for a single element
  84. explicit split_interval_set(const interval_type& elem): base_type() { this->add(elem); }
  85. /// Constructor for a single interval
  86. explicit split_interval_set(const domain_type& itv): base_type() { this->add(itv); }
  87. /// Assignment from a base interval_set.
  88. template<class SubType>
  89. void assign(const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  90. {
  91. this->clear();
  92. this->_set.insert(src.begin(), src.end());
  93. }
  94. /// Assignment operator for base type
  95. template<class SubType>
  96. split_interval_set& operator =
  97. (const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  98. {
  99. this->assign(src);
  100. return *this;
  101. }
  102. # ifndef BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  103. //==========================================================================
  104. //= Move semantics
  105. //==========================================================================
  106. /// Move constructor
  107. split_interval_set(split_interval_set&& src)
  108. : base_type(boost::move(src))
  109. {}
  110. /// Move assignment operator
  111. split_interval_set& operator = (split_interval_set src)
  112. {
  113. base_type::operator=(boost::move(src));
  114. return *this;
  115. }
  116. //==========================================================================
  117. # else
  118. /// Assignment operator
  119. split_interval_set& operator = (const split_interval_set& src)
  120. {
  121. base_type::operator=(src);
  122. return *this;
  123. }
  124. # endif // BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  125. private:
  126. // Private functions that shall be accessible by the baseclass:
  127. friend class
  128. interval_base_set<split_interval_set<DomainT,Compare,Interval,Alloc>,
  129. DomainT,Compare,Interval,Alloc>;
  130. iterator handle_inserted(iterator inserted_)
  131. {
  132. return inserted_;
  133. }
  134. iterator add_over(const interval_type& addend, iterator last_)
  135. {
  136. iterator first_ = this->_set.lower_bound(addend);
  137. //BOOST_ASSERT(next(last_) == this->_set.upper_bound(inter_val));
  138. iterator it_ = first_;
  139. interval_type rest_interval = addend;
  140. this->add_front(rest_interval, it_);
  141. this->add_main (rest_interval, it_, last_);
  142. this->add_rear (rest_interval, it_);
  143. return it_;
  144. }
  145. iterator add_over(const interval_type& addend)
  146. {
  147. std::pair<iterator,iterator> overlap = this->equal_range(addend);
  148. iterator first_ = overlap.first,
  149. end_ = overlap.second,
  150. last_ = end_; --last_;
  151. iterator it_ = first_;
  152. interval_type rest_interval = addend;
  153. this->add_front(rest_interval, it_);
  154. this->add_main (rest_interval, it_, last_);
  155. this->add_rear (rest_interval, it_);
  156. return it_;
  157. }
  158. } ;
  159. //-----------------------------------------------------------------------------
  160. // type traits
  161. //-----------------------------------------------------------------------------
  162. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  163. struct is_set<icl::split_interval_set<DomainT,Compare,Interval,Alloc> >
  164. {
  165. typedef is_set<icl::split_interval_set<DomainT,Compare,Interval,Alloc> > type;
  166. BOOST_STATIC_CONSTANT(bool, value = true);
  167. };
  168. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  169. struct is_interval_container<icl::split_interval_set<DomainT,Compare,Interval,Alloc> >
  170. {
  171. typedef is_interval_container<icl::split_interval_set<DomainT,Compare,Interval,Alloc> > type;
  172. BOOST_STATIC_CONSTANT(bool, value = true);
  173. };
  174. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  175. struct is_interval_splitter<icl::split_interval_set<DomainT,Compare,Interval,Alloc> >
  176. {
  177. typedef is_interval_splitter<icl::split_interval_set<DomainT,Compare,Interval,Alloc> > type;
  178. BOOST_STATIC_CONSTANT(bool, value = true);
  179. };
  180. //-----------------------------------------------------------------------------
  181. // type representation
  182. //-----------------------------------------------------------------------------
  183. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  184. struct type_to_string<icl::split_interval_set<DomainT,Compare,Interval,Alloc> >
  185. {
  186. static std::string apply()
  187. { return "sp_itv_set<"+ type_to_string<DomainT>::apply() +">"; }
  188. };
  189. }} // namespace icl boost
  190. #endif // BOOST_ICL_SPLIT_INTERVAL_SET_HPP_JOFA_990223