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

a \( D \)-ary heap of the ids \( [ 0 , n ) \) by their keys, the least on top, with decrease-key, its changes told through Policy More...

#include <ADS/priority/IndexedDaryHeap.h>

Classes

struct  Entry
 an item: an id with its key More...

Public types

using Key = K
 the type of the keys
using Item = int
 the items of PRIORITYQUEUE: the ids
using Priority = K
 and their priorities, the keys
using Handle = int
 what insert() gives: the id itself
using modifications
 the events: an id inserted or erased with its key, its key changed, an item moved from a place to another, the heap emptied or rebuilt
using Subject
 the subject
using allocator_type = A
 the type of the allocator

Public member functions

template<class... O>
 IndexedDaryHeap (int n=0, O &... o)
 the empty heap of the ids \( [ 0 , n ) \), the static observers o...
template<class... O>
 IndexedDaryHeap (std::allocator_arg_t, const A &alloc, int n=0, O &... o)
 the empty heap of the ids \( [ 0 , n ) \), the static observers o..., its vectors from alloc
allocator_type get_allocator (void) const
 the allocator of the vectors
void reset (int n)
 the empty heap of the ids \( [ 0 , n ) \), the room kept
void grow (int n)
 the ids raised to \( [ 0 , n ) \), the new ones not in the heap and the items kept, in \( O( 1 ) \) amortized time per new id: the vector of the places grows at least twice as large when it is full, so that a consumer whose ids appear one by one (the nodes of a search numbered as they are generated) grows the heap with them
int ids (void) const
 the number of ids, \( n \)
bool empty (void) const
 whether the heap has no item
std::size_t size (void) const
 the number of items, \( s \)
bool contains (int id) const
 whether the id is in the heap
const K & key (int id) const
 the key of an id in the heap
void push (int id, const K &k)
 inserts the id with the key \( k \), in \( O( \log_D s ) \) time
void decrease (int id, const K &k)
 lowers the key of the id to \( k \), in \( O( \log_D s ) \) time
void increase (int id, const K &k)
 raises the key of the id to \( k \), in \( O( D \log_D s ) \) time
void update (int id, const K &k)
 sets the key of the id to \( k \), by decrease() or by increase()
void erase (int id)
 removes the id from the heap, in \( O( D \log_D s ) \) time
bool pushOrDecrease (int id, const K &k)
 push( id , k ) if the id is not in the heap, decrease( id , k ) if \( k \) is less than its key, nothing otherwise; true if the key changed
const Entry & top (void) const
 the item of least key
Entry pop (void)
 removes the item of least key and returns it, in \( O( D \log_D s ) \) time
void clear (void)
 removes all the items, in \( O( s ) \) time
void extract (const K &theta, std::vector< int > &out)
 every id of key at most \( \theta \) appended to out, by increasing keys, each removed as by pop(), in \( O( ( k + 1 ) D \log_D s ) \) time for \( k \) ids
template<class F>
void rekey (F &&f)
 the key of each id in the heap set to f( id ), and the order of the heap made again from the bottom, in \( O( s ) \) time; Rebuilt to the observers, since any key and any place may change
Handle insert (int id, const K &k)
 push( id , k ), the id its handle, as the insert() of PRIORITYQUEUE (section 10.1) [see PriorityQueue.h]
int min (void) const
 the id of least key, as the min() of PRIORITYQUEUE
int deleteMin (void)
 pop(), the id of least key, as the deleteMin() of PRIORITYQUEUE
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 K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
class ADS::IndexedDaryHeap< K, D, Policy, A >

a \( D \)-ary heap of the ids \( [ 0 , n ) \) by their keys, the least on top, with decrease-key, its changes told through Policy

Member typedef documentation

◆ allocator_type

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
using ADS::IndexedDaryHeap< K, D, Policy, A >::allocator_type = A

the type of the allocator

