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

a priority queue on a heap with pointers, which keeps a pointer to the last leaf if L More...

#include <ADS/priority/LinkedHeapPriorityQueue.h>

Classes

struct  Shape
 the shape of the tree, for PathIterator More...

Public types

using Item = T
 the type of the items
using Priority = P
 the type of the priorities
using value_type = std::pair< P , T >
 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 = PairRef< const P & , const T & >
 the type of a reference to an item of the view
using const_reference = reference
 the type of a constant reference to an item
using const_iterator = PathIterator< LinkedHeapPriorityQueue , Shape >
 the iterator of the view, constant
using iterator = const_iterator
 the same as const_iterator

Public member functions

 LinkedHeapPriorityQueue (void)=default
 the empty queue
 LinkedHeapPriorityQueue (const LinkedHeapPriorityQueue &)=delete
 no copy: the nodes belong to the queue
LinkedHeapPriorityQueue & operator= (const LinkedHeapPriorityQueue &)=delete
 no copy: the nodes belong to the queue
 LinkedHeapPriorityQueue (LinkedHeapPriorityQueue &&other) noexcept
 the items of other, which is left empty
LinkedHeapPriorityQueue & operator= (LinkedHeapPriorityQueue &&other) noexcept
 the items of other, which is left empty, in place of these
 ~LinkedHeapPriorityQueue ()
 the queue and its nodes destroyed
bool empty (void) const
 whether the queue has no item
int size (void) const
 the number of items, \( n \)
const T & min (void) const
 the item of smallest priority, at the root
const P & minPriority (void) const
 the smallest priority
void insert (const T &x, const P &p)
 inserts \( x \) with the priority \( p \), a new last leaf moved up while smaller than its parent, in \( O( \log n ) \) time
T deleteMin (void)
 removes the item of smallest priority and returns it, the last leaf moved into the root and down while larger than a child, in \( O( \log n ) \) time
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()

Detailed description

template<class T, std::totally_ordered P = int, bool L = false>
class ADS::LinkedHeapPriorityQueue< T, P, L >

a priority queue on a heap with pointers, which keeps a pointer to the last leaf if L

Member typedef documentation

◆ const_iterator

template<class T, std::totally_ordered P = int, bool L = false>
using ADS::LinkedHeapPriorityQueue< T, P, L >::const_iterator = PathIterator< LinkedHeapPriorityQueue , Shape >

the iterator of the view, constant

◆ const_reference

template<class T, std::totally_ordered P = int, bool L = false>
using ADS::LinkedHeapPriorityQueue< T, P, L >::const_reference = reference

the type of a constant reference to an item

◆ difference_type

template<class T, std::totally_ordered P = int, bool L = false>
using ADS::LinkedHeapPriorityQueue< T, P, L >::difference_type = std::ptrdiff_t

the type of the distances between iterators

◆ Item

template<class T, std::totally_ordered P = int, bool L = false>
using ADS::LinkedHeapPriorityQueue< T, P, L >::Item = T

the type of the items

◆ iterator

template<class T, std::totally_ordered P = int, bool L = false>
using ADS::LinkedHeapPriorityQueue< T, P, L >::iterator = const_iterator

the same as const_iterator

◆ Priority

template<class T, std::totally_ordered P = int, bool L = false>
using ADS::LinkedHeapPriorityQueue< T, P, L >::Priority = P

the type of the priorities

◆ reference

template<class T, std::totally_ordered P = int, bool L = false>
using ADS::LinkedHeapPriorityQueue< T, P, L >::reference = PairRef< const P & , const T & >

the type of a reference to an item of the view

◆ size_type

template<class T, std::totally_ordered P = int, bool L = false>
using ADS::LinkedHeapPriorityQueue< T, P, L >::size_type = std::size_t

the type of the sizes

◆ value_type

template<class T, std::totally_ordered P = int, bool L = false>
using ADS::LinkedHeapPriorityQueue< T, P, L >::value_type = std::pair< P , T >

the type of the items of the view

