ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
IndexedDaryHeap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File IndexedDaryHeap.h -------------------------*/
3/*--------------------------------------------------------------------------*/
97/*--------------------------------------------------------------------------*/
98/*----------------------------- DEFINITIONS --------------------------------*/
99/*--------------------------------------------------------------------------*/
100
101#ifndef __ADS_IndexedDaryHeap
102 #define __ADS_IndexedDaryHeap
103/* self-identification: #endif constant */
104
105/*--------------------------------------------------------------------------*/
106/*------------------------------ INCLUDES ----------------------------------*/
107/*--------------------------------------------------------------------------*/
108
109#include "ADS/Contract.h"
110#include "ADS/Modification.h"
112#include "ADS/Portable.h"
113
114#include <algorithm>
115#include <cstddef>
116#include <type_traits>
117#include <utility>
118#include <vector>
119
120/*--------------------------------------------------------------------------*/
121/*--------------------------- NAMESPACE ------------------------------------*/
122/*--------------------------------------------------------------------------*/
123
124namespace ADS
125{
126
127/*--------------------------------------------------------------------------*/
128/*------------------------- CLASS IndexedDaryHeap --------------------------*/
129/*--------------------------------------------------------------------------*/
130
133template< class K , unsigned D = 4 , class Policy = NoObservers ,
134 class A = std::allocator< K > >
136{
137
138 static_assert( D >= 2 , "a heap needs at least two children per node" );
139
140/*--------------------------------------------------------------------------*/
141 public:
142 using Key = K;
143 using Item = int;
144 using Priority = K;
145 using Handle = int;
146
148 struct Entry
149 {
150 K key;
151 int id;
152 };
153
161 using Subject =
162 typename Policy::template bind< modifications , IndexedDaryHeap >;
163
167 template< class... O >
168 explicit IndexedDaryHeap( int n = 0 , O &... o );
169
173 template< class... O >
174 IndexedDaryHeap( std::allocator_arg_t , const A & alloc , int n = 0 ,
175 O &... o );
176
178 using allocator_type = A;
180 [[nodiscard]] allocator_type get_allocator( void ) const {
181 return( allocator_type( f_A.get_allocator() ) );
182 }
183
186 void reset( int n );
187
194 void grow( int n );
195
197 [[nodiscard]] int ids( void ) const { return( int( f_pos.size() ) ); }
198
200 [[nodiscard]] bool empty( void ) const { return( f_A.empty() ); }
201
203 [[nodiscard]] std::size_t size( void ) const { return( f_A.size() ); }
204
207 [[nodiscard]] bool contains( int id ) const;
208
211 [[nodiscard]] const K & key( int id ) const;
212
233 void push( int id , const K & k );
234
247 void decrease( int id , const K & k );
248
262 void increase( int id , const K & k );
263
267 void update( int id , const K & k );
268
288 void erase( int id );
289
294 bool pushOrDecrease( int id , const K & k );
295
298 [[nodiscard]] const Entry & top( void ) const;
299
322 Entry pop( void );
323
325 void clear( void );
326
330 void extract( const K & theta , std::vector< int > & out );
331
347 template< class F >
348 void rekey( F && f );
349
353 Handle insert( int id , const K & k ) {
354 push( id , k );
355 return( id );
356 }
357
360 [[nodiscard]] int min( void ) const { return( top().id ); }
361
364 int deleteMin( void ) { return( pop().id ); }
365
366/*--------------------------- THE OBSERVERS --------------------------------*/
367
369 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
371 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
372
375 template< class O >
376 int attach( O & o )
377 requires( Subject::dynamic )
378 {
379 return( f_sub.attach( o ) );
380 }
381
384 void detach( int id )
385 requires( Subject::dynamic )
386 {
387 f_sub.detach( id );
388 }
389
390/*--------------------------------------------------------------------------*/
391 private:
392 using Ins = mod::Inserted< int , K >;
393 using Era = mod::Erased< int , K >;
394 using Chg = mod::KeyChanged< int , K >;
396
398 template< class... E >
399 static constexpr bool heard = ( Subject::template listened< E > || ... );
400
402 template< class... E >
403 [[nodiscard]] bool alive( void ) const {
404 return( ( f_sub.template live< E >() || ... ) );
405 }
406
410 template< bool T = false >
411 void up( std::size_t i , Entry x , std::size_t from = npos );
412
414 template< bool T = false >
415 void down( std::size_t i , Entry x , std::size_t from = npos );
416
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 );
423
425 ADS_COLD_PATH void pushCold( int id , const K & k ) { pushTold( id , k ); }
426 ADS_COLD_PATH void decreaseCold( int id , const K & k ) {
427 decreaseTold( id , k );
428 }
429 ADS_COLD_PATH void increaseCold( int id , const K & k ) {
430 increaseTold( id , k );
431 }
432 ADS_COLD_PATH void eraseCold( int id ) { eraseTold( id ); }
433 ADS_COLD_PATH Entry popCold( void ) { return( popTold() ); }
434
436 static constexpr std::size_t npos = std::size_t( -1 );
437
440 [[nodiscard]] bool ordered( void ) const;
441
442 detail::AllocVector< A , Entry > f_A;
443 detail::AllocVector< A , int > f_pos;
445
446 }; // end( class IndexedDaryHeap )
447
448/*--------------------------------------------------------------------------*/
449/*---------------------------- CLASS TableHeap -----------------------------*/
450/*--------------------------------------------------------------------------*/
451
474template< class T , unsigned D = 4 >
476{
477
478/*--------------------------------------------------------------------------*/
479 public:
481 using Key = std::remove_cvref_t<
482 decltype( std::declval< const T & >()[ std::size_t() ] ) >;
483 using Item = int;
484 using Priority = Key;
485 using Handle = int;
486
490 explicit TableHeap( const T & t , int n = 0 ) : f_t( &t ) , f_h( n ) { }
491
495 void update( int id ) {
496 ADS_PRE( ( id >= 0 ) && ( std::size_t( id ) < f_t->size() ) );
497 if( id >= f_h.ids() )
498 f_h.grow( id + 1 );
499 const Key k = ( *f_t )[ std::size_t( id ) ];
500 if( f_h.contains( id ) )
501 f_h.update( id , k );
502 else
503 f_h.push( id , k );
504 }
505
507 void erase( int id ) {
508 if( contains( id ) )
509 f_h.erase( id );
510 }
511
513 [[nodiscard]] bool contains( int id ) const {
514 return( ( id >= 0 ) && ( id < f_h.ids() ) && f_h.contains( id ) );
515 }
516
518 [[nodiscard]] bool empty( void ) const { return( f_h.empty() ); }
519
521 [[nodiscard]] std::size_t size( void ) const { return( f_h.size() ); }
522
525 [[nodiscard]] const Key & key( int id ) const { return( f_h.key( id ) ); }
526
529 [[nodiscard]] int min( void ) const { return( f_h.min() ); }
530
533 int deleteMin( void ) { return( f_h.deleteMin() ); }
534
537 void extract( const Key & theta , std::vector< int > & out ) {
538 f_h.extract( theta , out );
539 }
540
542 void clear( void ) { f_h.clear(); }
543
546 void refresh( void ) {
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 );
551 for( int id : gone )
552 f_h.erase( id );
553 f_h.rekey(
554 [ this ]( int id ) -> Key { return( ( *f_t )[ std::size_t( id ) ] ); } );
555 }
556
558 [[nodiscard]] const IndexedDaryHeap< Key , D > & heap( void ) const {
559 return( f_h );
560 }
561
562/*------------------------- AS AN OBSERVER OF THE TABLE --------------------*/
563
565 template< class H , class V >
567 update( int( e.h ) );
568 }
569
571 template< class H , class K , class V >
572 void on( const mod::Inserted< H , K , V > & e ) {
573 update( int( e.h ) );
574 }
575
577 template< class H , class K , class V >
578 void on( const mod::Erased< H , K , V > & e ) noexcept {
579 erase( int( e.h ) );
580 }
581
583 void on( const mod::Cleared & ) noexcept { f_h.clear(); }
584
586 void on( const mod::Rebuilt & ) noexcept { refresh(); }
587
588/*--------------------------------------------------------------------------*/
589 private:
590 const T * f_t;
592
593 }; // end( class TableHeap )
594
595/*--------------------------------------------------------------------------*/
596
597} // end( namespace ADS )
598
599/*--------------------------------------------------------------------------*/
600/*-------------------------- IMPLEMENTATION --------------------------------*/
601/*--------------------------------------------------------------------------*/
602
603#include "ADS/priority/impl/IndexedDaryHeap.tpp"
604
605/*--------------------------------------------------------------------------*/
606
607#endif /* IndexedDaryHeap.h included */
608
609/*--------------------------------------------------------------------------*/
610/*----------------------- End File IndexedDaryHeap.h -----------------------*/
611/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96
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