This concept class describes the main interface of heaps. The various heap structures are efficient implementations of the abstract data type priority queue. They store items with specified values called priorities in such a way that finding and removing the item with minimum priority are efficient. The basic operations are adding and erasing items, changing the priority of an item, etc.
Heaps are crucial in several algorithms, such as Dijkstra and Prim. Any class that conforms to this concept can be used easily in such algorithms.
PR | Type of the priorities of the items. |
IM | A read-writable item map with int values, used internally to handle the cross references. |
CMP | A functor class for comparing the priorities. The default is std::less<PR> . |
#include <lemon/concepts/heap.h>
Public Types | |
enum | State { IN_HEAP = 0, PRE_HEAP = -1, POST_HEAP = -2 } |
Type to represent the states of the items. More... | |
typedef IM | ItemIntMap |
Type of the item-int map. | |
typedef PR | Prio |
Type of the priorities. | |
typedef ItemIntMap::Key | Item |
Type of the items stored in the heap. | |
Public Member Functions | |
Heap (ItemIntMap &map) | |
Constructor. | |
Heap (ItemIntMap &map, const CMP &comp) | |
Constructor. | |
int | size () const |
The number of items stored in the heap. | |
bool | empty () const |
Check if the heap is empty. | |
void | clear () |
Make the heap empty. | |
void | push (const Item &i, const Prio &p) |
Insert an item into the heap with the given priority. | |
Item | top () const |
Return the item having minimum priority. | |
Prio | prio () const |
The minimum priority. | |
void | pop () |
Remove the item having minimum priority. | |
void | erase (const Item &i) |
Remove the given item from the heap. | |
Prio | operator[] (const Item &i) const |
The priority of the given item. | |
void | set (const Item &i, const Prio &p) |
Set the priority of an item or insert it, if it is not stored in the heap. | |
void | decrease (const Item &i, const Prio &p) |
Decrease the priority of an item to the given value. | |
void | increase (const Item &i, const Prio &p) |
Increase the priority of an item to the given value. | |
State | state (const Item &i) const |
Return the state of an item. | |
void | state (const Item &i, State st) |
Set the state of an item in the heap. |
enum State |
Each item has a state associated to it. It can be "in heap", "pre-heap" or "post-heap". The latter two are indifferent from the heap's point of view, but may be useful to the user.
The item-int map must be initialized in such way that it assigns PRE_HEAP
(-1
) to any element to be put in the heap.
Heap | ( | ItemIntMap & | map | ) | [inline, explicit] |
Constructor.
map | A map that assigns int values to keys of type Item . It is used internally by the heap implementations to handle the cross references. The assigned value must be PRE_HEAP (-1 ) for each item. |
Heap | ( | ItemIntMap & | map, |
const CMP & | comp | ||
) | [inline, explicit] |
Constructor.
map | A map that assigns int values to keys of type Item . It is used internally by the heap implementations to handle the cross references. The assigned value must be PRE_HEAP (-1 ) for each item. |
comp | The function object used for comparing the priorities. |
int size | ( | ) | const [inline] |
This function returns the number of items stored in the heap.
bool empty | ( | ) | const [inline] |
This function returns true
if the heap is empty.
void clear | ( | ) | [inline] |
This functon makes the heap empty. It does not change the cross reference map. If you want to reuse a heap that is not surely empty, you should first clear it and then you should set the cross reference map to PRE_HEAP
for each item.
This function inserts the given item into the heap with the given priority.
i | The item to insert. |
p | The priority of the item. |
Item top | ( | ) | const [inline] |
This function returns the item having minimum priority.
Prio prio | ( | ) | const [inline] |
This function returns the minimum priority.
void pop | ( | ) | [inline] |
This function removes the item having minimum priority.
void erase | ( | const Item & | i | ) | [inline] |
This function removes the given item from the heap if it is already stored.
i | The item to delete. |
This function returns the priority of the given item.
i | The item. |
This method sets the priority of the given item if it is already stored in the heap. Otherwise it inserts the given item into the heap with the given priority.
i | The item. |
p | The priority. |
This function decreases the priority of an item to the given value.
i | The item. |
p | The priority. |
This function increases the priority of an item to the given value.
i | The item. |
p | The priority. |
This method returns PRE_HEAP
if the given item has never been in the heap, IN_HEAP
if it is in the heap at the moment, and POST_HEAP
otherwise. In the latter case it is possible that the item will get back to the heap again.
i | The item. |