ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
DialQueue.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File DialQueue.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
75/*--------------------------------------------------------------------------*/
76/*----------------------------- DEFINITIONS --------------------------------*/
77/*--------------------------------------------------------------------------*/
78
79#ifndef __ADS_DialQueue
80 #define __ADS_DialQueue
81/* self-identification: #endif constant */
82
83/*--------------------------------------------------------------------------*/
84/*------------------------------ INCLUDES ----------------------------------*/
85/*--------------------------------------------------------------------------*/
86
87#include "ADS/Contract.h"
88#include "ADS/Modification.h"
90
91#include <bit>
92#include <limits>
93#include <concepts>
94#include <cstddef>
95#include <cstdint>
96#include <type_traits>
97#include <utility>
98#include <vector>
99
100/*--------------------------------------------------------------------------*/
101/*--------------------------- NAMESPACE ------------------------------------*/
102/*--------------------------------------------------------------------------*/
103
104namespace ADS
105{
106
107/*--------------------------------------------------------------------------*/
108/*--------------------------- CLASS DialQueue ------------------------------*/
109/*--------------------------------------------------------------------------*/
110
113template< class T , std::integral K = std::uint32_t ,
114 class Policy = NoObservers >
116{
117
118/*--------------------------------------------------------------------------*/
119 public:
120 using Item = T;
121 using Key = K;
122
127 explicit DialQueue( std::size_t c = 1023 );
128
135 using Subject = typename Policy::template bind< modifications , DialQueue >;
136
139 template< class... O >
140 requires( sizeof...( O ) > 0 )
141 DialQueue( std::size_t c , O &... o );
142
161 void push( const T & x , K k );
162
166 [[nodiscard]] const T & top( void );
167
170 [[nodiscard]] K topKey( void );
171
176 [[nodiscard]] K peekKey( void ) const;
177
205 T pop( void );
206
208 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
209
211 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
212
215 void clear( void );
216
218 [[nodiscard]] std::size_t window( void ) const { return( f_B.size() ); }
219
236 void rebase( K k );
237
240 void extract( K theta , std::vector< T > & out );
241
242/*--------------------------- THE OBSERVERS --------------------------------*/
243
245 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
247 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
248
251 template< class O >
252 int attach( O & o )
253 requires( Subject::dynamic )
254 {
255 return( f_sub.attach( o ) );
256 }
257
260 void detach( int id )
261 requires( Subject::dynamic )
262 {
263 f_sub.detach( id );
264 }
265
266/*--------------------------------------------------------------------------*/
267 private:
268 using U = std::make_unsigned_t< K >;
269 static U encode( K k ) {
270 if constexpr( std::is_signed_v< K > )
271 return( U( k ) ^ ( U( 1 ) << ( std::numeric_limits< U >::digits - 1 ) ) );
272 else
273 return( k );
274 }
275
276 static K decode( U u ) {
277 if constexpr( std::is_signed_v< K > )
278 return( K( u ^ ( U( 1 ) << ( std::numeric_limits< U >::digits - 1 ) ) ) );
279 else
280 return( u );
281 }
282
284 void put( const T & x , U u );
285
287 void pull( void );
288
291 [[nodiscard]] std::size_t next( void ) const;
292
295 [[nodiscard]] bool counted( void ) const;
296
297 using Ins = mod::Inserted< T , K >;
298 using Era = mod::Erased< T , K >;
299
301 template< class E >
302 static constexpr bool heard = Subject::template listened< E >;
303
305 void pushPlain( const T & x , K k );
306 T popPlain( void );
307
309 ADS_COLD_PATH void pushCold( const T & x , K k );
310 ADS_COLD_PATH T popCold( void );
311
312 std::vector< std::vector< T > > f_B;
313 std::vector< std::uint64_t > f_bits;
314 U f_cur = 0;
315 std::size_t f_in = 0;
316 std::size_t f_n = 0;
317 RadixHeap< T , U > f_over;
319
320 }; // end( class DialQueue )
321
322/*--------------------------------------------------------------------------*/
323
324} // end( namespace ADS )
325
326/*--------------------------------------------------------------------------*/
327/*-------------------------- IMPLEMENTATION --------------------------------*/
328/*--------------------------------------------------------------------------*/
329
330#include "ADS/priority/impl/DialQueue.tpp"
331
332/*--------------------------------------------------------------------------*/
333
334#endif /* DialQueue.h included */
335
336/*--------------------------------------------------------------------------*/
337/*-------------------------- End File DialQueue.h --------------------------*/
338/*--------------------------------------------------------------------------*/
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...
A monotone priority queue of integer keys, beyond the book: the radix heap of Ahuja,...
T pop(void)
removes top() and returns it, in time if the overflow is not used
DialQueue(std::size_t c, O &... o)
an empty queue as DialQueue( c ), the static observers o...
typename Policy::template bind< modifications, DialQueue > Subject
the subject
Definition DialQueue.h:135
T Item
the type of the items
Definition DialQueue.h:120
K topKey(void)
the least key
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 queue emptied or rebuilt
Definition DialQueue.h:131
const T & top(void)
an item of least key, the last pushed among those of its bucket that did not come through the overflo...
void detach(int id)
detaches the observer number
Definition DialQueue.h:260
std::size_t size(void) const
the number of items,
Definition DialQueue.h:211
void clear(void)
removes the items, the buckets keeping their memory; any key may be pushed next
std::size_t window(void) const
the number of buckets of the window,
Definition DialQueue.h:218
bool empty(void) const
whether the queue has no item
Definition DialQueue.h:208
void push(const T &x, K k)
pushes with the key , in time if
int attach(O &o)
attaches the observer and gives its number
Definition DialQueue.h:252
Subject & subject(void)
the subject, for ADS::batch() and ADS::quiet()
Definition DialQueue.h:245
K Key
the type of the keys
Definition DialQueue.h:121
DialQueue(std::size_t c=1023)
an empty queue whose window has room for the keys up to above the last one extracted: buckets,...
K peekKey(void) const
the least key, which does not move : the keys pushed next need only be not smaller than the last one ...
void rebase(K k)
the keys pushed next may start from , the items placed again for , in time
const Subject & subject(void) const
the subject
Definition DialQueue.h:247
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()
#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
std::string encode(const std::basic_string< C > &text, const std::map< C, std::string > &code)
the text encoded by the codes, the code of each character in turn
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
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