ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::HeapPriorityQueue< T, P, D > Class Template Reference

a priority queue on a \( d \)-ary min-heap of bounded capacity, \( d \) the parameter D More...

#include <ADS/priority/HeapPriorityQueue.h>

Classes

struct  PriorityItem
 the PRIORITYITEM of the book: priority, item, place in the vector More...

Public types

using Item = T
 the type of the items
using Priority = P
 the type of the priorities
using Handle = PriorityItem *
 what insert() gives
using value_type = PriorityItem
 the type of the items of the view
using size_type = std::size_t
 the type of the sizes
using difference_type = std::ptrdiff_t
 the type of the distances between iterators
using reference = const PriorityItem &
 the type of a reference to an item of the view
using const_reference = const PriorityItem &
 the type of a constant reference to an item
using const_iterator
 the iterator of the view, constant
using iterator = const_iterator
 the same as const_iterator

Public member functions

 HeapPriorityQueue (int n)
 the empty queue of capacity \( n \) (the PriorityQueue( n ) of the book)
 HeapPriorityQueue (const HeapPriorityQueue &)=delete
 no copy: the handles point into the queue
HeapPriorityQueue & operator= (const HeapPriorityQueue &)=delete
 no copy: the handles point into the queue
 HeapPriorityQueue (HeapPriorityQueue &&other) noexcept
 the items of other, which is left empty; its handles stay valid here
HeapPriorityQueue & operator= (HeapPriorityQueue &&other) noexcept
 the items of other, which is left empty, in place of these; its handles stay valid here
 ~HeapPriorityQueue ()
 the queue and its items destroyed
bool empty (void) const
 whether the queue has no item
int size (void) const
 the number of items, \( n \)
int capacity (void) const
 the largest number of items, \( c \)
const T & min (void) const
 the item of smallest priority, the root \( H[ 0 ] \)
const P & minPriority (void) const
 the smallest priority
T deleteMin (void)
 removes the item of smallest priority and returns it, the last leaf moved to the root and down, in \( O( d \log_d n ) \) time; the handle of the item removed is no longer valid
Handle insert (const T &x, const P &p)
 inserts \( x \) with the priority \( p \) and gives its handle, a new leaf moved up, in \( O( \log_d n ) \) time
void decrease (Handle y, const P &p)
 lowers to \( p \) the priority of the item of the handle \( y \), which moves up, in \( O( \log_d n ) \) time
std::vector< P > priorities (void) const
 the heap: the priorities, by their place
const_iterator begin (void) const
 the first item of the view
const_iterator end (void) const
 the end of the view
const_iterator cbegin (void) const
 the same as begin()
const_iterator cend (void) const
 the same as end()

Static public member functions

static int parent (int i)
 the parent \( \lfloor ( i - 1 ) / d \rfloor \) of the place \( i > 0 \)
static int child (int i, int j)
 the child \( d i + j + 1 \) of the place \( i \), \( 0 \le j < d \)

Detailed description

template<class T, std::totally_ordered P = int, int D = 2>
requires ( D >= 2 )
class ADS::HeapPriorityQueue< T, P, D >

a priority queue on a \( d \)-ary min-heap of bounded capacity, \( d \) the parameter D

Member typedef documentation

◆ const_iterator

template<class T, std::totally_ordered P = int, int D = 2>
using ADS::HeapPriorityQueue< T, P, D >::const_iterator
Initial value:
the random-access iterator I on pointers read through them, *it being **base; the items are constant
Definition Iterators.h:507

the iterator of the view, constant

◆ const_reference

template<class T, std::totally_ordered P = int, int D = 2>
using ADS::HeapPriorityQueue< T, P, D >::const_reference = const PriorityItem &

the type of a constant reference to an item

◆ difference_type

template<class T, std::totally_ordered P = int, int D = 2>
using ADS::HeapPriorityQueue< T, P, D >::difference_type = std::ptrdiff_t

the type of the distances between iterators

◆ Handle

template<class T, std::totally_ordered P = int, int D = 2>
using ADS::HeapPriorityQueue< T, P, D >::Handle = PriorityItem *

what insert() gives

◆ Item

template<class T, std::totally_ordered P = int, int D = 2>
using ADS::HeapPriorityQueue< T, P, D >::Item = T

the type of the items

◆ iterator

template<class T, std::totally_ordered P = int, int D = 2>
using ADS::HeapPriorityQueue< T, P, D >::iterator = const_iterator

the same as const_iterator

◆ Priority

