split_interval_map.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2007-2012: 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_MAP_HPP_JOFA_000706
  10. #define BOOST_ICL_SPLIT_INTERVAL_MAP_HPP_JOFA_000706
  11. #include <boost/icl/interval_set.hpp>
  12. #include <boost/icl/interval_map.hpp>
  13. #include <boost/icl/interval_base_map.hpp>
  14. #include <boost/icl/split_interval_set.hpp>
  15. namespace boost{namespace icl
  16. {
  17. /** \brief implements a map as a map of intervals - on insertion
  18. overlapping intervals are split and associated values are combined. */
  19. template
  20. <
  21. typename DomainT,
  22. typename CodomainT,
  23. class Traits = icl::partial_absorber,
  24. ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT),
  25. ICL_COMBINE Combine = ICL_COMBINE_INSTANCE(icl::inplace_plus, CodomainT),
  26. ICL_SECTION Section = ICL_SECTION_INSTANCE(icl::inter_section, CodomainT),
  27. ICL_INTERVAL(ICL_COMPARE) Interval = ICL_INTERVAL_INSTANCE(ICL_INTERVAL_DEFAULT, DomainT, Compare),
  28. ICL_ALLOC Alloc = std::allocator
  29. >
  30. class split_interval_map:
  31. public interval_base_map<split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>,
  32. DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  33. {
  34. public:
  35. typedef Traits traits;
  36. typedef split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> type;
  37. typedef interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> joint_type;
  38. typedef type overloadable_type;
  39. typedef interval_base_map <type,
  40. DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> base_type;
  41. typedef DomainT domain_type;
  42. typedef CodomainT codomain_type;
  43. typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) interval_type;
  44. typedef typename base_type::iterator iterator;
  45. typedef typename base_type::value_type value_type;
  46. typedef typename base_type::element_type element_type;
  47. typedef typename base_type::segment_type segment_type;
  48. typedef typename base_type::domain_mapping_type domain_mapping_type;
  49. typedef typename base_type::interval_mapping_type interval_mapping_type;
  50. typedef typename base_type::ImplMapT ImplMapT;
  51. typedef typename base_type::codomain_combine codomain_combine;
  52. typedef interval_set<DomainT,Compare,Interval,Alloc> interval_set_type;
  53. typedef interval_set_type set_type;
  54. typedef set_type key_object_type;
  55. enum { fineness = 3 };
  56. public:
  57. //==========================================================================
  58. //= Construct, copy, destruct
  59. //==========================================================================
  60. /// Default constructor for the empty object
  61. split_interval_map(): base_type() {}
  62. /// Copy constructor
  63. split_interval_map(const split_interval_map& src): base_type(src) {}
  64. explicit split_interval_map(const domain_mapping_type& base_pair): base_type()
  65. { this->add(base_pair); }
  66. explicit split_interval_map(const value_type& value_pair): base_type()
  67. { this->add(value_pair); }
  68. /// Assignment operator
  69. split_interval_map& operator = (const split_interval_map& src)
  70. {
  71. base_type::operator=(src);
  72. return *this;
  73. }
  74. /// Assignment operator for base type
  75. template<class SubType>
  76. split_interval_map& operator =
  77. (const interval_base_map<SubType,DomainT,CodomainT,
  78. Traits,Compare,Combine,Section,Interval,Alloc>& src)
  79. { this->assign(src); return *this; }
  80. /// Assignment from a base interval_map.
  81. template<class SubType>
  82. void assign(const interval_base_map<SubType,DomainT,CodomainT,
  83. Traits,Compare,Combine,Section,Interval,Alloc>& src)
  84. {
  85. this->clear();
  86. this->_map.insert(src.begin(), src.end());
  87. }
  88. # ifndef BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  89. //==========================================================================
  90. //= Move semantics
  91. //==========================================================================
  92. /// Move constructor
  93. split_interval_map(split_interval_map&& src)
  94. : base_type(boost::move(src))
  95. {}
  96. /// Move assignment operator
  97. split_interval_map& operator = (split_interval_map&& src)
  98. {
  99. base_type::operator=(boost::move(src));
  100. return *this;
  101. }
  102. //==========================================================================
  103. # endif // BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  104. private:
  105. // Private functions that shall be accessible by the baseclass:
  106. friend class
  107. interval_base_map <split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>,
  108. DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>;
  109. iterator handle_inserted(iterator it_)const { return it_; }
  110. void handle_inserted(iterator, iterator)const{ }
  111. template<class Combiner>
  112. void handle_left_combined(iterator it_)
  113. {
  114. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  115. this->_map.erase(it_);
  116. }
  117. template<class Combiner>
  118. void handle_combined(iterator it_)
  119. {
  120. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  121. this->_map.erase(it_);
  122. }
  123. template<class Combiner>
  124. void handle_preceeded_combined(iterator prior_, iterator& it_)
  125. {
  126. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  127. {
  128. this->_map.erase(it_);
  129. it_ = prior_;
  130. }
  131. }
  132. template<class Combiner>
  133. void handle_succeeded_combined(iterator it_, iterator)
  134. {
  135. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  136. this->_map.erase(it_);
  137. }
  138. void handle_reinserted(iterator){}
  139. template<class Combiner>
  140. void gap_insert_at(iterator& it_, iterator prior_,
  141. const interval_type& end_gap, const codomain_type& co_val)
  142. {
  143. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  144. {
  145. this->_map.erase(it_);
  146. it_ = this->template gap_insert<Combiner>(prior_, end_gap, co_val);
  147. }
  148. else
  149. it_ = this->template gap_insert<Combiner>(it_, end_gap, co_val);
  150. }
  151. } ;
  152. //-----------------------------------------------------------------------------
  153. // type traits
  154. //-----------------------------------------------------------------------------
  155. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  156. struct is_map<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  157. {
  158. typedef is_map<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  159. BOOST_STATIC_CONSTANT(bool, value = true);
  160. };
  161. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  162. struct has_inverse<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  163. {
  164. typedef has_inverse<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  165. BOOST_STATIC_CONSTANT(bool, value = (has_inverse<CodomainT>::value));
  166. };
  167. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  168. struct is_interval_container<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  169. {
  170. typedef is_interval_container<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  171. BOOST_STATIC_CONSTANT(bool, value = true);
  172. };
  173. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  174. struct is_interval_splitter<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  175. {
  176. typedef is_interval_splitter<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  177. BOOST_STATIC_CONSTANT(bool, value = true);
  178. };
  179. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  180. struct absorbs_identities<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  181. {
  182. typedef absorbs_identities<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  183. BOOST_STATIC_CONSTANT(bool, value = (Traits::absorbs_identities));
  184. };
  185. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  186. struct is_total<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  187. {
  188. typedef is_total<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  189. BOOST_STATIC_CONSTANT(bool, value = (Traits::is_total));
  190. };
  191. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  192. struct type_to_string<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  193. {
  194. static std::string apply()
  195. {
  196. return "sp_itv_map<"+ type_to_string<DomainT>::apply() + ","
  197. + type_to_string<CodomainT>::apply() + ","
  198. + type_to_string<Traits>::apply() +">";
  199. }
  200. };
  201. }} // namespace icl boost
  202. #endif