interval_base_set.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2007-2011: 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_SET_H_JOFA_990223
  10. #define BOOST_ICL_INTERVAL_BASE_SET_H_JOFA_990223
  11. #include <boost/icl/impl_config.hpp>
  12. #if defined(ICL_USE_BOOST_MOVE_IMPLEMENTATION)
  13. # include <boost/container/set.hpp>
  14. #elif defined(ICL_USE_STD_IMPLEMENTATION)
  15. # include <set>
  16. #else // Default for implementing containers
  17. # include <set>
  18. #endif
  19. #include <limits>
  20. #include <boost/next_prior.hpp>
  21. #include <boost/icl/associative_interval_container.hpp>
  22. #include <boost/icl/type_traits/interval_type_default.hpp>
  23. #include <boost/icl/interval.hpp>
  24. #include <boost/icl/type_traits/infinity.hpp>
  25. #include <boost/icl/type_traits/is_interval_joiner.hpp>
  26. #include <boost/icl/type_traits/is_interval_separator.hpp>
  27. #include <boost/icl/type_traits/is_interval_splitter.hpp>
  28. #include <boost/icl/detail/interval_set_algo.hpp>
  29. #include <boost/icl/detail/exclusive_less_than.hpp>
  30. #include <boost/icl/right_open_interval.hpp>
  31. #include <boost/icl/continuous_interval.hpp>
  32. #include <boost/icl/detail/notate.hpp>
  33. #include <boost/icl/detail/element_iterator.hpp>
  34. namespace boost{namespace icl
  35. {
  36. /** \brief Implements a set as a set of intervals (base class) */
  37. template
  38. <
  39. typename SubType,
  40. typename DomainT,
  41. ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT),
  42. ICL_INTERVAL(ICL_COMPARE) Interval = ICL_INTERVAL_INSTANCE(ICL_INTERVAL_DEFAULT, DomainT, Compare),
  43. ICL_ALLOC Alloc = std::allocator
  44. >
  45. class interval_base_set
  46. {
  47. public:
  48. //==========================================================================
  49. //= Associated types
  50. //==========================================================================
  51. typedef interval_base_set<SubType,DomainT,Compare,Interval,Alloc> type;
  52. /// The designated \e derived or \e sub_type of this base class
  53. typedef SubType sub_type;
  54. /// Auxilliary type for overloadresolution
  55. typedef type overloadable_type;
  56. //--------------------------------------------------------------------------
  57. //- Associated types: Data
  58. //--------------------------------------------------------------------------
  59. /// The domain type of the set
  60. typedef DomainT domain_type;
  61. /// The codomaintype is the same as domain_type
  62. typedef DomainT codomain_type;
  63. /// The element type of the set
  64. typedef DomainT element_type;
  65. /// The interval type of the set
  66. typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) interval_type;
  67. /// The segment type of the set
  68. typedef interval_type segment_type;
  69. //--------------------------------------------------------------------------
  70. //- Associated types: Size
  71. //--------------------------------------------------------------------------
  72. /// The difference type of an interval which is sometimes different form the data_type
  73. typedef typename difference_type_of<domain_type>::type difference_type;
  74. /// The size type of an interval which is mostly std::size_t
  75. typedef typename size_type_of<domain_type>::type size_type;
  76. //--------------------------------------------------------------------------
  77. //- Associated types: Order
  78. //--------------------------------------------------------------------------
  79. /// Comparison functor for domain values
  80. typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
  81. typedef ICL_COMPARE_DOMAIN(Compare,segment_type) segment_compare;
  82. /// Comparison functor for intervals
  83. typedef exclusive_less_than<interval_type> interval_compare;
  84. /// Comparison functor for keys
  85. typedef exclusive_less_than<interval_type> key_compare;
  86. //--------------------------------------------------------------------------
  87. //- Associated types: Related types
  88. //--------------------------------------------------------------------------
  89. /// The atomized type representing the corresponding container of elements
  90. typedef typename ICL_IMPL_SPACE::set<DomainT,domain_compare,Alloc<DomainT> > atomized_type;
  91. //--------------------------------------------------------------------------
  92. //- Associated types: Implementation and stl related
  93. //--------------------------------------------------------------------------
  94. /// The allocator type of the set
  95. typedef Alloc<interval_type> allocator_type;
  96. /// allocator type of the corresponding element set
  97. typedef Alloc<DomainT> domain_allocator_type;
  98. /// Container type for the implementation
  99. typedef typename ICL_IMPL_SPACE::set<interval_type,key_compare,allocator_type> ImplSetT;
  100. /// key type of the implementing container
  101. typedef typename ImplSetT::key_type key_type;
  102. /// data type of the implementing container
  103. typedef typename ImplSetT::key_type data_type;
  104. /// value type of the implementing container
  105. typedef typename ImplSetT::value_type value_type;
  106. /// pointer type
  107. typedef typename ImplSetT::pointer pointer;
  108. /// const pointer type
  109. typedef typename ImplSetT::const_pointer const_pointer;
  110. /// reference type
  111. typedef typename ImplSetT::reference reference;
  112. /// const reference type
  113. typedef typename ImplSetT::const_reference const_reference;
  114. /// iterator for iteration over intervals
  115. typedef typename ImplSetT::iterator iterator;
  116. /// const_iterator for iteration over intervals
  117. typedef typename ImplSetT::const_iterator const_iterator;
  118. /// iterator for reverse iteration over intervals
  119. typedef typename ImplSetT::reverse_iterator reverse_iterator;
  120. /// const_iterator for iteration over intervals
  121. typedef typename ImplSetT::const_reverse_iterator const_reverse_iterator;
  122. /// element iterator: Depreciated, see documentation.
  123. typedef boost::icl::element_iterator<iterator> element_iterator;
  124. /// element const iterator: Depreciated, see documentation.
  125. typedef boost::icl::element_iterator<const_iterator> element_const_iterator;
  126. /// element reverse iterator: Depreciated, see documentation.
  127. typedef boost::icl::element_iterator<reverse_iterator> element_reverse_iterator;
  128. /// element const reverse iterator: Depreciated, see documentation.
  129. typedef boost::icl::element_iterator<const_reverse_iterator> element_const_reverse_iterator;
  130. BOOST_STATIC_CONSTANT(int, fineness = 0);
  131. public:
  132. //==========================================================================
  133. //= Construct, copy, destruct
  134. //==========================================================================
  135. /** Default constructor for the empty object */
  136. interval_base_set(){}
  137. /** Copy constructor */
  138. interval_base_set(const interval_base_set& src): _set(src._set)
  139. {
  140. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  141. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  142. }
  143. # ifndef BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  144. //==========================================================================
  145. //= Move semantics
  146. //==========================================================================
  147. /** Move constructor */
  148. interval_base_set(interval_base_set&& src): _set(boost::move(src._set))
  149. {
  150. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  151. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  152. }
  153. /** Move assignment operator */
  154. interval_base_set& operator = (interval_base_set src)
  155. { //call by value sice 'src' is a "sink value"
  156. this->_set = boost::move(src._set);
  157. return *this;
  158. }
  159. //==========================================================================
  160. # else
  161. /** Copy assignment operator */
  162. interval_base_set& operator = (const interval_base_set& src)
  163. {
  164. this->_set = src._set;
  165. return *this;
  166. }
  167. # endif // BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  168. /** swap the content of containers */
  169. void swap(interval_base_set& operand) { _set.swap(operand._set); }
  170. //==========================================================================
  171. //= Containedness
  172. //==========================================================================
  173. /** sets the container empty */
  174. void clear() { icl::clear(*that()); }
  175. /** is the container empty? */
  176. bool empty()const { return icl::is_empty(*that()); }
  177. //==========================================================================
  178. //= Size
  179. //==========================================================================
  180. /** An interval set's size is it's cardinality */
  181. size_type size()const
  182. {
  183. return icl::cardinality(*that());
  184. }
  185. /** Size of the iteration over this container */
  186. std::size_t iterative_size()const
  187. {
  188. return _set.size();
  189. }
  190. //==========================================================================
  191. //= Selection
  192. //==========================================================================
  193. /** Find the interval, that contains element \c key_value */
  194. const_iterator find(const element_type& key_value)const
  195. {
  196. return icl::find(*this, key_value);
  197. //CL return this->_set.find(icl::singleton<segment_type>(key));
  198. }
  199. /** Find the first interval, that collides with interval \c key_interval */
  200. const_iterator find(const interval_type& key_interval)const
  201. {
  202. return this->_set.find(key_interval);
  203. }
  204. //==========================================================================
  205. //= Addition
  206. //==========================================================================
  207. /** Add a single element \c key to the set */
  208. SubType& add(const element_type& key)
  209. {
  210. return icl::add(*that(), key);
  211. }
  212. /** Add an interval of elements \c inter_val to the set */
  213. SubType& add(const segment_type& inter_val)
  214. {
  215. _add(inter_val);
  216. return *that();
  217. }
  218. /** Add an interval of elements \c inter_val to the set. Iterator
  219. \c prior_ is a hint to the position \c inter_val can be
  220. inserted after. */
  221. iterator add(iterator prior_, const segment_type& inter_val)
  222. {
  223. return _add(prior_, inter_val);
  224. }
  225. //==========================================================================
  226. //= Subtraction
  227. //==========================================================================
  228. /** Subtract a single element \c key from the set */
  229. SubType& subtract(const element_type& key)
  230. {
  231. return icl::subtract(*that(), key);
  232. }
  233. /** Subtract an interval of elements \c inter_val from the set */
  234. SubType& subtract(const segment_type& inter_val);
  235. //==========================================================================
  236. //= Insertion
  237. //==========================================================================
  238. /** Insert an element \c key into the set */
  239. SubType& insert(const element_type& key)
  240. {
  241. return add(key);
  242. }
  243. /** Insert an interval of elements \c inter_val to the set */
  244. SubType& insert(const segment_type& inter_val)
  245. {
  246. return add(inter_val);
  247. }
  248. /** Insert an interval of elements \c inter_val to the set. Iterator
  249. \c prior_ is a hint to the position \c inter_val can be
  250. inserted after. */
  251. iterator insert(iterator prior_, const segment_type& inter_val)
  252. {
  253. return add(prior_, inter_val);
  254. }
  255. //==========================================================================
  256. //= Erasure
  257. //==========================================================================
  258. /** Erase an element \c key from the set */
  259. SubType& erase(const element_type& key)
  260. {
  261. return subtract(key);
  262. }
  263. /** Erase an interval of elements \c inter_val from the set */
  264. SubType& erase(const segment_type& inter_val)
  265. {
  266. return subtract(inter_val);
  267. }
  268. /** Erase the interval that iterator \c position points to. */
  269. void erase(iterator position)
  270. {
  271. _set.erase(position);
  272. }
  273. /** Erase all intervals in the range <tt>[first,past)</tt> of iterators. */
  274. void erase(iterator first, iterator past)
  275. {
  276. _set.erase(first, past);
  277. }
  278. //==========================================================================
  279. //= Symmetric difference
  280. //==========================================================================
  281. /** If \c *this set contains \c key it is erased, otherwise it is added. */
  282. SubType& flip(const element_type& key)
  283. {
  284. return icl::flip(*that(), key);
  285. }
  286. /** If \c *this set contains \c inter_val it is erased, otherwise it is added. */
  287. SubType& flip(const segment_type& inter_val)
  288. {
  289. return icl::flip(*that(), inter_val);
  290. }
  291. //==========================================================================
  292. //= Iterator related
  293. //==========================================================================
  294. iterator begin() { return _set.begin(); }
  295. iterator end() { return _set.end(); }
  296. const_iterator begin()const { return _set.begin(); }
  297. const_iterator end()const { return _set.end(); }
  298. reverse_iterator rbegin() { return _set.rbegin(); }
  299. reverse_iterator rend() { return _set.rend(); }
  300. const_reverse_iterator rbegin()const { return _set.rbegin(); }
  301. const_reverse_iterator rend()const { return _set.rend(); }
  302. iterator lower_bound(const value_type& interval)
  303. { return _set.lower_bound(interval); }
  304. iterator upper_bound(const value_type& interval)
  305. { return _set.upper_bound(interval); }
  306. const_iterator lower_bound(const value_type& interval)const
  307. { return _set.lower_bound(interval); }
  308. const_iterator upper_bound(const value_type& interval)const
  309. { return _set.upper_bound(interval); }
  310. std::pair<iterator,iterator> equal_range(const key_type& interval)
  311. {
  312. return std::pair<iterator,iterator>
  313. (_set.lower_bound(interval), _set.upper_bound(interval));
  314. }
  315. std::pair<const_iterator,const_iterator>
  316. equal_range(const key_type& interval)const
  317. {
  318. return std::pair<const_iterator,const_iterator>
  319. (_set.lower_bound(interval), _set.upper_bound(interval));
  320. }
  321. private:
  322. iterator _add(const segment_type& addend);
  323. iterator _add(iterator prior, const segment_type& addend);
  324. protected:
  325. void add_front(const interval_type& inter_val, iterator& first_);
  326. void add_main(interval_type& inter_val, iterator& it_, const iterator& last_);
  327. void add_segment(const interval_type& inter_val, iterator& it_);
  328. void add_rear(const interval_type& inter_val, iterator& it_);
  329. protected:
  330. sub_type* that() { return static_cast<sub_type*>(this); }
  331. const sub_type* that()const { return static_cast<const sub_type*>(this); }
  332. protected:
  333. ImplSetT _set;
  334. } ;
  335. template <class SubType, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  336. inline void interval_base_set<SubType,DomainT,Compare,Interval,Alloc>
  337. ::add_front(const interval_type& inter_val, iterator& first_)
  338. {
  339. // If the collision sequence has a left residual 'left_resid' it will
  340. // be split, to provide a standardized start of algorithms:
  341. // The addend interval 'inter_val' covers the beginning of the collision sequence.
  342. // only for the first there can be a left_resid: a part of *first_ left of inter_val
  343. interval_type left_resid = right_subtract(*first_, inter_val);
  344. if(!icl::is_empty(left_resid))
  345. { // [------------ . . .
  346. // [left_resid---first_ --- . . .
  347. iterator prior_ = cyclic_prior(*this, first_);
  348. const_cast<interval_type&>(*first_) = left_subtract(*first_, left_resid);
  349. //NOTE: Only splitting
  350. this->_set.insert(prior_, left_resid);
  351. }
  352. //POST:
  353. // [----- inter_val ---- . . .
  354. // ...[-- first_ --...
  355. }
  356. template <class SubType, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  357. inline void interval_base_set<SubType,DomainT,Compare,Interval,Alloc>
  358. ::add_segment(const interval_type& inter_val, iterator& it_)
  359. {
  360. interval_type lead_gap = right_subtract(inter_val, *it_);
  361. if(!icl::is_empty(lead_gap))
  362. // [lead_gap--- . . .
  363. // [prior_) [-- it_ ...
  364. this->_set.insert(cyclic_prior(*this, it_), lead_gap);
  365. // . . . --------- . . . addend interval
  366. // [-- it_ --) has a common part with the first overval
  367. ++it_;
  368. }
  369. template <class SubType, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  370. inline void interval_base_set<SubType,DomainT,Compare,Interval,Alloc>
  371. ::add_main(interval_type& rest_interval, iterator& it_, const iterator& last_)
  372. {
  373. interval_type cur_interval;
  374. while(it_ != last_)
  375. {
  376. cur_interval = *it_ ;
  377. add_segment(rest_interval, it_);
  378. // shrink interval
  379. rest_interval = left_subtract(rest_interval, cur_interval);
  380. }
  381. }
  382. template <class SubType, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  383. inline void interval_base_set<SubType,DomainT,Compare,Interval,Alloc>
  384. ::add_rear(const interval_type& inter_val, iterator& it_)
  385. {
  386. iterator prior_ = cyclic_prior(*this, it_);
  387. interval_type cur_itv = *it_;
  388. interval_type lead_gap = right_subtract(inter_val, cur_itv);
  389. if(!icl::is_empty(lead_gap))
  390. // [lead_gap--- . . .
  391. // [prior_) [-- it_ ...
  392. this->_set.insert(prior_, lead_gap);
  393. interval_type end_gap = left_subtract(inter_val, cur_itv);
  394. if(!icl::is_empty(end_gap))
  395. // [---------------end_gap)
  396. // [-- it_ --)
  397. it_ = this->_set.insert(it_, end_gap);
  398. else
  399. {
  400. // only for the last there can be a right_resid: a part of *it_ right of addend
  401. interval_type right_resid = left_subtract(cur_itv, inter_val);
  402. if(!icl::is_empty(right_resid))
  403. {
  404. // [--------------)
  405. // [-- it_ --right_resid)
  406. const_cast<interval_type&>(*it_) = right_subtract(*it_, right_resid);
  407. it_ = this->_set.insert(it_, right_resid);
  408. }
  409. }
  410. }
  411. //==============================================================================
  412. //= Addition
  413. //==============================================================================
  414. template <class SubType, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  415. inline typename interval_base_set<SubType,DomainT,Compare,Interval,Alloc>::iterator
  416. interval_base_set<SubType,DomainT,Compare,Interval,Alloc>
  417. ::_add(const segment_type& addend)
  418. {
  419. typedef typename interval_base_set<SubType,DomainT,Compare,Interval,Alloc>::iterator iterator;
  420. if(icl::is_empty(addend))
  421. return this->_set.end();
  422. std::pair<iterator,bool> insertion = this->_set.insert(addend);
  423. if(insertion.second)
  424. return that()->handle_inserted(insertion.first);
  425. else
  426. {
  427. iterator last_ = prior(this->_set.upper_bound(addend));
  428. return that()->add_over(addend, last_);
  429. }
  430. }
  431. template <class SubType, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  432. inline typename interval_base_set<SubType,DomainT,Compare,Interval,Alloc>::iterator
  433. interval_base_set<SubType,DomainT,Compare,Interval,Alloc>
  434. ::_add(iterator prior_, const segment_type& addend)
  435. {
  436. typedef typename interval_base_set<SubType,DomainT,Compare,Interval,Alloc>::iterator iterator;
  437. if(icl::is_empty(addend))
  438. return prior_;
  439. iterator insertion = this->_set.insert(prior_, addend);
  440. if(*insertion == addend)
  441. return that()->handle_inserted(insertion);
  442. else
  443. {
  444. iterator last_ = prior(this->_set.upper_bound(addend));
  445. return that()->add_over(addend, last_);
  446. }
  447. }
  448. //==============================================================================
  449. //= Subtraction
  450. //==============================================================================
  451. template <class SubType, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  452. inline SubType& interval_base_set<SubType,DomainT,Compare,Interval,Alloc>
  453. ::subtract(const segment_type& minuend)
  454. {
  455. if(icl::is_empty(minuend))
  456. return *that();
  457. std::pair<iterator, iterator> exterior = equal_range(minuend);
  458. if(exterior.first == exterior.second)
  459. return *that();
  460. iterator first_ = exterior.first;
  461. iterator end_ = exterior.second;
  462. iterator last_ = prior(end_);
  463. interval_type left_resid = right_subtract(*first_, minuend);
  464. interval_type right_resid;
  465. if(first_ != end_)
  466. right_resid = left_subtract(*last_ , minuend);
  467. this->_set.erase(first_, end_);
  468. if(!icl::is_empty(left_resid))
  469. this->_set.insert(left_resid);
  470. if(!icl::is_empty(right_resid))
  471. this->_set.insert(right_resid);
  472. return *that();
  473. }
  474. //-----------------------------------------------------------------------------
  475. // type traits
  476. //-----------------------------------------------------------------------------
  477. template<class SubType,
  478. class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  479. struct is_set<icl::interval_base_set<SubType,DomainT,Compare,Interval,Alloc> >
  480. {
  481. typedef is_set<icl::interval_base_set<SubType,DomainT,Compare,Interval,Alloc> > type;
  482. BOOST_STATIC_CONSTANT(bool, value = true);
  483. };
  484. template<class SubType,
  485. class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  486. struct is_interval_container<icl::interval_base_set<SubType,DomainT,Compare,Interval,Alloc> >
  487. {
  488. typedef is_interval_container<icl::interval_base_set<SubType,DomainT,Compare,Interval,Alloc> > type;
  489. BOOST_STATIC_CONSTANT(bool, value = true);
  490. };
  491. }} // namespace icl boost
  492. #endif