split_interval_set.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 operator
  88. split_interval_set& operator = (const split_interval_set& src)
  89. {
  90. base_type::operator=(src);
  91. return *this;
  92. }
  93. /// Assignment operator for base type
  94. template<class SubType>
  95. split_interval_set& operator =
  96. (const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  97. { this->assign(src); return *this; }
  98. /// Assignment from a base interval_set.
  99. template<class SubType>
  100. void assign(const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  101. {
  102. this->clear();
  103. this->_set.insert(src.begin(), src.end());
  104. }
  105. # ifndef BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  106. //==========================================================================
  107. //= Move semantics
  108. //==========================================================================
  109. /// Move constructor
  110. split_interval_set(split_interval_set&& src)
  111. : base_type(boost::move(src))
  112. {}
  113. /// Move assignment operator
  114. split_interval_set& operator = (split_interval_set&& src)
  115. {
  116. base_type::operator=(boost::move(src));
  117. return *this;
  118. }
  119. //==========================================================================
  120. # endif // BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  121. private:
  122. // Private functions that shall be accessible by the baseclass:
  123. friend class
  124. interval_base_set<split_interval_set<DomainT,Compare,Interval,Alloc>,
  125. DomainT,Compare,Interval,Alloc>;
  126. iterator handle_inserted(iterator inserted_)
  127. {
  128. return inserted_;
  129. }
  130. iterator add_over(const interval_type& addend, iterator last_)
  131. {
  132. iterator first_ = this->_set.lower_bound(addend);
  133. //BOOST_ASSERT(next(last_) == this->_set.upper_bound(inter_val));
  134. iterator it_ = first_;
  135. interval_type rest_interval = addend;
  136. this->add_front(rest_interval, it_);
  137. this->add_main (rest_interval, it_, last_);
  138. this->add_rear (rest_interval, it_);
  139. return it_;
  140. }
  141. iterator add_over(const interval_type& addend)
  142. {
  143. std::pair<iterator,iterator> overlap = this->equal_range(addend);
  144. iterator first_ = overlap.first,
  145. end_ = overlap.second,
  146. last_ = end_; --last_;
  147. iterator it_ = first_;
  148. interval_type rest_interval = addend;
  149. this->add_front(rest_interval, it_);
  150. this->add_main (rest_interval, it_, last_);
  151. this->add_rear (rest_interval, it_);
  152. return it_;
  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::split_interval_set<DomainT,Compare,Interval,Alloc> >
  160. {
  161. typedef is_set<icl::split_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::split_interval_set<DomainT,Compare,Interval,Alloc> >
  166. {
  167. typedef is_interval_container<icl::split_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_splitter<icl::split_interval_set<DomainT,Compare,Interval,Alloc> >
  172. {
  173. typedef is_interval_splitter<icl::split_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::split_interval_set<DomainT,Compare,Interval,Alloc> >
  181. {
  182. static std::string apply()
  183. { return "sp_itv_set<"+ type_to_string<DomainT>::apply() +">"; }
  184. };
  185. }} // namespace icl boost
  186. #endif // BOOST_ICL_SPLIT_INTERVAL_SET_HPP_JOFA_990223