◆ Handle

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
using ADS::IndexedDaryHeap< K, D, Policy, A >::Handle = int

what insert() gives: the id itself

◆ Item

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
using ADS::IndexedDaryHeap< K, D, Policy, A >::Item = int

the items of PRIORITYQUEUE: the ids

◆ Key

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
using ADS::IndexedDaryHeap< K, D, Policy, A >::Key = K

the type of the keys

◆ modifications

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
using ADS::IndexedDaryHeap< K, D, Policy, A >::modifications
Initial value:
Changed< Key, H, K > KeyChanged
the key of an item changed
Definition Modification.h:627
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 id inserted or erased with its key, its key changed, an item moved from a place to another, the heap emptied or rebuilt

◆ Priority

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
using ADS::IndexedDaryHeap< K, D, Policy, A >::Priority = K

and their priorities, the keys

◆ Subject

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
using ADS::IndexedDaryHeap< K, D, Policy, A >::Subject
Initial value:
typename Policy::template bind< modifications , IndexedDaryHeap >

the subject

Constructor and destructor documentation

◆ IndexedDaryHeap() [1/2]

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
template<class... O>
ADS::IndexedDaryHeap< K, D, Policy, A >::IndexedDaryHeap ( int n = 0,
O &... o )
explicit

the empty heap of the ids \( [ 0 , n ) \), the static observers o...

Precondition
\( n \ge 0 \)

◆ IndexedDaryHeap() [2/2]

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
template<class... O>
ADS::IndexedDaryHeap< K, D, Policy, A >::IndexedDaryHeap ( std::allocator_arg_t ,
const A & alloc,
int n = 0,
O &... o )

the empty heap of the ids \( [ 0 , n ) \), the static observers o..., its vectors from alloc

Precondition
\( n \ge 0 \)

Member function documentation

◆ attach()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
template<class O>
requires ( Subject::dynamic )
int ADS::IndexedDaryHeap< K, D, Policy, A >::attach ( O & o)
inline

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

Precondition
no batch is open

◆ clear()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
void ADS::IndexedDaryHeap< K, D, Policy, A >::clear ( void )

removes all the items, in \( O( s ) \) time

◆ contains()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
bool ADS::IndexedDaryHeap< K, D, Policy, A >::contains ( int id) const
nodiscard

whether the id is in the heap

Precondition
\( \mathit{id} \in [ 0 , n ) \)

◆ decrease()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
void ADS::IndexedDaryHeap< K, D, Policy, A >::decrease ( int id,
const K & k )

lowers the key of the id to \( k \), in \( O( \log_D s ) \) time

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

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{decrease}( \mathit{id} , k ) \cr 2 & \quad \mathrm{up}( \mathit{pos}[ \mathit{id} ] , ( k , \mathit{id} ) ) \end{array}} \]

Precondition
the id is in the heap, \( k \) not greater than its key

◆ deleteMin()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
int ADS::IndexedDaryHeap< K, D, Policy, A >::deleteMin ( void )
inline

pop(), the id of least key, as the deleteMin() of PRIORITYQUEUE

Precondition
the heap is not empty

◆ detach()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
void ADS::IndexedDaryHeap< K, D, Policy, A >::detach ( int id)
inline

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

Precondition
it is attached, no batch is open

◆ empty()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
bool ADS::IndexedDaryHeap< K, D, Policy, A >::empty ( void ) const
inlinenodiscard

whether the heap has no item

◆ erase()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
void ADS::IndexedDaryHeap< K, D, Policy, A >::erase ( int id)

removes the id from the heap, in \( O( D \log_D s ) \) time

