interval_map.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2008-2012: Joachim Faulhaber
  3. +------------------------------------------------------------------------------+
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENCE.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. +-----------------------------------------------------------------------------*/
  8. #ifndef BOOST_ICL_INTERVAL_MAP_HPP_JOFA_080705
  9. #define BOOST_ICL_INTERVAL_MAP_HPP_JOFA_080705
  10. #include <boost/assert.hpp>
  11. #include <boost/icl/type_traits/is_map.hpp>
  12. #include <boost/icl/interval_set.hpp>
  13. #include <boost/icl/interval_base_map.hpp>
  14. namespace boost{namespace icl
  15. {
  16. template<class DomainT, class CodomainT, class Traits,
  17. ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section,
  18. ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  19. class split_interval_map;
  20. /** \brief implements a map as a map of intervals - on insertion
  21. overlapping intervals are split and associated values are combined.*/
  22. template
  23. <
  24. typename DomainT,
  25. typename CodomainT,
  26. class Traits = icl::partial_absorber,
  27. ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT),
  28. ICL_COMBINE Combine = ICL_COMBINE_INSTANCE(icl::inplace_plus, CodomainT),
  29. ICL_SECTION Section = ICL_SECTION_INSTANCE(icl::inter_section, CodomainT),
  30. ICL_INTERVAL(ICL_COMPARE) Interval = ICL_INTERVAL_INSTANCE(ICL_INTERVAL_DEFAULT, DomainT, Compare),
  31. ICL_ALLOC Alloc = std::allocator
  32. >
  33. class interval_map:
  34. public interval_base_map<interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>,
  35. DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  36. {
  37. public:
  38. typedef Traits traits;
  39. typedef interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> type;
  40. typedef split_interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> split_type;
  41. typedef type overloadable_type;
  42. typedef type joint_type;
  43. typedef interval_base_map<type,
  44. DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> base_type;
  45. typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) interval_type;
  46. typedef typename base_type::iterator iterator;
  47. typedef typename base_type::value_type value_type;
  48. typedef typename base_type::element_type element_type;
  49. typedef typename base_type::segment_type segment_type;
  50. typedef typename base_type::domain_type domain_type;
  51. typedef typename base_type::codomain_type codomain_type;
  52. typedef typename base_type::domain_mapping_type domain_mapping_type;
  53. typedef typename base_type::interval_mapping_type interval_mapping_type;
  54. typedef typename base_type::ImplMapT ImplMapT;
  55. typedef typename base_type::size_type size_type;
  56. typedef typename base_type::codomain_combine codomain_combine;
  57. typedef interval_set<DomainT,Compare,Interval,Alloc> interval_set_type;
  58. typedef interval_set_type set_type;
  59. typedef set_type key_object_type;
  60. enum { fineness = 1 };
  61. public:
  62. //==========================================================================
  63. //= Construct, copy, destruct
  64. //==========================================================================
  65. /// Default constructor for the empty object
  66. interval_map(): base_type() {}
  67. /// Copy constructor
  68. interval_map(const interval_map& src): base_type(src) {}
  69. /// Copy constructor for base_type
  70. template<class SubType>
  71. explicit interval_map
  72. (const interval_base_map<SubType,DomainT,CodomainT,
  73. Traits,Compare,Combine,Section,Interval,Alloc>& src)
  74. { this->assign(src); }
  75. explicit interval_map(const domain_mapping_type& base_pair): base_type()
  76. { this->add(base_pair); }
  77. explicit interval_map(const value_type& value_pair): base_type()
  78. { this->add(value_pair); }
  79. /// Assignment from a base interval_map.
  80. template<class SubType>
  81. void assign(const interval_base_map<SubType,DomainT,CodomainT,
  82. Traits,Compare,Combine,Section,Interval,Alloc>& src)
  83. {
  84. typedef interval_base_map<SubType,DomainT,CodomainT,
  85. Traits,Compare,Combine,Section,Interval,Alloc> base_map_type;
  86. this->clear();
  87. iterator prior_ = this->_map.end();
  88. ICL_const_FORALL(typename base_map_type, it_, src)
  89. prior_ = this->add(prior_, *it_);
  90. }
  91. /// Assignment operator for base type
  92. template<class SubType>
  93. interval_map& operator =
  94. (const interval_base_map<SubType,DomainT,CodomainT,
  95. Traits,Compare,Combine,Section,Interval,Alloc>& src)
  96. {
  97. this->assign(src);
  98. return *this;
  99. }
  100. # ifndef BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  101. //==========================================================================
  102. //= Move semantics
  103. //==========================================================================
  104. /// Move constructor
  105. interval_map(interval_map&& src)
  106. : base_type(boost::move(src))
  107. {}
  108. /// Move assignment operator
  109. interval_map& operator = (interval_map src)
  110. {
  111. base_type::operator=(boost::move(src));
  112. return *this;
  113. }
  114. //==========================================================================
  115. # else
  116. /// Assignment operator
  117. interval_map& operator = (const interval_map& src)
  118. {
  119. base_type::operator=(src);
  120. return *this;
  121. }
  122. # endif // BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  123. private:
  124. // Private functions that shall be accessible by the baseclass:
  125. friend class
  126. interval_base_map <interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>,
  127. DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>;
  128. iterator handle_inserted(iterator it_)
  129. {
  130. return segmental::join_neighbours(*this, it_);
  131. }
  132. void handle_inserted(iterator prior_, iterator it_)
  133. {
  134. if(prior_ != this->_map.end() && segmental::joinable(*this, prior_, it_))
  135. segmental::join_on_right(*this, prior_, it_);
  136. }
  137. template<class Combiner>
  138. void handle_left_combined(iterator it_)
  139. {
  140. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  141. this->_map.erase(it_);
  142. else
  143. segmental::join_left(*this, it_);
  144. }
  145. template<class Combiner>
  146. void handle_combined(iterator it_)
  147. {
  148. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  149. this->_map.erase(it_);
  150. else
  151. segmental::join_neighbours(*this, it_);
  152. }
  153. template<class Combiner>
  154. void handle_preceeded_combined(iterator prior_, iterator& it_)
  155. {
  156. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  157. {
  158. this->_map.erase(it_);
  159. it_ = prior_;
  160. }
  161. else // After a new combination (e.g. combiner=max) joining neighbours may be possible
  162. segmental::join_neighbours(*this, it_);
  163. }
  164. template<class Combiner>
  165. void handle_succeeded_combined(iterator it_, iterator next_)
  166. {
  167. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  168. {
  169. this->_map.erase(it_);
  170. segmental::join_right(*this, next_);
  171. }
  172. else
  173. {
  174. segmental::join_left(*this, it_);
  175. segmental::join_neighbours(*this, next_);
  176. }
  177. }
  178. void handle_reinserted(iterator insertion_)
  179. {
  180. segmental::join_right(*this, insertion_);
  181. }
  182. template<class Combiner>
  183. void gap_insert_at(iterator& it_, iterator prior_,
  184. const interval_type& end_gap, const codomain_type& co_val)
  185. {
  186. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable((*it_).second))
  187. {
  188. this->_map.erase(it_);
  189. it_ = this->template gap_insert<Combiner>(prior_, end_gap, co_val);
  190. segmental::join_right(*this, it_);
  191. }
  192. else
  193. {
  194. segmental::join_left(*this, it_);
  195. iterator inserted_ = this->template gap_insert<Combiner>(it_, end_gap, co_val);
  196. it_ = segmental::join_neighbours(*this, inserted_);
  197. }
  198. }
  199. } ;
  200. //-----------------------------------------------------------------------------
  201. // type traits
  202. //-----------------------------------------------------------------------------
  203. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  204. struct is_map<icl::interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  205. {
  206. typedef is_map<icl::interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  207. BOOST_STATIC_CONSTANT(bool, value = true);
  208. };
  209. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  210. struct has_inverse<icl::interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  211. {
  212. typedef has_inverse<icl::interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  213. BOOST_STATIC_CONSTANT(bool, value = (has_inverse<CodomainT>::value));
  214. };
  215. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  216. struct is_interval_container<icl::interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  217. {
  218. typedef is_interval_container<icl::interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  219. BOOST_STATIC_CONSTANT(bool, value = true);
  220. };
  221. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  222. struct absorbs_identities<icl::interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  223. {
  224. typedef absorbs_identities<icl::interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  225. BOOST_STATIC_CONSTANT(bool, value = (Traits::absorbs_identities));
  226. };
  227. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  228. struct is_total<icl::interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  229. {
  230. typedef is_total<icl::interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  231. BOOST_STATIC_CONSTANT(bool, value = (Traits::is_total));
  232. };
  233. //-----------------------------------------------------------------------------
  234. // type representation
  235. //-----------------------------------------------------------------------------
  236. template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  237. struct type_to_string<icl::interval_map<DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  238. {
  239. static std::string apply()
  240. {
  241. return "itv_map<"+ type_to_string<DomainT>::apply() + ","
  242. + type_to_string<CodomainT>::apply() + ","
  243. + type_to_string<Traits>::apply() + ">";
  244. }
  245. };
  246. }} // namespace icl boost
  247. #endif