Loading...
Searching...
No Matches
RadixHeap< IM > Class Template Reference
Data Structures » Heap Structures

## Detailed Description

template<typename IM>
class lemon::RadixHeap< IM >

This class implements the radix heap data structure. It practically conforms to the heap concept, but it has some limitations due its special implementation. The type of the priorities must be `int` and the priority of an item cannot be decreased under the priority of the last removed item.

Template Parameters

  IM | A read-writable item map with `int` values, used internally to handle the cross references.

`#include <lemon/radix_heap.h>`

## Classes

class | PriorityUnderflowError
| Exception thrown by RadixHeap. More...

## 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 int | Prio
| Type of the priorities.
typedef ItemIntMap::Key | Item
| Type of the items stored in the heap.

## Public Member Functions

| RadixHeap (ItemIntMap &map, int minimum=0, int capacity=0)
| Constructor.
int | size () const
| The number of items stored in the heap.
bool | empty () const
| Check if the heap is empty.
void | clear (int minimum=0, int capacity=0)
| 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.

## Member Enumeration Documentation

## State

template<typename IM>

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.

Enumerator
IN_HEAP | = 0.
PRE_HEAP | = -1.
POST_HEAP | = -2.

## Constructor & Destructor Documentation

## RadixHeap()

template<typename IM>

RadixHeap | ( | ItemIntMap & | map,
|  | int | minimum = 0,
|  | int | capacity = 0 )

inline

Constructor.

Parameters

  map | A map that assigns `int` values to the items. It is used internally to handle the cross references. The assigned value must be `PRE_HEAP` (-1) for each item.
  minimum | The initial minimum value of the heap.
  capacity | The initial capacity of the heap.

## Member Function Documentation

## size()

template<typename IM>

int size | ( |  | ) | const

inline

This function returns the number of items stored in the heap.

## empty()

template<typename IM>

bool empty | ( |  | ) | const

inline

This function returns `true` if the heap is empty.

## clear()

template<typename IM>

void clear | ( | int | minimum = 0,
|  | int | capacity = 0 )

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.

Parameters

  minimum | The minimum value of the heap.
  capacity | The capacity of the heap.

## push()

template<typename IM>

void push | ( | const Item & | i,
|  | const Prio & | p )

inline

This function inserts the given item into the heap with the given priority.

Parameters

  i | The item to insert.
  p | The priority of the item.

Precondition
  i must not be stored in the heap.

Warning
  This method may throw an `UnderFlowPriorityException`.

## top()

template<typename IM>

Item top | ( |  | ) | const

inline

This function returns the item having minimum priority.

Precondition
  The heap must be non-empty.

## prio()

template<typename IM>

Prio prio | ( |  | ) | const

inline

This function returns the minimum priority.

Precondition
  The heap must be non-empty.

## pop()

template<typename IM>

void pop | ( |  | ) |

inline

This function removes the item having minimum priority.

Precondition
  The heap must be non-empty.

## erase()

template<typename IM>

void erase | ( | const Item & | i | ) |

inline

This function removes the given item from the heap if it is already stored.

Parameters

  i | The item to delete.

Precondition
  i must be in the heap.

## operator[]()

template<typename IM>

Prio operator[] | ( | const Item & | i | ) | const

inline

This function returns the priority of the given item.

Parameters

  i | The item.

Precondition
  i must be in the heap.

## set()

template<typename IM>

void set | ( | const Item & | i,
|  | const Prio & | p )

inline

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.

Parameters

  i | The item.
  p | The priority.

Precondition
  i must be in the heap.

Warning
  This method may throw an `UnderFlowPriorityException`.

## decrease()

template<typename IM>

void decrease | ( | const Item & | i,
|  | const Prio & | p )

inline

This function decreases the priority of an item to the given value.

Parameters

  i | The item.
  p | The priority.

Precondition
  i must be stored in the heap with priority at least p.

Warning
  This method may throw an `UnderFlowPriorityException`.

## increase()

template<typename IM>

void increase | ( | const Item & | i,
|  | const Prio & | p )

inline

This function increases the priority of an item to the given value.

Parameters

  i | The item.
  p | The priority.

Precondition
  i must be stored in the heap with priority at most p.

## state() [1/2]

template<typename IM>

State state | ( | const Item & | i | ) | const

inline

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.

Parameters

  i | The item.

## state() [2/2]

template<typename IM>

void state | ( | const Item & | i,
|  | State | st )

inline

This function sets the state of the given item in the heap. It can be used to manually clear the heap when it is important to achive better time complexity.

Parameters

  i | The item.
  st | The state. It should not be `IN_HEAP`.