split_interval_map.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 from a base interval_map.
  69. template<class SubType>
  70. void assign(const interval_base_map<SubType,DomainT,CodomainT,
  71. Traits,Compare,Combine,Section,Interval,Alloc>& src)
  72. {
  73. this->clear();
  74. this->_map.insert(src.begin(), src.end());
  75. }
  76. /// Assignment operator for base type
  77. template<class SubType>
  78. split_interval_map& operator =
  79. (const interval_base_map<SubType,DomainT,CodomainT,
  80. Traits,Compare,Combine,Section,Interval,Alloc>& src)
  81. {
  82. this->assign(src);
  83. return *this;
  84. }
  85. # ifndef BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  86. //==========================================================================
  87. //= Move semantics
  88. //==========================================================================
  89. /// Move constructor
  90. split_interval_map(split_interval_map&& src)
  91. : base_type(boost::move(src))
  92. {}
  93. /// Move assignment operator
  94. split_interval_map& operator = (split_interval_map src)
  95. {
  96. base_type::operator=(boost::move(src));
  97. return *this;
  98. }
  99. //==========================================================================
  100. # else
  101. /// Assignment operator
  102. split_interval_map& operator = (const split_interval_map& src)
  103. {
  104. base_type::operator=(src);
  105. return *this;
  106. }
  107. # endif // BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  108. private:
  109. // Private functions that shall be accessible by the baseclass:
  110. friend class
  111. interval_base_map <split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>,
  112. DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>;
  113. iterator handle_inserted(iterator it_)const { return it_; }
  114. void handle_inserted(iterator, iterator)const{ }
  115. template<class Combiner>
  116. void handle_left_combined(iterator it_)
  117. {
  118. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  119. this->_map.erase(it_);
  120. }
  121. template<class Combiner>
  122. void handle_combined(iterator it_)
  123. {
  124. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  125. this->_map.erase(it_);
  126. }
  127. template<class Combiner>
  128. void handle_preceeded_combined(iterator prior_, iterator& it_)
  129. {
  130. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  131. {
  132. this->_map.erase(it_);
  133. it_ = prior_;
  134. }
  135. }
  136. template<class Combiner>
  137. void handle_succeeded_combined(iterator it_, iterator)
  138. {
  139. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  140. this->_map.erase(it_);
  141. }
  142. void handle_reinserted(iterator){}
  143. template<class Combiner>
  144. void gap_insert_at(iterator& it_, iterator prior_,
  145. const interval_type& end_gap, const codomain_type& co_val)
  146. {
  147. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  148. {
  149. this->_map.erase(it_);
  150. it_ = this->template gap_insert<Combiner>(prior_, end_gap, co_val);
  151. }
  152. else
  153. it_ = this->template gap_insert<Combiner>(it_, end_gap, co_val);
  154. }
  155. } ;
  156. //-----------------------------------------------------------------------------
  157. // type traits
  158. //-----------------------------------------------------------------------------
  159. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  160. struct is_map<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  161. {
  162. typedef is_map<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  163. BOOST_STATIC_CONSTANT(bool, value = true);
  164. };
  165. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  166. struct has_inverse<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  167. {
  168. typedef has_inverse<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  169. BOOST_STATIC_CONSTANT(bool, value = (has_inverse<CodomainT>::value));
  170. };
  171. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  172. struct is_interval_container<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  173. {
  174. typedef is_interval_container<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  175. BOOST_STATIC_CONSTANT(bool, value = true);
  176. };
  177. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  178. struct is_interval_splitter<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  179. {
  180. typedef is_interval_splitter<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  181. BOOST_STATIC_CONSTANT(bool, value = true);
  182. };
  183. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  184. struct absorbs_identities<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  185. {
  186. typedef absorbs_identities<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  187. BOOST_STATIC_CONSTANT(bool, value = (Traits::absorbs_identities));
  188. };
  189. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  190. struct is_total<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  191. {
  192. typedef is_total<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  193. BOOST_STATIC_CONSTANT(bool, value = (Traits::is_total));
  194. };
  195. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  196. struct type_to_string<icl::split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  197. {
  198. static std::string apply()
  199. {
  200. return "sp_itv_map<"+ type_to_string<DomainT>::apply() + ","
  201. + type_to_string<CodomainT>::apply() + ","
  202. + type_to_string<Traits>::apply() +">";
  203. }
  204. };
  205. }} // namespace icl boost
  206. #endif