template<class T, std::totally_ordered P = int, int D = 2>
using ADS::HeapPriorityQueue< T, P, D >::Priority = P

the type of the priorities

◆ reference

template<class T, std::totally_ordered P = int, int D = 2>
using ADS::HeapPriorityQueue< T, P, D >::reference = const PriorityItem &

the type of a reference to an item of the view

◆ size_type

template<class T, std::totally_ordered P = int, int D = 2>
using ADS::HeapPriorityQueue< T, P, D >::size_type = std::size_t

the type of the sizes

◆ value_type

template<class T, std::totally_ordered P = int, int D = 2>
using ADS::HeapPriorityQueue< T, P, D >::value_type = PriorityItem

the type of the items of the view

Constructor and destructor documentation

◆ HeapPriorityQueue() [1/3]

template<class T, std::totally_ordered P = int, int D = 2>
ADS::HeapPriorityQueue< T, P, D >::HeapPriorityQueue ( int n)
explicit

the empty queue of capacity \( n \) (the PriorityQueue( n ) of the book)

Precondition
\( n \ge 0 \)

◆ HeapPriorityQueue() [2/3]

template<class T, std::totally_ordered P = int, int D = 2>
ADS::HeapPriorityQueue< T, P, D >::HeapPriorityQueue ( const HeapPriorityQueue< T, P, D > & )
delete

no copy: the handles point into the queue

◆ HeapPriorityQueue() [3/3]

template<class T, std::totally_ordered P = int, int D = 2>
ADS::HeapPriorityQueue< T, P, D >::HeapPriorityQueue ( HeapPriorityQueue< T, P, D > && other)
noexcept

the items of other, which is left empty; its handles stay valid here

◆ ~HeapPriorityQueue()

template<class T, std::totally_ordered P = int, int D = 2>
ADS::HeapPriorityQueue< T, P, D >::~HeapPriorityQueue ( )

the queue and its items destroyed

Member function documentation

◆ begin()

template<class T, std::totally_ordered P = int, int D = 2>
const_iterator ADS::HeapPriorityQueue< T, P, D >::begin ( void ) const
inlinenodiscard

the first item of the view

◆ capacity()

template<class T, std::totally_ordered P = int, int D = 2>
int ADS::HeapPriorityQueue< T, P, D >::capacity ( void ) const
nodiscard

the largest number of items, \( c \)

◆ cbegin()

template<class T, std::totally_ordered P = int, int D = 2>
const_iterator ADS::HeapPriorityQueue< T, P, D >::cbegin ( void ) const
inlinenodiscard

the same as begin()

◆ cend()

template<class T, std::totally_ordered P = int, int D = 2>
const_iterator ADS::HeapPriorityQueue< T, P, D >::cend ( void ) const
inlinenodiscard

the same as end()

◆ child()

template<class T, std::totally_ordered P = int, int D = 2>
int ADS::HeapPriorityQueue< T, P, D >::child ( int i,
int j )
inlinestatic

the child \( d i + j + 1 \) of the place \( i \), \( 0 \le j < d \)

◆ decrease()

template<class T, std::totally_ordered P = int, int D = 2>
void ADS::HeapPriorityQueue< T, P, D >::decrease ( Handle y,
const P & p )

lowers to \( p \) the priority of the item of the handle \( y \), which moves up, in \( O( \log_d n ) \) time

In pseudocode (section 10.1.3):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{decrease}( y , p ) \cr 2 & \quad y.\mathit{priority} \gets p;\ \mathrm{up}( y.\mathit{pos} )\quad \triangleright\ \mathrm{swap} \text{ keeps } \mathit{pos} \text{ up to date} \end{array}} \]

Precondition
\( y \) is an item of this queue and \( p \) is not larger than its priority
Postcondition
\( y \) has the priority \( p \), at its place

◆ deleteMin()

template<class T, std::totally_ordered P = int, int D = 2>
T ADS::HeapPriorityQueue< T, P, D >::deleteMin ( void )

removes the item of smallest priority and returns it, the last leaf moved to the root and down, in \( O( d \log_d n ) \) time; the handle of the item removed is no longer valid

