|
| | RealTimeMinQueue (C less=C()) |
| | the empty queue, ordered by less
|
| | RealTimeMinQueue (const RealTimeMinQueue &o)=default |
| | a copy, item by item, in \( O( n ) \) time
|
| | RealTimeMinQueue (RealTimeMinQueue &&o) noexcept=default |
| | the items of o, which is left empty
|
| RealTimeMinQueue & | operator= (const RealTimeMinQueue &o)=default |
| | the items of o, by copy and swap
|
| RealTimeMinQueue & | operator= (RealTimeMinQueue &&o) noexcept=default |
| | the items of o, exchanged with those of this queue
|
| void | swap (RealTimeMinQueue &o) noexcept |
| | exchanges the items with o
|
| bool | empty (void) const |
| | true if there is no item
|
| std::size_t | size (void) const |
| | the number \( n \) of items
|
| void | enqueue (const T &x) |
| | \( x \) at the back, on the stack \( R \), and three moves of a rotation if one is going on
|
| T | dequeue (void) |
| | removes the item at the front, the top of \( F \), and returns it, with three moves of a rotation if one is going on
|
| const T & | top (void) const |
| | the item at the front, the top of \( F \)
|
| const T & | min (void) const |
| | the least item, the least of the minima of the parts
|
| template<class F> |
| void | forEach (F &&f) const |
| | f( x ) on the items from the front to the back, \( O( n ) \)
|
| void | clear (void) |
| | removes all the items
|
template<class T, class C = std::less<>>
class ADS::RealTimeMinQueue< T, C >
a QUEUE with its least item, every operation in \( O( 1 ) \) worst-case time
template<class T, class C = std::less<>>
removes the item at the front, the top of \( F \), and returns it, with three moves of a rotation if one is going on
In pseudocode (Hood and Melville, beyond the book), \( R' \) the frozen back, \( N \) the new front, \( i \) the next place of \( F \) to move:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{dequeue}() \cr
2 & \quad x \gets \mathrm{pop}( F ) \cr
3 & \quad \textbf{if}\ \text{a rotation is going on}\ \textbf{then}
\ \mathrm{steps}( 3 ) \cr
4 & \quad \textbf{else if}\ |R| > |F|\ \textbf{then}\ R' \gets R;
\ R \gets \emptyset;\ N \gets \emptyset;\ i \gets 0;
\ \mathrm{steps}( 3 ) \cr
5 & \quad \textbf{return}\ x \cr
6 & \textbf{procedure}\ \mathrm{steps}( k ) \cr
7 & \quad \textbf{repeat}\ k\ \textbf{times} \cr
8 & \qquad \textbf{if}\ R' \ne \emptyset\ \textbf{then}
\ \mathrm{push}( N , \mathrm{pop}( R' ) ) \cr
9 & \qquad \textbf{else if}\ i < |F|\ \textbf{then}\ \mathrm{push}( N ,
F[ i ] );\ i \gets i + 1 \cr
10 & \qquad \textbf{else}\ F \gets N;\ \text{the rotation ends};
\ \textbf{return}
\end{array}}
\]
(enqueue() runs the lines 3 and 4 after its push on \( R \)).
- Precondition
- the queue is not empty
- Postcondition
- one item less
template<class T, class C = std::less<>>
the least item, the least of the minima of the parts
In pseudocode, \( r_0 \) the size of \( R' \) when the rotation started, and \( \mu \) the least place of a cell:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{min}() \cr
2 & \quad X \gets \emptyset \cr
3 & \quad \textbf{if}\ F \ne \emptyset\ \textbf{then}
\ a \gets \mu( \text{the top of } F ) \cr
4 & \qquad \textbf{if}\ a \text{ is a live place of } F
\ \textbf{then}\ X \gets \{ F[ a ] \}\ \textbf{else}\ X \gets
\{ N[ r_0 + a ] \} \cr
5 & \quad \textbf{foreach}\ P \in \{ R' , N , R \}\ \textbf{do}
\ \textbf{if}\ P \ne \emptyset\ \textbf{then}\ X \gets X
\cup \{ \min P \} \cr
6 & \quad \textbf{return}\ \text{the least item of } X
\end{array}}
\]
- Precondition
- the queue is not empty