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...
|
| 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} \)
|
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
template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< 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
template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
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
template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< 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
template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
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
template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< 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
template<class K, unsigned D = 4, class Policy = NoObservers, class A = std::allocator< K >>
template<class 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}}
\]