ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::DaryHeap< T, Better, D, BottomUp, A > Class Template Reference

a \( D \)-ary heap of plain items, the best one by better() on top; pop() by the walk down from the top, or from the bottom up if BottomUp More...

#include <ADS/priority/DaryHeap.h>

Public types

using Item = T
 the type of the items
using value_type = 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 = const T &
 the type of a reference to an item of the view
using const_reference = const T &
 the type of a constant reference to an item
using iterator = const T *
 the same as const_iterator
using const_iterator = const T *
 the iterator of the view, constant
using allocator_type = A
 the type of the allocator

Public member functions

 DaryHeap (Better better=Better(), const A &alloc=A())
 the empty heap of the order better, its vector from alloc
void push (const T &x)
 pushes \( x \), moving it up while better than its parent, in \( O( \log_D n ) \) time
const T & top (void) const
 the best item
T pop (void)
 removes the best item and returns it, the last item moved down from the root, in \( O( D \log_D n ) \) time
T erase (std::size_t i)
 removes the item in the place \( i \) of the view and returns it, in \( O( D \log_D n ) \) time: the last item takes its place and moves up or down (exercise 10.3 of Morin's book)
const T * secondTop (void) const
 the second best item, the best of the children of the root, or nil if there are fewer than two items, in \( O( D ) \) time (exercise 10.9 of Morin's book)
std::vector< T > smallest (std::size_t k) const
 the \( k \) best items in order, or all of them if fewer, in \( O( k D \log k ) \) time (exercise 10.10 of Morin's book)
bool empty (void) const
 whether the heap has no item
std::size_t size (void) const
 the number of items, \( n \)
void reserve (std::size_t n)
 room for \( n \) items, so that the pushes up to \( n \) do not reallocate
void clear (void)
 removes all the items
template<std::input_iterator I, std::sentinel_for< I > S>
void assign (I first, S last)
 the items of \( [ \mathit{first} , \mathit{last} ) \) in place of those of the heap, arranged by the construction of Floyd in \( O( n ) \) time for \( n \) items; the heap is unchanged if a copy throws
allocator_type get_allocator (void) const
 the allocator of the vector
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 constexpr std::size_t parent (std::size_t i)
 the parent of the place \( i > 0 \), \( \lfloor ( i - 1 ) / D \rfloor \) (the layout of Eytzinger with \( D \) children, exercise 10.4 of Morin's book)
static constexpr std::size_t child (std::size_t i, std::size_t j)
 the child \( j \in \{ 0 , \ldots , D - 1 \} \) of the place \( i \), \( D i + j + 1 \) (exercise 10.4 of Morin's book)

Detailed description

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
class ADS::DaryHeap< T, Better, D, BottomUp, A >

a \( D \)-ary heap of plain items, the best one by better() on top; pop() by the walk down from the top, or from the bottom up if BottomUp

Member typedef documentation

◆ allocator_type

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
using ADS::DaryHeap< T, Better, D, BottomUp, A >::allocator_type = A

the type of the allocator

◆ const_iterator

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
using ADS::DaryHeap< T, Better, D, BottomUp, A >::const_iterator = const T *

the iterator of the view, constant

◆ const_reference

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
using ADS::DaryHeap< T, Better, D, BottomUp, A >::const_reference = const T &

the type of a constant reference to an item

◆ difference_type

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
using ADS::DaryHeap< T, Better, D, BottomUp, A >::difference_type = std::ptrdiff_t

the type of the distances between iterators

◆ Item

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
using ADS::DaryHeap< T, Better, D, BottomUp, A >::Item = T

the type of the items

◆ iterator

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
using ADS::DaryHeap< T, Better, D, BottomUp, A >::iterator = const T *

the same as const_iterator

◆ reference

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
using ADS::DaryHeap< T, Better, D, BottomUp, A >::reference = const T &

the type of a reference to an item of the view

◆ size_type

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
using ADS::DaryHeap< T, Better, D, BottomUp, A >::size_type = std::size_t

the type of the sizes

◆ value_type

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
using ADS::DaryHeap< T, Better, D, BottomUp, A >::value_type = T

the type of the items of the view

Constructor and destructor documentation

◆ DaryHeap()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
ADS::DaryHeap< T, Better, D, BottomUp, A >::DaryHeap ( Better better = Better(),
const A & alloc = A() )
explicit

the empty heap of the order better, its vector from alloc

Member function documentation

◆ assign()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
template<std::input_iterator I, std::sentinel_for< I > S>
void ADS::DaryHeap< T, Better, D, BottomUp, A >::assign ( I first,
S last )

the items of \( [ \mathit{first} , \mathit{last} ) \) in place of those of the heap, arranged by the construction of Floyd in \( O( n ) \) time for \( n \) items; the heap is unchanged if a copy throws

In pseudocode (the heapBuild() of section 10.1.4 with \( D \) children, beyond the book), the leaves being heaps already:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{assign}( \mathit{first} , \mathit{last} ) \cr 2 & \quad A \gets \text{a copy of the items};\ n \gets |A| \cr 3 & \quad \textbf{for}\ i \gets \lfloor ( n - 2 ) / D \rfloor \ \textbf{downto}\ 0\ \textbf{do}\ \text{move } A[ i ] \text{ down as in } \mathrm{pop} \end{array}} \]

◆ begin()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
const_iterator ADS::DaryHeap< T, Better, D, BottomUp, A >::begin ( void ) const
inlinenodiscard

the first item of the view

◆ cbegin()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
const_iterator ADS::DaryHeap< T, Better, D, BottomUp, A >::cbegin ( void ) const
inlinenodiscard

the same as begin()

◆ cend()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
const_iterator ADS::DaryHeap< T, Better, D, BottomUp, A >::cend ( void ) const
inlinenodiscard

the same as end()

◆ child()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
constexpr std::size_t ADS::DaryHeap< T, Better, D, BottomUp, A >::child ( std::size_t i,
std::size_t j )
inlinestaticnodiscardconstexpr

the child \( j \in \{ 0 , \ldots , D - 1 \} \) of the place \( i \), \( D i + j + 1 \) (exercise 10.4 of Morin's book)

◆ clear()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
void ADS::DaryHeap< T, Better, D, BottomUp, A >::clear ( void )

removes all the items

◆ empty()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
bool ADS::DaryHeap< T, Better, D, BottomUp, A >::empty ( void ) const
nodiscard

whether the heap has no item

◆ end()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
const_iterator ADS::DaryHeap< T, Better, D, BottomUp, A >::end ( void ) const
inlinenodiscard

the end of the view

◆ erase()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
T ADS::DaryHeap< T, Better, D, BottomUp, A >::erase ( std::size_t i)

removes the item in the place \( i \) of the view and returns it, in \( O( D \log_D n ) \) time: the last item takes its place and moves up or down (exercise 10.3 of Morin's book)

In pseudocode (exercise 10.3 of Morin's book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{erase}( i ) \cr 2 & \quad r \gets A[ i ];\ n \gets n - 1;\ x \gets A[ n ] \cr 3 & \quad \textbf{if}\ i = n\ \textbf{then return}\ r \cr 4 & \quad \textbf{if}\ i > 0\ \textbf{and}\ \mathrm{better}( x , A[ \mathrm{parent}( i ) ] )\ \textbf{then}\ \text{move } x \text{ up from } i \cr 5 & \quad \textbf{else}\ \text{move } x \text{ down from } i \cr 6 & \quad \textbf{return}\ r \end{array}} \]

The places of the items change at each operation and the heap gives no handle to follow them, so that a caller must find \( i \) first, in \( O( n ) \) time: this is why the exercise calls it of little use [see IndexedDaryHeap.h for the heap whose items know their place].

Precondition
\( i < n \)

◆ get_allocator()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
allocator_type ADS::DaryHeap< T, Better, D, BottomUp, A >::get_allocator ( void ) const
inlinenodiscard

the allocator of the vector

◆ parent()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
constexpr std::size_t ADS::DaryHeap< T, Better, D, BottomUp, A >::parent ( std::size_t i)
inlinestaticnodiscardconstexpr

the parent of the place \( i > 0 \), \( \lfloor ( i - 1 ) / D \rfloor \) (the layout of Eytzinger with \( D \) children, exercise 10.4 of Morin's book)

◆ pop()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
T ADS::DaryHeap< T, Better, D, BottomUp, A >::pop ( void )

removes the best item and returns it, the last item moved down from the root, in \( O( D \log_D n ) \) time

In pseudocode (the heap of section 10.1.3 with \( D \) children; the walk from the bottom up of Floyd and Wegener if BottomUp, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pop}() \cr 2 & \quad r \gets A[ 0 ];\ n \gets n - 1;\ x \gets A[ n ];\ i \gets 0 \cr 3 & \quad \textbf{if}\ n = 0\ \textbf{then return}\ r \cr 4 & \quad \textbf{while}\ D i + 1 < n\ \textbf{do} \cr 5 & \qquad b \gets \text{the best of the children } A[ D i + 1 ] , \ldots , A[ \min\{ D i + D , n - 1 \} ] \cr 6 & \qquad \textbf{if not}\ \mathit{BottomUp}\ \textbf{and not} \ \mathrm{better}( A[ b ] , x )\ \textbf{then}\ \textbf{break} \cr 7 & \qquad A[ i ] \gets A[ b ];\ i \gets b \cr 8 & \quad \textbf{if}\ \mathit{BottomUp}\ \textbf{then} \ \text{move } x \text{ up from } i \text{ as by } \mathrm{push} \cr 9 & \quad \textbf{else}\ A[ i ] \gets x \cr 10 & \quad \textbf{return}\ r \end{array}} \]

Precondition
the heap is not empty

◆ push()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
void ADS::DaryHeap< T, Better, D, BottomUp, A >::push ( const T & x)

pushes \( x \), moving it up while better than its parent, in \( O( \log_D n ) \) time

In pseudocode (the heap of section 10.1.3 with \( D \) children, beyond the book), the hole moved up instead of the item swapped:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{push}( x ) \cr 2 & \quad i \gets n;\ n \gets n + 1 \cr 3 & \quad \textbf{while}\ i > 0\ \textbf{and}\ \mathrm{better}( x , A[ \lfloor ( i - 1 ) / D \rfloor ] )\ \textbf{do} \cr 4 & \qquad A[ i ] \gets A[ \lfloor ( i - 1 ) / D \rfloor ];\ i \gets \lfloor ( i - 1 ) / D \rfloor \cr 5 & \quad A[ i ] \gets x \end{array}} \]

◆ reserve()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
void ADS::DaryHeap< T, Better, D, BottomUp, A >::reserve ( std::size_t n)

room for \( n \) items, so that the pushes up to \( n \) do not reallocate

◆ secondTop()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
const T * ADS::DaryHeap< T, Better, D, BottomUp, A >::secondTop ( void ) const
nodiscard

the second best item, the best of the children of the root, or nil if there are fewer than two items, in \( O( D ) \) time (exercise 10.9 of Morin's book)

◆ size()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
std::size_t ADS::DaryHeap< T, Better, D, BottomUp, A >::size ( void ) const
nodiscard

the number of items, \( n \)

◆ smallest()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
std::vector< T > ADS::DaryHeap< T, Better, D, BottomUp, A >::smallest ( std::size_t k) const
nodiscard

the \( k \) best items in order, or all of them if fewer, in \( O( k D \log k ) \) time (exercise 10.10 of Morin's book)

In pseudocode (exercise 10.10 of Morin's book), \( Q \) a heap of places by their items, each place entering it after its parent:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{smallest}( k ) \cr 2 & \quad Q \gets \{ 0 \};\ S \gets () \cr 3 & \quad \textbf{while}\ |S| < k\ \textbf{and}\ Q \ne \emptyset \ \textbf{do} \cr 4 & \qquad i \gets \mathrm{deleteMin}( Q );\ S \gets S \cdot ( A[ i ] ) \cr 5 & \qquad \text{put the children of } i \text{ in } Q \cr 6 & \quad \textbf{return}\ S \end{array}} \]

where \( Q \) holds at most \( k ( D - 1 ) + 1 \) places.

◆ top()

template<class T, class Better = std::less< T >, unsigned D = 4, bool BottomUp = false, class A = HugePageAllocator< T >>
const T & ADS::DaryHeap< T, Better, D, BottomUp, A >::top ( void ) const
nodiscard

the best item

Precondition
the heap is not empty

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