This class implements the radix heap data structure. A heap is a data structure for storing items with specified values called priorities in such a way that finding the item with minimum priority is efficient. This heap type can store only items with int priority. In a heap one can change the priority of an item, add or erase an item, but the priority cannot be decreased under the last removed item's priority.
IM | A read and writable Item int map, used internally to handle the cross references. |
#include <lemon/radix_heap.h>
Classes | |
class | UnderFlowPriorityError |
Exception thrown by RadixHeap. More... | |
Public Types | |
enum | State |
Type to represent the items states. More... | |
Public Member Functions | |
RadixHeap (ItemIntMap &map, int minimal=0, int capacity=0) | |
The constructor. | |
int | size () const |
Returns the number of items stored in the heap. | |
bool | empty () const |
Checks if the heap stores no items. | |
void | clear (int minimal=0, int capacity=0) |
Make empty this heap. | |
void | push (const Item &i, const Prio &p) |
Insert an item into the heap with the given priority. | |
Item | top () const |
Returns the item with minimum priority. | |
Prio | prio () const |
Returns the minimum priority. | |
void | pop () |
Deletes the item with minimum priority. | |
void | erase (const Item &i) |
Deletes i from the heap. | |
Prio | operator[] (const Item &i) const |
Returns the priority of i . | |
void | set (const Item &i, const Prio &p) |
i gets to the heap with priority p independently if i was already there. | |
void | decrease (const Item &i, const Prio &p) |
Decreases the priority of i to p . | |
void | increase (const Item &i, const Prio &p) |
Increases the priority of i to p . | |
State | state (const Item &i) const |
Returns if item is in, has already been in, or has never been in the heap. | |
void | state (const Item &i, State st) |
Sets the state of the item in the heap. | |
Private Member Functions | |
void | remove (int index) |
Remove item from the box list. | |
void | insert (int box, int index) |
Insert item into the box list. | |
void | extend () |
Add a new box to the box list. | |
void | bubble_up (int index) |
Move an item up into the proper box. | |
int | findUp (int start, int pr) |
Find up the proper box for the item with the given prio. | |
void | bubble_down (int index) |
Move an item down into the proper box. | |
int | findDown (int start, int pr) |
Find up the proper box for the item with the given prio. | |
int | findFirst () |
Find the first not empty box. | |
int | minValue (int box) |
Gives back the minimal prio of the box. | |
void | moveDown () |
Rearrange the items of the heap and makes the first box not empty. | |
enum State |
Each Item element have a state associated to it. It may 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 ItemIntMap should be initialized in such way that it maps PRE_HEAP (-1) to any element to be put in the heap...
|
inline |
The constructor.
map | It should be given to the constructor, since it is used internally to handle the cross references. The value of the map should be PRE_HEAP (-1) for each element. |
minimal | The initial minimal value of the heap. |
capacity | It determines the initial capacity of the heap. |
|
inline |
The number of items stored in the heap.
|
inline |
Returns true
if and only if the heap stores no items.
|
inline |
Make empty this heap. It does not change the cross reference map. If you want to reuse a heap what is not surely empty you should first clear the heap and after that you should set the cross reference map for each item to PRE_HEAP
.
|
inline |
Adds i
to the heap with priority p
.
i | The item to insert. |
p | The priority of the item. |
|
inline |
This method returns the item with minimum priority.
|
inline |
It returns the minimum priority.
|
inline |
This method deletes the item with minimum priority.
|
inline |
This method deletes item i
from the heap, if i
was already stored in the heap.
i | The item to erase. |
|
inline |
This function returns the priority of item i
.
i
must be in the heap. i | The item. |
|
inline |
This method calls push(i
, p
) if i
is not stored in the heap and sets the priority of i
to p
otherwise. It may throw an UnderFlowPriorityException.
i | The item. |
p | The priority. |
|
inline |
This method decreases the priority of item i
to p
.
i
must be stored in the heap with priority at least p
, and should
be greater or equal to the last removed item's priority. i | The item. |
p | The priority. |
|
inline |
This method sets the priority of item i
to p
.
i
must be stored in the heap with priority at most p
i | The item. |
p | The priority. |
|
inline |
This method returns PRE_HEAP if 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 item
will get back to the heap again.
i | The item. |
|
inline |
Sets the state of the item
in the heap. It can be used to manually clear the heap when it is important to achive the better time complexity.
i | The item. |
st | The state. It should not be IN_HEAP . |