Constructor and destructor documentation

◆ LinkedHeapPriorityQueue() [1/3]

template<class T, std::totally_ordered P = int, bool L = false>
ADS::LinkedHeapPriorityQueue< T, P, L >::LinkedHeapPriorityQueue ( void )
default

the empty queue

◆ LinkedHeapPriorityQueue() [2/3]

template<class T, std::totally_ordered P = int, bool L = false>
ADS::LinkedHeapPriorityQueue< T, P, L >::LinkedHeapPriorityQueue ( const LinkedHeapPriorityQueue< T, P, L > & )
delete

no copy: the nodes belong to the queue

◆ LinkedHeapPriorityQueue() [3/3]

template<class T, std::totally_ordered P = int, bool L = false>
ADS::LinkedHeapPriorityQueue< T, P, L >::LinkedHeapPriorityQueue ( LinkedHeapPriorityQueue< T, P, L > && other)
noexcept

the items of other, which is left empty

◆ ~LinkedHeapPriorityQueue()

template<class T, std::totally_ordered P = int, bool L = false>
ADS::LinkedHeapPriorityQueue< T, P, L >::~LinkedHeapPriorityQueue ( )

the queue and its nodes destroyed

Member function documentation

◆ begin()

template<class T, std::totally_ordered P = int, bool L = false>
const_iterator ADS::LinkedHeapPriorityQueue< T, P, L >::begin ( void ) const
inlinenodiscard

the first item of the view

◆ cbegin()

template<class T, std::totally_ordered P = int, bool L = false>
const_iterator ADS::LinkedHeapPriorityQueue< T, P, L >::cbegin ( void ) const
inlinenodiscard

the same as begin()

◆ cend()

template<class T, std::totally_ordered P = int, bool L = false>
const_iterator ADS::LinkedHeapPriorityQueue< T, P, L >::cend ( void ) const
inlinenodiscard

the same as end()

◆ deleteMin()

template<class T, std::totally_ordered P = int, bool L = false>
T ADS::LinkedHeapPriorityQueue< T, P, L >::deleteMin ( void )

removes the item of smallest priority and returns it, the last leaf moved into the root and down while larger than a child, in \( O( \log n ) \) time

In pseudocode (the heap of section 10.1.3 on pointers, beyond the book), \( \mathit{last} \) found as by insert(), or by the walk symmetric to \( \mathrm{nextParent}() \) if L:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{deleteMin}() \cr 2 & \quad x \gets \mathit{root}.\mathit{value} \cr 3 & \quad \textbf{if}\ n = 1\ \textbf{then}\ \mathit{root} \gets \textbf{nil};\ n \gets 0;\ \textbf{return}\ x \cr 4 & \quad \ell \gets \mathit{last}\ \text{if } L \text{, else } \mathrm{at}( n ) \cr 5 & \quad \text{move the contents of } \ell \text{ to } \mathit{root};\ \text{detach } \ell \text{ from its parent}; \ n \gets n - 1 \cr 6 & \quad v \gets \mathit{root} \cr 7 & \quad \textbf{loop} \cr 8 & \qquad m \gets \text{the child of } v \text{ of least priority,} \text{ if smaller than } v \text{, else } v \cr 9 & \qquad \textbf{if}\ m = v\ \textbf{then return}\ x \cr 10 & \qquad \text{swap the contents of } v \text{ and } m;\ v \gets m \end{array}} \]

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

◆ empty()

template<class T, std::totally_ordered P = int, bool L = false>
bool ADS::LinkedHeapPriorityQueue< T, P, L >::empty ( void ) const
nodiscard

whether the queue has no item

◆ end()

template<class T, std::totally_ordered P = int, bool L = false>
const_iterator ADS::LinkedHeapPriorityQueue< T, P, L >::end ( void ) const
inlinenodiscard

the end of the view

◆ insert()

template<class T, std::totally_ordered P = int, bool L = false>
void ADS::LinkedHeapPriorityQueue< T, P, L >::insert ( const T & x,
const P & p )

