interval_base_map.hpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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/type_traits/ice.hpp>
  13. #include <boost/mpl/and.hpp>
  14. #include <boost/mpl/or.hpp>
  15. #include <boost/mpl/not.hpp>
  16. #include <boost/icl/detail/notate.hpp>
  17. #include <boost/icl/detail/design_config.hpp>
  18. #include <boost/icl/detail/on_absorbtion.hpp>
  19. #include <boost/icl/detail/interval_map_algo.hpp>
  20. #include <boost/icl/associative_interval_container.hpp>
  21. #include <boost/icl/type_traits/is_interval_splitter.hpp>
  22. #include <boost/icl/map.hpp>
  23. namespace boost{namespace icl
  24. {
  25. template<class DomainT, class CodomainT>
  26. struct mapping_pair
  27. {
  28. DomainT key;
  29. CodomainT data;
  30. mapping_pair():key(), data(){}
  31. mapping_pair(const DomainT& key_value, const CodomainT& data_value)
  32. :key(key_value), data(data_value){}
  33. mapping_pair(const std::pair<DomainT,CodomainT>& std_pair)
  34. :key(std_pair.first), data(std_pair.second){}
  35. };
  36. /** \brief Implements a map as a map of intervals (base class) */
  37. template
  38. <
  39. class SubType,
  40. typename DomainT,
  41. typename CodomainT,
  42. class Traits = icl::partial_absorber,
  43. ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT),
  44. ICL_COMBINE Combine = ICL_COMBINE_INSTANCE(icl::inplace_plus, CodomainT),
  45. ICL_SECTION Section = ICL_SECTION_INSTANCE(icl::inter_section, CodomainT),
  46. ICL_INTERVAL(ICL_COMPARE) Interval = ICL_INTERVAL_INSTANCE(ICL_INTERVAL_DEFAULT, DomainT, Compare),
  47. ICL_ALLOC Alloc = std::allocator
  48. >
  49. class interval_base_map
  50. {
  51. public:
  52. //==========================================================================
  53. //= Associated types
  54. //==========================================================================
  55. typedef interval_base_map<SubType,DomainT,CodomainT,
  56. Traits,Compare,Combine,Section,Interval,Alloc>
  57. type;
  58. /// The designated \e derived or \e sub_type of this base class
  59. typedef SubType sub_type;
  60. /// Auxilliary type for overloadresolution
  61. typedef type overloadable_type;
  62. /// Traits of an itl map
  63. typedef Traits traits;
  64. //--------------------------------------------------------------------------
  65. //- Associated types: Related types
  66. //--------------------------------------------------------------------------
  67. /// The atomized type representing the corresponding container of elements
  68. typedef typename icl::map<DomainT,CodomainT,
  69. Traits,Compare,Combine,Section,Alloc> atomized_type;
  70. //--------------------------------------------------------------------------
  71. //- Associated types: Data
  72. //--------------------------------------------------------------------------
  73. /// Domain type (type of the keys) of the map
  74. typedef DomainT domain_type;
  75. typedef typename boost::call_traits<DomainT>::param_type domain_param;
  76. /// Domain type (type of the keys) of the map
  77. typedef CodomainT codomain_type;
  78. /// Auxiliary type to help the compiler resolve ambiguities when using std::make_pair
  79. typedef mapping_pair<domain_type,codomain_type> domain_mapping_type;
  80. /// Conceptual is a map a set of elements of type \c element_type
  81. typedef domain_mapping_type element_type;
  82. /// The interval type of the map
  83. typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) interval_type;
  84. /// Auxiliary type for overload resolution
  85. typedef std::pair<interval_type,CodomainT> interval_mapping_type;
  86. /// Type of an interval containers segment, that is spanned by an interval
  87. typedef std::pair<interval_type,CodomainT> segment_type;
  88. //--------------------------------------------------------------------------
  89. //- Associated types: Size
  90. //--------------------------------------------------------------------------
  91. /// The difference type of an interval which is sometimes different form the domain_type
  92. typedef typename difference_type_of<domain_type>::type difference_type;
  93. /// The size type of an interval which is mostly std::size_t
  94. typedef typename size_type_of<domain_type>::type size_type;
  95. //--------------------------------------------------------------------------
  96. //- Associated types: Functors
  97. //--------------------------------------------------------------------------
  98. /// Comparison functor for domain values
  99. typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
  100. typedef ICL_COMPARE_DOMAIN(Compare,segment_type) segment_compare;
  101. /// Combine functor for codomain value aggregation
  102. typedef ICL_COMBINE_CODOMAIN(Combine,CodomainT) codomain_combine;
  103. /// Inverse Combine functor for codomain value aggregation
  104. typedef typename inverse<codomain_combine>::type inverse_codomain_combine;
  105. /// Intersection functor for codomain values
  106. typedef typename mpl::if_
  107. <has_set_semantics<codomain_type>
  108. , ICL_SECTION_CODOMAIN(Section,CodomainT)
  109. , codomain_combine
  110. >::type codomain_intersect;
  111. /// Inverse Combine functor for codomain value intersection
  112. typedef typename inverse<codomain_intersect>::type inverse_codomain_intersect;
  113. /// Comparison functor for intervals which are keys as well
  114. typedef exclusive_less_than<interval_type> interval_compare;
  115. /// Comparison functor for keys
  116. typedef exclusive_less_than<interval_type> key_compare;
  117. //--------------------------------------------------------------------------
  118. //- Associated types: Implementation and stl related
  119. //--------------------------------------------------------------------------
  120. /// The allocator type of the set
  121. typedef Alloc<std::pair<const interval_type, codomain_type> >
  122. allocator_type;
  123. /// Container type for the implementation
  124. typedef ICL_IMPL_SPACE::map<interval_type,codomain_type,
  125. key_compare,allocator_type> ImplMapT;
  126. /// key type of the implementing container
  127. typedef typename ImplMapT::key_type key_type;
  128. /// value type of the implementing container
  129. typedef typename ImplMapT::value_type value_type;
  130. /// data type of the implementing container
  131. typedef typename ImplMapT::value_type::second_type data_type;
  132. /// pointer type
  133. typedef typename ImplMapT::pointer pointer;
  134. /// const pointer type
  135. typedef typename ImplMapT::const_pointer const_pointer;
  136. /// reference type
  137. typedef typename ImplMapT::reference reference;
  138. /// const reference type
  139. typedef typename ImplMapT::const_reference const_reference;
  140. /// iterator for iteration over intervals
  141. typedef typename ImplMapT::iterator iterator;
  142. /// const_iterator for iteration over intervals
  143. typedef typename ImplMapT::const_iterator const_iterator;
  144. /// iterator for reverse iteration over intervals
  145. typedef typename ImplMapT::reverse_iterator reverse_iterator;
  146. /// const_iterator for iteration over intervals
  147. typedef typename ImplMapT::const_reverse_iterator const_reverse_iterator;
  148. /// element iterator: Depreciated, see documentation.
  149. typedef boost::icl::element_iterator<iterator> element_iterator;
  150. /// const element iterator: Depreciated, see documentation.
  151. typedef boost::icl::element_iterator<const_iterator> element_const_iterator;
  152. /// element reverse iterator: Depreciated, see documentation.
  153. typedef boost::icl::element_iterator<reverse_iterator> element_reverse_iterator;
  154. /// element const reverse iterator: Depreciated, see documentation.
  155. typedef boost::icl::element_iterator<const_reverse_iterator> element_const_reverse_iterator;
  156. typedef typename on_absorbtion<type, codomain_combine,
  157. Traits::absorbs_identities>::type on_codomain_absorbtion;
  158. public:
  159. BOOST_STATIC_CONSTANT(bool,
  160. is_total_invertible = ( Traits::is_total
  161. && has_inverse<codomain_type>::value));
  162. BOOST_STATIC_CONSTANT(int, fineness = 0);
  163. public:
  164. //==========================================================================
  165. //= Construct, copy, destruct
  166. //==========================================================================
  167. /** Default constructor for the empty object */
  168. interval_base_map()
  169. {
  170. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  171. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  172. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<CodomainT>));
  173. BOOST_CONCEPT_ASSERT((EqualComparableConcept<CodomainT>));
  174. }
  175. /** Copy constructor */
  176. interval_base_map(const interval_base_map& src): _map(src._map)
  177. {
  178. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  179. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  180. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<CodomainT>));
  181. BOOST_CONCEPT_ASSERT((EqualComparableConcept<CodomainT>));
  182. }
  183. /** Copy assignment operator */
  184. interval_base_map& operator = (const interval_base_map& src)
  185. {
  186. this->_map = src._map;
  187. return *this;
  188. }
  189. # ifndef BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  190. //==========================================================================
  191. //= Move semantics
  192. //==========================================================================
  193. /** Move constructor */
  194. interval_base_map(interval_base_map&& src): _map(boost::move(src._map))
  195. {
  196. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  197. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  198. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<CodomainT>));
  199. BOOST_CONCEPT_ASSERT((EqualComparableConcept<CodomainT>));
  200. }
  201. /** Move assignment operator */
  202. interval_base_map& operator = (interval_base_map&& src)
  203. {
  204. this->_map = boost::move(src._map);
  205. return *this;
  206. }
  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_ = 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_ = insertion.first;
  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_ = it_, inserted_;
  919. if(prior_ != this->_map.end())
  920. --prior_;
  921. interval_type rest_interval = inter_val, left_gap, cur_itv;
  922. interval_type last_interval = last_ ->first;
  923. while(it_ != end_ )
  924. {
  925. cur_itv = (*it_).first ;
  926. left_gap = right_subtract(rest_interval, cur_itv);
  927. if(!icl::is_empty(left_gap))
  928. {
  929. inserted_ = this->_map.insert(prior_, value_type(left_gap, co_val));
  930. it_ = that()->handle_inserted(inserted_);
  931. }
  932. // shrink interval
  933. rest_interval = left_subtract(rest_interval, cur_itv);
  934. prior_ = it_;
  935. ++it_;
  936. }
  937. //insert_rear(rest_interval, co_val, last_):
  938. interval_type end_gap = left_subtract(rest_interval, last_interval);
  939. if(!icl::is_empty(end_gap))
  940. {
  941. inserted_ = this->_map.insert(prior_, value_type(end_gap, co_val));
  942. it_ = that()->handle_inserted(inserted_);
  943. }
  944. else
  945. it_ = prior_;
  946. }
  947. 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>
  948. inline typename interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>::iterator
  949. interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  950. ::_insert(const segment_type& addend)
  951. {
  952. interval_type inter_val = addend.first;
  953. if(icl::is_empty(inter_val))
  954. return this->_map.end();
  955. const codomain_type& co_val = addend.second;
  956. if(on_codomain_absorbtion::is_absorbable(co_val))
  957. return this->_map.end();
  958. std::pair<iterator,bool> insertion = this->_map.insert(addend);
  959. if(insertion.second)
  960. return that()->handle_inserted(insertion.first);
  961. else
  962. {
  963. // Detect the first and the end iterator of the collision sequence
  964. iterator first_ = this->_map.lower_bound(inter_val),
  965. last_ = insertion.first;
  966. //assert((++last_) == this->_map.upper_bound(inter_val));
  967. iterator it_ = first_;
  968. insert_main(inter_val, co_val, it_, last_);
  969. return it_;
  970. }
  971. }
  972. 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>
  973. inline typename interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>::iterator
  974. interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  975. ::_insert(iterator prior_, const segment_type& addend)
  976. {
  977. interval_type inter_val = addend.first;
  978. if(icl::is_empty(inter_val))
  979. return prior_;
  980. const codomain_type& co_val = addend.second;
  981. if(on_codomain_absorbtion::is_absorbable(co_val))
  982. return prior_;
  983. std::pair<iterator,bool> insertion = insert_at(prior_, inter_val, co_val);
  984. if(insertion.second)
  985. return that()->handle_inserted(insertion.first);
  986. {
  987. // Detect the first and the end iterator of the collision sequence
  988. std::pair<iterator,iterator> overlap = equal_range(inter_val);
  989. iterator it_ = overlap.first,
  990. last_ = prior(overlap.second);
  991. insert_main(inter_val, co_val, it_, last_);
  992. return it_;
  993. }
  994. }
  995. //==============================================================================
  996. //= Erasure segment_type
  997. //==============================================================================
  998. 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>
  999. inline void interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  1000. ::erase_rest(interval_type& inter_val, const CodomainT& co_val,
  1001. iterator& it_, const iterator& last_)
  1002. {
  1003. // For all intervals within loop: (*it_).first are contained_in inter_val
  1004. while(it_ != last_)
  1005. if((*it_).second == co_val)
  1006. this->_map.erase(it_++);
  1007. else it_++;
  1008. //erase_rear:
  1009. if((*it_).second == co_val)
  1010. {
  1011. interval_type right_resid = left_subtract((*it_).first, inter_val);
  1012. if(icl::is_empty(right_resid))
  1013. this->_map.erase(it_);
  1014. else
  1015. const_cast<interval_type&>((*it_).first) = right_resid;
  1016. }
  1017. }
  1018. 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>
  1019. inline SubType& interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  1020. ::erase(const segment_type& minuend)
  1021. {
  1022. interval_type inter_val = minuend.first;
  1023. if(icl::is_empty(inter_val))
  1024. return *that();
  1025. const codomain_type& co_val = minuend.second;
  1026. if(on_codomain_absorbtion::is_absorbable(co_val))
  1027. return *that();
  1028. std::pair<iterator,iterator> exterior = equal_range(inter_val);
  1029. if(exterior.first == exterior.second)
  1030. return *that();
  1031. iterator first_ = exterior.first, end_ = exterior.second,
  1032. last_ = cyclic_prior(*this, end_);
  1033. iterator second_= first_; ++second_;
  1034. if(first_ == last_)
  1035. { // [----inter_val----)
  1036. // .....first_==last_.....
  1037. // only for the last there can be a right_resid: a part of *it_ right of minuend
  1038. interval_type right_resid = left_subtract((*first_).first, inter_val);
  1039. if((*first_).second == co_val)
  1040. {
  1041. interval_type left_resid = right_subtract((*first_).first, inter_val);
  1042. if(!icl::is_empty(left_resid)) // [----inter_val----)
  1043. { // [left_resid)..first_==last_......
  1044. const_cast<interval_type&>((*first_).first) = left_resid;
  1045. if(!icl::is_empty(right_resid))
  1046. this->_map.insert(first_, value_type(right_resid, co_val));
  1047. }
  1048. else if(!icl::is_empty(right_resid))
  1049. const_cast<interval_type&>((*first_).first) = right_resid;
  1050. else
  1051. this->_map.erase(first_);
  1052. }
  1053. }
  1054. else
  1055. {
  1056. // first AND NOT last
  1057. if((*first_).second == co_val)
  1058. {
  1059. interval_type left_resid = right_subtract((*first_).first, inter_val);
  1060. if(icl::is_empty(left_resid))
  1061. this->_map.erase(first_);
  1062. else
  1063. const_cast<interval_type&>((*first_).first) = left_resid;
  1064. }
  1065. erase_rest(inter_val, co_val, second_, last_);
  1066. }
  1067. return *that();
  1068. }
  1069. //==============================================================================
  1070. //= Erasure key_type
  1071. //==============================================================================
  1072. 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>
  1073. inline SubType& interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc>
  1074. ::erase(const interval_type& minuend)
  1075. {
  1076. if(icl::is_empty(minuend))
  1077. return *that();
  1078. std::pair<iterator, iterator> exterior = equal_range(minuend);
  1079. if(exterior.first == exterior.second)
  1080. return *that();
  1081. iterator first_ = exterior.first,
  1082. end_ = exterior.second,
  1083. last_ = prior(end_);
  1084. interval_type left_resid = right_subtract((*first_).first, minuend);
  1085. interval_type right_resid = left_subtract(last_ ->first, minuend);
  1086. if(first_ == last_ )
  1087. if(!icl::is_empty(left_resid))
  1088. {
  1089. const_cast<interval_type&>((*first_).first) = left_resid;
  1090. if(!icl::is_empty(right_resid))
  1091. this->_map.insert(first_, value_type(right_resid, (*first_).second));
  1092. }
  1093. else if(!icl::is_empty(right_resid))
  1094. const_cast<interval_type&>((*first_).first) = left_subtract((*first_).first, minuend);
  1095. else
  1096. this->_map.erase(first_);
  1097. else
  1098. { // [-------- minuend ---------)
  1099. // [left_resid fst) . . . . [lst right_resid)
  1100. iterator second_= first_; ++second_;
  1101. iterator start_ = icl::is_empty(left_resid)? first_: second_;
  1102. iterator stop_ = icl::is_empty(right_resid)? end_ : last_ ;
  1103. this->_map.erase(start_, stop_); //erase [start_, stop_)
  1104. if(!icl::is_empty(left_resid))
  1105. const_cast<interval_type&>((*first_).first) = left_resid;
  1106. if(!icl::is_empty(right_resid))
  1107. const_cast<interval_type&>(last_ ->first) = right_resid;
  1108. }
  1109. return *that();
  1110. }
  1111. //-----------------------------------------------------------------------------
  1112. // type traits
  1113. //-----------------------------------------------------------------------------
  1114. template
  1115. <
  1116. class SubType,
  1117. class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc
  1118. >
  1119. struct is_map<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  1120. {
  1121. typedef is_map<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  1122. BOOST_STATIC_CONSTANT(bool, value = true);
  1123. };
  1124. template
  1125. <
  1126. class SubType,
  1127. class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc
  1128. >
  1129. struct has_inverse<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  1130. {
  1131. typedef has_inverse<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  1132. BOOST_STATIC_CONSTANT(bool, value = (has_inverse<CodomainT>::value));
  1133. };
  1134. template
  1135. <
  1136. class SubType,
  1137. class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc
  1138. >
  1139. struct is_interval_container<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  1140. {
  1141. typedef is_interval_container<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  1142. BOOST_STATIC_CONSTANT(bool, value = true);
  1143. };
  1144. template
  1145. <
  1146. class SubType,
  1147. class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc
  1148. >
  1149. struct absorbs_identities<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  1150. {
  1151. typedef absorbs_identities<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  1152. BOOST_STATIC_CONSTANT(bool, value = (Traits::absorbs_identities));
  1153. };
  1154. template
  1155. <
  1156. class SubType,
  1157. class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc
  1158. >
  1159. struct is_total<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> >
  1160. {
  1161. typedef is_total<icl::interval_base_map<SubType,DomainT,CodomainT,Traits,Compare,Combine,Section,Interval,Alloc> > type;
  1162. BOOST_STATIC_CONSTANT(bool, value = (Traits::is_total));
  1163. };
  1164. }} // namespace icl boost
  1165. #endif