101#ifndef __ADS_IndexedDaryHeap
102 #define __ADS_IndexedDaryHeap
116#include <type_traits>
133template<
class K ,
unsigned D = 4 ,
class Policy =
NoObservers ,
134 class A = std::allocator< K > >
138 static_assert( D >= 2 ,
"a heap needs at least two children per node" );
162 typename Policy::template bind< modifications , IndexedDaryHeap >;
167 template<
class... O >
173 template<
class... O >
197 [[nodiscard]]
int ids(
void )
const {
return(
int( f_pos.size() ) ); }
200 [[nodiscard]]
bool empty(
void )
const {
return( f_A.empty() ); }
203 [[nodiscard]] std::size_t
size(
void )
const {
return( f_A.size() ); }
211 [[nodiscard]]
const K &
key(
int id )
const;
233 void push(
int id ,
const K & k );
298 [[nodiscard]]
const Entry &
top(
void )
const;
330 void extract(
const K & theta , std::vector< int > & out );
360 [[nodiscard]]
int min(
void )
const {
return(
top().
id ); }
377 requires( Subject::dynamic )
379 return( f_sub.attach( o ) );
385 requires( Subject::dynamic )
398 template<
class... E >
399 static constexpr bool heard = ( Subject::template listened< E > || ... );
402 template<
class... E >
403 [[nodiscard]]
bool alive(
void )
const {
404 return( ( f_sub.template live< E >() || ... ) );
410 template<
bool T = false >
411 void up( std::size_t i , Entry x , std::size_t from = npos );
414 template<
bool T = false >
415 void down( std::size_t i , Entry x , std::size_t from = npos );
418 void pushTold(
int id ,
const K & k );
419 void decreaseTold(
int id ,
const K & k );
420 void increaseTold(
int id ,
const K & k );
421 void eraseTold(
int id );
422 Entry popTold(
void );
425 ADS_COLD_PATH void pushCold(
int id ,
const K & k ) { pushTold(
id , k ); }
427 decreaseTold(
id , k );
430 increaseTold(
id , k );
436 static constexpr std::size_t npos = std::size_t( -1 );
440 [[nodiscard]]
bool ordered(
void )
const;
442 detail::AllocVector< A , Entry > f_A;
443 detail::AllocVector< A , int > f_pos;
474template<
class T ,
unsigned D = 4 >
481 using Key = std::remove_cvref_t<
482 decltype( std::declval< const T & >()[ std::size_t() ] ) >;
490 explicit TableHeap(
const T & t ,
int n = 0 ) : f_t( &t ) , f_h( n ) { }
496 ADS_PRE( (
id >= 0 ) && ( std::size_t(
id ) < f_t->size() ) );
497 if(
id >= f_h.ids() )
499 const Key k = ( *f_t )[ std::size_t(
id ) ];
500 if( f_h.contains(
id ) )
501 f_h.update(
id , k );
514 return( (
id >= 0 ) && (
id < f_h.ids() ) && f_h.contains(
id ) );
518 [[nodiscard]]
bool empty(
void )
const {
return( f_h.empty() ); }
521 [[nodiscard]] std::size_t
size(
void )
const {
return( f_h.size() ); }
525 [[nodiscard]]
const Key &
key(
int id )
const {
return( f_h.key(
id ) ); }
529 [[nodiscard]]
int min(
void )
const {
return( f_h.min() ); }
537 void extract(
const Key & theta , std::vector< int > & out ) {
538 f_h.extract( theta , out );
547 std::vector< int > gone;
548 for(
int id =
int( f_t->size() ) ;
id < f_h.ids() ; ++
id )
549 if( f_h.contains(
id ) )
550 gone.push_back(
id );
554 [
this ](
int id ) ->
Key {
return( ( *f_t )[ std::size_t(
id ) ] ); } );
565 template<
class H ,
class V >
571 template<
class H ,
class K ,
class V >
577 template<
class H ,
class K ,
class V >
603#include "ADS/priority/impl/IndexedDaryHeap.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...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
a -ary heap of the ids by their keys, the least on top, with decrease-key, its changes told through ...
Definition IndexedDaryHeap.h:136
int min(void) const
the id of least key, as the min() of PRIORITYQUEUE
Definition IndexedDaryHeap.h:360
bool contains(int id) const
whether the id is in the heap
void push(int id, const K &k)
inserts the id with the key , in time
void extract(const K &theta, std::vector< int > &out)
every id of key at most appended to out, by increasing keys, each removed as by pop(),...
void erase(int id)
removes the id from the heap, in time
int attach(O &o)
attaches the observer and gives its number
Definition IndexedDaryHeap.h:376
void rekey(F &&f)
the key of each id in the heap set to f( id ), and the order of the heap made again from the bottom,...
void decrease(int id, const K &k)
lowers the key of the id to , in time
void clear(void)
removes all the items, in time
const Entry & top(void) const
the item of least key
Entry pop(void)
removes the item of least key and returns it, in time
int Handle
Definition IndexedDaryHeap.h:145
bool pushOrDecrease(int id, const K &k)
push( id , k ) if the id is not in the heap, decrease( id , k ) if is less than its key,...
void update(int id, const K &k)
sets the key of the id to , by decrease() or by increase()
std::size_t size(void) const
the number of items,
Definition IndexedDaryHeap.h:203
void grow(int n)
the ids raised to , the new ones not in the heap and the items kept, in amortized time per new id: t...
const K & key(int id) const
the key of an id in the heap
const Subject & subject(void) const
the subject
Definition IndexedDaryHeap.h:371
std::allocator< Key > allocator_type
Definition IndexedDaryHeap.h:178
IndexedDaryHeap(std::allocator_arg_t, const A &alloc, int n=0, O &... o)
the empty heap of the ids , the static observers o..., its vectors from alloc
Handle insert(int id, const K &k)
push( id , k ), the id its handle, as the insert() of PRIORITYQUEUE (section 10.1) [see PriorityQueue...
Definition IndexedDaryHeap.h:353
allocator_type get_allocator(void) const
the allocator of the vectors
Definition IndexedDaryHeap.h:180
mod::list< mod::Inserted< int, Key >, mod::Erased< int, Key >, mod::KeyChanged< int, Key >, mod::Relocated< std::size_t >, mod::Cleared, mod::Rebuilt > modifications
Definition IndexedDaryHeap.h:156
typename Policy::template bind< modifications, IndexedDaryHeap > Subject
Definition IndexedDaryHeap.h:161
int deleteMin(void)
pop(), the id of least key, as the deleteMin() of PRIORITYQUEUE
Definition IndexedDaryHeap.h:364
bool empty(void) const
whether the heap has no item
Definition IndexedDaryHeap.h:200
void increase(int id, const K &k)
raises the key of the id to , in time
int ids(void) const
the number of ids,
Definition IndexedDaryHeap.h:197
void reset(int n)
the empty heap of the ids , the room kept
Key Priority
Definition IndexedDaryHeap.h:144
Key Key
Definition IndexedDaryHeap.h:142
Subject & subject(void)
the subject, for ADS::batch() and ADS::quiet()
Definition IndexedDaryHeap.h:369
void detach(int id)
detaches the observer number
Definition IndexedDaryHeap.h:384
IndexedDaryHeap(int n=0, O &... o)
the empty heap of the ids , the static observers o...
int Item
Definition IndexedDaryHeap.h:143
void on(const mod::Inserted< H, K, V > &e)
a value of the table inserted: its id updated
Definition IndexedDaryHeap.h:572
void refresh(void)
the ids that the table no longer has taken out, and the keys of the others read again,...
Definition IndexedDaryHeap.h:546
void on(const mod::Erased< H, K, V > &e) noexcept
a value of the table erased: its id out of the queue
Definition IndexedDaryHeap.h:578
void on(const mod::Cleared &) noexcept
the table emptied: the queue too
Definition IndexedDaryHeap.h:583
Key Priority
and their priorities, the keys
Definition IndexedDaryHeap.h:484
int deleteMin(void)
removes the id of least key and returns it
Definition IndexedDaryHeap.h:533
TableHeap(const T &t, int n=0)
the empty queue of the ids of the table , which must outlive it
Definition IndexedDaryHeap.h:490
const IndexedDaryHeap< Key, D > & heap(void) const
the heap, for the reads
Definition IndexedDaryHeap.h:558
void extract(const Key &theta, std::vector< int > &out)
every id of key at most appended to out, by increasing keys, each removed
Definition IndexedDaryHeap.h:537
void update(int id)
the id in the queue with its key in the table, inserted or moved, the ids raised to if fewer
Definition IndexedDaryHeap.h:495
bool contains(int id) const
whether the id is in the queue
Definition IndexedDaryHeap.h:513
std::remove_cvref_t< decltype(std::declval< const T & >()[std::size_t()]) > Key
the type of the keys, those of the table
Definition IndexedDaryHeap.h:481
const Key & key(int id) const
the key of an id in the queue, as read last from the table
Definition IndexedDaryHeap.h:525
int Item
the items: the ids
Definition IndexedDaryHeap.h:483
int Handle
the id itself
Definition IndexedDaryHeap.h:485
void on(const mod::Rebuilt &) noexcept
the table rebuilt: refresh()
Definition IndexedDaryHeap.h:586
void erase(int id)
the id out of the queue, if it is there
Definition IndexedDaryHeap.h:507
void on(const mod::Changed< mod::Value, H, V > &e)
a value of the table written: its id updated
Definition IndexedDaryHeap.h:566
int min(void) const
the id of least key
Definition IndexedDaryHeap.h:529
std::size_t size(void) const
the number of ids in the queue
Definition IndexedDaryHeap.h:521
void clear(void)
the queue emptied
Definition IndexedDaryHeap.h:542
bool empty(void) const
whether the queue is empty
Definition IndexedDaryHeap.h:518
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Changed< Key, H, K > KeyChanged
the key of an item changed
Definition Modification.h:627
#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
an item: an id with its key
Definition IndexedDaryHeap.h:149
int id
the id
Definition IndexedDaryHeap.h:151
K key
the key
Definition IndexedDaryHeap.h:150
nobody listens: the subject is empty and every test is false at compile time, so that the structure c...
Definition Modification.h:1958
the attribute A of the item of handle changed from from to to
Definition Modification.h:600
H h
< the value
Definition Modification.h:606
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
H h
< the value
Definition Modification.h:542
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