interval_base_map.hpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  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_INTERVAL_BASE_MAP_HPP_JOFA_990223
  10. #define BOOST_ICL_INTERVAL_BASE_MAP_HPP_JOFA_990223
  11. #include <limits>
  12. #include <boost/mpl/and.hpp>
  13. #include <boost/mpl/or.hpp>
  14. #include <boost/mpl/not.hpp>
  15. #include <boost/icl/detail/notate.hpp>
  16. #include <boost/icl/detail/design_config.hpp>
  17. #include <boost/icl/detail/on_absorbtion.hpp>
  18. #include <boost/icl/detail/interval_map_algo.hpp>
  19. #include <boost/icl/associative_interval_container.hpp>
  20. #include <boost/icl/type_traits/is_interval_splitter.hpp>
  21. #include <boost/icl/map.hpp>
  22. namespace boost{namespace icl
  23. {
  24. template<class DomainT, class CodomainT>
  25. struct mapping_pair
  26. {
  27. DomainT key;
  28. CodomainT data;
  29. mapping_pair():key(), data(){}
  30. mapping_pair(const DomainT& key_value, const CodomainT& data_value)
  31. :key(key_value), data(data_value){}
  32. mapping_pair(const std::pair<DomainT,CodomainT>& std_pair)
  33. :key(std_pair.first), data(std_pair.second){}
  34. };
  35. /** \brief Implements a map as a map of intervals (base class) */
  36. template
  37. <
  38. class SubType,
  39. typename DomainT,
  40. typename CodomainT,
  41. class Traits = icl::partial_absorber,
  42. ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT),
  43. ICL_COMBINE Combine = ICL_COMBINE_INSTANCE(icl::inplace_plus, CodomainT),
  44. ICL_SECTION Section = ICL_SECTION_INSTANCE(icl::inter_section, CodomainT),
  45. ICL_INTERVAL(ICL_COMPARE) Interval = ICL_INTERVAL_INSTANCE(ICL_INTERVAL_DEFAULT, DomainT, Compare),
  46. ICL_ALLOC Alloc = std::allocator
  47. >
  48. class interval_base_map
  49. {
  50. public:
  51. //==========================================================================
  52. //= Associated types
  53. //==========================================================================
  54. typedef interval_base_map<SubType,DomainT,CodomainT,
  55. Traits,Compare,Combine,Section,Interval,Alloc>
  56. type;
  57. /// The designated \e derived or \e sub_type of this base class
  58. typedef SubType sub_type;
  59. /// Auxilliary type for overloadresolution
  60. typedef type overloadable_type;
  61. /// Traits of an itl map
  62. typedef Traits traits;
  63. //--------------------------------------------------------------------------
  64. //- Associated types: Related types
  65. //--------------------------------------------------------------------------
  66. /// The atomized type representing the corresponding container of elements
  67. typedef typename icl::map<DomainT,CodomainT,
  68. Traits,Compare,Combine,Section,Alloc> atomized_type;
  69. //--------------------------------------------------------------------------
  70. //- Associated types: Data
  71. //--------------------------------------------------------------------------
  72. /// Domain type (type of the keys) of the map
  73. typedef DomainT domain_type;
  74. typedef typename boost::call_traits<DomainT>::param_type domain_param;
  75. /// Domain type (type of the keys) of the map
  76. typedef CodomainT codomain_type;
  77. /// Auxiliary type to help the compiler resolve ambiguities when using std::make_pair
  78. typedef mapping_pair<domain_type,codomain_type> domain_mapping_type;
  79. /// Conceptual is a map a set of elements of type \c element_type
  80. typedef domain_mapping_type element_type;
  81. /// The interval type of the map
  82. typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) interval_type;
  83. /// Auxiliary type for overload resolution
  84. typedef std::pair<interval_type,CodomainT> interval_mapping_type;
  85. /// Type of an interval containers segment, that is spanned by an interval
  86. typedef std::pair<interval_type,CodomainT> segment_type;
  87. //--------------------------------------------------------------------------
  88. //- Associated types: Size
  89. //--------------------------------------------------------------------------
  90. /// The difference type of an interval which is sometimes different form the domain_type
  91. typedef typename difference_type_of<domain_type>::type difference_type;
  92. /// The size type of an interval which is mostly std::size_t
  93. typedef typename size_type_of<domain_type>::type size_type;
  94. //--------------------------------------------------------------------------
  95. //- Associated types: Functors
  96. //--------------------------------------------------------------------------
  97. /// Comparison functor for domain values
  98. typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
  99. typedef ICL_COMPARE_DOMAIN(Compare,segment_type) segment_compare;
  100. /// Combine functor for codomain value aggregation
  101. typedef ICL_COMBINE_CODOMAIN(Combine,CodomainT) codomain_combine;
  102. /// Inverse Combine functor for codomain value aggregation
  103. typedef typename inverse<codomain_combine>::type inverse_codomain_combine;
  104. /// Intersection functor for codomain values
  105. typedef typename mpl::if_
  106. <has_set_semantics<codomain_type>
  107. , ICL_SECTION_CODOMAIN(Section,CodomainT)
  108. , codomain_combine
  109. >::type codomain_intersect;
  110. /// Inverse Combine functor for codomain value intersection
  111. typedef typename inverse<codomain_intersect>::type inverse_codomain_intersect;
  112. /// Comparison functor for intervals which are keys as well
  113. typedef exclusive_less_than<interval_type> interval_compare;
  114. /// Comparison functor for keys
  115. typedef exclusive_less_than<interval_type> key_compare;
  116. //--------------------------------------------------------------------------
  117. //- Associated types: Implementation and stl related
  118. //--------------------------------------------------------------------------
  119. /// The allocator type of the set
  120. typedef Alloc<std::pair<const interval_type, codomain_type> >
  121. allocator_type;
  122. /// Container type for the implementation
  123. typedef ICL_IMPL_SPACE::map<interval_type,codomain_type,
  124. key_compare,allocator_type> ImplMapT;
  125. /// key type of the implementing container
  126. typedef typename ImplMapT::key_type key_type;
  127. /// value type of the implementing container
  128. typedef typename ImplMapT::value_type value_type;
  129. /// data type of the implementing container
  130. typedef typename ImplMapT::value_type::second_type data_type;
  131. /// pointer type
  132. typedef typename ImplMapT::pointer pointer;
  133. /// const pointer type
  134. typedef typename ImplMapT::const_pointer const_pointer;
  135. /// reference type
  136. typedef typename ImplMapT::reference reference;
  137. /// const reference type
  138. typedef typename ImplMapT::const_reference const_reference;
  139. /// iterator for iteration over intervals
  140. typedef typename ImplMapT::iterator iterator;
  141. /// const_iterator for iteration over intervals
  142. typedef typename ImplMapT::const_iterator const_iterator;
  143. /// iterator for reverse iteration over intervals
  144. typedef typename ImplMapT::reverse_iterator reverse_iterator;
  145. /// const_iterator for iteration over intervals
  146. typedef typename ImplMapT::const_reverse_iterator const_reverse_iterator;
  147. /// element iterator: Depreciated, see documentation.
  148. typedef boost::icl::element_iterator<iterator> element_iterator;
  149. /// const element iterator: Depreciated, see documentation.
  150. typedef boost::icl::element_iterator<const_iterator> element_const_iterator;
  151. /// element reverse iterator: Depreciated, see documentation.
  152. typedef boost::icl::element_iterator<reverse_iterator> element_reverse_iterator;
  153. /// element const reverse iterator: Depreciated, see documentation.
  154. typedef boost::icl::element_iterator<const_reverse_iterator> element_const_reverse_iterator;
  155. typedef typename on_absorbtion<type, codomain_combine,
  156. Traits::absorbs_identities>::type on_codomain_absorbtion;
  157. public:
  158. BOOST_STATIC_CONSTANT(bool,
  159. is_total_invertible = ( Traits::is_total
  160. && has_inverse<codomain_type>::value));
  161. BOOST_STATIC_CONSTANT(int, fineness = 0);
  162. public:
  163. //==========================================================================
  164. //= Construct, copy, destruct
  165. //==========================================================================
  166. /** Default constructor for the empty object */
  167. interval_base_map()
  168. {
  169. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  170. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  171. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<CodomainT>));
  172. BOOST_CONCEPT_ASSERT((EqualComparableConcept<CodomainT>));
  173. }
  174. /** Copy constructor */
  175. interval_base_map(const interval_base_map& src): _map(src._map)
  176. {
  177. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  178. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  179. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<CodomainT>));
  180. BOOST_CONCEPT_ASSERT((EqualComparableConcept<CodomainT>));
  181. }
  182. # ifndef BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  183. //==========================================================================
  184. //= Move semantics
  185. //==========================================================================
  186. /** Move constructor */
  187. interval_base_map(interval_base_map&& src): _map(boost::move(src._map))
  188. {
  189. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  190. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  191. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<CodomainT>));
  192. BOOST_CONCEPT_ASSERT((EqualComparableConcept<CodomainT>));
  193. }
  194. /** Move assignment operator */
  195. interval_base_map& operator = (interval_base_map src)
  196. { //call by value sice 'src' is a "sink value"
  197. this->_map = boost::move(src._map);
  198. return *this;
  199. }
  200. //==========================================================================
  201. # else
  202. /** Copy assignment operator */
  203. interval_base_map& operator = (const interval_base_map& src)
  204. {
  205. this->_map = src._map;
  206. return *this;
  207. }
  208. # endif // BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  209. /** swap the content of containers */
  210. void swap(interval_base_map& object) { _map.swap(object._map); }
  211. //==========================================================================
  212. //= Containedness
  213. //==========================================================================
  214. /** clear the map */
  215. void clear() { icl::clear(*that()); }
  216. /** is the map empty? */
  217. bool empty()const { return icl::is_empty(*that()); }
  218. //==========================================================================
  219. //= Size
  220. //==========================================================================
  221. /** An interval map's size is it's cardinality */
  222. size_type size()const
  223. {
  224. return icl::cardinality(*that());
  225. }
  226. /** Size of the iteration over this container */
  227. std::size_t iterative_size()const
  228. {
  229. return _map.size();
  230. }
  231. //==========================================================================
  232. //= Selection
  233. //==========================================================================
  234. /** Find the interval value pair, that contains \c key */
  235. const_iterator find(const domain_type& key_value)const
  236. {
  237. return icl::find(*this, key_value);
  238. }
  239. /** Find the first interval value pair, that collides with interval
  240. \c key_interval */
  241. const_iterator find(const interval_type& key_interval)const
  242. {
  243. return _map.find(key_interval);
  244. }
  245. /** Total select function. */
  246. codomain_type operator()(const domain_type& key_value)const
  247. {
  248. const_iterator it_ = icl::find(*this, key_value);
  249. return it_==end() ? identity_element<codomain_type>::value()
  250. : (*it_).second;
  251. }
  252. //==========================================================================
  253. //= Addition
  254. //==========================================================================
  255. /** Addition of a key value pair to the map */
  256. SubType& add(const element_type& key_value_pair)
  257. {
  258. return icl::add(*that(), key_value_pair);
  259. }
  260. /** Addition of an interval value pair to the map. */
  261. SubType& add(const segment_type& interval_value_pair)
  262. {
  263. this->template _add<codomain_combine>(interval_value_pair);
  264. return *that();
  265. }
  266. /** Addition of an interval value pair \c interval_value_pair to the map.
  267. Iterator \c prior_ is a hint to the position \c interval_value_pair can be
  268. inserted after. */
  269. iterator add(iterator prior_, const segment_type& interval_value_pair)
  270. {
  271. return this->template _add<codomain_combine>(prior_, interval_value_pair);
  272. }
  273. //==========================================================================
  274. //= Subtraction
  275. //==========================================================================
  276. /** Subtraction of a key value pair from the map */
  277. SubType& subtract(const element_type& key_value_pair)
  278. {
  279. return icl::subtract(*that(), key_value_pair);
  280. }
  281. /** Subtraction of an interval value pair from the map. */
  282. SubType& subtract(const segment_type& interval_value_pair)
  283. {
  284. on_invertible<type, is_total_invertible>
  285. ::subtract(*that(), interval_value_pair);
  286. return *that();
  287. }
  288. //==========================================================================
  289. //= Insertion
  290. //==========================================================================
  291. /** Insertion of a \c key_value_pair into the map. */
  292. SubType& insert(const element_type& key_value_pair)
  293. {
  294. return icl::insert(*that(), key_value_pair);
  295. }
  296. /** Insertion of an \c interval_value_pair into the map. */
  297. SubType& insert(const segment_type& interval_value_pair)
  298. {
  299. _insert(interval_value_pair);
  300. return *that();
  301. }
  302. /** Insertion of an \c interval_value_pair into the map. Iterator \c prior_.
  303. serves as a hint to insert after the element \c prior point to. */
  304. iterator insert(iterator prior, const segment_type& interval_value_pair)
  305. {
  306. return _insert(prior, interval_value_pair);
  307. }
  308. /** With <tt>key_value_pair = (k,v)</tt> set value \c v for key \c k */
  309. SubType& set(const element_type& key_value_pair)
  310. {
  311. return icl::set_at(*that(), key_value_pair);
  312. }
  313. /** With <tt>interval_value_pair = (I,v)</tt> set value \c v
  314. for all keys in interval \c I in the map. */
  315. SubType& set(const segment_type& interval_value_pair)
  316. {
  317. return icl::set_at(*that(), interval_value_pair);
  318. }
  319. //==========================================================================
  320. //= Erasure
  321. //==========================================================================
  322. /** Erase a \c key_value_pair from the map. */
  323. SubType& erase(const element_type& key_value_pair)
  324. {
  325. icl::erase(*that(), key_value_pair);
  326. return *that();
  327. }
  328. /** Erase an \c interval_value_pair from the map. */
  329. SubType& erase(const segment_type& interval_value_pair);
  330. /** Erase a key value pair for \c key. */
  331. SubType& erase(const domain_type& key)
  332. {
  333. return icl::erase(*that(), key);
  334. }
  335. /** Erase all value pairs within the range of the
  336. interval <tt>inter_val</tt> from the map. */
  337. SubType& erase(const interval_type& inter_val);
  338. /** Erase all value pairs within the range of the interval that iterator
  339. \c position points to. */
  340. void erase(iterator position){ this->_map.erase(position); }
  341. /** Erase all value pairs for a range of iterators <tt>[first,past)</tt>. */
  342. void erase(iterator first, iterator past){ this->_map.erase(first, past); }
  343. //==========================================================================
  344. //= Intersection
  345. //==========================================================================
  346. /** The intersection of \c interval_value_pair and \c *this map is added to \c section. */
  347. void add_intersection(SubType& section, const segment_type& interval_value_pair)const
  348. {
  349. on_definedness<SubType, Traits::is_total>
  350. ::add_intersection(section, *that(), interval_value_pair);
  351. }
  352. //==========================================================================
  353. //= Symmetric difference
  354. //==========================================================================
  355. /** If \c *this map contains \c key_value_pair it is erased, otherwise it is added. */
  356. SubType& flip(const element_type& key_value_pair)
  357. {
  358. return icl::flip(*that(), key_value_pair);
  359. }
  360. /** If \c *this map contains \c interval_value_pair it is erased, otherwise it is added. */
  361. SubType& flip(const segment_type& interval_value_pair)
  362. {
  363. on_total_absorbable<SubType, Traits::is_total, Traits::absorbs_identities>
  364. ::flip(*that(), interval_value_pair);
  365. return *that();
  366. }
  367. //==========================================================================
  368. //= Iterator related
  369. //==========================================================================
  370. iterator lower_bound(const key_type& interval)
  371. { return _map.lower_bound(interval); }
  372. iterator upper_bound(const key_type& interval)
  373. { return _map.upper_bound(interval); }
  374. const_iterator lower_bound(const key_type& interval)const
  375. { return _map.lower_bound(interval); }
  376. const_iterator upper_bound(const key_type& interval)const
  377. { return _map.upper_bound(interval); }
  378. std::pair<iterator,iterator> equal_range(const key_type& interval)
  379. {
  380. return std::pair<iterator,iterator>
  381. (lower_bound(interval), upper_bound(interval));
  382. }
  383. std::pair<const_iterator,const_iterator>
  384. equal_range(const key_type& interval)const
  385. {
  386. return std::pair<const_iterator,const_iterator>
  387. (lower_bound(interval), upper_bound(interval));
  388. }
  389. iterator begin() { return _map.begin(); }
  390. iterator end() { return _map.end(); }
  391. const_iterator begin()const { return _map.begin(); }
  392. const_iterator end()const { return _map.end(); }
  393. reverse_iterator rbegin() { return _map.rbegin(); }
  394. reverse_iterator rend() { return _map.rend(); }
  395. const_reverse_iterator rbegin()const { return _map.rbegin(); }
  396. const_reverse_iterator rend()const { return _map.rend(); }
  397. private:
  398. template<class Combiner>
  399. iterator _add(const segment_type& interval_value_pair);
  400. template<class Combiner>
  401. iterator _add(iterator prior_, const segment_type& interval_value_pair);
  402. template<class Combiner>
  403. void _subtract(const segment_type& interval_value_pair);
  404. iterator _insert(const segment_type& interval_value_pair);
  405. iterator _insert(iterator prior_, const segment_type& interval_value_pair);
  406. private:
  407. template<class Combiner>
  408. void add_segment(const interval_type& inter_val, const CodomainT& co_val, iterator& it_);
  409. template<class Combiner>
  410. void add_main(interval_type& inter_val, const CodomainT& co_val,
  411. iterator& it_, const iterator& last_);
  412. template<class Combiner>
  413. void add_rear(const interval_type& inter_val, const CodomainT& co_val, iterator& it_);
  414. void add_front(const interval_type& inter_val, iterator& first_);
  415. private:
  416. void subtract_front(const interval_type& inter_val, iterator& first_);
  417. template<class Combiner>
  418. void subtract_main(const CodomainT& co_val, iterator& it_, const iterator& last_);
  419. template<class Combiner>
  420. void subtract_rear(interval_type& inter_val, const CodomainT& co_val, iterator& it_);
  421. private:
  422. void insert_main(const interval_type&, const CodomainT&, iterator&, const iterator&);
  423. void erase_rest ( interval_type&, const CodomainT&, iterator&, const iterator&);
  424. template<class FragmentT>
  425. void total_add_intersection(SubType& section, const FragmentT& fragment)const
  426. {
  427. section += *that();
  428. section.add(fragment);
  429. }
  430. void partial_add_intersection(SubType& section, const segment_type& operand)const
  431. {
  432. interval_type inter_val = operand.first;
  433. if(icl::is_empty(inter_val))
  434. return;
  435. std::pair<const_iterator, const_iterator> exterior = equal_range(inter_val);
  436. if(exterior.first == exterior.second)
  437. return;
  438. for(const_iterator it_=exterior.first; it_ != exterior.second; it_++)
  439. {
  440. interval_type common_interval = (*it_).first & inter_val;
  441. if(!icl::is_empty(common_interval))
  442. {
  443. section.template _add<codomain_combine> (value_type(common_interval, (*it_).second) );
  444. section.template _add<codomain_intersect>(value_type(common_interval, operand.second));
  445. }
  446. }
  447. }
  448. void partial_add_intersection(SubType& section, const element_type& operand)const
  449. {
  450. partial_add_intersection(section, make_segment<type>(operand));
  451. }
  452. protected:
  453. template <class Combiner>
  454. iterator gap_insert(iterator prior_, const interval_type& inter_val,
  455. const codomain_type& co_val )
  456. {
  457. // inter_val is not conained in this map. Insertion will be successful
  458. BOOST_ASSERT(this->_map.find(inter_val) == this->_map.end());
  459. BOOST_ASSERT((!on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable(co_val)));
  460. return this->_map.insert(prior_, value_type(inter_val, version<Combiner>()(co_val)));
  461. }
  462. template <class Combiner>
  463. std::pair<iterator, bool>
  464. add_at(const iterator& prior_, const interval_type& inter_val,
  465. const codomain_type& co_val )
  466. {
  467. // Never try to insert an identity element into an identity element absorber here:
  468. BOOST_ASSERT((!(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable(co_val))));
  469. iterator inserted_
  470. = this->_map.insert(prior_, value_type(inter_val, Combiner::identity_element()));
  471. if((*inserted_).first == inter_val && (*inserted_).second == Combiner::identity_element())
  472. {
  473. Combiner()((*inserted_).second, co_val);
  474. return std::pair<iterator,bool>(inserted_, true);
  475. }
  476. else
  477. return std::pair<iterator,bool>(inserted_, false);
  478. }
  479. std::pair<iterator, bool>
  480. insert_at(const iterator& prior_, const interval_type& inter_val,
  481. const codomain_type& co_val )
  482. {
  483. iterator inserted_
  484. = this->_map.insert(prior_, value_type(inter_val, co_val));
  485. if(inserted_ == prior_)
  486. return std::pair<iterator,bool>(inserted_, false);
  487. else if((*inserted_).first == inter_val)
  488. return std::pair<iterator,bool>(inserted_, true);
  489. else
  490. return std::pair<iterator,bool>(inserted_, false);
  491. }
  492. protected:
  493. sub_type* that() { return static_cast<sub_type*>(this); }
  494. const sub_type* that()const { return static_cast<const sub_type*>(this); }
  495. protected:
  496. ImplMapT _map;
  497. private:
  498. //--------------------------------------------------------------------------
  499. template<class Type, bool is_total_invertible>
  500. struct on_invertible;
  501. template<class Type>
  502. struct on_invertible<Type, true>
  503. {
  504. typedef typename Type::segment_type segment_type;
  505. typedef typename Type::inverse_codomain_combine inverse_codomain_combine;
  506. static void subtract(Type& object, const segment_type& operand)
  507. { object.template _add<inverse_codomain_combine>(operand); }
  508. };
  509. template<class Type>
  510. struct on_invertible<Type, false>
  511. {
  512. typedef typename Type::segment_type segment_type;
  513. typedef typename Type::inverse_codomain_combine inverse_codomain_combine;
  514. static void subtract(Type& object, const segment_type& operand)
  515. { object.template _subtract<inverse_codomain_combine>(operand); }
  516. };
  517. friend struct on_invertible<type, true>;
  518. friend struct on_invertible<type, false>;
  519. //--------------------------------------------------------------------------
  520. //--------------------------------------------------------------------------
  521. template<class Type, bool is_total>
  522. struct on_definedness;
  523. template<class Type>
  524. struct on_definedness<Type, true>
  525. {
  526. static void add_intersection(Type& section, const Type& object,
  527. const segment_type& operand)
  528. { object.total_add_intersection(section, operand); }
  529. };
  530. template<class Type>
  531. struct on_definedness<Type, false>
  532. {
  533. static void add_intersection(Type& section, const Type& object,
  534. const segment_type& operand)
  535. { object.partial_add_intersection(section, operand); }
  536. };
  537. friend struct on_definedness<type, true>;
  538. friend struct on_definedness<type, false>;
  539. //--------------------------------------------------------------------------
  540. //--------------------------------------------------------------------------
  541. template<class Type, bool has_set_semantics>
  542. struct on_codomain_model;
  543. template<class Type>
  544. struct on_codomain_model<Type, true>
  545. {
  546. typedef typename Type::interval_type interval_type;
  547. typedef typename Type::codomain_type codomain_type;
  548. typedef typename Type::segment_type segment_type;
  549. typedef typename Type::codomain_combine codomain_combine;
  550. typedef typename Type::inverse_codomain_intersect inverse_codomain_intersect;
  551. static void add(Type& intersection, interval_type& common_interval,
  552. const codomain_type& flip_value, const codomain_type& co_value)
  553. {
  554. codomain_type common_value = flip_value;
  555. inverse_codomain_intersect()(common_value, co_value);
  556. intersection.template
  557. _add<codomain_combine>(segment_type(common_interval, common_value));
  558. }
  559. };
  560. template<class Type>
  561. struct on_codomain_model<Type, false>
  562. {
  563. typedef typename Type::interval_type interval_type;
  564. typedef typename Type::codomain_type codomain_type;
  565. typedef typename Type::segment_type segment_type;
  566. typedef typename Type::codomain_combine codomain_combine;
  567. static void add(Type& intersection, interval_type& common_interval,
  568. const codomain_type&, const codomain_type&)
  569. {
  570. intersection.template
  571. _add<codomain_combine>(segment_type(common_interval,
  572. identity_element<codomain_type>::value()));
  573. }
  574. };
  575. friend struct on_codomain_model<type, true>;
  576. friend struct on_codomain_model<type, false>;
  577. //--------------------------------------------------------------------------
  578. //--------------------------------------------------------------------------
  579. template<class Type, bool is_total, bool absorbs_identities>
  580. struct on_total_absorbable;
  581. template<class Type>
  582. struct on_total_absorbable<Type, true, true>
  583. {
  584. static void flip(Type& object, const typename Type::segment_type&)
  585. { icl::clear(object); }
  586. };
  587. #ifdef BOOST_MSVC
  588. #pragma warning(push)
  589. #pragma warning(disable:4127) // conditional expression is constant
  590. #endif
  591. template<class Type>
  592. struct on_total_absorbable<Type, true, false>
  593. {
  594. typedef typename Type::segment_type segment_type;
  595. typedef typename Type::codomain_type codomain_type;
  596. static void flip(Type& object, const segment_type& operand)
  597. {
  598. object += operand;
  599. ICL_FORALL(typename Type, it_, object)
  600. (*it_).second = identity_element<codomain_type>::value();
  601. if(mpl::not_<is_interval_splitter<Type> >::value)
  602. icl::join(object);
  603. }
  604. };
  605. #ifdef BOOST_MSVC
  606. #pragma warning(pop)
  607. #endif
  608. template<class Type, bool absorbs_identities>
  609. struct on_total_absorbable<Type, false, absorbs_identities>
  610. {
  611. typedef typename Type::segment_type segment_type;
  612. typedef typename Type::codomain_type codomain_type;
  613. typedef typename Type::interval_type interval_type;
  614. typedef typename Type::value_type value_type;
  615. typedef typename Type::const_iterator const_iterator;
  616. typedef typename Type::set_type set_type;
  617. typedef typename Type::inverse_codomain_intersect inverse_codomain_intersect;
  618. static void flip(Type& object, const segment_type& interval_value_pair)
  619. {
  620. // That which is common shall be subtracted
  621. // That which is not shall be added
  622. // So interval_value_pair has to be 'complementary added' or flipped
  623. interval_type span = interval_value_pair.first;
  624. std::pair<const_iterator, const_iterator> exterior
  625. = object.equal_range(span);
  626. const_iterator first_ = exterior.first;
  627. const_iterator end_ = exterior.second;
  628. interval_type covered, left_over, common_interval;
  629. const codomain_type& x_value = interval_value_pair.second;
  630. const_iterator it_ = first_;
  631. set_type eraser;
  632. Type intersection;
  633. while(it_ != end_ )
  634. {
  635. const codomain_type& co_value = (*it_).second;
  636. covered = (*it_++).first;
  637. //[a ... : span
  638. // [b ... : covered
  639. //[a b) : left_over
  640. left_over = right_subtract(span, covered);
  641. //That which is common ...
  642. common_interval = span & covered;
  643. if(!icl::is_empty(common_interval))
  644. {
  645. // ... shall be subtracted
  646. icl::add(eraser, common_interval);
  647. on_codomain_model<Type, has_set_semantics<codomain_type>::value>
  648. ::add(intersection, common_interval, x_value, co_value);
  649. }
  650. icl::add(object, value_type(left_over, x_value)); //That which is not shall be added
  651. // Because this is a collision free addition I don't have to distinguish codomain_types.
  652. //... d) : span
  653. //... c) : covered
  654. // [c d) : span'
  655. span = left_subtract(span, covered);
  656. }
  657. //If span is not empty here, it is not in the set so it shall be added
  658. icl::add(object, value_type(span, x_value));
  659. //finally rewrite the common segments
  660. icl::erase(object, eraser);
  661. object += intersection;
  662. }
  663. };
  664. //--------------------------------------------------------------------------
  665. } ;
  666. //==============================================================================
  667. //= Addition detail
  668. //==============================================================================
  669. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  670. inline void interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  671. ::add_front(const interval_type& inter_val, iterator& first_)
  672. {
  673. // If the collision sequence has a left residual 'left_resid' it will
  674. // be split, to provide a standardized start of algorithms:
  675. // The addend interval 'inter_val' covers the beginning of the collision sequence.
  676. // only for the first there can be a left_resid: a part of *first_ left of inter_val
  677. interval_type left_resid = right_subtract((*first_).first, inter_val);
  678. if(!icl::is_empty(left_resid))
  679. { // [------------ . . .
  680. // [left_resid---first_ --- . . .
  681. iterator prior_ = cyclic_prior(*this, first_);
  682. const_cast<interval_type&>((*first_).first)
  683. = left_subtract((*first_).first, left_resid);
  684. //NOTE: Only splitting
  685. this->_map.insert(prior_, segment_type(left_resid, (*first_).second));
  686. }
  687. //POST:
  688. // [----- inter_val ---- . . .
  689. // ...[-- first_ --...
  690. }
  691. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  692. template<class Combiner>
  693. inline void interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  694. ::add_segment(const interval_type& inter_val, const CodomainT& co_val, iterator& it_)
  695. {
  696. interval_type lead_gap = right_subtract(inter_val, (*it_).first);
  697. if(!icl::is_empty(lead_gap))
  698. {
  699. // [lead_gap--- . . .
  700. // [-- it_ ...
  701. iterator prior_ = it_==this->_map.begin()? it_ : prior(it_);
  702. iterator inserted_ = this->template gap_insert<Combiner>(prior_, lead_gap, co_val);
  703. that()->handle_inserted(prior_, inserted_);
  704. }
  705. // . . . --------- . . . addend interval
  706. // [-- it_ --) has a common part with the first overval
  707. Combiner()((*it_).second, co_val);
  708. that()->template handle_left_combined<Combiner>(it_++);
  709. }
  710. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  711. template<class Combiner>
  712. inline void interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  713. ::add_main(interval_type& inter_val, const CodomainT& co_val,
  714. iterator& it_, const iterator& last_)
  715. {
  716. interval_type cur_interval;
  717. while(it_!=last_)
  718. {
  719. cur_interval = (*it_).first ;
  720. add_segment<Combiner>(inter_val, co_val, it_);
  721. // shrink interval
  722. inter_val = left_subtract(inter_val, cur_interval);
  723. }
  724. }
  725. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  726. template<class Combiner>
  727. inline void interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  728. ::add_rear(const interval_type& inter_val, const CodomainT& co_val, iterator& it_)
  729. {
  730. iterator prior_ = cyclic_prior(*that(), it_);
  731. interval_type cur_itv = (*it_).first ;
  732. interval_type lead_gap = right_subtract(inter_val, cur_itv);
  733. if(!icl::is_empty(lead_gap))
  734. { // [lead_gap--- . . .
  735. // [prior) [-- it_ ...
  736. iterator inserted_ = this->template gap_insert<Combiner>(prior_, lead_gap, co_val);
  737. that()->handle_inserted(prior_, inserted_);
  738. }
  739. interval_type end_gap = left_subtract(inter_val, cur_itv);
  740. if(!icl::is_empty(end_gap))
  741. {
  742. // [----------------end_gap)
  743. // . . . -- it_ --)
  744. Combiner()((*it_).second, co_val);
  745. that()->template gap_insert_at<Combiner>(it_, prior_, end_gap, co_val);
  746. }
  747. else
  748. {
  749. // only for the last there can be a right_resid: a part of *it_ right of x
  750. interval_type right_resid = left_subtract(cur_itv, inter_val);
  751. if(icl::is_empty(right_resid))
  752. {
  753. // [---------------)
  754. // [-- it_ ---)
  755. Combiner()((*it_).second, co_val);
  756. that()->template handle_preceeded_combined<Combiner>(prior_, it_);
  757. }
  758. else
  759. {
  760. // [--------------)
  761. // [-- it_ --right_resid)
  762. const_cast<interval_type&>((*it_).first) = right_subtract((*it_).first, right_resid);
  763. //NOTE: This is NOT an insertion that has to take care for correct application of
  764. // the Combiner functor. It only reestablished that state after splitting the
  765. // 'it_' interval value pair. Using _map_insert<Combiner> does not work here.
  766. iterator insertion_ = this->_map.insert(it_, value_type(right_resid, (*it_).second));
  767. that()->handle_reinserted(insertion_);
  768. Combiner()((*it_).second, co_val);
  769. that()->template handle_preceeded_combined<Combiner>(insertion_, it_);
  770. }
  771. }
  772. }
  773. //==============================================================================
  774. //= Addition
  775. //==============================================================================
  776. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  777. template<class Combiner>
  778. inline typename interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>::iterator
  779. interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  780. ::_add(const segment_type& addend)
  781. {
  782. typedef typename on_absorbtion<type,Combiner,
  783. absorbs_identities<type>::value>::type on_absorbtion_;
  784. const interval_type& inter_val = addend.first;
  785. if(icl::is_empty(inter_val))
  786. return this->_map.end();
  787. const codomain_type& co_val = addend.second;
  788. if(on_absorbtion_::is_absorbable(co_val))
  789. return this->_map.end();
  790. std::pair<iterator,bool> insertion
  791. = this->_map.insert(value_type(inter_val, version<Combiner>()(co_val)));
  792. if(insertion.second)
  793. return that()->handle_inserted(insertion.first);
  794. else
  795. {
  796. // Detect the first and the end iterator of the collision sequence
  797. iterator first_ = this->_map.lower_bound(inter_val),
  798. last_ = prior(this->_map.upper_bound(inter_val));
  799. //assert(end_ == this->_map.upper_bound(inter_val));
  800. iterator it_ = first_;
  801. interval_type rest_interval = inter_val;
  802. add_front (rest_interval, it_ );
  803. add_main<Combiner>(rest_interval, co_val, it_, last_);
  804. add_rear<Combiner>(rest_interval, co_val, it_ );
  805. return it_;
  806. }
  807. }
  808. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  809. template<class Combiner>
  810. inline typename interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>::iterator
  811. interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  812. ::_add(iterator prior_, const segment_type& addend)
  813. {
  814. typedef typename on_absorbtion<type,Combiner,
  815. absorbs_identities<type>::value>::type on_absorbtion_;
  816. const interval_type& inter_val = addend.first;
  817. if(icl::is_empty(inter_val))
  818. return prior_;
  819. const codomain_type& co_val = addend.second;
  820. if(on_absorbtion_::is_absorbable(co_val))
  821. return prior_;
  822. std::pair<iterator,bool> insertion
  823. = add_at<Combiner>(prior_, inter_val, co_val);
  824. if(insertion.second)
  825. return that()->handle_inserted(insertion.first);
  826. else
  827. {
  828. // Detect the first and the end iterator of the collision sequence
  829. std::pair<iterator,iterator> overlap = equal_range(inter_val);
  830. iterator it_ = overlap.first,
  831. last_ = prior(overlap.second);
  832. interval_type rest_interval = inter_val;
  833. add_front (rest_interval, it_ );
  834. add_main<Combiner>(rest_interval, co_val, it_, last_);
  835. add_rear<Combiner>(rest_interval, co_val, it_ );
  836. return it_;
  837. }
  838. }
  839. //==============================================================================
  840. //= Subtraction detail
  841. //==============================================================================
  842. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  843. inline void interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  844. ::subtract_front(const interval_type& inter_val, iterator& it_)
  845. {
  846. interval_type left_resid = right_subtract((*it_).first, inter_val);
  847. if(!icl::is_empty(left_resid)) // [--- inter_val ---)
  848. { //[prior_) [left_resid)[--- it_ . . .
  849. iterator prior_ = cyclic_prior(*this, it_);
  850. const_cast<interval_type&>((*it_).first) = left_subtract((*it_).first, left_resid);
  851. this->_map.insert(prior_, value_type(left_resid, (*it_).second));
  852. // The segemnt *it_ is split at inter_val.first(), so as an invariant
  853. // segment *it_ is always "under" inter_val and a left_resid is empty.
  854. }
  855. }
  856. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  857. template<class Combiner>
  858. inline void interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  859. ::subtract_main(const CodomainT& co_val, iterator& it_, const iterator& last_)
  860. {
  861. while(it_ != last_)
  862. {
  863. Combiner()((*it_).second, co_val);
  864. that()->template handle_left_combined<Combiner>(it_++);
  865. }
  866. }
  867. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  868. template<class Combiner>
  869. inline void interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  870. ::subtract_rear(interval_type& inter_val, const CodomainT& co_val, iterator& it_)
  871. {
  872. interval_type right_resid = left_subtract((*it_).first, inter_val);
  873. if(icl::is_empty(right_resid))
  874. {
  875. Combiner()((*it_).second, co_val);
  876. that()->template handle_combined<Combiner>(it_);
  877. }
  878. else
  879. {
  880. const_cast<interval_type&>((*it_).first) = right_subtract((*it_).first, right_resid);
  881. iterator next_ = this->_map.insert(it_, value_type(right_resid, (*it_).second));
  882. Combiner()((*it_).second, co_val);
  883. that()->template handle_succeeded_combined<Combiner>(it_, next_);
  884. }
  885. }
  886. //==============================================================================
  887. //= Subtraction
  888. //==============================================================================
  889. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  890. template<class Combiner>
  891. inline void interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  892. ::_subtract(const segment_type& minuend)
  893. {
  894. interval_type inter_val = minuend.first;
  895. if(icl::is_empty(inter_val))
  896. return;
  897. const codomain_type& co_val = minuend.second;
  898. if(on_absorbtion<type,Combiner,Traits::absorbs_identities>::is_absorbable(co_val))
  899. return;
  900. std::pair<iterator, iterator> exterior = equal_range(inter_val);
  901. if(exterior.first == exterior.second)
  902. return;
  903. iterator last_ = prior(exterior.second);
  904. iterator it_ = exterior.first;
  905. subtract_front (inter_val, it_ );
  906. subtract_main <Combiner>( co_val, it_, last_);
  907. subtract_rear <Combiner>(inter_val, co_val, it_ );
  908. }
  909. //==============================================================================
  910. //= Insertion
  911. //==============================================================================
  912. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  913. inline void interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  914. ::insert_main(const interval_type& inter_val, const CodomainT& co_val,
  915. iterator& it_, const iterator& last_)
  916. {
  917. iterator end_ = boost::next(last_);
  918. iterator prior_ = cyclic_prior(*this,it_), inserted_;
  919. interval_type rest_interval = inter_val, left_gap, cur_itv;
  920. interval_type last_interval = last_ ->first;
  921. while(it_ != end_ )
  922. {
  923. cur_itv = (*it_).first ;
  924. left_gap = right_subtract(rest_interval, cur_itv);
  925. if(!icl::is_empty(left_gap))
  926. {
  927. inserted_ = this->_map.insert(prior_, value_type(left_gap, co_val));
  928. it_ = that()->handle_inserted(inserted_);
  929. }
  930. // shrink interval
  931. rest_interval = left_subtract(rest_interval, cur_itv);
  932. prior_ = it_;
  933. ++it_;
  934. }
  935. //insert_rear(rest_interval, co_val, last_):
  936. interval_type end_gap = left_subtract(rest_interval, last_interval);
  937. if(!icl::is_empty(end_gap))
  938. {
  939. inserted_ = this->_map.insert(prior_, value_type(end_gap, co_val));
  940. it_ = that()->handle_inserted(inserted_);
  941. }
  942. else
  943. it_ = prior_;
  944. }
  945. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  946. inline typename interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>::iterator
  947. interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  948. ::_insert(const segment_type& addend)
  949. {
  950. interval_type inter_val = addend.first;
  951. if(icl::is_empty(inter_val))
  952. return this->_map.end();
  953. const codomain_type& co_val = addend.second;
  954. if(on_codomain_absorbtion::is_absorbable(co_val))
  955. return this->_map.end();
  956. std::pair<iterator,bool> insertion = this->_map.insert(addend);
  957. if(insertion.second)
  958. return that()->handle_inserted(insertion.first);
  959. else
  960. {
  961. // Detect the first and the end iterator of the collision sequence
  962. iterator first_ = this->_map.lower_bound(inter_val),
  963. last_ = prior(this->_map.upper_bound(inter_val));
  964. //assert((++last_) == this->_map.upper_bound(inter_val));
  965. iterator it_ = first_;
  966. insert_main(inter_val, co_val, it_, last_);
  967. return it_;
  968. }
  969. }
  970. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  971. inline typename interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>::iterator
  972. interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  973. ::_insert(iterator prior_, const segment_type& addend)
  974. {
  975. interval_type inter_val = addend.first;
  976. if(icl::is_empty(inter_val))
  977. return prior_;
  978. const codomain_type& co_val = addend.second;
  979. if(on_codomain_absorbtion::is_absorbable(co_val))
  980. return prior_;
  981. std::pair<iterator,bool> insertion = insert_at(prior_, inter_val, co_val);
  982. if(insertion.second)
  983. return that()->handle_inserted(insertion.first);
  984. {
  985. // Detect the first and the end iterator of the collision sequence
  986. std::pair<iterator,iterator> overlap = equal_range(inter_val);
  987. iterator it_ = overlap.first,
  988. last_ = prior(overlap.second);
  989. insert_main(inter_val, co_val, it_, last_);
  990. return it_;
  991. }
  992. }
  993. //==============================================================================
  994. //= Erasure segment_type
  995. //==============================================================================
  996. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  997. inline void interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  998. ::erase_rest(interval_type& inter_val, const CodomainT& co_val,
  999. iterator& it_, const iterator& last_)
  1000. {
  1001. // For all intervals within loop: (*it_).first are contained_in inter_val
  1002. while(it_ != last_)
  1003. if((*it_).second == co_val)
  1004. this->_map.erase(it_++);
  1005. else it_++;
  1006. //erase_rear:
  1007. if((*it_).second == co_val)
  1008. {
  1009. interval_type right_resid = left_subtract((*it_).first, inter_val);
  1010. if(icl::is_empty(right_resid))
  1011. this->_map.erase(it_);
  1012. else
  1013. const_cast<interval_type&>((*it_).first) = right_resid;
  1014. }
  1015. }
  1016. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  1017. inline SubType& interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  1018. ::erase(const segment_type& minuend)
  1019. {
  1020. interval_type inter_val = minuend.first;
  1021. if(icl::is_empty(inter_val))
  1022. return *that();
  1023. const codomain_type& co_val = minuend.second;
  1024. if(on_codomain_absorbtion::is_absorbable(co_val))
  1025. return *that();
  1026. std::pair<iterator,iterator> exterior = equal_range(inter_val);
  1027. if(exterior.first == exterior.second)
  1028. return *that();
  1029. iterator first_ = exterior.first, end_ = exterior.second,
  1030. last_ = cyclic_prior(*this, end_);
  1031. iterator second_= first_; ++second_;
  1032. if(first_ == last_)
  1033. { // [----inter_val----)
  1034. // .....first_==last_.....
  1035. // only for the last there can be a right_resid: a part of *it_ right of minuend
  1036. interval_type right_resid = left_subtract((*first_).first, inter_val);
  1037. if((*first_).second == co_val)
  1038. {
  1039. interval_type left_resid = right_subtract((*first_).first, inter_val);
  1040. if(!icl::is_empty(left_resid)) // [----inter_val----)
  1041. { // [left_resid)..first_==last_......
  1042. const_cast<interval_type&>((*first_).first) = left_resid;
  1043. if(!icl::is_empty(right_resid))
  1044. this->_map.insert(first_, value_type(right_resid, co_val));
  1045. }
  1046. else if(!icl::is_empty(right_resid))
  1047. const_cast<interval_type&>((*first_).first) = right_resid;
  1048. else
  1049. this->_map.erase(first_);
  1050. }
  1051. }
  1052. else
  1053. {
  1054. // first AND NOT last
  1055. if((*first_).second == co_val)
  1056. {
  1057. interval_type left_resid = right_subtract((*first_).first, inter_val);
  1058. if(icl::is_empty(left_resid))
  1059. this->_map.erase(first_);
  1060. else
  1061. const_cast<interval_type&>((*first_).first) = left_resid;
  1062. }
  1063. erase_rest(inter_val, co_val, second_, last_);
  1064. }
  1065. return *that();
  1066. }
  1067. //==============================================================================
  1068. //= Erasure key_type
  1069. //==============================================================================
  1070. template <class SubType, class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  1071. inline SubType& interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  1072. ::erase(const interval_type& minuend)
  1073. {
  1074. if(icl::is_empty(minuend))
  1075. return *that();
  1076. std::pair<iterator, iterator> exterior = equal_range(minuend);
  1077. if(exterior.first == exterior.second)
  1078. return *that();
  1079. iterator first_ = exterior.first,
  1080. end_ = exterior.second,
  1081. last_ = prior(end_);
  1082. interval_type left_resid = right_subtract((*first_).first, minuend);
  1083. interval_type right_resid = left_subtract(last_ ->first, minuend);
  1084. if(first_ == last_ )
  1085. if(!icl::is_empty(left_resid))
  1086. {
  1087. const_cast<interval_type&>((*first_).first) = left_resid;
  1088. if(!icl::is_empty(right_resid))
  1089. this->_map.insert(first_, value_type(right_resid, (*first_).second));
  1090. }
  1091. else if(!icl::is_empty(right_resid))
  1092. const_cast<interval_type&>((*first_).first) = left_subtract((*first_).first, minuend);
  1093. else
  1094. this->_map.erase(first_);
  1095. else
  1096. { // [-------- minuend ---------)
  1097. // [left_resid fst) . . . . [lst right_resid)
  1098. iterator second_= first_; ++second_;
  1099. iterator start_ = icl::is_empty(left_resid)? first_: second_;
  1100. iterator stop_ = icl::is_empty(right_resid)? end_ : last_ ;
  1101. this->_map.erase(start_, stop_); //erase [start_, stop_)
  1102. if(!icl::is_empty(left_resid))
  1103. const_cast<interval_type&>((*first_).first) = left_resid;
  1104. if(!icl::is_empty(right_resid))
  1105. const_cast<interval_type&>(last_ ->first) = right_resid;
  1106. }
  1107. return *that();
  1108. }
  1109. //-----------------------------------------------------------------------------
  1110. // type traits
  1111. //-----------------------------------------------------------------------------
  1112. template
  1113. <
  1114. class SubType,
  1115. class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc
  1116. >
  1117. struct is_map<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  1118. {
  1119. typedef is_map<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  1120. BOOST_STATIC_CONSTANT(bool, value = true);
  1121. };
  1122. template
  1123. <
  1124. class SubType,
  1125. class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc
  1126. >
  1127. struct has_inverse<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  1128. {
  1129. typedef has_inverse<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  1130. BOOST_STATIC_CONSTANT(bool, value = (has_inverse<CodomainT>::value));
  1131. };
  1132. template
  1133. <
  1134. class SubType,
  1135. class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc
  1136. >
  1137. struct is_interval_container<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  1138. {
  1139. typedef is_interval_container<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  1140. BOOST_STATIC_CONSTANT(bool, value = true);
  1141. };
  1142. template
  1143. <
  1144. class SubType,
  1145. class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc
  1146. >
  1147. struct absorbs_identities<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  1148. {
  1149. typedef absorbs_identities<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  1150. BOOST_STATIC_CONSTANT(bool, value = (Traits::absorbs_identities));
  1151. };
  1152. template
  1153. <
  1154. class SubType,
  1155. class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc
  1156. >
  1157. struct is_total<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  1158. {
  1159. typedef is_total<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  1160. BOOST_STATIC_CONSTANT(bool, value = (Traits::is_total));
  1161. };
  1162. }} // namespace icl boost
  1163. #endif