ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess > Class Template Reference

the exact PRIORITY QUEUE of one owner, of which a thief takes about half of the pairs, offered by the owner at its next operation; with a bound per pair of the type Bd (none if void), ordered by BdLess, it also knows the least bound of the pairs that its owner holds More...

#include <ADS/concurrent/StealablePriorityQueue.h>

Public types

using Key = K
 the type of the keys
using Item = T
 the type of the items
using Bound = std::conditional_t< bounded , Bd , detail::SpqNoBound >
 the type of the bounds, detail::SpqNoBound if none
using Entry = detail::SpqEntry< K , T , Bd >
 a pair of the queue: its key, its bound if any, and its item

Public member functions

 StealablePriorityQueue (Less less=Less(), BdLess bl=BdLess())
 the empty queue of the orders less of the keys and bl of the bounds
 StealablePriorityQueue (const StealablePriorityQueue &)=delete
 no copy, since the thieves share it by its address
StealablePriorityQueue & operator= (const StealablePriorityQueue &)=delete
 no copy, since the thieves share it by its address
 ~StealablePriorityQueue ()
 the free of the offer not claimed
void push (K k, T x)
 pushes \( ( k , x ) \) into the smaller heap, \( O( \log m ) \), then answers a thief that asked
void push (K k, Bound b, T x)
 pushes \( ( k , b , x ) \), the pair of the key \( k \) with the bound \( b \), as push( k , x ) above, and \( b \) into the bounds of its heap
std::optional< Bound > minBound (void) const
 the least bound of the pairs that the owner holds, nothing if it holds none: those of its two heaps and, while no thief has claimed it, those of its offer, whose least bound the owner keeps from the moment it made it; exact at the moment of the load of the offer (Lemma 12 of Priority queues that are stolen), \( O( \log m ) \) amortized
std::optional< Entry > pop (void)
 the least pair of the queue, removed, nothing if it is empty, after answering a thief that asked; \( O( \log m ) \) but for the offer taken back
const Entry & top (void)
 the least pair of the queue, the offer taken back if it holds it
void serve (void)
 answers a thief that asked, offering about half of the pairs if there are at least two and no offer waits; \( O( 1 ) \), or \( O( m ) \) when the pairs are dealt again, and a load without order when no thief asked
std::size_t steal (StealablePriorityQueue &v)
 steals into this queue, empty, the offer of the queue \( v \), or asks \( v \) for one and waits a little for it (as long as a Backoff spins); the number of the pairs stolen, 0 if none; called by the owner of this queue, a thief of \( v \).
std::size_t size (void) const
 the number of the pairs of the queue, those of the offer included while no thief has claimed it; for the owner
bool empty (void) const
 whether size() is 0
bool stealable (void) const
 whether the queue offers pairs or holds at least two, a hint read with no order; any thread may call it, e.g., a thief that chooses its victim
bool offering (void) const
 whether an offer of the owner waits for a thief, for the owner
std::pair< std::size_t, std::size_t > halves (void) const
 the pairs that the two heaps hold, \( |H_0| \) and \( |H_1| \)
bool consistent (void) const
 the two heaps ordered, and the hint of the thieves equal to their size; for the owner
template<class F>
std::size_t drain (F &&f)
 the calls f( e ) on the pairs of the queue, removed, in increasing order of the keys: the offer still held is taken back first, and both heaps are emptied, so that the queue holds nothing afterwards (e.g., a queue pruned whole by its minBound()); their number

Static public attributes

static constexpr bool bounded = ! std::is_void_v< Bd >
 whether the pairs have bounds

Detailed description

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
class ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >

the exact PRIORITY QUEUE of one owner, of which a thief takes about half of the pairs, offered by the owner at its next operation; with a bound per pair of the type Bd (none if void), ordered by BdLess, it also knows the least bound of the pairs that its owner holds

Member typedef documentation

◆ Bound

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
using ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::Bound = std::conditional_t< bounded , Bd , detail::SpqNoBound >

the type of the bounds, detail::SpqNoBound if none

◆ Entry

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
using ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::Entry = detail::SpqEntry< K , T , Bd >

a pair of the queue: its key, its bound if any, and its item

◆ Item

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
using ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::Item = T

the type of the items

◆ Key

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
using ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::Key = K

the type of the keys

Constructor and destructor documentation

◆ StealablePriorityQueue() [1/2]

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::StealablePriorityQueue ( Less less = Less(),
BdLess bl = BdLess() )
explicit

the empty queue of the orders less of the keys and bl of the bounds

◆ StealablePriorityQueue() [2/2]

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::StealablePriorityQueue ( const StealablePriorityQueue< K, T, Less, Bd, BdLess > & )
delete

no copy, since the thieves share it by its address

◆ ~StealablePriorityQueue()

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::~StealablePriorityQueue ( )

the free of the offer not claimed

Member function documentation

◆ consistent()

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
bool ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::consistent ( void ) const
nodiscard

the two heaps ordered, and the hint of the thieves equal to their size; for the owner

◆ drain()

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
template<class F>
std::size_t ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::drain ( F && f)

the calls f( e ) on the pairs of the queue, removed, in increasing order of the keys: the offer still held is taken back first, and both heaps are emptied, so that the queue holds nothing afterwards (e.g., a queue pruned whole by its minBound()); their number

◆ empty()

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
bool ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::empty ( void ) const
inlinenodiscard

whether size() is 0

◆ halves()

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
std::pair< std::size_t, std::size_t > ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::halves ( void ) const
inlinenodiscard

the pairs that the two heaps hold, \( |H_0| \) and \( |H_1| \)

◆ minBound()

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
std::optional< Bound > ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::minBound ( void ) const
nodiscard

the least bound of the pairs that the owner holds, nothing if it holds none: those of its two heaps and, while no thief has claimed it, those of its offer, whose least bound the owner keeps from the moment it made it; exact at the moment of the load of the offer (Lemma 12 of Priority queues that are stolen), \( O( \log m ) \) amortized

In pseudocode (beyond the book), \( \beta( H ) \) the least bound of the heap \( H \) and \( b_O \) that of the offer:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{minBound}() \cr 2 & \quad b \gets \min\{ \beta( H_j ) : H_j \ne \emptyset \} \cr 3 & \quad \textbf{if}\ \mathit{offered}\ \textbf{and}\ O \ne \textbf{nil}\ \textbf{then}\ b \gets \min\{ b , b_O \}\quad \triangleright\ \text{acquire} \cr 4 & \quad \textbf{return}\ b\quad \triangleright\ \textbf{nil} \text{ if there is no pair} \cr 5 & \textbf{function}\ \beta( H )\quad \triangleright\ \ell \text{ the least bound known, } \mu \text{ those of the} \text{ subtrees} \cr 6 & \quad \textbf{if}\ \mu \text{ is kept}\ \textbf{then return} \ \mu( 0 ) \cr 7 & \quad \textbf{if}\ \ell \text{ is known}\ \textbf{then return} \ \ell\quad \triangleright\ \text{no pop took a pair of bound } \ell \cr 8 & \quad \ell \gets \text{the least bound of the pairs of } H \text{, by a scan} \cr 9 & \quad \textbf{if}\ |H| \ge 64\ \textbf{and}\ \text{the pairs} \text{ scanned} > 16 ( \text{pops} + |H| ) + 2^{16}\ \textbf{then} \ \text{build } \mu \cr 10 & \quad \textbf{return}\ \ell \end{array}} \]

◆ offering()

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
bool ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::offering ( void ) const
nodiscard

whether an offer of the owner waits for a thief, for the owner

◆ operator=()

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
StealablePriorityQueue & ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::operator= ( const StealablePriorityQueue< K, T, Less, Bd, BdLess > & )
delete

no copy, since the thieves share it by its address

◆ pop()

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
std::optional< Entry > ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::pop ( void )

the least pair of the queue, removed, nothing if it is empty, after answering a thief that asked; \( O( \log m ) \) but for the offer taken back

In pseudocode (beyond the book), \( O \) the offer, \( k_O \) its least key and \( \mathit{offered} \) the knowledge of the owner that it made it:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pop}() \cr 2 & \quad \mathrm{serve}();\ \mathrm{settle}() \cr 3 & \quad \textbf{if}\ H_0 = H_1 = \emptyset\ \textbf{then return nil} \cr 4 & \quad \textbf{return}\ \mathrm{pop}( H_j ) \text{ for the } j \text{ of the least top, } 0 \text{ on a tie} \cr 5 & \textbf{procedure}\ \mathrm{settle}() \cr 6 & \quad \textbf{if}\ \mathit{offered}\ \textbf{and}\ ( H_0 = H_1 = \emptyset\ \textbf{or}\ k_O < \text{the least local key} ) \ \textbf{then} \cr 7 & \qquad G \gets \mathrm{exchange}( O , \textbf{nil} ); \ \mathit{offered} \gets \textbf{false}\quad \triangleright \ \text{acquire and release} \cr 8 & \qquad \textbf{if}\ G \ne \textbf{nil}\ \textbf{then}\ \text{the} \text{ pairs of } G \text{ and of the smaller heap into one heap} \end{array}} \]

◆ push() [1/2]

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
void ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::push ( K k,
Bound b,
T x )
inline

pushes \( ( k , b , x ) \), the pair of the key \( k \) with the bound \( b \), as push( k , x ) above, and \( b \) into the bounds of its heap

◆ push() [2/2]

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
void ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::push ( K k,
T x )
inline

pushes \( ( k , x ) \) into the smaller heap, \( O( \log m ) \), then answers a thief that asked

In pseudocode (beyond the book), \( H_0 \) and \( H_1 \) the two heaps and \( \tau \) the turn of the pushes:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{push}( k , x ) \cr 2 & \quad \textbf{if}\ |H_0| \ne |H_1|\ \textbf{then}\ j \gets \text{the smaller}\ \textbf{else}\ \tau \gets 1 - \tau;\ j \gets \tau \cr 3 & \quad \mathrm{push}( H_j , ( k , x ) );\ \mathrm{serve}() \end{array}} \]

◆ serve()

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
void ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::serve ( void )

answers a thief that asked, offering about half of the pairs if there are at least two and no offer waits; \( O( 1 ) \), or \( O( m ) \) when the pairs are dealt again, and a load without order when no thief asked

In pseudocode (beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{serve}() \cr 2 & \quad \textbf{if not}\ \mathit{hungry}\ \textbf{then return} \quad \triangleright\ \text{relaxed} \cr 3 & \quad \textbf{if}\ \mathit{offered}\ \textbf{and}\ O \ne \textbf{nil}\ \textbf{then return}\quad \triangleright\ \text{one} \text{ offer at a time} \cr 4 & \quad \mathit{offered} \gets \textbf{false};\ \mathit{hungry} \gets \textbf{false} \cr 5 & \quad \textbf{if}\ |H_0| + |H_1| < 2\ \textbf{then return} \cr 6 & \quad j \gets \text{the heap whose top is not the least}; \ m \gets |H_0| + |H_1| \cr 7 & \quad \textbf{if}\ 5 |H_j| < m\ \textbf{or}\ 5 |H_j| > 4 m \ \textbf{then} \cr 8 & \qquad \text{deal the pairs of } H_0 \text{ and } H_1 \text{ in turn to two new heaps};\ j \gets \text{as at line } 6 \cr 9 & \quad k_O \gets \text{the key of } \mathrm{top}( H_j );\ O \gets H_j;\ H_j \gets \emptyset;\ \mathit{offered} \gets \textbf{true} \quad \triangleright\ \text{release} \end{array}} \]

◆ size()

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
std::size_t ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::size ( void ) const
nodiscard

the number of the pairs of the queue, those of the offer included while no thief has claimed it; for the owner

◆ steal()

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
std::size_t ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::steal ( StealablePriorityQueue< K, T, Less, Bd, BdLess > & v)

steals into this queue, empty, the offer of the queue \( v \), or asks \( v \) for one and waits a little for it (as long as a Backoff spins); the number of the pairs stolen, 0 if none; called by the owner of this queue, a thief of \( v \).

The offer is one of the two interleaved heaps of \( v \), about half of its pairs, and the pairs stolen keep their keys, their bounds and their items: from the exchange on they are of this queue, and minBound() of \( v \) no longer counts them

In pseudocode (beyond the book), \( O_v \) the offer of \( v \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{steal}( v ) \cr 2 & \quad \textbf{if}\ O_v = \textbf{nil}\ \textbf{then} \cr 3 & \qquad \textbf{if}\ v \text{ holds less than two pairs} \ \textbf{then return}\ 0\quad \triangleright\ \text{a hint of } v \cr 4 & \qquad \mathit{hungry}_v \gets \textbf{true};\ \text{wait a little} \text{ while } O_v = \textbf{nil} \cr 5 & \qquad \textbf{if}\ O_v = \textbf{nil}\ \textbf{then return}\ 0 \cr 6 & \quad G \gets \mathrm{exchange}( O_v , \textbf{nil} )\quad \triangleright\ \text{acquire and release} \cr 7 & \quad \textbf{if}\ G = \textbf{nil}\ \textbf{then return}\ 0 \quad \triangleright\ \text{another thread was first} \cr 8 & \quad \text{deal the pairs of } G \text{ in turn to } H_0 \text{ and } H_1;\ \textbf{return}\ |G| \end{array}} \]

Precondition
\( v \) is not this queue, and this queue is empty

◆ stealable()

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
bool ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::stealable ( void ) const
inlinenodiscard

whether the queue offers pairs or holds at least two, a hint read with no order; any thread may call it, e.g., a thief that chooses its victim

◆ top()

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
const Entry & ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::top ( void )

the least pair of the queue, the offer taken back if it holds it

Precondition
the queue is not empty

Member data documentation

◆ bounded

template<class K, class T, class Less = std::less< K >, class Bd = void, class BdLess = std::less<>>
bool ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess >::bounded = ! std::is_void_v< Bd >
staticconstexpr

whether the pairs have bounds


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