SimpleBucketHeap< IM, MIN > Class Template Reference

- Main Page
- Modules
- Classes
- Namespaces
- Files
- Related Pages

- Class List
- Class Index
- Class Hierarchy
- Class Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
List of all members | Public Types | Public Member Functions
SimpleBucketHeap< IM, MIN > Class Template Reference
Heap Structures

## Detailed Description

### template<typename IM, bool MIN = true>
class lemon::SimpleBucketHeap< IM, MIN >

This class implements a simplified bucket heap data structure. It does not provide some functionality, but it is faster and simpler than BucketHeap. The main difference is that BucketHeap stores a doubly-linked list for each key while this class stores only simply-linked lists. It supports erasing only for the item having minimum priority and it does not support key increasing and decreasing.

Note that this implementation does not conform to the heap concept due to the lack of some functionality.

Template Parameters

  IM | A read-writable item map with `int` values, used internally to handle the cross references.
  MIN | Indicate if the heap is a min-heap or a max-heap. The default is min-heap. If this parameter is set to `false`, then the comparison is reversed, so the top(), prio() and pop() functions deal with the item having maximum priority instead of the minimum.

See Also
  BucketHeap

`#include <lemon/bucket_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 int | Prio
| Type of the priorities.
typedef ItemIntMap::Key | Item
| Type of the items stored in the heap.
typedef std::pair< Item, Prio > | Pair
| Type of the item-priority pairs.

## Public Member Functions
| SimpleBucketHeap (ItemIntMap &map)
| Constructor. More...
int | size () const
| The number of items stored in the heap. More...
bool | empty () const
| Check if the heap is empty. More...
void | clear ()
| Make the heap empty. More...
void | push (const Pair &p)
| Insert a pair of item and priority into the heap. More...
void | push (const Item &i, const Prio &p)
| Insert an item into the heap with the given priority. More...
Item | top () const
| Return the item having minimum priority. More...
Prio | prio () const
| The minimum priority. More...
void | pop ()
| Remove the item having minimum priority. More...
Prio | operator[] (const Item &i) const
| The priority of the given item. More...
State | state (const Item &i) const
| Return the state of an item. More...

## Member Enumeration Documentation

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

SimpleBucketHeap | ( | ItemIntMap & | map | ) |
| inlineexplicit

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.

## Member Function Documentation

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.

void push | ( | const Pair & | p | ) |
| inline

This function inserts `p.first` to the heap with priority `p.second`.

Parameters

  p | The pair to insert.

Precondition
  `p.first` must not be stored in the heap.

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.

Item top | ( |  | ) | const
| inline

This function returns the item having minimum priority.

Precondition
  The heap must be non-empty.

Prio prio | ( |  | ) | const
| inline

This function returns the minimum priority.

Precondition
  The heap must be non-empty.

void pop | ( |  | ) |
| inline

This function removes the item having minimum priority.

Precondition
  The heap must be non-empty.

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.

Warning
  This operator is not a constant time function because it scans the whole data structure to find the proper value.

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.