In pseudocode (beyond the book): the last item takes the place of the id, and moves up or down from there, since it may be less than the parent of the place, if it came from another subtree, or greater than a child:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{erase}( \mathit{id} ) \cr 2 & \quad i \gets \mathit{pos}[ \mathit{id} ];\ \mathit{pos}[ \mathit{id} ] \gets -1;\ s \gets s - 1;\ x \gets A[ s ] \cr 3 & \quad \textbf{if}\ i = s\ \textbf{then return} \cr 4 & \quad \textbf{if}\ i > 0\ \textbf{and}\ x.\mathit{key} < A[ \lfloor ( i - 1 ) / D \rfloor ].\mathit{key}\ \textbf{then} \ \mathrm{up}( i , x ) \cr 5 & \quad \textbf{else}\ \mathrm{down}( i , x ) \end{array}} \]

Precondition
the id is in the heap

◆ extract()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
void ADS::IndexedDaryHeap< K, D, Policy, A >::extract ( const K & theta,
std::vector< int > & out )

every id of key at most \( \theta \) appended to out, by increasing keys, each removed as by pop(), in \( O( ( k + 1 ) D \log_D s ) \) time for \( k \) ids

◆ get_allocator()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
allocator_type ADS::IndexedDaryHeap< K, D, Policy, A >::get_allocator ( void ) const
inlinenodiscard

the allocator of the vectors

◆ grow()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
void ADS::IndexedDaryHeap< K, D, Policy, A >::grow ( int n)

the ids raised to \( [ 0 , n ) \), the new ones not in the heap and the items kept, in \( O( 1 ) \) amortized time per new id: the vector of the places grows at least twice as large when it is full, so that a consumer whose ids appear one by one (the nodes of a search numbered as they are generated) grows the heap with them

Precondition
\( n \ge \) ids()

◆ ids()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
int ADS::IndexedDaryHeap< K, D, Policy, A >::ids ( void ) const
inlinenodiscard

the number of ids, \( n \)

◆ increase()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
void ADS::IndexedDaryHeap< K, D, Policy, A >::increase ( int id,
const K & k )

raises the key of the id to \( k \), in \( O( D \log_D s ) \) time

In pseudocode (beyond the book), with \( \mathrm{down}() \) of pop():

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{increase}( \mathit{id} , k ) \cr 2 & \quad \mathrm{down}( \mathit{pos}[ \mathit{id} ] , ( k , \mathit{id} ) ) \end{array}} \]

Precondition
the id is in the heap, \( k \) not less than its key

◆ insert()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
Handle ADS::IndexedDaryHeap< K, D, Policy, A >::insert ( int id,
const K & k )
inline

push( id , k ), the id its handle, as the insert() of PRIORITYQUEUE (section 10.1) [see PriorityQueue.h]

Precondition
as push()

◆ key()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
const K & ADS::IndexedDaryHeap< K, D, Policy, A >::key ( int id) const
nodiscard

the key of an id in the heap

Precondition
the id is in the heap

◆ min()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
int ADS::IndexedDaryHeap< K, D, Policy, A >::min ( void ) const
inlinenodiscard

the id of least key, as the min() of PRIORITYQUEUE

Precondition
the heap is not empty

◆ pop()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
Entry ADS::IndexedDaryHeap< K, D, Policy, A >::pop ( void )

removes the item of least key and returns it, in \( O( D \log_D s ) \) time

In pseudocode (beyond the book), the last item moved down from the root:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pop}() \cr 2 & \quad r \gets A[ 0 ];\ \mathit{pos}[ r.\mathit{id} ] \gets -1; \ s \gets s - 1;\ x \gets A[ s ];\ i \gets 0 \cr 3 & \quad \textbf{if}\ s = 0\ \textbf{then return}\ r \cr 4 & \quad \textbf{while}\ D i + 1 < s\ \textbf{do} \cr 5 & \qquad b \gets \text{the child of } i \text{ of least key} \cr 6 & \qquad \textbf{if not}\ A[ b ].\mathit{key} < x.\mathit{key} \ \textbf{then}\ \textbf{break} \cr 7 & \qquad A[ i ] \gets A[ b ];\ \mathit{pos}[ A[ i ].\mathit{id} ] \gets i;\ i \gets b \cr 8 & \quad A[ i ] \gets x;\ \mathit{pos}[ x.\mathit{id} ] \gets i \cr 9 & \quad \textbf{return}\ r \end{array}} \]

