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

the range minima of an array by the sparse table More...

#include <ADS/search/RangeMinimum.h>

Public types

using value_type = T
 the type of the items
using size_type = std::size_t
 the type of the sizes
using difference_type = std::ptrdiff_t
 the type of the distances
using const_reference = const T &
 a reference to an item
using reference = const_reference
 the same, constant
using const_iterator = typename std::vector< T >::const_iterator
 an iterator on the items
using iterator = const_iterator
 the same, constant

Public member functions

 SparseTable (void)=default
 an empty table
 SparseTable (std::vector< T > A, Compare less={})
 the table of \( A \), \( O( n \log n ) \)
std::size_t query (std::size_t i, std::size_t j) const
 the place of the leftmost minimum of \( A[ i .. j ] \), \( O( 1 ) \)
std::size_t size (void) const
 the number \( n \) of the items
const T & operator[] (std::size_t i) const
 the item of place \( i \)
const_iterator begin (void) const
 the first item
const_iterator end (void) const
 past the last item
const_iterator cbegin (void) const
 the first item
const_iterator cend (void) const
 past the last item

Detailed description

template<class T, class Compare = std::less< T >>
class ADS::SparseTable< T, Compare >

the range minima of an array by the sparse table

Member typedef documentation

◆ const_iterator

template<class T, class Compare = std::less< T >>
using ADS::SparseTable< T, Compare >::const_iterator = typename std::vector< T >::const_iterator

an iterator on the items

◆ const_reference

template<class T, class Compare = std::less< T >>
using ADS::SparseTable< T, Compare >::const_reference = const T &

a reference to an item

◆ difference_type

template<class T, class Compare = std::less< T >>
using ADS::SparseTable< T, Compare >::difference_type = std::ptrdiff_t

the type of the distances

◆ iterator

template<class T, class Compare = std::less< T >>
using ADS::SparseTable< T, Compare >::iterator = const_iterator

the same, constant

◆ reference

template<class T, class Compare = std::less< T >>
using ADS::SparseTable< T, Compare >::reference = const_reference

the same, constant

◆ size_type

template<class T, class Compare = std::less< T >>
using ADS::SparseTable< T, Compare >::size_type = std::size_t

the type of the sizes

◆ value_type

template<class T, class Compare = std::less< T >>
using ADS::SparseTable< T, Compare >::value_type = T

the type of the items

Constructor and destructor documentation

◆ SparseTable() [1/2]

template<class T, class Compare = std::less< T >>
ADS::SparseTable< T, Compare >::SparseTable ( void )
default

an empty table

◆ SparseTable() [2/2]

template<class T, class Compare = std::less< T >>
ADS::SparseTable< T, Compare >::SparseTable ( std::vector< T > A,
Compare less = {} )
explicit

the table of \( A \), \( O( n \log n ) \)

In pseudocode (Bender and Farach-Colton, beyond the book), \( M[ L ][ i ] \) the place of the leftmost minimum of \( A[ i .. i + 2^L - 1 ] \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{build}( A ) \cr 2 & \quad \textbf{foreach}\ i\ \textbf{do}\ M[ 0 ][ i ] \gets i \cr 3 & \quad \textbf{for}\ L \gets 1\ \textbf{to}\ \lfloor \log_2 n \rfloor \ \textbf{do} \cr 4 & \qquad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 2^L\ \textbf{do} \cr 5 & \qquad\quad M[ L ][ i ] \gets \text{the better of } M[ L - 1 ][ i ] \text{ and } M[ L - 1 ][ i + 2^{L-1} ] \end{array}} \]

where the better place is the one of the smaller item, the left one on a tie.

Member function documentation

◆ begin()

template<class T, class Compare = std::less< T >>
const_iterator ADS::SparseTable< T, Compare >::begin ( void ) const
inlinenodiscard

the first item

◆ cbegin()

template<class T, class Compare = std::less< T >>
const_iterator ADS::SparseTable< T, Compare >::cbegin ( void ) const
inlinenodiscard

the first item

◆ cend()

template<class T, class Compare = std::less< T >>
const_iterator ADS::SparseTable< T, Compare >::cend ( void ) const
inlinenodiscard

past the last item

◆ end()

template<class T, class Compare = std::less< T >>
const_iterator ADS::SparseTable< T, Compare >::end ( void ) const
inlinenodiscard

past the last item

◆ operator[]()

template<class T, class Compare = std::less< T >>
const T & ADS::SparseTable< T, Compare >::operator[] ( std::size_t i) const
nodiscard

the item of place \( i \)

Precondition
\( i < n \)

◆ query()

template<class T, class Compare = std::less< T >>
std::size_t ADS::SparseTable< T, Compare >::query ( std::size_t i,
std::size_t j ) const
nodiscard

the place of the leftmost minimum of \( A[ i .. j ] \), \( O( 1 ) \)

In pseudocode (Bender and Farach-Colton, beyond the book), two ranges of length \( 2^L \) covering \( [ i , j ] \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{query}( i , j ) \cr 2 & \quad L \gets \lfloor \log_2 ( j - i + 1 ) \rfloor \cr 3 & \quad \textbf{return}\ \text{the better of } M[ L ][ i ] \text{ and } M[ L ][ j - 2^L + 1 ] \end{array}} \]

Precondition
\( i \le j < n \)
Postcondition
a leftmost minimum of \( A[ i .. j ] \) (audit)

◆ size()

template<class T, class Compare = std::less< T >>
std::size_t ADS::SparseTable< T, Compare >::size ( void ) const
inlinenodiscard

the number \( n \) of the items


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