75#ifndef __ADS_interop_lemon_Heaps
76 #define __ADS_interop_lemon_Heaps
83#if ! __has_include( <lemon/core.h> )
84 #error "the interoperation with LEMON needs the headers of LEMON"
109template<
class PR ,
class CMP >
115 return( CMP()( a.
p , b.
p ) );
125template<
class PR ,
class IM ,
class CMP = std::less< PR > ,
unsigned D = 4 ,
126 class Policy = NoObservers >
130 static_assert( std::is_empty_v< CMP > ,
"the comparator is stateless" );
137 using Pair = std::pair< Item , Prio >;
151 [[nodiscard]]
int size(
void )
const {
return(
int( f_q.size() ) ); }
154 [[nodiscard]]
bool empty(
void )
const {
return( f_q.empty() ); }
173 [[nodiscard]]
Item top(
void )
const {
return( f_item[ f_q.top().id ] ); }
177 [[nodiscard]]
Prio prio(
void )
const {
return( f_q.top().key.p ); }
181 void pop(
void ) { release( f_q.pop().id ); }
185 const int s = f_iim[ i ];
195 return( f_q.key( f_iim[ i ] ).p );
201 f_q.update( f_iim[ i ] ,
Key{ p } );
209 f_q.decrease( f_iim[ i ] ,
Key{ p } );
215 f_q.increase( f_iim[ i ] ,
Key{ p } );
220 const int s = f_iim[ i ];
231 f_iim.set( i ,
int( st ) );
236 [[nodiscard]]
Heap &
heap(
void ) {
return( f_q ); }
238 [[nodiscard]]
const Heap &
heap(
void )
const {
return( f_q ); }
242 return( f_q.subject() );
248 ADS_PRE( ( s >= 0 ) && ( std::size_t( s ) < f_item.size() ) );
249 return( f_item[ s ] );
255 void release(
int s ) {
256 f_iim.set( f_item[ s ] ,
int(
POST_HEAP ) );
257 f_free.push_back( s );
262 std::vector< Item > f_item;
263 std::vector< int > f_free;
267template<
class PR ,
class IM ,
class CMP ,
unsigned D ,
class Policy >
272 if( f_free.empty() ) {
273 s = int( f_item.size() );
274 f_item.push_back( i );
293template<
class Q ,
class PR ,
class IM >
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" );
306 using Pair = std::pair< Item , Prio >;
320 [[nodiscard]]
int size(
void )
const {
return( f_live ); }
323 [[nodiscard]]
bool empty(
void )
const {
return( f_live == 0 ); }
339 const int s = int( f_item.size() );
340 f_item.push_back( i );
341 f_prio.push_back( p );
345 f_q.push( s , Key( p ) );
356 return( f_item[ f_q.top() ] );
363 return( f_prio[ f_q.top() ] );
370 const int s = f_q.pop();
371 f_iim.set( f_item[ s ] ,
int(
POST_HEAP ) );
379 return( f_prio[ f_iim[ i ] ] );
387 const int s = f_iim[ i ];
390 f_q.push( s , Key( p ) );
399 const int s = f_iim[ i ];
417 const int s = f_iim[ i ];
428 f_iim.set( i ,
int( st ) );
433 using Key =
typename Q::Key;
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() ) )
448 std::vector< Item > f_item;
449 std::vector< Prio > f_prio;
450 std::vector< char > f_in;
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
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