ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::RadixHeap< T, K, B, A, Policy > Class Template Reference

a monotone queue of items with integer keys of the type K, the least key first, in buckets by digits of \( B \) bits More...

#include <ADS/priority/RadixHeap.h>

Public types

using Item = T
 the type of the items
using Key = K
 the type of the keys
using modifications
 the events: an item inserted or erased with its key, the heap emptied or rebuilt
using Subject = typename Policy::template bind< modifications , RadixHeap >
 the subject
using allocator_type = A
 the type of the allocator

Public member functions

 RadixHeap (void)=default
 the empty heap
 RadixHeap (const A &alloc)
 the empty heap, its buckets from alloc
template<class... O>
requires ( ( sizeof...( O ) > 0 ) && ( ! std::is_convertible_v< O & , const A & > && ... ) && ( ! std::is_base_of_v< RadixHeap ,
O > && ... ) )
 RadixHeap (O &... o)
 the empty heap, the static observers o...
template<class... O>
 RadixHeap (std::allocator_arg_t, const A &alloc, O &... o)
 the empty heap, its buckets from alloc, the static observers o...
allocator_type get_allocator (void) const
 the allocator of the buckets
void push (const T &x, K k)
 pushes \( x \) with the key \( k \), in \( O( 1 ) \) time
const T & top (void)
 an item of least key, the last pushed among those
K topKey (void)
 the least key
K peekKey (void) const
 the least key, read from the least key of the first bucket in use, which does not move the buckets: the keys pushed next need only be not smaller than the last one extracted, as if topKey() were not called
T pop (void)
 removes top() and returns it, in \( O( W / B + 2^B W / ( 64 B ) ) \) amortized time
template<class F>
void extractBelow (K limit, F &&f)
 removes the items of keys below limit, calling f( item , key ) for each of them in no given order, which count as extracted: the buckets whose keys are all below limit are emptied whole, with no move, and the first one that holds a key not below it is moved to the rows below for its least key, as by pop(), until the limit falls between two buckets or within one of the row 0
bool empty (void) const
 whether the queue has no item
std::size_t size (void) const
 the number of items, \( n \)
void clear (void)
 removes the items, the buckets keeping their memory; any key may be pushed next
void rebase (K k)
 the keys pushed next may start from \( k \), the items placed again for \( \ell = k \), in \( O( n + 2^B W / B ) \) time
void extract (K theta, std::vector< T > &out)
 every item of key at most \( \theta \) appended to out, by increasing keys, each extracted as by pop()
Subject & subject (void)
 the subject, for ADS::batch() and ADS::quiet()
const Subject & subject (void) const
 the subject
template<class O>
requires ( Subject::dynamic )
int attach (O &o)
 attaches the observer \( o \) and gives its number
void detach (int id)
 detaches the observer number \( \mathit{id} \)

Detailed description

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
class ADS::RadixHeap< T, K, B, A, Policy >

a monotone queue of items with integer keys of the type K, the least key first, in buckets by digits of \( B \) bits

Member typedef documentation

◆ allocator_type

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
using ADS::RadixHeap< T, K, B, A, Policy >::allocator_type = A

the type of the allocator

◆ Item

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
using ADS::RadixHeap< T, K, B, A, Policy >::Item = T

the type of the items

◆ Key

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
using ADS::RadixHeap< T, K, B, A, Policy >::Key = K

the type of the keys

◆ modifications

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
using ADS::RadixHeap< T, K, B, A, Policy >::modifications
Initial value:
the structure emptied
Definition Modification.h:699
the item of handle , key and value erased, told before the removal outside a batch
Definition Modification.h:568
anything may have changed: forget and recompute
Definition Modification.h:708
a list of types, the events of a structure [see Modification.h]
Definition Modification.h:482

the events: an item inserted or erased with its key, the heap emptied or rebuilt

◆ Subject

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
using ADS::RadixHeap< T, K, B, A, Policy >::Subject = typename Policy::template bind< modifications , RadixHeap >

the subject

Constructor and destructor documentation

◆ RadixHeap() [1/4]

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
ADS::RadixHeap< T, K, B, A, Policy >::RadixHeap ( void )
default

the empty heap

◆ RadixHeap() [2/4]

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
ADS::RadixHeap< T, K, B, A, Policy >::RadixHeap ( const A & alloc)
inlineexplicit

the empty heap, its buckets from alloc

