108#ifndef __ADS_BucketQueue
109 #define __ADS_BucketQueue
125#include <type_traits>
144 static constexpr bool least =
false;
159 { P::least } -> std::convertible_to< bool >;
186 using Subject =
typename Policy::template bind< modifications , BucketQueue >;
192 template<
class... O >
193 requires( (
sizeof...( O ) > 0 ) &&
194 ( ! std::is_base_of_v< BucketQueue , O > && ... ) )
232 void push(
const T & x , K f , K g );
258 [[nodiscard]]
const T &
top(
void );
323 [[nodiscard]]
bool empty(
void )
const;
326 [[nodiscard]] std::size_t
size(
void )
const;
333 void extract( K theta , std::vector< T > & out );
346 requires( Subject::dynamic )
348 return( f_sub.attach( o ) );
354 requires( Subject::dynamic )
360 static constexpr std::size_t
D = 256;
363 static constexpr std::size_t
R = 64;
367 using Index = std::uint32_t;
368 static constexpr Index NIL = ~Index( 0 );
369 static constexpr std::size_t S = 4;
385 using Runs = std::vector< Run >;
395 std::vector< Index > big;
397 Index & head( std::size_t g ) {
398 return( g < S ? small[ g ] : big[ g - S ] );
400 Index head( std::size_t g )
const {
401 return( g < S ? small[ g ] : g - S < big.size() ? big[ g - S ] :
NIL );
404 std::size_t lists(
void )
const {
return( S + big.size() ); }
406 bool dense(
void )
const {
return( big.size() >
D - S ); }
443 f_c = f_q->f_cells[ f_c ].next;
459 return( ( f_f == o.f_f ) && ( f_g == o.f_g ) && ( f_c == o.f_c ) );
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();
472 for( ; f_g < r + l.lists() ; ++f_g ) {
473 if constexpr( Tie::least )
474 f_c = f_g < l.lists()
476 : f_q->f_runs[ l.runs ][ r + l.lists() - 1 - f_g ].head;
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 );
489 std::size_t f_f = 0 , f_g = 0;
519 static constexpr bool heard = Subject::template listened< E >;
522 void pushPlain(
const T & x , K f , K g );
531 Layer & front(
void );
534 [[nodiscard]]
static bool inRuns(
const Layer & l );
537 Index &
top( Layer & l );
540 [[nodiscard]] K topKey(
const Layer & l )
const;
543 Index cell(
const T & x );
547 bool pushRun( Layer & l ,
const T & x , K g );
551 static Index & run( Runs & v , K g );
554 T popRun( Layer & l );
560 void taken( Layer & l );
563 void densify( Layer & l );
566 void release( Layer & l );
569 [[nodiscard]]
bool counted(
void )
const;
573 [[nodiscard]]
bool ordered(
const Layer & l )
const;
575 std::vector< Layer > f_layers;
576 std::vector< Cell > f_cells;
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;
594[[nodiscard]]
constexpr std::uint64_t
packFG( std::uint32_t f ,
595 std::uint32_t g )
noexcept
597 return( ( std::uint64_t( f ) << 32 ) | std::uint32_t( ~g ) );
601[[nodiscard]]
constexpr std::uint32_t
keyF( std::uint64_t k )
noexcept
603 return( std::uint32_t( k >> 32 ) );
607[[nodiscard]]
constexpr std::uint32_t
keyG( std::uint64_t k )
noexcept
609 return( ~std::uint32_t( k ) );
624template<
class E ,
class C >
628 using Item =
decltype( E::item );
637 f_q.push( std::move( e.item ) ,
keyF( e.key ) ,
keyG( e.key ) );
642 [[nodiscard]] E
top(
void )
const {
643 const auto t = f_q.topEntry();
644 return( E{
packFG( t.f , t.g ) , t.item } );
650 auto e = f_q.popEntry();
651 return( E{
packFG( e.f , e.g ) , std::move( e.item ) } );
655 [[nodiscard]]
bool empty(
void )
const {
return( f_q.empty() ); }
658 [[nodiscard]] std::size_t
size(
void )
const {
return( f_q.size() ); }
675#include "ADS/priority/impl/BucketQueue.tpp"
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
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