ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BucketQueue.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File BucketQueue.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
104/*--------------------------------------------------------------------------*/
105/*----------------------------- DEFINITIONS --------------------------------*/
106/*--------------------------------------------------------------------------*/
107
108#ifndef __ADS_BucketQueue
109 #define __ADS_BucketQueue
110/* self-identification: #endif constant */
111
112/*--------------------------------------------------------------------------*/
113/*------------------------------ INCLUDES ----------------------------------*/
114/*--------------------------------------------------------------------------*/
115
116#include "ADS/Contract.h"
117#include "ADS/Modification.h"
118
119#include <algorithm>
120#include <concepts>
121#include <cstddef>
122#include <cstdint>
123#include <iterator>
124#include <stdexcept>
125#include <type_traits>
126#include <utility>
127#include <vector>
128
129/*--------------------------------------------------------------------------*/
130/*--------------------------- NAMESPACE ------------------------------------*/
131/*--------------------------------------------------------------------------*/
132
133namespace ADS
134{
135
136/*--------------------------------------------------------------------------*/
137/*------------------------ THE POLICIES OF THE TIES ------------------------*/
138/*--------------------------------------------------------------------------*/
139
143{
144 static constexpr bool least = false;
145 };
146
151{
152 static constexpr bool least = true;
153 };
154
157template< class P >
158concept BucketTie = requires {
159 { P::least } -> std::convertible_to< bool >;
160 };
161
162/*--------------------------------------------------------------------------*/
163/*-------------------------- CLASS BucketQueue -----------------------------*/
164/*--------------------------------------------------------------------------*/
165
168template< class T , std::integral K = int , BucketTie Tie = LargestSecondKey ,
169 class Policy = NoObservers >
171{
172
173/*--------------------------------------------------------------------------*/
174 public:
175 using Item = T;
176 using Key = K;
177 using Ties = Tie;
178
184 mod::Rebuilt >;
186 using Subject = typename Policy::template bind< modifications , BucketQueue >;
187
189 BucketQueue( void ) = default;
190
192 template< class... O >
193 requires( ( sizeof...( O ) > 0 ) &&
194 ( ! std::is_base_of_v< BucketQueue , O > && ... ) )
195 explicit BucketQueue( O &... o ) : f_sub( o... ) { }
196
232 void push( const T & x , K f , K g );
233
258 [[nodiscard]] const T & top( void );
259
262 [[nodiscard]] K topF( void );
263
267 [[nodiscard]] K topG( void );
268
292 T pop( void );
293
295 struct Entry
296 {
298 K f ,
300 };
301
307
309 struct TopEntry
310 {
311 const T & item;
312 K f ,
314 };
315
320 [[nodiscard]] TopEntry topEntry( void );
321
323 [[nodiscard]] bool empty( void ) const;
324
326 [[nodiscard]] std::size_t size( void ) const;
327
329 void clear( void );
330
333 void extract( K theta , std::vector< T > & out );
334
335/*--------------------------- THE OBSERVERS --------------------------------*/
336
338 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
340 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
341
344 template< class O >
345 int attach( O & o )
346 requires( Subject::dynamic )
347 {
348 return( f_sub.attach( o ) );
349 }
350
353 void detach( int id )
354 requires( Subject::dynamic )
355 {
356 f_sub.detach( id );
357 }
358
360 static constexpr std::size_t D = 256;
361
363 static constexpr std::size_t R = 64;
364
365/*--------------------------------------------------------------------------*/
366 private:
367 using Index = std::uint32_t;
368 static constexpr Index NIL = ~Index( 0 );
369 static constexpr std::size_t S = 4;
370
372 struct Cell
373 {
374 T item;
375 Index next;
376 };
377
379 struct Run
380 {
381 K g;
382 Index head;
383 };
384
385 using Runs = std::vector< Run >;
386
388 struct Layer
389 {
390 Index count = 0;
391 Index topG = 0;
393 Index runs = NIL;
394 Index small[ S ] = { NIL , NIL , NIL , NIL };
395 std::vector< Index > big;
396
397 Index & head( std::size_t g ) {
398 return( g < S ? small[ g ] : big[ g - S ] );
399 }
400 Index head( std::size_t g ) const {
401 return( g < S ? small[ g ] : g - S < big.size() ? big[ g - S ] : NIL );
402 }
404 std::size_t lists( void ) const { return( S + big.size() ); }
406 bool dense( void ) const { return( big.size() > D - S ); }
407 };
408
409 public:
410/*------------------------- THE STANDARD INTERFACE -------------------------*/
411
412 // a constant view of the items in the order of the extractions (by f
413 // increasing, then by g decreasing, or increasing by LeastSecondKey, each
414 // list from the last item pushed), forward: in a layer the runs from the
415 // last, then the dense lists from the last (the dense lists from the
416 // first, then the runs from the last); it skips the empty lists without
417 // touching the lazy bounds that top() moves
418
420 {
421 public:
422 using iterator_category = std::forward_iterator_tag;
423 using value_type = T;
424 using difference_type = std::ptrdiff_t;
425 using reference = const T &;
426 using pointer = const T *;
427
429 const_iterator( void ) = default;
431 const_iterator( const BucketQueue * q , std::size_t f ) : f_q( q ) ,
432 f_f( f ) {
433 settle();
434 }
435
437 reference operator*( void ) const { return( f_q->f_cells[ f_c ].item ); }
439 pointer operator->( void ) const { return( &**this ); }
440
443 f_c = f_q->f_cells[ f_c ].next;
444 if( f_c == NIL ) {
445 ++f_g;
446 settle();
447 }
448 return( *this );
449 }
450
452 auto t = *this;
453 ++*this;
454 return( t );
455 }
456
458 bool operator==( const const_iterator & o ) const {
459 return( ( f_f == o.f_f ) && ( f_g == o.f_g ) && ( f_c == o.f_c ) );
460 }
461
462 private:
466 void settle( void ) {
467 const auto & L = f_q->f_layers;
468 for( ; f_f < L.size() ; ++f_f , f_g = 0 ) {
469 const Layer & l = L[ f_f ];
470 const std::size_t r = l.runs == NIL ? 0 : f_q->f_runs[ l.runs ].size();
471 if( l.count )
472 for( ; f_g < r + l.lists() ; ++f_g ) {
473 if constexpr( Tie::least )
474 f_c = f_g < l.lists()
475 ? l.head( f_g )
476 : f_q->f_runs[ l.runs ][ r + l.lists() - 1 - f_g ].head;
477 else
478 f_c = f_g < r ? f_q->f_runs[ l.runs ][ r - 1 - f_g ].head
479 : l.head( r + l.lists() - 1 - f_g );
480 if( f_c != NIL )
481 return;
482 }
483 }
484 f_g = 0;
485 f_c = NIL;
486 }
487
488 const BucketQueue * f_q = nullptr;
489 std::size_t f_f = 0 , f_g = 0;
490 Index f_c = NIL;
491 };
492
493 using value_type = T;
494 using size_type = std::size_t;
495 using difference_type = std::ptrdiff_t;
496 using reference = const T &;
497 using const_reference = const T &;
499
501 [[nodiscard]] const_iterator begin( void ) const {
502 return( const_iterator( this , 0 ) );
503 }
504
505 [[nodiscard]] const_iterator end( void ) const {
506 return( const_iterator( this , f_layers.size() ) );
507 }
508
509 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
511 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
512
513 private:
514 using Ins = mod::Inserted< T , K , K >;
515 using Era = mod::Erased< T , K , K >;
516
518 template< class E >
519 static constexpr bool heard = Subject::template listened< E >;
520
522 void pushPlain( const T & x , K f , K g );
523 T popPlain( void );
524
526 ADS_COLD_PATH void pushCold( const T & x , K f , K g );
527 ADS_COLD_PATH T popCold( void );
528
531 Layer & front( void );
532
534 [[nodiscard]] static bool inRuns( const Layer & l );
535
537 Index & top( Layer & l );
538
540 [[nodiscard]] K topKey( const Layer & l ) const;
541
543 Index cell( const T & x );
544
547 bool pushRun( Layer & l , const T & x , K g );
548
551 static Index & run( Runs & v , K g );
552
554 T popRun( Layer & l );
555
557 T take( Index & h );
558
560 void taken( Layer & l );
561
563 void densify( Layer & l );
564
566 void release( Layer & l );
567
569 [[nodiscard]] bool counted( void ) const;
570
573 [[nodiscard]] bool ordered( const Layer & l ) const;
574
575 std::vector< Layer > f_layers;
576 std::vector< Cell > f_cells;
577 Index f_free = NIL;
578 std::size_t f_minF = 0;
579 std::size_t f_size = 0;
580 std::vector< Runs > f_runs;
581 std::vector< Index > f_spare;
583
584 }; // end( class BucketQueue )
585
586/*--------------------------------------------------------------------------*/
587/*-------------------------------- FUNCTIONS -------------------------------*/
588/*--------------------------------------------------------------------------*/
589
594[[nodiscard]] constexpr std::uint64_t packFG( std::uint32_t f ,
595 std::uint32_t g ) noexcept
596{
597 return( ( std::uint64_t( f ) << 32 ) | std::uint32_t( ~g ) );
598 }
599
601[[nodiscard]] constexpr std::uint32_t keyF( std::uint64_t k ) noexcept
602{
603 return( std::uint32_t( k >> 32 ) );
604 }
605
607[[nodiscard]] constexpr std::uint32_t keyG( std::uint64_t k ) noexcept
608{
609 return( ~std::uint32_t( k ) );
610 }
611
612/*--------------------------------------------------------------------------*/
613/*--------------------------- CLASS BucketHeap -----------------------------*/
614/*--------------------------------------------------------------------------*/
615
624template< class E , class C >
626{
627
628 using Item = decltype( E::item );
629
630/*--------------------------------------------------------------------------*/
631 public:
633 explicit BucketHeap( const C & = C() ) { }
634
636 void push( E && e ) {
637 f_q.push( std::move( e.item ) , keyF( e.key ) , keyG( e.key ) );
638 }
639
642 [[nodiscard]] E top( void ) const {
643 const auto t = f_q.topEntry();
644 return( E{ packFG( t.f , t.g ) , t.item } );
645 }
646
649 E pop( void ) {
650 auto e = f_q.popEntry();
651 return( E{ packFG( e.f , e.g ) , std::move( e.item ) } );
652 }
653
655 [[nodiscard]] bool empty( void ) const { return( f_q.empty() ); }
656
658 [[nodiscard]] std::size_t size( void ) const { return( f_q.size() ); }
659
660/*--------------------------------------------------------------------------*/
661 private:
664
665 }; // end( class BucketHeap )
666
667/*--------------------------------------------------------------------------*/
668
669} // end( namespace ADS )
670
671/*--------------------------------------------------------------------------*/
672/*-------------------------- IMPLEMENTATION --------------------------------*/
673/*--------------------------------------------------------------------------*/
674
675#include "ADS/priority/impl/BucketQueue.tpp"
676
677/*--------------------------------------------------------------------------*/
678
679#endif /* BucketQueue.h included */
680
681/*--------------------------------------------------------------------------*/
682/*------------------------- End File BucketQueue.h -------------------------*/
683/*--------------------------------------------------------------------------*/
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...
std::size_t size(void) const
the number of the pairs
Definition BucketQueue.h:658
E top(void) const
the least pair
Definition BucketQueue.h:642
BucketHeap(const C &=C())
the empty queue
Definition BucketQueue.h:633
E pop(void)
removes the least pair and returns it
Definition BucketQueue.h:649
void push(E &&e)
pushes e
Definition BucketQueue.h:636
bool empty(void) const
whether the queue has no pair
Definition BucketQueue.h:655
Definition BucketQueue.h:420
const_iterator operator++(int)
on the next item, returning the iterator before
Definition BucketQueue.h:451
bool operator==(const const_iterator &o) const
whether on the same item, or both at the end
Definition BucketQueue.h:458
std::ptrdiff_t difference_type
the distances
Definition BucketQueue.h:424
const_iterator & operator++(void)
on the next item in the order of the extractions
Definition BucketQueue.h:442
reference operator*(void) const
the item
Definition BucketQueue.h:437
const_iterator(const BucketQueue *q, std::size_t f)
on the first item of the layers from of the queue q
Definition BucketQueue.h:431
const T * pointer
a constant pointer to an item
Definition BucketQueue.h:426
const T & reference
a constant reference to an item
Definition BucketQueue.h:425
std::forward_iterator_tag iterator_category
forward
Definition BucketQueue.h:422
T value_type
the type of the items
Definition BucketQueue.h:423
pointer operator->(void) const
the address of the item
Definition BucketQueue.h:439
const_iterator(void)=default
the singular iterator
a two-level bucket queue: least first, then largest (or, by the policy Tie, least) ,...
Definition BucketQueue.h:171
T pop(void)
removes top() and returns it
Entry popEntry(void)
removes top() and returns it with its keys and , finding the top once instead of three times through...
const_iterator cbegin(void) const
begin()
Definition BucketQueue.h:509
T value_type
the type of the items
Definition BucketQueue.h:493
K Key
the type of the keys
Definition BucketQueue.h:176
static constexpr std::size_t R
the runs of that a layer keeps sparse, at most
Definition BucketQueue.h:363
mod::list< mod::Inserted< T, K, K >, mod::Erased< T, K, K >, mod::Cleared, mod::Rebuilt > modifications
the events: an item inserted or erased with its keys and , the queue emptied or rebuilt
Definition BucketQueue.h:181
int attach(O &o)
attaches the observer and gives its number
Definition BucketQueue.h:345
T Item
the type of the items
Definition BucketQueue.h:175
static constexpr std::size_t D
the below which the lists of a layer are always dense
Definition BucketQueue.h:360
const T & top(void)
an item of least and, among those, of largest (of least with LeastSecondKey), the last pushed
void clear(void)
removes all the items and frees the layers
TopEntry topEntry(void)
top() with its keys and , finding the top once instead of three times through top(),...
BucketQueue(void)=default
the empty queue
const_iterator cend(void) const
end()
Definition BucketQueue.h:511
Subject & subject(void)
the subject, for ADS::batch() and ADS::quiet()
Definition BucketQueue.h:338
typename Policy::template bind< modifications, BucketQueue > Subject
the subject
Definition BucketQueue.h:186
K topG(void)
the of top(), i.e., the largest (by LeastSecondKey, the least) of the least
bool empty(void) const
whether the queue has no item
const T & const_reference
a constant reference
Definition BucketQueue.h:497
K topF(void)
the of top(), i.e., the least in the queue
std::size_t size(void) const
the number of items, the stale ones included
void extract(K theta, std::vector< T > &out)
every item of first key at most appended to out, in the order of pop(), each extracted as by it
const T & reference
a constant reference
Definition BucketQueue.h:496
const Subject & subject(void) const
the subject
Definition BucketQueue.h:340
void push(const T &x, K f, K g)
pushes with the keys and
BucketQueue(O &... o)
the empty queue, the static observers o...
Definition BucketQueue.h:195
Tie Ties
the order among the items of one f
Definition BucketQueue.h:177
std::ptrdiff_t difference_type
the distances of iterators
Definition BucketQueue.h:495
const_iterator begin(void) const
on the item of top()
Definition BucketQueue.h:501
void detach(int id)
detaches the observer number
Definition BucketQueue.h:353
const_iterator end(void) const
past the last item
Definition BucketQueue.h:505
std::size_t size_type
the type of the sizes
Definition BucketQueue.h:494
const_iterator iterator
the same as const_iterator
Definition BucketQueue.h:498
a policy of the ties of BucketQueue: whether the least comes first
Definition BucketQueue.h:158
#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
constexpr int NIL
no node
Definition Visit.h:128
constexpr std::uint32_t keyG(std::uint64_t k) noexcept
the of a key of packFG()
Definition BucketQueue.h:607
constexpr std::uint64_t packFG(std::uint32_t f, std::uint32_t g) noexcept
the key of 64 bits of the pair , in the high half and the complement of in the low one,...
Definition BucketQueue.h:594
constexpr std::uint32_t keyF(std::uint64_t k) noexcept
the of a key of packFG()
Definition BucketQueue.h:601
Definition ADT.h:96
an item with its keys
Definition BucketQueue.h:296
K f
its first key
Definition BucketQueue.h:298
K g
its second key
Definition BucketQueue.h:299
T item
the item
Definition BucketQueue.h:297
the item of top() with its keys, read in place
Definition BucketQueue.h:310
K f
its first key
Definition BucketQueue.h:312
const T & item
the item, valid up to the next change of the queue
Definition BucketQueue.h:311
K g
its second key
Definition BucketQueue.h:313
the order of BucketQueue among the items of one : the largest first (A*, toward the deeper nodes),...
Definition BucketQueue.h:143
static constexpr bool least
not the least g first
Definition BucketQueue.h:144
the order of BucketQueue among the items of one : the least first, i.e., the lexicographic order of ...
Definition BucketQueue.h:151
static constexpr bool least
the least g first
Definition BucketQueue.h:152
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