◆ RadixHeap() [3/4]

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
template<class... O>
requires ( ( sizeof...( O ) > 0 ) && ( ! std::is_convertible_v< O & , const A & > && ... ) && ( ! std::is_base_of_v< RadixHeap ,
O > && ... ) )
ADS::RadixHeap< T, K, B, A, Policy >::RadixHeap ( O &... o)
inlineexplicit

the empty heap, the static observers o...

◆ RadixHeap() [4/4]

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
template<class... O>
ADS::RadixHeap< T, K, B, A, Policy >::RadixHeap ( std::allocator_arg_t ,
const A & alloc,
O &... o )
inline

the empty heap, its buckets from alloc, the static observers o...

Member function documentation

◆ attach()

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
template<class O>
requires ( Subject::dynamic )
int ADS::RadixHeap< T, K, B, A, Policy >::attach ( O & o)
inline

attaches the observer \( o \) and gives its number

Precondition
no batch is open

◆ clear()

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
void ADS::RadixHeap< T, K, B, A, Policy >::clear ( void )

removes the items, the buckets keeping their memory; any key may be pushed next

◆ detach()

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
void ADS::RadixHeap< T, K, B, A, Policy >::detach ( int id)
inline

detaches the observer number \( \mathit{id} \)

Precondition
it is attached, no batch is open

◆ empty()

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
bool ADS::RadixHeap< T, K, B, A, Policy >::empty ( void ) const
inlinenodiscard

whether the queue has no item

◆ extract()

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
void ADS::RadixHeap< T, K, B, A, Policy >::extract ( K theta,
std::vector< T > & out )

every item of key at most \( \theta \) appended to out, by increasing keys, each extracted as by pop()

◆ extractBelow()

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
template<class F>
void ADS::RadixHeap< T, K, B, A, Policy >::extractBelow ( K limit,
F && f )

removes the items of keys below limit, calling f( item , key ) for each of them in no given order, which count as extracted: the buckets whose keys are all below limit are emptied whole, with no move, and the first one that holds a key not below it is moved to the rows below for its least key, as by pop(), until the limit falls between two buckets or within one of the row 0

In pseudocode (beyond the book), \( \lambda \) the limit and \( \mathit{hi}( b ) \) the largest key that the bucket \( b \) may hold for \( \ell \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{extractBelow}( \lambda , f ) \cr 2 & \quad \textbf{while}\ n > 0\ \textbf{do} \cr 3 & \qquad b \gets \text{the first bucket in use} \cr 4 & \qquad \textbf{if}\ \mathit{lo}[ b ] \ge \lambda \ \textbf{then}\ \textbf{break} \cr 5 & \qquad \textbf{if}\ \mathit{hi}( b ) < \lambda\ \textbf{then} \ f( x , k ) \text{ for each } ( k , x ) \in B[ b ];\ \text{empty } b \cr 6 & \qquad \textbf{else if}\ b \ge 2^B\ \textbf{then}\ \ell \gets \mathit{lo}[ b ];\ \text{move the items of } B[ b ] \text{ to} \text{ their buckets} \cr 7 & \qquad \textbf{else} \cr 8 & \qquad\quad f( x , k ) \text{ for each } ( k , x ) \in B[ b ] \text{ with } k < \lambda \text{, the others kept in } B[ b ] \cr 9 & \qquad\quad \textbf{if}\ B[ b ] \ne \emptyset\ \textbf{then} \ \textbf{break} \end{array}} \]

◆ get_allocator()

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
allocator_type ADS::RadixHeap< T, K, B, A, Policy >::get_allocator ( void ) const
inlinenodiscard

the allocator of the buckets

◆ peekKey()

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
K ADS::RadixHeap< T, K, B, A, Policy >::peekKey ( void ) const
nodiscard

the least key, read from the least key of the first bucket in use, which does not move the buckets: the keys pushed next need only be not smaller than the last one extracted, as if topKey() were not called

Precondition
the queue is not empty

◆ pop()

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
T ADS::RadixHeap< T, K, B, A, Policy >::pop ( void )

removes top() and returns it, in \( O( W / B + 2^B W / ( 64 B ) ) \) amortized time

In pseudocode (Ahuja, Mehlhorn, Orlin and Tarjan, beyond the book), with \( \mathrm{bucket}() \) of push():

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pop}() \cr 2 & \quad b \gets \text{the first bucket in use, by the lowest bit} \text{ of the mask} \cr 3 & \quad \textbf{if}\ b \ge 2^B\ \textbf{then}\quad \triangleright \ \text{above the row 0} \cr 4 & \qquad \ell \gets \mathit{lo}[ b ];\ \mathit{lo}[ b ] \gets \infty;\ \text{mark } b \text{ not in use} \cr 5 & \qquad \textbf{foreach}\ ( k , x ) \in B[ b ]\ \textbf{do} \ \text{push } ( k , x ) \text{ on } B[ \mathrm{bucket}( k ) ] \text{, updating } \mathit{lo} \text{ and the mask} \cr 6 & \qquad B[ b ] \gets \emptyset;\ b \gets \mathrm{bucket}( \ell ) \cr 7 & \quad \textbf{else}\ \ell \gets \ell \text{ with its last digit} \text{ set to } b \cr 8 & \quad ( k , x ) \gets \text{the pair popped from } B[ b ] \cr 9 & \quad \textbf{if}\ B[ b ] = \emptyset\ \textbf{then} \ \mathit{lo}[ b ] \gets \infty;\ \text{mark } b \text{ not in use} \cr 10 & \quad n \gets n - 1;\ \textbf{return}\ x \end{array}} \]

Precondition
the queue is not empty

◆ push()

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
void ADS::RadixHeap< T, K, B, A, Policy >::push ( const T & x,
K k )

pushes \( x \) with the key \( k \), in \( O( 1 ) \) time

In pseudocode (Ahuja, Mehlhorn, Orlin and Tarjan, beyond the book), \( \mathit{lo}[ b ] \) the least key of the bucket \( b \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{push}( x , k ) \cr 2 & \quad b \gets \mathrm{bucket}( k );\ \text{push } ( k , x ) \text{ on } B[ b ];\ \text{mark } b \text{ in use} \cr 3 & \quad \mathit{lo}[ b ] \gets \min\{ \mathit{lo}[ b ] , k \};\ n \gets n + 1 \cr 4 & \textbf{function}\ \mathrm{bucket}( k ) \cr 5 & \quad r \gets \text{the digit of the highest bit of } k \oplus \ell \text{, 0 if } k = \ell \cr 6 & \quad \textbf{return}\ 2^B r + \text{the digit } r \text{ of } k \end{array}} \]

Precondition
\( k \) is not smaller than any key extracted, by pop() or extractBelow(), or read by top() and topKey() (any key before the first of them and after clear())

◆ rebase()

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
void ADS::RadixHeap< T, K, B, A, Policy >::rebase ( K k)

the keys pushed next may start from \( k \), the items placed again for \( \ell = k \), in \( O( n + 2^B W / B ) \) time

In pseudocode (beyond the book), with \( \mathrm{bucket}() \) of push():

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{rebase}( k ) \cr 2 & \quad E \gets \text{the pairs } ( k' , x ) \text{ of all the} \text{ buckets};\ \text{empty the buckets} \cr 3 & \quad \ell \gets k;\ \text{the largest key extracted} \gets k \cr 4 & \quad \textbf{foreach}\ ( k' , x ) \in E\ \textbf{do}\ \text{push } ( k' , x ) \text{ on } B[ \mathrm{bucket}( k' ) ] \end{array}} \]

Precondition
no key in the heap is below \( k \)

◆ size()

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
std::size_t ADS::RadixHeap< T, K, B, A, Policy >::size ( void ) const
inlinenodiscard

the number of items, \( n \)

◆ subject() [1/2]

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
Subject & ADS::RadixHeap< T, K, B, A, Policy >::subject ( void )
inlinenodiscard

the subject, for ADS::batch() and ADS::quiet()

◆ subject() [2/2]

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
const Subject & ADS::RadixHeap< T, K, B, A, Policy >::subject ( void ) const
inlinenodiscard

the subject

◆ top()

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
const T & ADS::RadixHeap< T, K, B, A, Policy >::top ( void )
nodiscard

an item of least key, the last pushed among those

Precondition
the queue is not empty

◆ topKey()

template<class T, std::integral K = std::uint32_t, unsigned B = 4, class A = std::allocator< T >, class Policy = NoObservers>
K ADS::RadixHeap< T, K, B, A, Policy >::topKey ( void )
nodiscard

the least key

Precondition
the queue is not empty

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