Public Types | Public Member Functions

BinHeap< PR, IM, CMP > Class Template Reference


Detailed Description

template<typename PR, typename IM, typename CMP = std::less<PR>>
class lemon::BinHeap< PR, IM, CMP >

This class implements the binary 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. CMP specifies the ordering of the priorities. In a heap one can change the priority of an item, add or erase an item, etc.

Template Parameters:
PRType of the priority of the items.
IMA read and writable item map with int values, used internally to handle the cross references.
CMPA functor class for the ordering of the priorities. The default is std::less<PR>.
See also:
FibHeap
Dijkstra

#include <lemon/bin_heap.h>

List of all members.

Public Types

enum  State { IN_HEAP = 0, PRE_HEAP = -1, POST_HEAP = -2 }
 

Type to represent the items states.

More...
typedef IM ItemIntMap
 
typedef PR Prio
 
typedef ItemIntMap::Key Item
 
typedef std::pair< Item, PrioPair
 
typedef CMP Compare
 

Public Member Functions

 BinHeap (ItemIntMap &map)
 The constructor.
 BinHeap (ItemIntMap &map, const Compare &comp)
 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 ()
 Make empty this heap.
void push (const Pair &p)
 Insert a pair of item and priority into the heap.
void push (const Item &i, const Prio &p)
 Insert an item into the heap with the given heap.
Item top () const
 Returns the item with minimum priority relative to Compare.
Prio prio () const
 Returns the minimum priority relative to Compare.
void pop ()
 Deletes the item with minimum priority relative to Compare.
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.
void replace (const Item &i, const Item &j)
 Replaces an item in the heap.

Member Enumeration Documentation

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 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

BinHeap ( ItemIntMap map) [inline, explicit]

The constructor.

Parameters:
mapshould be given to the constructor, since it is used internally to handle the cross references. The value of the map must be PRE_HEAP (-1) for every item.
BinHeap ( ItemIntMap map,
const Compare comp 
) [inline]

The constructor.

Parameters:
mapshould 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.
compThe comparator function object.

Member Function Documentation

int size ( ) const [inline]

The number of items stored in the heap.

bool empty ( ) const [inline]

Returns true if and only if the heap stores no items.

void clear ( ) [inline]

Make empty this heap. It does not change the cross reference map. If you want to reuse 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.

void push ( const Pair p) [inline]

Adds p.first to the heap with priority p.second.

Parameters:
pThe pair to insert.
void push ( const Item i,
const Prio p 
) [inline]

Adds i to the heap with priority p.

Parameters:
iThe item to insert.
pThe priority of the item.
Item top ( ) const [inline]

This method returns the item with minimum priority relative to Compare.

Precondition:
The heap must be nonempty.
Prio prio ( ) const [inline]

It returns the minimum priority relative to Compare.

Precondition:
The heap must be nonempty.
void pop ( ) [inline]

This method deletes the item with minimum priority relative to Compare from the heap.

Precondition:
The heap must be non-empty.
void erase ( const Item i) [inline]

This method deletes item i from the heap.

Parameters:
iThe item to erase.
Precondition:
The item should be in the heap.
Prio operator[] ( const Item i) const [inline]

This function returns the priority of item i.

Parameters:
iThe item.
Precondition:
i must be in the heap.
void set ( const Item i,
const Prio p 
) [inline]

This method calls push(i, p) if i is not stored in the heap and sets the priority of i to p otherwise.

Parameters:
iThe item.
pThe priority.
void decrease ( const Item i,
const Prio p 
) [inline]

This method decreases the priority of item i to p.

Parameters:
iThe item.
pThe priority.
Precondition:
i must be stored in the heap with priority at least p relative to Compare.
void increase ( const Item i,
const Prio p 
) [inline]

This method sets the priority of item i to p.

Parameters:
iThe item.
pThe priority.
Precondition:
i must be stored in the heap with priority at most p relative to Compare.
State state ( const Item i) const [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.

Parameters:
iThe item.
void state ( const Item i,
State  st 
) [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.

Parameters:
iThe item.
stThe state. It should not be IN_HEAP.
void replace ( const Item i,
const Item j 
) [inline]

The i item is replaced with j item. The i item should be in the heap, while the j should be out of the heap. The i item will out of the heap and j will be in the heap with the same prioriority as prevoiusly the i item.

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines