interval_base_set.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. /** Assignment operator */
  144. interval_base_set& operator = (const interval_base_set& src)
  145. {
  146. this->_set = src._set;
  147. return *this;
  148. }
  149. # ifndef BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  150. //==========================================================================
  151. //= Move semantics
  152. //==========================================================================
  153. /** Move constructor */
  154. interval_base_set(interval_base_set&& src): _set(boost::move(src._set))
  155. {
  156. BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
  157. BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
  158. }
  159. /** Move assignment operator */
  160. interval_base_set& operator = (interval_base_set&& src)
  161. {
  162. this->_set = boost::move(src._set);
  163. return *this;
  164. }
  165. //==========================================================================
  166. # endif // BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  167. /** swap the content of containers */
  168. void swap(interval_base_set& operand) { _set.swap(operand._set); }
  169. //==========================================================================
  170. //= Containedness
  171. //==========================================================================
  172. /** sets the container empty */
  173. void clear() { icl::clear(*that()); }
  174. /** is the container empty? */
  175. bool empty()const { return icl::is_empty(*that()); }
  176. //==========================================================================
  177. //= Size
  178. //==========================================================================
  179. /** An interval set's size is it's cardinality */
  180. size_type size()const
  181. {
  182. return icl::cardinality(*that());
  183. }
  184. /** Size of the iteration over this container */
  185. std::size_t iterative_size()const
  186. {
  187. return _set.size();
  188. }
  189. //==========================================================================
  190. //= Selection
  191. //==========================================================================
  192. /** Find the interval, that contains element \c key_value */
  193. const_iterator find(const element_type& key_value)const
  194. {
  195. return icl::find(*this, key_value);
  196. //CL return this->_set.find(icl::singleton<segment_type>(key));
  197. }
  198. /** Find the first interval, that collides with interval \c key_interval */
  199. const_iterator find(const interval_type& key_interval)const
  200. {
  201. return this->_set.find(key_interval);
  202. }
  203. //==========================================================================
  204. //= Addition
  205. //==========================================================================
  206. /** Add a single element \c key to the set */
  207. SubType& add(const element_type& key)
  208. {
  209. return icl::add(*that(), key);
  210. }
  211. /** Add an interval of elements \c inter_val to the set */
  212. SubType& add(const segment_type& inter_val)
  213. {
  214. _add(inter_val);
  215. return *that();
  216. }
  217. /** Add an interval of elements \c inter_val to the set. Iterator
  218. \c prior_ is a hint to the position \c inter_val can be
  219. inserted after. */
  220. iterator add(iterator prior_, const segment_type& inter_val)
  221. {
  222. return _add(prior_, inter_val);
  223. }
  224. //==========================================================================
  225. //= Subtraction
  226. //==========================================================================
  227. /** Subtract a single element \c key from the set */
  228. SubType& subtract(const element_type& key)
  229. {
  230. return icl::subtract(*that(), key);
  231. }
  232. /** Subtract an interval of elements \c inter_val from the set */
  233. SubType& subtract(const segment_type& inter_val);
  234. //==========================================================================
  235. //= Insertion
  236. //==========================================================================
  237. /** Insert an element \c key into the set */
  238. SubType& insert(const element_type& key)
  239. {
  240. return add(key);
  241. }
  242. /** Insert an interval of elements \c inter_val to the set */
  243. SubType& insert(const segment_type& inter_val)
  244. {
  245. return add(inter_val);
  246. }
  247. /** Insert an interval of elements \c inter_val to the set. Iterator
  248. \c prior_ is a hint to the position \c inter_val can be
  249. inserted after. */
  250. iterator insert(iterator prior_, const segment_type& inter_val)
  251. {
  252. return add(prior_, inter_val);
  253. }
  254. //==========================================================================
  255. //= Erasure
  256. //==========================================================================
  257. /** Erase an element \c key from the set */
  258. SubType& erase(const element_type& key)
  259. {
  260. return subtract(key);
  261. }
  262. /** Erase an interval of elements \c inter_val from the set */
  263. SubType& erase(const segment_type& inter_val)
  264. {
  265. return subtract(inter_val);
  266. }
  267. /** Erase the interval that iterator \c position points to. */
  268. void erase(iterator position)
  269. {
  270. _set.erase(position);
  271. }
  272. /** Erase all intervals in the range <tt>[first,past)</tt> of iterators. */
  273. void erase(iterator first, iterator past)
  274. {
  275. _set.erase(first, past);
  276. }
  277. //==========================================================================
  278. //= Symmetric difference
  279. //==========================================================================
  280. /** If \c *this set contains \c key it is erased, otherwise it is added. */
  281. SubType& flip(const element_type& key)
  282. {
  283. return icl::flip(*that(), key);
  284. }
  285. /** If \c *this set contains \c inter_val it is erased, otherwise it is added. */
  286. SubType& flip(const segment_type& inter_val)
  287. {
  288. return icl::flip(*that(), inter_val);
  289. }
  290. //==========================================================================
  291. //= Iterator related
  292. //==========================================================================
  293. iterator begin() { return _set.begin(); }
  294. iterator end() { return _set.end(); }
  295. const_iterator begin()const { return _set.begin(); }
  296. const_iterator end()const { return _set.end(); }
  297. reverse_iterator rbegin() { return _set.rbegin(); }
  298. reverse_iterator rend() { return _set.rend(); }
  299. const_reverse_iterator rbegin()const { return _set.rbegin(); }
  300. const_reverse_iterator rend()const { return _set.rend(); }
  301. iterator lower_bound(const value_type& interval)
  302. { return _set.lower_bound(interval); }
  303. iterator upper_bound(const value_type& interval)
  304. { return _set.upper_bound(interval); }
  305. const_iterator lower_bound(const value_type& interval)const
  306. { return _set.lower_bound(interval); }
  307. const_iterator upper_bound(const value_type& interval)const
  308. { return _set.upper_bound(interval); }
  309. std::pair<iterator,iterator> equal_range(const key_type& interval)
  310. {
  311. return std::pair<iterator,iterator>
  312. (_set.lower_bound(interval), _set.upper_bound(interval));
  313. }
  314. std::pair<const_iterator,const_iterator>
  315. equal_range(const key_type& interval)const
  316. {
  317. return std::pair<const_iterator,const_iterator>
  318. (_set.lower_bound(interval), _set.upper_bound(interval));
  319. }
  320. private:
  321. iterator _add(const segment_type& addend);
  322. iterator _add(iterator prior, const segment_type& addend);
  323. protected:
  324. void add_front(const interval_type& inter_val, iterator& first_);
  325. void add_main(interval_type& inter_val, iterator& it_, const iterator& last_);
  326. void add_segment(const interval_type& inter_val, iterator& it_);
  327. void add_rear(const interval_type& inter_val, iterator& it_);
  328. protected:
  329. sub_type* that() { return static_cast<sub_type*>(this); }
  330. const sub_type* that()const { return static_cast<const sub_type*>(this); }
  331. protected:
  332. ImplSetT _set;
  333. } ;
  334. template <class SubType, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  335. inline void interval_base_set<SubType,DomainT,Compare,Interval,Alloc>
  336. ::add_front(const interval_type& inter_val, iterator& first_)
  337. {
  338. // If the collision sequence has a left residual 'left_resid' it will
  339. // be split, to provide a standardized start of algorithms:
  340. // The addend interval 'inter_val' covers the beginning of the collision sequence.
  341. // only for the first there can be a left_resid: a part of *first_ left of inter_val
  342. interval_type left_resid = right_subtract(*first_, inter_val);
  343. if(!icl::is_empty(left_resid))
  344. { // [------------ . . .
  345. // [left_resid---first_ --- . . .
  346. iterator prior_ = cyclic_prior(*this, first_);
  347. const_cast<interval_type&>(*first_) = left_subtract(*first_, left_resid);
  348. //NOTE: Only splitting
  349. this->_set.insert(prior_, left_resid);
  350. }
  351. //POST:
  352. // [----- inter_val ---- . . .
  353. // ...[-- first_ --...
  354. }
  355. template <class SubType, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  356. inline void interval_base_set<SubType,DomainT,Compare,Interval,Alloc>
  357. ::add_segment(const interval_type& inter_val, iterator& it_)
  358. {
  359. interval_type lead_gap = right_subtract(inter_val, *it_);
  360. if(!icl::is_empty(lead_gap))
  361. // [lead_gap--- . . .
  362. // [prior_) [-- it_ ...
  363. this->_set.insert(prior(it_), lead_gap);
  364. // . . . --------- . . . addend interval
  365. // [-- it_ --) has a common part with the first overval
  366. ++it_;
  367. }
  368. template <class SubType, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  369. inline void interval_base_set<SubType,DomainT,Compare,Interval,Alloc>
  370. ::add_main(interval_type& rest_interval, iterator& it_, const iterator& last_)
  371. {
  372. interval_type cur_interval;
  373. while(it_ != last_)
  374. {
  375. cur_interval = *it_ ;
  376. add_segment(rest_interval, it_);
  377. // shrink interval
  378. rest_interval = left_subtract(rest_interval, cur_interval);
  379. }
  380. }
  381. template <class SubType, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  382. inline void interval_base_set<SubType,DomainT,Compare,Interval,Alloc>
  383. ::add_rear(const interval_type& inter_val, iterator& it_)
  384. {
  385. iterator prior_ = cyclic_prior(*this, it_);
  386. interval_type cur_itv = *it_;
  387. interval_type lead_gap = right_subtract(inter_val, cur_itv);
  388. if(!icl::is_empty(lead_gap))
  389. // [lead_gap--- . . .
  390. // [prior_) [-- it_ ...
  391. this->_set.insert(prior_, lead_gap);
  392. interval_type end_gap = left_subtract(inter_val, cur_itv);
  393. if(!icl::is_empty(end_gap))
  394. // [---------------end_gap)
  395. // [-- it_ --)
  396. it_ = this->_set.insert(it_, end_gap);
  397. else
  398. {
  399. // only for the last there can be a right_resid: a part of *it_ right of addend
  400. interval_type right_resid = left_subtract(cur_itv, inter_val);
  401. if(!icl::is_empty(right_resid))
  402. {
  403. // [--------------)
  404. // [-- it_ --right_resid)
  405. const_cast<interval_type&>(*it_) = right_subtract(*it_, right_resid);
  406. it_ = this->_set.insert(it_, right_resid);
  407. }
  408. }
  409. }
  410. //==============================================================================
  411. //= Addition
  412. //==============================================================================
  413. template <class SubType, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  414. inline typename interval_base_set<SubType,DomainT,Compare,Interval,Alloc>::iterator
  415. interval_base_set<SubType,DomainT,Compare,Interval,Alloc>
  416. ::_add(const segment_type& addend)
  417. {
  418. if(icl::is_empty(addend))
  419. return this->_set.end();
  420. std::pair<iterator,bool> insertion = this->_set.insert(addend);
  421. if(insertion.second)
  422. return that()->handle_inserted(insertion.first);
  423. else
  424. return that()->add_over(addend, insertion.first);
  425. }
  426. template <class SubType, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  427. inline typename interval_base_set<SubType,DomainT,Compare,Interval,Alloc>::iterator
  428. interval_base_set<SubType,DomainT,Compare,Interval,Alloc>
  429. ::_add(iterator prior_, const segment_type& addend)
  430. {
  431. if(icl::is_empty(addend))
  432. return prior_;
  433. iterator insertion = this->_set.insert(prior_, addend);
  434. if(*insertion == addend)
  435. return that()->handle_inserted(insertion);
  436. else
  437. return that()->add_over(addend);
  438. }
  439. //==============================================================================
  440. //= Subtraction
  441. //==============================================================================
  442. template <class SubType, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  443. inline SubType& interval_base_set<SubType,DomainT,Compare,Interval,Alloc>
  444. ::subtract(const segment_type& minuend)
  445. {
  446. if(icl::is_empty(minuend))
  447. return *that();
  448. std::pair<iterator, iterator> exterior = equal_range(minuend);
  449. if(exterior.first == exterior.second)
  450. return *that();
  451. iterator first_ = exterior.first;
  452. iterator end_ = exterior.second;
  453. iterator last_ = prior(end_);
  454. interval_type left_resid = right_subtract(*first_, minuend);
  455. interval_type right_resid;
  456. if(first_ != end_)
  457. right_resid = left_subtract(*last_ , minuend);
  458. this->_set.erase(first_, end_);
  459. if(!icl::is_empty(left_resid))
  460. this->_set.insert(left_resid);
  461. if(!icl::is_empty(right_resid))
  462. this->_set.insert(right_resid);
  463. return *that();
  464. }
  465. //-----------------------------------------------------------------------------
  466. // type traits
  467. //-----------------------------------------------------------------------------
  468. template<class SubType,
  469. class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  470. struct is_set<icl::interval_base_set<SubType,DomainT,Compare,Interval,Alloc> >
  471. {
  472. typedef is_set<icl::interval_base_set<SubType,DomainT,Compare,Interval,Alloc> > type;
  473. BOOST_STATIC_CONSTANT(bool, value = true);
  474. };
  475. template<class SubType,
  476. class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  477. struct is_interval_container<icl::interval_base_set<SubType,DomainT,Compare,Interval,Alloc> >
  478. {
  479. typedef is_interval_container<icl::interval_base_set<SubType,DomainT,Compare,Interval,Alloc> > type;
  480. BOOST_STATIC_CONSTANT(bool, value = true);
  481. };
  482. }} // namespace icl boost
  483. #endif