Precondition
the heap is not empty

◆ push()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
void ADS::IndexedDaryHeap< K, D, Policy, A >::push ( int id,
const K & k )

inserts the id with the key \( k \), in \( O( \log_D s ) \) time

In pseudocode (the heap of section 10.1.3 with \( D \) children and the places of the ids, beyond the book), \( \mathit{pos} \) the place of each id:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{push}( \mathit{id} , k ) \cr 2 & \quad s \gets s + 1;\ \mathrm{up}( s - 1 , ( k , \mathit{id} ) ) \cr 3 & \textbf{procedure}\ \mathrm{up}( i , x ) \cr 4 & \quad \textbf{while}\ i > 0\ \textbf{and}\ x.\mathit{key} < A[ \lfloor ( i - 1 ) / D \rfloor ].\mathit{key}\ \textbf{do} \cr 5 & \qquad A[ i ] \gets A[ \lfloor ( i - 1 ) / D \rfloor ]; \ \mathit{pos}[ A[ i ].\mathit{id} ] \gets i;\ i \gets \lfloor ( i - 1 ) / D \rfloor \cr 6 & \quad A[ i ] \gets x;\ \mathit{pos}[ x.\mathit{id} ] \gets i \end{array}} \]

Precondition
\( \mathit{id} \in [ 0 , n ) \), not in the heap

◆ pushOrDecrease()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
bool ADS::IndexedDaryHeap< K, D, Policy, A >::pushOrDecrease ( int id,
const K & k )

push( id , k ) if the id is not in the heap, decrease( id , k ) if \( k \) is less than its key, nothing otherwise; true if the key changed

Precondition
\( \mathit{id} \in [ 0 , n ) \)

◆ rekey()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
template<class F>
void ADS::IndexedDaryHeap< K, D, Policy, A >::rekey ( F && f)

the key of each id in the heap set to f( id ), and the order of the heap made again from the bottom, in \( O( s ) \) time; Rebuilt to the observers, since any key and any place may change

In pseudocode (the construction of Floyd on \( D \) children, beyond the book), with \( \mathrm{down}() \) of pop():

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{rekey}( f ) \cr 2 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ s - 1\ \textbf{do} \ A[ i ].\mathit{key} \gets f( A[ i ].\mathit{id} ) \cr 3 & \quad \textbf{for}\ i \gets \lfloor ( s - 2 ) / D \rfloor \ \textbf{downto}\ 0\ \textbf{do}\ \mathrm{down}( i , A[ i ] ) \end{array}} \]

◆ reset()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
void ADS::IndexedDaryHeap< K, D, Policy, A >::reset ( int n)

the empty heap of the ids \( [ 0 , n ) \), the room kept

Precondition
\( n \ge 0 \)

◆ size()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
std::size_t ADS::IndexedDaryHeap< K, D, Policy, A >::size ( void ) const
inlinenodiscard

the number of items, \( s \)

◆ subject() [1/2]

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
Subject & ADS::IndexedDaryHeap< K, D, Policy, A >::subject ( void )
inlinenodiscard

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

◆ subject() [2/2]

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
const Subject & ADS::IndexedDaryHeap< K, D, Policy, A >::subject ( void ) const
inlinenodiscard

the subject

◆ top()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
const Entry & ADS::IndexedDaryHeap< K, D, Policy, A >::top ( void ) const
nodiscard

the item of least key

Precondition
the heap is not empty

◆ update()

template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
void ADS::IndexedDaryHeap< K, D, Policy, A >::update ( int id,
const K & k )

sets the key of the id to \( k \), by decrease() or by increase()

Precondition
the id is in the heap

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