ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RadixHeap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File RadixHeap.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
97/*--------------------------------------------------------------------------*/
98/*----------------------------- DEFINITIONS --------------------------------*/
99/*--------------------------------------------------------------------------*/
100
101#ifndef __ADS_RadixHeap
102 #define __ADS_RadixHeap
103/* self-identification: #endif constant */
104
105/*--------------------------------------------------------------------------*/
106/*------------------------------ INCLUDES ----------------------------------*/
107/*--------------------------------------------------------------------------*/
108
109#include "ADS/Contract.h"
110#include "ADS/Modification.h"
112
113#include <array>
114#include <bit>
115#include <concepts>
116#include <cstddef>
117#include <cstdint>
118#include <limits>
119#include <type_traits>
120#include <utility>
121#include <vector>
122
123/*--------------------------------------------------------------------------*/
124/*--------------------------- NAMESPACE ------------------------------------*/
125/*--------------------------------------------------------------------------*/
126
127namespace ADS
128{
129
130/*--------------------------------------------------------------------------*/
131/*--------------------------- CLASS RadixHeap ------------------------------*/
132/*--------------------------------------------------------------------------*/
133
136template< class T , std::integral K = std::uint32_t , unsigned B = 4 ,
137 class A = std::allocator< T > , class Policy = NoObservers >
139{
140
141 static_assert( ( B >= 1 ) && ( B <= 8 ) , "a digit of 1 to 8 bits" );
142
143/*--------------------------------------------------------------------------*/
144 public:
145 using Item = T;
146 using Key = K;
147
154 using Subject = typename Policy::template bind< modifications , RadixHeap >;
155
157 RadixHeap( void ) = default;
158
160 explicit RadixHeap( const A & alloc )
161 : f_B( buckets( alloc , std::make_index_sequence< NB >() ) ) { }
162
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 > && ... ) )
168 explicit RadixHeap( O &... o ) : f_sub( o... ) { }
169
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 >() ) ) ,
174 f_sub( o... ) { }
175
177 using allocator_type = A;
179 [[nodiscard]] allocator_type get_allocator( void ) const {
180 return( allocator_type( f_B[ 0 ].get_allocator() ) );
181 }
182
203 void push( const T & x , K k );
204
207 [[nodiscard]] const T & top( void );
208
211 [[nodiscard]] K topKey( void );
212
217 [[nodiscard]] K peekKey( void ) const;
218
248 T pop( void );
249
280 template< class F >
281 void extractBelow( K limit , F && f );
282
284 [[nodiscard]] bool empty( void ) const { return( f_size == 0 ); }
285
287 [[nodiscard]] std::size_t size( void ) const { return( f_size ); }
288
291 void clear( void );
292
310 void rebase( K k );
311
314 void extract( K theta , std::vector< T > & out );
315
316/*--------------------------- THE OBSERVERS --------------------------------*/
317
319 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
321 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
322
325 template< class O >
326 int attach( O & o )
327 requires( Subject::dynamic )
328 {
329 return( f_sub.attach( o ) );
330 }
331
334 void detach( int id )
335 requires( Subject::dynamic )
336 {
337 f_sub.detach( id );
338 }
339
340/*--------------------------------------------------------------------------*/
341 private:
342 using U = std::make_unsigned_t< K >;
343 using Ins = mod::Inserted< T , K >;
344 using Era = mod::Erased< T , K >;
345
347 template< class E >
348 static constexpr bool heard = Subject::template listened< E >;
349
351 T popPlain( void );
352
354 template< class F >
355 void extractPlain( K limit , F && f );
356
358 void pushTold( const T & x , K k );
359 T popTold( void );
360
362 ADS_COLD_PATH void pushCold( const T & x , K k ) { pushTold( x , k ); }
363 ADS_COLD_PATH T popCold( void ) { return( popTold() ); }
364
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;
370
372 struct Entry
373 {
374 U key;
375 T item;
376 };
377
379 static U encode( K k ) {
380 if constexpr( std::is_signed_v< K > )
381 return( U( k ) ^ ( U( 1 ) << ( W - 1 ) ) );
382 else
383 return( k );
384 }
385
386 static K decode( U u ) {
387 if constexpr( std::is_signed_v< K > )
388 return( K( u ^ ( U( 1 ) << ( W - 1 ) ) ) );
389 else
390 return( u );
391 }
392
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 ) ) );
398 }
399
401 void place( unsigned i , Entry && e ) {
402 if( e.key < f_min[ i ] )
403 f_min[ i ] = e.key;
404 f_mask[ i >> 6 ] |= std::uint64_t( 1 ) << ( i & 63 );
405 f_B[ i ].push_back( std::move( e ) );
406 }
407
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 ) );
414 }
415
417 unsigned first( void ) const {
418 for( unsigned w = 0 ; ; ++w )
419 if( f_mask[ w ] )
420 return( w * 64 + unsigned( std::countr_zero( f_mask[ w ] ) ) );
421 }
422
424 void pull( void );
425
427 [[nodiscard]] bool bucketed( void ) const;
428
430 using Bucket = detail::AllocVector< A , Entry >;
431
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 ) )... } } );
438 }
439
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 = {};
443 U f_last = 0;
444 U f_floor = 0;
445 unsigned f_cur = 0;
446 std::size_t f_size = 0;
448
449 static std::array< U , NB > fill_max( void ) {
450 std::array< U , NB > a;
451 a.fill( std::numeric_limits< U >::max() );
452 return( a );
453 }
454
455 }; // end( class RadixHeap )
456
457/*--------------------------------------------------------------------------*/
458
459} // end( namespace ADS )
460
461/*--------------------------------------------------------------------------*/
462/*-------------------------- IMPLEMENTATION --------------------------------*/
463/*--------------------------------------------------------------------------*/
464
465#include "ADS/priority/impl/RadixHeap.tpp"
466
467/*--------------------------------------------------------------------------*/
468
469#endif /* RadixHeap.h included */
470
471/*--------------------------------------------------------------------------*/
472/*-------------------------- End File RadixHeap.h --------------------------*/
473/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96
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