ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Heaps.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File Heaps.h -------------------------------*/
3/*--------------------------------------------------------------------------*/
71/*--------------------------------------------------------------------------*/
72/*----------------------------- DEFINITIONS --------------------------------*/
73/*--------------------------------------------------------------------------*/
74
75#ifndef __ADS_interop_lemon_Heaps
76 #define __ADS_interop_lemon_Heaps
77/* self-identification: #endif constant */
78
79/*--------------------------------------------------------------------------*/
80/*------------------------------ INCLUDES ----------------------------------*/
81/*--------------------------------------------------------------------------*/
82
83#if ! __has_include( <lemon/core.h> )
84 #error "the interoperation with LEMON needs the headers of LEMON"
85#endif
86
87#include "ADS/Contract.h"
88#include "ADS/Modification.h"
90
91#include <cstddef>
92#include <functional>
93#include <type_traits>
94#include <utility>
95#include <vector>
96
97/*--------------------------------------------------------------------------*/
98/*--------------------------- NAMESPACE ------------------------------------*/
99/*--------------------------------------------------------------------------*/
100
101namespace ADS::lemon_interop
102{
103
104/*--------------------------------------------------------------------------*/
105/*---------------------------- CLASS CmpKey --------------------------------*/
106/*--------------------------------------------------------------------------*/
107
109template< class PR , class CMP >
110struct CmpKey
111{
112 PR p;
114 friend bool operator<( const CmpKey & a , const CmpKey & b ) {
115 return( CMP()( a.p , b.p ) );
116 }
117 };
118
119/*--------------------------------------------------------------------------*/
120/*---------------------------- CLASS DaryHeap ------------------------------*/
121/*--------------------------------------------------------------------------*/
122
125template< class PR , class IM , class CMP = std::less< PR > , unsigned D = 4 ,
126 class Policy = NoObservers >
128{
129
130 static_assert( std::is_empty_v< CMP > , "the comparator is stateless" );
131
132/*--------------------------------------------------------------------------*/
133 public:
134 using ItemIntMap = IM;
135 using Prio = PR;
136 using Item = typename IM::Key;
137 using Pair = std::pair< Item , Prio >;
140
142 enum State { IN_HEAP = 0 , PRE_HEAP = -1 , POST_HEAP = -2 };
143
145 explicit DaryHeap( ItemIntMap & m ) : f_iim( m ) { }
146
148 DaryHeap( ItemIntMap & m , const CMP & ) : f_iim( m ) { }
149
151 [[nodiscard]] int size( void ) const { return( int( f_q.size() ) ); }
152
154 [[nodiscard]] bool empty( void ) const { return( f_q.empty() ); }
155
157 void clear( void ) {
158 f_q.reset( 0 );
159 f_item.clear();
160 f_free.clear();
161 }
162
165 void push( const Item & i , const Prio & p );
166
169 void push( const Pair & x ) { push( x.first , x.second ); }
170
173 [[nodiscard]] Item top( void ) const { return( f_item[ f_q.top().id ] ); }
174
177 [[nodiscard]] Prio prio( void ) const { return( f_q.top().key.p ); }
178
181 void pop( void ) { release( f_q.pop().id ); }
182
184 void erase( const Item & i ) {
185 const int s = f_iim[ i ];
186 if( s >= 0 ) {
187 f_q.erase( s );
188 release( s );
189 }
190 }
191
194 [[nodiscard]] Prio operator[]( const Item & i ) const {
195 return( f_q.key( f_iim[ i ] ).p );
196 }
197
199 void set( const Item & i , const Prio & p ) {
200 if( state( i ) == IN_HEAP )
201 f_q.update( f_iim[ i ] , Key{ p } );
202 else
203 push( i , p );
204 }
205
208 void decrease( const Item & i , const Prio & p ) {
209 f_q.decrease( f_iim[ i ] , Key{ p } );
210 }
211
214 void increase( const Item & i , const Prio & p ) {
215 f_q.increase( f_iim[ i ] , Key{ p } );
216 }
217
219 [[nodiscard]] State state( const Item & i ) const {
220 const int s = f_iim[ i ];
221 return( s >= 0 ? IN_HEAP : State( s ) );
222 }
223
226 void state( const Item & i , State st ) {
227 if( st == IN_HEAP )
228 return;
229 if( state( i ) == IN_HEAP )
230 erase( i );
231 f_iim.set( i , int( st ) );
232 }
233
236 [[nodiscard]] Heap & heap( void ) { return( f_q ); }
238 [[nodiscard]] const Heap & heap( void ) const { return( f_q ); }
239
241 [[nodiscard]] typename Heap::Subject & subject( void ) {
242 return( f_q.subject() );
243 }
244
247 [[nodiscard]] const Item & item( int s ) const {
248 ADS_PRE( ( s >= 0 ) && ( std::size_t( s ) < f_item.size() ) );
249 return( f_item[ s ] );
250 }
251
252/*--------------------------------------------------------------------------*/
253 private:
255 void release( int s ) {
256 f_iim.set( f_item[ s ] , int( POST_HEAP ) );
257 f_free.push_back( s );
258 }
259
260 ItemIntMap & f_iim;
261 Heap f_q;
262 std::vector< Item > f_item;
263 std::vector< int > f_free;
264
265 }; // end( class DaryHeap )
266
267template< class PR , class IM , class CMP , unsigned D , class Policy >
269 const Prio & p )
270{
271 int s;
272 if( f_free.empty() ) {
273 s = int( f_item.size() );
274 f_item.push_back( i );
275 f_q.grow( s + 1 );
276 }
277 else {
278 s = f_free.back();
279 f_free.pop_back();
280 f_item[ s ] = i;
281 }
282 f_iim.set( i , s );
283 f_q.push( s , Key{ p } );
284 }
285
286/*--------------------------------------------------------------------------*/
287/*-------------------------- CLASS MonotoneHeap ----------------------------*/
288/*--------------------------------------------------------------------------*/
289
293template< class Q , class PR , class IM >
295{
296
297 static_assert( std::is_integral_v< PR > , "integer priorities" );
298 static_assert( std::is_same_v< typename Q::Item , int > ,
299 "a queue of the slots, int" );
300
301/*--------------------------------------------------------------------------*/
302 public:
303 using ItemIntMap = IM;
304 using Prio = PR;
305 using Item = typename IM::Key;
306 using Pair = std::pair< Item , Prio >;
307
309 enum State { IN_HEAP = 0 , PRE_HEAP = -1 , POST_HEAP = -2 };
310
312 explicit MonotoneHeap( ItemIntMap & m ) : f_iim( m ) { }
313
316 template< class C >
317 MonotoneHeap( ItemIntMap & m , const C & ) : f_iim( m ) { }
318
320 [[nodiscard]] int size( void ) const { return( f_live ); }
321
323 [[nodiscard]] bool empty( void ) const { return( f_live == 0 ); }
324
326 void clear( void ) {
327 f_q.clear();
328 f_item.clear();
329 f_prio.clear();
330 f_in.clear();
331 f_live = 0;
332 }
333
337 void push( const Item & i , const Prio & p ) {
338 ADS_PRE( p >= Prio( 0 ) );
339 const int s = int( f_item.size() );
340 f_item.push_back( i );
341 f_prio.push_back( p );
342 f_in.push_back( 1 );
343 f_iim.set( i , s );
344 ++f_live;
345 f_q.push( s , Key( p ) );
346 }
347
350 void push( const Pair & x ) { push( x.first , x.second ); }
351
354 [[nodiscard]] Item top( void ) const {
355 skip();
356 return( f_item[ f_q.top() ] );
357 }
358
361 [[nodiscard]] Prio prio( void ) const {
362 skip();
363 return( f_prio[ f_q.top() ] );
364 }
365
368 void pop( void ) {
369 skip();
370 const int s = f_q.pop();
371 f_iim.set( f_item[ s ] , int( POST_HEAP ) );
372 f_in[ s ] = 0;
373 --f_live;
374 }
375
378 [[nodiscard]] Prio operator[]( const Item & i ) const {
379 return( f_prio[ f_iim[ i ] ] );
380 }
381
386 void decrease( const Item & i , const Prio & p ) {
387 const int s = f_iim[ i ];
388 ADS_PRE( s >= 0 );
389 f_prio[ s ] = p;
390 f_q.push( s , Key( p ) );
391 }
392
395 void increase( const Item & i , const Prio & p ) { decrease( i , p ); }
396
398 void erase( const Item & i ) {
399 const int s = f_iim[ i ];
400 if( s < 0 )
401 return;
402 f_iim.set( i , int( POST_HEAP ) );
403 f_in[ s ] = 0;
404 --f_live;
405 }
406
408 void set( const Item & i , const Prio & p ) {
409 if( state( i ) == IN_HEAP )
410 decrease( i , p );
411 else
412 push( i , p );
413 }
414
416 [[nodiscard]] State state( const Item & i ) const {
417 const int s = f_iim[ i ];
418 return( s >= 0 ? IN_HEAP : State( s ) );
419 }
420
423 void state( const Item & i , State st ) {
424 if( st == IN_HEAP )
425 return;
426 if( state( i ) == IN_HEAP )
427 erase( i );
428 f_iim.set( i , int( st ) );
429 }
430
431/*--------------------------------------------------------------------------*/
432 private:
433 using Key = typename Q::Key;
434
437 void skip( void ) const {
438 while( ! f_q.empty() ) {
439 const int s = f_q.top();
440 if( f_in[ s ] && ( Key( f_prio[ s ] ) == f_q.topKey() ) )
441 return;
442 f_q.pop();
443 }
444 }
445
446 ItemIntMap & f_iim;
447 mutable Q f_q;
448 std::vector< Item > f_item;
449 std::vector< Prio > f_prio;
450 std::vector< char > f_in;
451 int f_live = 0;
452
453 }; // end( class MonotoneHeap )
454
455/*--------------------------------------------------------------------------*/
456
457} // end( namespace ADS::lemon_interop )
458
459/*--------------------------------------------------------------------------*/
460
461#endif /* Heaps.h included */
462
463/*--------------------------------------------------------------------------*/
464/*---------------------------- End File Heaps.h ----------------------------*/
465/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A -ary heap of the integers with keys and decrease-key, beyond the book: the heap of DaryHeap [see D...
The typed modifications, beyond the book: the vocabulary by which a structure tells the observers att...
a -ary heap of the ids by their keys, the least on top, with decrease-key, its changes told through ...
Definition IndexedDaryHeap.h:136
void push(int id, const K &k)
inserts the id with the key , in time
typename Policy::template bind< modifications, IndexedDaryHeap > Subject
Definition IndexedDaryHeap.h:161
Heap::Subject & subject(void)
the subject of the heap of ADS
Definition Heaps.h:241
bool empty(void) const
whether there is no item
Definition Heaps.h:154
void decrease(const Item &i, const Prio &p)
the priority of lowered to
Definition Heaps.h:208
IM ItemIntMap
the cross-reference map
Definition Heaps.h:134
DaryHeap(ItemIntMap &m)
the empty heap on the map
Definition Heaps.h:145
void push(const Pair &x)
inserts the pair
Definition Heaps.h:169
Prio operator[](const Item &i) const
the priority of the item
Definition Heaps.h:194
std::pair< Item, Prio > Pair
an item with its priority
Definition Heaps.h:137
void set(const Item &i, const Prio &p)
the priority of set to , inserting it if needed
Definition Heaps.h:199
typename IM::Key Item
the items
Definition Heaps.h:136
State
the states of an item, those of LEMON
Definition Heaps.h:142
@ IN_HEAP
Definition Heaps.h:142
@ PRE_HEAP
Definition Heaps.h:142
@ POST_HEAP
Definition Heaps.h:142
int size(void) const
the number of items
Definition Heaps.h:151
Heap & heap(void)
the heap of ADS, whose subject tells the changes, the slots their handles
Definition Heaps.h:236
void increase(const Item &i, const Prio &p)
the priority of raised to
Definition Heaps.h:214
CmpKey< PR, CMP > Key
the keys of the heap of ADS
Definition Heaps.h:138
void clear(void)
all the items removed, the map left as it is (as in LEMON)
Definition Heaps.h:157
const Item & item(int s) const
the item of the slot , the handle of the events
Definition Heaps.h:247
void pop(void)
removes the item of least priority, POST_HEAP from now on
Definition Heaps.h:181
Item top(void) const
the item of least priority
Definition Heaps.h:173
const Heap & heap(void) const
the heap of ADS
Definition Heaps.h:238
void erase(const Item &i)
removes the item , if it is in the heap
Definition Heaps.h:184
void push(const Item &i, const Prio &p)
inserts the item with the priority , in a free slot
Definition Heaps.h:268
DaryHeap(ItemIntMap &m, const CMP &)
the empty heap on the map , the comparator not kept
Definition Heaps.h:148
State state(const Item &i) const
the state of the item , read from the map
Definition Heaps.h:219
void state(const Item &i, State st)
the state of set to , removing it from the heap if it was there; IN_HEAP changes nothing
Definition Heaps.h:226
Prio prio(void) const
the least priority
Definition Heaps.h:177
IndexedDaryHeap< Key, D, Policy > Heap
the heap of ADS
Definition Heaps.h:139
PR Prio
the priorities
Definition Heaps.h:135
void clear(void)
all the items removed, the map left as it is
Definition Heaps.h:326
Item top(void) const
the item of least priority
Definition Heaps.h:354
Prio operator[](const Item &i) const
the priority of the item
Definition Heaps.h:378
void push(const Item &i, const Prio &p)
inserts with the priority , in a new slot
Definition Heaps.h:337
Prio prio(void) const
the least priority
Definition Heaps.h:361
typename IM::Key Item
the items
Definition Heaps.h:305
void state(const Item &i, State st)
the state of set to , removing it from the heap if it was there; IN_HEAP changes nothing
Definition Heaps.h:423
State
the states of an item, those of LEMON
Definition Heaps.h:309
@ IN_HEAP
Definition Heaps.h:309
@ PRE_HEAP
Definition Heaps.h:309
@ POST_HEAP
Definition Heaps.h:309
void set(const Item &i, const Prio &p)
the priority of set to , inserting it if needed
Definition Heaps.h:408
MonotoneHeap(ItemIntMap &m)
the empty heap on the map
Definition Heaps.h:312
bool empty(void) const
whether there is no item
Definition Heaps.h:323
void pop(void)
removes the item of least priority, POST_HEAP from now on
Definition Heaps.h:368
PR Prio
the priorities
Definition Heaps.h:304
IM ItemIntMap
the cross-reference map
Definition Heaps.h:303
void erase(const Item &i)
removes the item , if it is in the heap; its copies go stale
Definition Heaps.h:398
State state(const Item &i) const
the state of the item , read from the map
Definition Heaps.h:416
void decrease(const Item &i, const Prio &p)
the priority of set to by a new copy, the old one stale
Definition Heaps.h:386
std::pair< Item, Prio > Pair
an item with its priority
Definition Heaps.h:306
int size(void) const
the number of items
Definition Heaps.h:320
void push(const Pair &x)
inserts the pair
Definition Heaps.h:350
MonotoneHeap(ItemIntMap &m, const C &)
the empty heap on the map , the comparator, which must be std::less, not kept
Definition Heaps.h:317
void increase(const Item &i, const Prio &p)
as decrease(): the copy of the smaller priority goes stale
Definition Heaps.h:395
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition Digraphs.h:89
a priority of LEMON as a key of ADS, ordered by the comparator CMP
Definition Heaps.h:111
PR p
the priority
Definition Heaps.h:112
friend bool operator<(const CmpKey &a, const CmpKey &b)
whether comes before
Definition Heaps.h:114