QuadHeap
Declared in lemon/quad_heap.h, in the group Heap Structures; the same class on the Doxygen site.
-
template< typename PR , typename IM , typename CMP >
class QuadHeap Fourary (quaternary) heap data structure.
This class implements the Fourary (quaternary) heap data structure. It fully conforms to the heap concept.
The fourary heap is a specialization of the D-ary heap for
D=4. It is similar to the binary heap, but its nodes have at most four children, instead of two.See also
See also
- Template Parameters:
PR – Type of the priorities of the items.
IM – A reference item map with
intvalues (one whoseoperator[] gives a reference, which the heap writes through), used internally to handle the cross references.CMP – A functor class for comparing the priorities. The default is
std::less< PR >.
Public Types
-
enum State
Type to represent the states of the items.
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.Values:
-
enumerator IN_HEAP
= 0.
-
enumerator PRE_HEAP
= -1.
-
enumerator POST_HEAP
= -2.
-
enumerator IN_HEAP
-
typedef ItemIntMap::Key Item
Type of the items stored in the heap.
Public Functions
-
inline explicit QuadHeap( ItemIntMap & map )
Constructor.
- Parameters:
map – A map that assigns
intvalues to the items. It is used internally to handle the cross references. The assigned value must bePRE_HEAP(-1) for each item.
-
inline QuadHeap( ItemIntMap & map , const Compare & comp )
Constructor.
- Parameters:
map – A map that assigns
intvalues to the items. It is used internally to handle the cross references. The assigned value must bePRE_HEAP(-1) for each item.comp – The function object used for comparing the priorities.
-
inline int size() const
The number of items stored in the heap.
This function returns the number of items stored in the heap.
-
inline bool empty() const
Check if the heap is empty.
This function returns
trueif the heap is empty.
-
inline void clear()
Make the heap empty.
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_HEAPfor each item.
-
inline void push( const Pair & p )
Insert a pair of item and priority into the heap.
This function inserts
p.firstto the heap with priorityp.second.- Parameters:
p – The pair to insert.
- Pre:
p.firstmust not be stored in the heap.
-
inline void push( const Item & i , const Prio & p )
Insert an item into the heap with the given priority.
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.
- Pre:
i must not be stored in the heap.
-
inline Item top() const
Return the item having minimum priority.
This function returns the item having minimum priority.
- Pre:
The heap must be non-empty.
-
inline Prio prio() const
The minimum priority.
This function returns the minimum priority.
- Pre:
The heap must be non-empty.
-
inline void pop()
Remove the item having minimum priority.
This function removes the item having minimum priority.
- Pre:
The heap must be non-empty.
-
inline void erase( const Item & i )
Remove the given item from the heap.
This function removes the given item from the heap if it is already stored.
- Parameters:
i – The item to delete.
- Pre:
i must be in the heap.
-
inline Prio operator[]( const Item & i ) const
The priority of the given item.
This function returns the priority of the given item.
- Parameters:
i – The item.
- Pre:
i must be in the heap.
-
inline 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.
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.
-
inline void decrease( const Item & i , const Prio & p )
Decrease the priority of an item to the given value.
This function decreases the priority of an item to the given value.
- Parameters:
i – The item.
p – The priority.
- Pre:
i must be stored in the heap with priority at least p.
-
inline void increase( const Item & i , const Prio & p )
Increase the priority of an item to the given value.
This function increases the priority of an item to the given value.
- Parameters:
i – The item.
p – The priority.
- Pre:
i must be stored in the heap with priority at most p.
-
inline State state( const Item & i ) const
Return the state of an item.
This method returns
PRE_HEAPif the given item has never been in the heap,IN_HEAPif it is in the heap at the moment, andPOST_HEAPotherwise. In the latter case it is possible that the item will get back to the heap again.- Parameters:
i – The item.
-
inline void state( const Item & i , State st )
Set the state of an item in the heap.
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.
-
inline void replace( const Item & i , const Item & j )
Replace an item in the heap.
This function replaces item
iwith itemj. Itemimust be in the heap, whilejmust be out of the heap. After calling this method, itemiwill be out of the heap andjwill be in the heap with the same prioriority as itemihad before.