27#if defined(IntegralIntervals_RECURSES)
28#error Recursive header files inclusion detected in IntegralIntervals.h
31#define IntegralIntervals_RECURSES
33#if !defined IntegralIntervals_h
35#define IntegralIntervals_h
39#include "DGtal/base/Common.h"
40#include "DGtal/kernel/CBoundedNumber.h"
61 template <
typename TInteger >
99 template <
typename InputIterator>
102 if ( it == itE )
return;
105 for ( ++it; it != itE; ++it )
108 if ( first <= x && x <= last )
continue;
109 if ( x == last+1 ) { last = x;
continue; }
110 if ( x == first-1 ) { first = x;
continue; }
133 for (
const auto& I :
myData ) nb += 1 + I.second - I.first;
146 return myData.size() <= 1;
153 for (
const auto& I :
myData )
154 for (
Integer x = I.first; x <= I.second; x++ )
161 std::vector<Integer> S;
162 for (
const auto& I :
myData )
163 for (
Integer x = I.first; x <= I.second; x++ )
172 if (
empty() )
return 0;
177 const Size m = (i+j)/2;
181 if ( m == 0 )
return 0;
184 else if ( I.second < x )
217 else if ( I.first < it->first )
220 if ( it !=
myData.begin() )
222 auto it_prev = it; --it_prev;
223 if ( I.first <= it_prev->second + 1 )
225 it_prev->second = I.second;
237 it->second = std::max( it->second, I.second );
259 for ( std::size_t i = 0; i <
myData.size(); )
263 if ( I.second < J.first )
265 if ( J.second < I.first )
273 Interval K1( J.first, I.first - 1 );
274 Interval K2( I.second + 1, J.second );
275 bool K1_exist = K1.second >= K1.first;
276 bool K2_exist = K2.second >= K2.first;
277 if ( K1_exist && K2_exist )
303 for (
const auto& I : other.
myData )
313 for (
const auto& I : other.
myData )
345 return A_plus_B.
subtract( A_delta_B );
353 Self A_minus_B = *
this;
355 Self B_minus_A = other;
357 return A_minus_B.
add( B_minus_A );
369 for (
auto& I :
R.myData )
371 I.first = 2*I.first-1;
372 I.second = 2*I.second+1;
384 for (
auto i = 0; i <
R.myData.size(); )
386 auto& I =
R.myData[ i ];
387 if ( ( I.first & 0x1 ) == 0 ) I.first -= 1;
388 if ( ( I.second & 0x1 ) == 0 ) I.second += 1;
393 if ( i <
R.myData.size() )
395 auto& Inext =
R.myData[ i ];
396 if ( Inext.first <= I.second+1 )
398 I.second = Inext.second;
399 R.myData.erase(
R.myData.begin() + i );
414 if ( ( I.first & 0x1 ) != 0 ) I.first -= 1;
415 if ( ( I.second & 0x1 ) != 0 ) I.second += 1;
416 for (
auto x = I.first; x <= I.second; x += 2 )
417 C.push_back( x >> 1 );
419 auto last = std::unique( C.begin(), C.end() );
420 C.erase( last, C.end() );
428 auto it =
myData.cbegin();
429 for (
const auto& I : other.
myData )
432 while ( it !=
myData.cend() && it->second < I.second ) ++it;
433 if ( it ==
myData.cend() )
return false;
434 if ( I.first < it->first )
return false;
443 if (
myData.size() != other.
myData.size() )
return false;
444 auto it =
myData.cbegin();
445 for (
const auto& I : other.
myData )
447 if ( it->first != I.first || it->second != I.second )
return false;
463 for (
const auto& I :
myData )
464 out <<
" (" << I.first <<
"," << I.second <<
")";
471 for (
const auto& I :
myData )
472 if ( I.first > I.second )
return false;
491 while ( it_next !=
myData.end() )
493 if ( it->second >= ( it_next->first - 1 ) )
495 it->second = std::max( it->second, it_next->second );
503 myData.erase( it, it_next );
519 const Size m = (i+j)/2;
526 else if ( I.second < x )
529 return myData.begin() + m;
532 return myData.begin() + i;
548 template <
typename TInteger>
552 object.selfDisplay( out );
567#undef IntegralIntervals_RECURSES
BOOST_CONCEPT_ASSERT((concepts::CBoundedNumber< TInteger >))
IntegerRange extremaOfCells() const
Self set_intersection(const Self &other) const
void erase(Integer f, Integer l)
void insert(const Interval &I)
void extend(CIterator it)
std::set< Integer > integerSet() const
std::vector< Integer > integerVector() const
bool includes(const Self &other) const
Self & add(const Self &other)
CIterator lowerBound(Integer x)
Self & operator=(const Self &other)=default
std::pair< Integer, Integer > Interval
IntegralIntervals()=default
Default Constructor.
Self set_symmetric_difference(const Self &other) const
void erase(const Interval &I)
Self & subtract(const Self &other)
IntegralIntervals(const Self &other)=default
void clear()
Clears the data structure.
Self & operator=(Self &&other)=default
bool equals(const Self &other) const
typename Container::iterator CIterator
std::vector< Interval > Container
void selfDisplay(std::ostream &out) const
Self starOfPoints() const
IntegralIntervals(Self &&other)=default
Container myData
The sorted sequence of integral intervals.
const Container & data() const
void insert(Integer f, Integer l)
Size count(Integer x) const
IntegralIntervals(InputIterator it, InputIterator itE)
Self set_union(const Self &other) const
std::vector< Integer > IntegerRange
Self set_difference(const Self &other) const
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
Aim: The concept CBoundedNumber specifies what are the bounded numbers. Models of this concept should...