comparison DEPENDENCIES/generic/include/boost/icl/interval_base_set.hpp @ 16:2665513ce2d3

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