ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::IntervalTree< T > Class Template Reference

a multiset of closed intervals of T with stabbing queries More...

#include <ADS/geometry/IntervalTree.h>

Classes

struct  MaxEnd
 the augmentation: the largest right end, nothing for no interval More...

Public types

using Interval = std::pair< T , T >
 \( [ a , b ] \)

Public member functions

 IntervalTree (void)=default
 no interval
 IntervalTree (std::vector< Interval > I)
 the intervals of \( I \), sorted, counted and inserted by multiInsert()
template<Executor E>
 IntervalTree (E &ex, std::vector< Interval > I)
 the intervals of \( I \), as above, on the threads of ex
void insert (const T &a, const T &b)
 one more copy of \( [ a , b ] \), \( O( \log n ) \)
void remove (const T &a, const T &b)
 one copy less of \( [ a , b ] \), if there, \( O( \log n ) \)
std::size_t size (void) const
 the intervals, with their copies
bool stabbed (const T &x) const
 whether an interval contains \( x \), \( O( \log n ) \)
template<class F>
void stab (const T &x, F &&f) const
 f( a , b ) once per copy of each interval that contains \( x \), by increasing \( ( a , b ) \); \( O( \min( n , ( k + 1 ) \log n ) ) \) for \( k \) distinct intervals reported
std::size_t count (const T &x) const
 the intervals that contain \( x \), with their copies, by stab(); \( O( \min( n , ( k + 1 ) \log n ) ) \)

Detailed description

template<std::totally_ordered T>
class ADS::IntervalTree< T >

a multiset of closed intervals of T with stabbing queries

Member typedef documentation

◆ Interval

template<std::totally_ordered T>
using ADS::IntervalTree< T >::Interval = std::pair< T , T >

\( [ a , b ] \)

Constructor and destructor documentation

◆ IntervalTree() [1/3]

template<std::totally_ordered T>
ADS::IntervalTree< T >::IntervalTree ( void )
default

no interval

◆ IntervalTree() [2/3]

template<std::totally_ordered T>
ADS::IntervalTree< T >::IntervalTree ( std::vector< Interval > I)
explicit

the intervals of \( I \), sorted, counted and inserted by multiInsert()

In pseudocode (the construction of the augmented maps of Sun and Blelloch, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{IntervalTree}( I ) \cr 2 & \quad \text{sort } I \text{ by } ( a , b ) \cr 3 & \quad B \gets \text{the distinct intervals of } I \text{, each with } \text{its copies} \cr 4 & \quad \mathrm{multiInsert}( T , B )\quad \triangleright\ \text{joins } \text{of balanced halves, } \mathit{aug} \text{ kept by the join} \cr 5 & \quad n \gets |I| \end{array}} \]

Precondition
\( a \le b \) for each one

◆ IntervalTree() [3/3]

template<std::totally_ordered T>
template<Executor E>
ADS::IntervalTree< T >::IntervalTree ( E & ex,
std::vector< Interval > I )

the intervals of \( I \), as above, on the threads of ex

Precondition
\( a \le b \) for each one

Member function documentation

◆ count()

template<std::totally_ordered T>
std::size_t ADS::IntervalTree< T >::count ( const T & x) const
nodiscard

the intervals that contain \( x \), with their copies, by stab(); \( O( \min( n , ( k + 1 ) \log n ) ) \)

In pseudocode:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{count}( x ) \cr 2 & \quad \textbf{return}\ \text{the calls of } f \text{ in } \mathrm{stab}( x , f ) \end{array}} \]

◆ insert()

template<std::totally_ordered T>
void ADS::IntervalTree< T >::insert ( const T & a,
const T & b )

one more copy of \( [ a , b ] \), \( O( \log n ) \)

In pseudocode:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( a , b ) \cr 2 & \quad c \gets \text{the copies of } ( a , b ) \text{ in } T \text{, 0 if none} \cr 3 & \quad \mathrm{insert}( T , ( a , b ) , c + 1 );\ n \gets n + 1 \end{array}} \]

Precondition
\( a \le b \)

◆ remove()

template<std::totally_ordered T>
void ADS::IntervalTree< T >::remove ( const T & a,
const T & b )

one copy less of \( [ a , b ] \), if there, \( O( \log n ) \)

In pseudocode:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( a , b ) \cr 2 & \quad c \gets \text{the copies of } ( a , b ) \text{ in } T \cr 3 & \quad \textbf{if}\ c = \textbf{nil}\ \textbf{then return} \cr 4 & \quad \textbf{if}\ c = 1\ \textbf{then}\ \mathrm{remove}( T , ( a , b ) )\ \textbf{else}\ \mathrm{insert}( T , ( a , b ) , c - 1 ) \cr 5 & \quad n \gets n - 1 \end{array}} \]

◆ size()

template<std::totally_ordered T>
std::size_t ADS::IntervalTree< T >::size ( void ) const
inlinenodiscard

the intervals, with their copies

◆ stab()

template<std::totally_ordered T>
template<class F>
void ADS::IntervalTree< T >::stab ( const T & x,
F && f ) const

f( a , b ) once per copy of each interval that contains \( x \), by increasing \( ( a , b ) \); \( O( \min( n , ( k + 1 ) \log n ) ) \) for \( k \) distinct intervals reported

In pseudocode (Sun and Blelloch, beyond the book), a visit in symmetric order cut where no interval can contain \( x \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{visit}( u , x , f ) \cr 2 & \quad \textbf{if}\ u = \textbf{nil}\ \textbf{or}\ \mathit{aug}( u ) < x\ \textbf{then return} \cr 3 & \quad \mathrm{visit}( u.\mathit{left} , x , f ) \cr 4 & \quad \textbf{if}\ x < a( u )\ \textbf{then return}\quad \triangleright\ \text{no } a \le x \text{ to the right} \cr 5 & \quad \textbf{if}\ b( u ) \ge x\ \textbf{then}\ \text{call } f( a( u ) , b( u ) ) \text{ once per copy} \cr 6 & \quad \mathrm{visit}( u.\mathit{right} , x , f ) \cr 7 & \textbf{procedure}\ \mathrm{stab}( x , f ) \cr 8 & \quad \mathrm{visit}( \text{the root of } T , x , f ) \end{array}} \]

◆ stabbed()

template<std::totally_ordered T>
bool ADS::IntervalTree< T >::stabbed ( const T & x) const
nodiscard

whether an interval contains \( x \), \( O( \log n ) \)

In pseudocode (Sun and Blelloch, beyond the book), \( m \) the largest right end of the intervals with \( a \le x \) met so far:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{stabbed}( x ) \cr 2 & \quad m \gets \textbf{nil};\ u \gets \text{the root of } T \cr 3 & \quad \textbf{while}\ u \ne \textbf{nil}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ x < a( u )\ \textbf{then}\ u \gets u.\mathit{left} \cr 5 & \qquad \textbf{else} \cr 6 & \qquad\quad m \gets \max( m , \mathit{aug}( u.\mathit{left} ) , b( u ) );\ u \gets u.\mathit{right} \cr 7 & \quad \textbf{return}\ m \ne \textbf{nil}\ \textbf{and}\ m \ge x \end{array}} \]


The documentation for this class was generated from the following file: