101#ifndef __ADS_RadixHeap
102 #define __ADS_RadixHeap
119#include <type_traits>
136template<
class T , std::integral K = std::uint32_t ,
unsigned B = 4 ,
137 class A = std::allocator< T > ,
class Policy =
NoObservers >
141 static_assert( ( B >= 1 ) && ( B <= 8 ) ,
"a digit of 1 to 8 bits" );
154 using Subject =
typename Policy::template bind< modifications , RadixHeap >;
161 : f_B( buckets( alloc , std::make_index_sequence< NB >() ) ) { }
164 template<
class... O >
165 requires( (
sizeof...( O ) > 0 ) &&
166 ( ! std::is_convertible_v< O & , const A & > && ... ) &&
167 ( ! std::is_base_of_v< RadixHeap , O > && ... ) )
171 template<
class... O >
172 RadixHeap( std::allocator_arg_t ,
const A & alloc , O &... o )
173 : f_B( buckets( alloc , std::make_index_sequence< NB >() ) ) ,
203 void push(
const T & x , K k );
207 [[nodiscard]]
const T &
top(
void );
284 [[nodiscard]]
bool empty(
void )
const {
return( f_size == 0 ); }
287 [[nodiscard]] std::size_t
size(
void )
const {
return( f_size ); }
314 void extract( K theta , std::vector< T > & out );
327 requires( Subject::dynamic )
329 return( f_sub.attach( o ) );
335 requires( Subject::dynamic )
342 using U = std::make_unsigned_t< K >;
348 static constexpr bool heard = Subject::template listened< E >;
355 void extractPlain( K limit , F && f );
358 void pushTold(
const T & x , K k );
362 ADS_COLD_PATH void pushCold(
const T & x , K k ) { pushTold( x , k ); }
365 static constexpr unsigned W = std::numeric_limits< U >::digits;
366 static constexpr unsigned ROWS = ( W + B - 1 ) / B;
367 static constexpr unsigned R = 1u << B;
368 static constexpr unsigned NB = ROWS * R;
369 static constexpr unsigned NW = ( NB + 63 ) / 64;
379 static U encode( K k ) {
380 if constexpr( std::is_signed_v< K > )
381 return( U( k ) ^ ( U( 1 ) << ( W - 1 ) ) );
386 static K decode( U u ) {
387 if constexpr( std::is_signed_v< K > )
388 return( K( u ^ ( U( 1 ) << ( W - 1 ) ) ) );
394 unsigned bucket( U x )
const {
395 const U d = x ^ f_last;
396 const unsigned r = d ? unsigned( std::bit_width( d ) - 1 ) / B : 0;
397 return( r * R +
unsigned( ( x >> ( r * B ) ) & U( R - 1 ) ) );
401 void place(
unsigned i , Entry && e ) {
402 if( e.key < f_min[ i ] )
404 f_mask[ i >> 6 ] |= std::uint64_t( 1 ) << ( i & 63 );
405 f_B[ i ].push_back( std::move( e ) );
409 U ceiling(
unsigned i )
const {
410 const unsigned r = i / R , sh = ( r + 1 ) * B;
411 const U high = sh >= W ? U( 0 ) : U( ( f_last >> sh ) << sh );
412 const U low = r * B >= W ? ~U( 0 ) : U( ( U( 1 ) << ( r * B ) ) - 1 );
413 return( U( high | ( U( i % R ) << ( r * B ) ) | low ) );
417 unsigned first(
void )
const {
418 for(
unsigned w = 0 ; ; ++w )
420 return( w * 64 +
unsigned( std::countr_zero( f_mask[ w ] ) ) );
427 [[nodiscard]]
bool bucketed(
void )
const;
430 using Bucket = detail::AllocVector< A , Entry >;
433 template< std::size_t... i >
434 static std::array< Bucket , NB > buckets(
const A & a ,
435 std::index_sequence< i... > ) {
436 return( std::array< Bucket , NB >{
437 { (
static_cast< void >( i ) , Bucket( a ) )... } } );
440 std::array< Bucket , NB > f_B;
441 std::array< U , NB > f_min = fill_max();
442 std::array< std::uint64_t , NW > f_mask = {};
446 std::size_t f_size = 0;
449 static std::array< U , NB > fill_max(
void ) {
450 std::array< U , NB > a;
451 a.fill( std::numeric_limits< U >::max() );
465#include "ADS/priority/impl/RadixHeap.tpp"
The rules of the allocator-aware containers of the standard ([container.reqmts], [allocator....
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The typed modifications, beyond the book: the vocabulary by which a structure tells the observers att...
RadixHeap(void)=default
the empty heap
RadixHeap(O &... o)
the empty heap, the static observers o...
Definition RadixHeap.h:168
allocator_type get_allocator(void) const
the allocator of the buckets
Definition RadixHeap.h:179
K peekKey(void) const
the least key, read from the least key of the first bucket in use, which does not move the buckets: t...
void detach(int id)
detaches the observer number
Definition RadixHeap.h:334
void extractBelow(K limit, F &&f)
removes the items of keys below limit, calling f( item , key ) for each of them in no given order,...
void push(const T &x, K k)
pushes with the key , in time
mod::list< mod::Inserted< T, K >, mod::Erased< T, K >, mod::Cleared, mod::Rebuilt > modifications
the events: an item inserted or erased with its key, the heap emptied or rebuilt
Definition RadixHeap.h:150
A allocator_type
the type of the allocator
Definition RadixHeap.h:177
void clear(void)
removes the items, the buckets keeping their memory; any key may be pushed next
std::size_t size(void) const
the number of items,
Definition RadixHeap.h:287
typename Policy::template bind< modifications, RadixHeap > Subject
the subject
Definition RadixHeap.h:154
Subject & subject(void)
the subject, for ADS::batch() and ADS::quiet()
Definition RadixHeap.h:319
const Subject & subject(void) const
the subject
Definition RadixHeap.h:321
bool empty(void) const
whether the queue has no item
Definition RadixHeap.h:284
int attach(O &o)
attaches the observer and gives its number
Definition RadixHeap.h:326
T pop(void)
removes top() and returns it, in amortized time
K topKey(void)
the least key
const T & top(void)
an item of least key, the last pushed among those
void rebase(K k)
the keys pushed next may start from , the items placed again for , in time
RadixHeap(std::allocator_arg_t, const A &alloc, O &... o)
the empty heap, its buckets from alloc, the static observers o...
Definition RadixHeap.h:172
void extract(K theta, std::vector< T > &out)
every item of key at most appended to out, by increasing keys, each extracted as by pop()
K Key
the type of the keys
Definition RadixHeap.h:146
RadixHeap(const A &alloc)
the empty heap, its buckets from alloc
Definition RadixHeap.h:160
T Item
the type of the items
Definition RadixHeap.h:145
#define ADS_COLD_PATH
a function of a rare path: never inlined, laid out away from the hot code
Definition Portable.h:107
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
nobody listens: the subject is empty and every test is false at compile time, so that the structure c...
Definition Modification.h:1958
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
the item of handle , key and value inserted
Definition Modification.h:536
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