In pseudocode (section 10.1.3), on the places \( H[ 0 .. n - 1 ] \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{deleteMin}() \cr 2 & \quad \mathit{dim} \gets n - 1;\ \mathrm{swap}( H , 0 , \mathit{dim} )\quad \triangleright\ \text{the last leaf at the root} \cr 3 & \quad \mathrm{minHeapRestore}( H , 0 , \mathit{dim} );\ x \gets \text{the item of } H[ \mathit{dim} ];\ n \gets \mathit{dim} \cr 4 & \quad \textbf{return}\ x \cr 5 & \textbf{procedure}\ \mathrm{minHeapRestore}( H , i , \mathit{dim} )\quad \triangleright\ \text{the } d \text{ children of } i \text{ are } d i + 1 .. d i + d \cr 6 & \quad \textbf{loop} \cr 7 & \qquad \mathit{min} \gets \text{the place of least priority among } i \text{ and its children below } \mathit{dim} \cr 8 & \qquad \textbf{if}\ \mathit{min} = i\ \textbf{then return} \cr 9 & \qquad \mathrm{swap}( H , i , \mathit{min} );\ i \gets \mathit{min}\quad \triangleright\ \text{one level down} \end{array}} \]

Precondition
the queue is not empty
Postcondition
one item less, and none of smaller priority than the one removed

◆ empty()

template<class T, std::totally_ordered P = int, int D = 2>
bool ADS::HeapPriorityQueue< T, P, D >::empty ( void ) const
nodiscard

whether the queue has no item

◆ end()

template<class T, std::totally_ordered P = int, int D = 2>
const_iterator ADS::HeapPriorityQueue< T, P, D >::end ( void ) const
inlinenodiscard

the end of the view

◆ insert()

template<class T, std::totally_ordered P = int, int D = 2>
Handle ADS::HeapPriorityQueue< T, P, D >::insert ( const T & x,
const P & p )

inserts \( x \) with the priority \( p \) and gives its handle, a new leaf moved up, in \( O( \log_d n ) \) time

In pseudocode (section 10.1.3), \( \mathrm{parent}( i ) \) the parent of the place \( i \) and \( \mathrm{swap}() \) keeping the place of each item:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insert}( x , p ) \cr 2 & \quad h \gets \text{a new item } ( p , x ) \text{ at the place } n; \ H[ n ] \gets h;\ n \gets n + 1 \cr 3 & \quad \mathrm{up}( n - 1 );\ \textbf{return}\ h \cr 4 & \textbf{procedure}\ \mathrm{up}( i )\quad \triangleright \ \text{the item in } i \text{ climbs while its parent has a larger priority} \cr 5 & \quad \textbf{while}\ i > 0\ \textbf{and}\ H[ i ].\mathit{priority} < H[ \mathrm{parent}( i ) ].\mathit{priority}\ \textbf{do} \cr 6 & \qquad \mathrm{swap}( H , i , \mathrm{parent}( i ) );\ i \gets \mathrm{parent}( i ) \end{array}} \]

Precondition
the queue is not full
Postcondition
one item more, the handle at its place

◆ min()

template<class T, std::totally_ordered P = int, int D = 2>
const T & ADS::HeapPriorityQueue< T, P, D >::min ( void ) const
nodiscard

the item of smallest priority, the root \( H[ 0 ] \)

Precondition
the queue is not empty

◆ minPriority()

template<class T, std::totally_ordered P = int, int D = 2>
const P & ADS::HeapPriorityQueue< T, P, D >::minPriority ( void ) const
nodiscard

the smallest priority

Precondition
the queue is not empty

◆ operator=() [1/2]

template<class T, std::totally_ordered P = int, int D = 2>
HeapPriorityQueue & ADS::HeapPriorityQueue< T, P, D >::operator= ( const HeapPriorityQueue< T, P, D > & )
delete

no copy: the handles point into the queue

◆ operator=() [2/2]

template<class T, std::totally_ordered P = int, int D = 2>
HeapPriorityQueue & ADS::HeapPriorityQueue< T, P, D >::operator= ( HeapPriorityQueue< T, P, D > && other)
noexcept

the items of other, which is left empty, in place of these; its handles stay valid here

◆ parent()

template<class T, std::totally_ordered P = int, int D = 2>
int ADS::HeapPriorityQueue< T, P, D >::parent ( int i)
inlinestatic

the parent \( \lfloor ( i - 1 ) / d \rfloor \) of the place \( i > 0 \)

◆ priorities()

template<class T, std::totally_ordered P = int, int D = 2>
std::vector< P > ADS::HeapPriorityQueue< T, P, D >::priorities ( void ) const
nodiscard

the heap: the priorities, by their place

◆ size()

template<class T, std::totally_ordered P = int, int D = 2>
int ADS::HeapPriorityQueue< T, P, D >::size ( void ) const
nodiscard

the number of items, \( n \)


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