inserts \( x \) with the priority \( p \), a new last leaf moved up while smaller than its parent, in \( O( \log n ) \) time

In pseudocode (the heap of section 10.1.3 on pointers, beyond the book), \( \mathrm{at}( k ) \) the node \( k \) of the levels, by the bits of \( k \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( x , p ) \cr 2 & \quad n \gets n + 1 \cr 3 & \quad \textbf{if}\ n = 1\ \textbf{then}\ \mathit{root} \gets \text{a node } ( p , x );\ \textbf{return} \cr 4 & \quad u \gets \mathrm{nextParent}()\ \text{if } L \text{, else } \mathrm{at}( \lfloor n / 2 \rfloor ) \cr 5 & \quad v \gets \text{a node } ( p , x ) \text{ as the left child of } u \text{, or the right one if the left is taken} \cr 6 & \quad \textbf{while}\ v \ne \mathit{root}\ \textbf{and} \ v.\mathit{priority} < v.\mathit{parent}.\mathit{priority} \ \textbf{do} \cr 7 & \qquad \text{swap the contents of } v \text{ and } v.\mathit{parent};\ v \gets v.\mathit{parent} \cr 8 & \textbf{function}\ \mathrm{at}( k ) \cr 9 & \quad u \gets \mathit{root} \cr 10 & \quad \textbf{foreach}\ \text{bit } b \text{ of } k \text{ after the leading one, from the highest}\ \textbf{do} \cr 11 & \qquad u \gets u.\mathit{left}\ \text{if } b = 0 \text{, else } u.\mathit{right} \cr 12 & \quad \textbf{return}\ u \cr 13 & \textbf{function}\ \mathrm{nextParent}()\quad \triangleright \ \text{the node whose child the next leaf is} \cr 14 & \quad u \gets \mathit{last} \cr 15 & \quad \textbf{if}\ u = \mathit{root}\ \textbf{then return}\ u \cr 16 & \quad \textbf{if}\ u \text{ is a left child}\ \textbf{then} \ \textbf{return}\ u.\mathit{parent} \cr 17 & \quad \textbf{while}\ u \ne \mathit{root}\ \textbf{and}\ u \text{ is a right child}\ \textbf{do}\ u \gets u.\mathit{parent} \cr 18 & \quad \textbf{if}\ u \ne \mathit{root}\ \textbf{then}\ u \gets u.\mathit{parent}.\mathit{right} \cr 19 & \quad \textbf{while}\ u.\mathit{left} \ne \textbf{nil} \ \textbf{do}\ u \gets u.\mathit{left} \cr 20 & \quad \textbf{return}\ u \end{array}} \]

Postcondition
one item more

◆ min()

template<class T, std::totally_ordered P = int, bool L = false>
const T & ADS::LinkedHeapPriorityQueue< T, P, L >::min ( void ) const
nodiscard

the item of smallest priority, at the root

Precondition
the queue is not empty

◆ minPriority()

template<class T, std::totally_ordered P = int, bool L = false>
const P & ADS::LinkedHeapPriorityQueue< T, P, L >::minPriority ( void ) const
nodiscard

the smallest priority

Precondition
the queue is not empty

◆ operator=() [1/2]

template<class T, std::totally_ordered P = int, bool L = false>
LinkedHeapPriorityQueue & ADS::LinkedHeapPriorityQueue< T, P, L >::operator= ( const LinkedHeapPriorityQueue< T, P, L > & )
delete

no copy: the nodes belong to the queue

◆ operator=() [2/2]

template<class T, std::totally_ordered P = int, bool L = false>
LinkedHeapPriorityQueue & ADS::LinkedHeapPriorityQueue< T, P, L >::operator= ( LinkedHeapPriorityQueue< T, P, L > && other)
noexcept

the items of other, which is left empty, in place of these

◆ size()

template<class T, std::totally_ordered P = int, bool L = false>
int ADS::LinkedHeapPriorityQueue< T, P, L >::size ( void ) const
nodiscard

the number of items, \( n \)


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