the sequence heap of plain items, the best one by better() on top
More...
#include <ADS/priority/SequenceHeap.h>
|
| using | Item = T |
| | the type of the items
|
|
| | SequenceHeap (Better better=Better()) |
| | the empty heap of the order better
|
| void | push (const T &x) |
| | pushes \( x \), in \( O( \log n ) \) amortized time
|
| const T & | top (void) |
| | the best item
|
| T | pop (void) |
| | removes the best item and returns it, in \( O( \log n ) \) amortized time
|
| bool | empty (void) const |
| | whether the heap has no item
|
| std::size_t | size (void) const |
| | the number of items, \( n \)
|
| void | clear (void) |
| | removes all the items
|
|
| static constexpr std::size_t | M = 4096 |
| | the items of the insertion heap and of a buffer, \( m \)
|
| static constexpr std::size_t | K = 16 |
| | the sequences of a group, \( k \)
|
template<class T, class Better = std::less< T >>
class ADS::SequenceHeap< T, Better >
the sequence heap of plain items, the best one by better() on top
◆ Item
template<class T, class Better = std::less< T >>
◆ SequenceHeap()
template<class T, class Better = std::less< T >>
the empty heap of the order better
◆ clear()
template<class T, class Better = std::less< T >>
◆ empty()
template<class T, class Better = std::less< T >>
whether the heap has no item
◆ pop()
template<class T, class Better = std::less< T >>
removes the best item and returns it, in \( O( \log n ) \) amortized time
In pseudocode (Sanders, beyond the book; the incremental quicksort of Paredes and Navarro for the bulk), with the names of push():
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{pop}() \cr
2 & \quad \textbf{if}\ \text{the bulk is open }\ \textbf{then} \cr
3 & \qquad \textbf{if}\ \text{it is the first look at more than } m
\text{ items }\ \textbf{then}\ \text{put its best item last by a}
\text{ scan} \cr
4 & \qquad \textbf{else if}\ \text{its best item is not last }
\ \textbf{then}\ \text{close it: to } I \text{ if at most } m
\text{, else to the quicksort} \cr
5 & \qquad \textbf{if}\ \text{the bulk is still open }
\ \textbf{then}\ n \gets n - 1;\ \textbf{return}\ \text{its last}
\text{ item, removed} \cr
6 & \quad \textbf{if}\ I \text{ is not a heap}\ \textbf{then}
\ \text{make it one from the bottom up} \cr
7 & \quad \textbf{if}\ D = \emptyset\ \textbf{then}
\ \mathrm{refillDeletion}() \cr
8 & \quad n \gets n - 1 \cr
9 & \quad \textbf{if}\ D = \emptyset\ \textbf{or}\ \mathrm{better}(
\mathrm{top}( I ) , \text{the first of } D )\ \textbf{then}
\ \textbf{return}\ \mathrm{deleteMin}( I ) \cr
10 & \quad \textbf{return}\ \text{the first of } D \text{, removed} \cr
11 & \textbf{procedure}\ \mathrm{refillDeletion}() \cr
12 & \quad \textbf{foreach}\ i\ \textbf{do}\ \textbf{if}
\ G_i.\mathit{buf} = \emptyset\ \textbf{then}\ \text{refill it}
\text{ with the first } m \text{ items of the merge of } G_i \cr
13 & \quad \text{refill the buffer of the bulk, if empty, with its next}
\text{ best items by the quicksort} \cr
14 & \quad \textbf{while}\ |D| < m\ \textbf{and}\ \text{some buffer}
\text{ has items }\ \textbf{do} \cr
15 & \qquad \text{move to } D \text{ the best first item of the}
\text{ buffers, by a heap of them} \cr
16 & \qquad \text{refill that buffer from its source if it ran out}
\end{array}}
\]
- Precondition
- the heap is not empty
◆ push()
template<class T, class Better = std::less< T >>
pushes \( x \), in \( O( \log n ) \) amortized time
In pseudocode (Sanders, beyond the book), \( I \) the insertion heap, \( D \) the deletion buffer, \( G_i \) the group \( i \) with its buffer \( G_i.\mathit{buf} \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{push}( x ) \cr
2 & \quad \textbf{if}\ n = 0\ \textbf{then}\ \text{open a bulk} \cr
3 & \quad n \gets n + 1 \cr
4 & \quad \textbf{if}\ \text{the bulk is open }\ \textbf{then}
\ \text{append } x \text{ to it};\ \textbf{return} \cr
5 & \quad \textbf{if}\ |I| = m\ \textbf{then}\ \mathrm{flush}() \cr
6 & \quad \text{insert } x \text{ in } I \text{, moved up if } I
\text{ is a heap} \cr
7 & \textbf{procedure}\ \mathrm{flush}() \cr
8 & \quad S \gets \text{the items of } I \text{, sorted};\ I \gets
\emptyset \cr
9 & \quad \textbf{if}\ \text{the first of } S \text{ is not better }
\text{than the last of } D \text{ and of } G_1.\mathit{buf}
\ \textbf{then} \cr
10 & \qquad \mathrm{join}( 1 , S );\ \textbf{return} \cr
11 & \quad s \gets \text{the merge of } S \text{ with } D \text{ and }
G_1.\mathit{buf} \cr
12 & \quad D \gets \text{the first } |D| \text{ items of } s;
\ G_1.\mathit{buf} \gets \text{the next } |G_1.\mathit{buf}| \cr
13 & \quad \mathrm{join}( 1 , \text{the rest of } s ) \cr
14 & \textbf{procedure}\ \mathrm{join}( i , s ) \cr
15 & \quad \textbf{if}\ G_i \text{ has } k \text{ sequences}
\ \textbf{then} \cr
16 & \qquad \textbf{if}\ \text{they hold at most half of their room }
\textbf{then}\ \text{merge them into fewer full ones} \cr
17 & \qquad \textbf{else}\ \mathrm{join}( i + 1 , \text{the merge of}
\text{ the sequences of } G_i \text{ and of } G_{i+1}.\mathit{buf}
);\ G_i \gets \emptyset \cr
18 & \quad \text{add } s \text{ to the sequences of } G_i
\end{array}}
\]
◆ size()
template<class T, class Better = std::less< T >>
the number of items, \( n \)
◆ top()
template<class T, class Better = std::less< T >>
the best item
- Precondition
- the heap is not empty
template<class T, class Better = std::less< T >>
the sequences of a group, \( k \)
template<class T, class Better = std::less< T >>
the items of the insertion heap and of a buffer, \( m \)
The documentation for this class was generated from the following file: