ug4
|
updateable priority queue class. unlike most PQ implementations, we need a method to inform the PQ of updated elements thats why we cannot use priority_queue from the STL. maxheap works on an external array of elements T, note that none of the elements of m_arr[0]..m_arr[size-1] are in the heap at the begining. You can insert elements by using maxheap::insert(i); More...
#include <maxheap.h>
Public Member Functions | |
size_t | arr_size () const |
returns size of external array More... | |
void | create (const std::vector< T > &v) |
void | create (size_t n, const T *arr_) |
creates the heap on a external array arr_[0]..arr_[n-1] More... | |
int | get_max () const |
returns the index in m_arr of maximal element of the heap More... | |
size_t | height () const |
returns nr of elements in the heap. More... | |
void | insert_item (int i) |
bool | is_in (size_t i) |
maxheap () | |
maxheap (const std::vector< T > &v) | |
maxheap (size_t n, const T *arr_) | |
void | print () const |
void | remove (int i) |
removes index i in m_arr More... | |
int | remove_max () |
returns the index in m_arr of maximal element of the m_heap and removes it from the m_heap More... | |
void | reset () |
void | update (int i) |
update the heap position of array element i (m_arr[i]) use this method if you have changed the value used in operator < of element i. More... | |
~maxheap () | |
deconstructor More... | |
Private Member Functions | |
void | downheap (int i) |
int | leftchild (int index) const |
maxheap (const maxheap< T > *other) | |
void | myswap (int i, int j) |
int | parent (int index) const |
int | rightchild (int index) const |
void | upheap (int i) |
Private Attributes | |
const T * | m_arr |
stdvector< int > | m_heap |
size_t | m_height |
stdvector< int > | m_posinheap |
size_t | m_size |
updateable priority queue class. unlike most PQ implementations, we need a method to inform the PQ of updated elements thats why we cannot use priority_queue from the STL. maxheap works on an external array of elements T, note that none of the elements of m_arr[0]..m_arr[size-1] are in the heap at the begining. You can insert elements by using maxheap::insert(i);
T | type of elements in the maxheap. Have to support bool operator <(const T &a, const T &b) |
|
private |
|
inline |
References ug::maxheap< T >::m_arr, ug::maxheap< T >::m_height, and ug::maxheap< T >::m_size.
|
inline |
constructor
n | maximal number of elements |
arr_ | array with elements which are to compare. note that non of these are in the heap in the beginning |
References ug::maxheap< T >::create(), ug::maxheap< T >::m_heap, ug::maxheap< T >::m_posinheap, and ug::maxheap< T >::m_size.
|
inline |
|
inline |
deconstructor
|
inline |
returns size of external array
References ug::maxheap< T >::m_size.
|
inline |
References ug::maxheap< T >::create().
|
inline |
creates the heap on a external array arr_[0]..arr_[n-1]
create
References ug::maxheap< T >::m_arr, ug::maxheap< T >::m_heap, ug::maxheap< T >::m_height, ug::maxheap< T >::m_posinheap, and ug::maxheap< T >::m_size.
Referenced by ug::maxheap< T >::create(), and ug::maxheap< T >::maxheap().
|
inlineprivate |
i | index in m_arr for which to downheap |
References ug::maxheap< T >::leftchild(), ug::maxheap< T >::m_arr, ug::maxheap< T >::m_posinheap, ug::maxheap< T >::myswap(), and ug::maxheap< T >::rightchild().
Referenced by ug::maxheap< T >::remove(), and ug::maxheap< T >::update().
|
inline |
returns the index in m_arr of maximal element of the heap
References ug::maxheap< T >::m_heap.
|
inline |
returns nr of elements in the heap.
References ug::maxheap< T >::m_height.
|
inline |
insertItem inserts Item m_arr[i] (see constructor) into the heap
i | index of item in m_arr |
References ug::maxheap< T >::is_in(), ug::maxheap< T >::m_heap, ug::maxheap< T >::m_height, ug::maxheap< T >::m_posinheap, ug::maxheap< T >::m_size, UG_ASSERT, and ug::maxheap< T >::upheap().
|
inline |
References ug::maxheap< T >::m_posinheap.
Referenced by ug::maxheap< T >::insert_item(), ug::maxheap< T >::remove(), and ug::maxheap< T >::update().
|
inlineprivate |
returns the index in m_arr of the left child of i all indices in m_arr, NOT in m_heap
index | index in m_arr of element |
References ug::maxheap< T >::m_heap, ug::maxheap< T >::m_height, ug::maxheap< T >::m_posinheap, and p.
Referenced by ug::maxheap< T >::downheap().
|
inlineprivate |
swaps elements i and j of array m_posinheap and their counterparts in m_heap
References ug::maxheap< T >::m_heap, and ug::maxheap< T >::m_posinheap.
Referenced by ug::maxheap< T >::downheap(), ug::maxheap< T >::remove(), and ug::maxheap< T >::upheap().
|
inlineprivate |
returns the index in m_arr of the parent of i all indices in m_arr, NOT in m_heap
index | index in m_arr of element |
References ug::maxheap< T >::m_heap, ug::maxheap< T >::m_posinheap, and p.
Referenced by ug::maxheap< T >::print(), ug::maxheap< T >::update(), and ug::maxheap< T >::upheap().
|
inline |
debug print output
References ug::maxheap< T >::m_arr, ug::maxheap< T >::m_heap, ug::maxheap< T >::m_height, ug::maxheap< T >::m_size, and ug::maxheap< T >::parent().
|
inline |
removes index i in m_arr
References ug::maxheap< T >::downheap(), ug::maxheap< T >::is_in(), ug::maxheap< T >::m_heap, ug::maxheap< T >::m_height, ug::maxheap< T >::m_posinheap, and ug::maxheap< T >::myswap().
Referenced by ug::maxheap< T >::remove_max().
|
inline |
returns the index in m_arr of maximal element of the m_heap and removes it from the m_heap
References ug::maxheap< T >::m_heap, ug::maxheap< T >::m_height, ug::maxheap< T >::remove(), and UG_ASSERT.
|
inline |
reset set m_height 0 (= remove all items from the heap)
References ug::maxheap< T >::m_height.
|
inlineprivate |
returns the index in m_arr of the right child of i all indices in m_arr, NOT in m_heap
index | index in m_arr of element |
References ug::maxheap< T >::m_heap, ug::maxheap< T >::m_height, ug::maxheap< T >::m_posinheap, and p.
Referenced by ug::maxheap< T >::downheap().
|
inline |
update the heap position of array element i (m_arr[i]) use this method if you have changed the value used in operator < of element i.
i | index in m_arr for which to update |
References ug::maxheap< T >::downheap(), ug::maxheap< T >::is_in(), ug::maxheap< T >::m_arr, ug::maxheap< T >::parent(), and ug::maxheap< T >::upheap().
|
inlineprivate |
i | index in m_arr for which to upheap |
References ug::maxheap< T >::m_arr, ug::maxheap< T >::m_posinheap, ug::maxheap< T >::myswap(), and ug::maxheap< T >::parent().
Referenced by ug::maxheap< T >::insert_item(), and ug::maxheap< T >::update().
|
private |
|
private |
Referenced by ug::maxheap< T >::create(), ug::maxheap< T >::get_max(), ug::maxheap< T >::insert_item(), ug::maxheap< T >::leftchild(), ug::maxheap< T >::maxheap(), ug::maxheap< T >::myswap(), ug::maxheap< T >::parent(), ug::maxheap< T >::print(), ug::maxheap< T >::remove(), ug::maxheap< T >::remove_max(), and ug::maxheap< T >::rightchild().
|
private |
Referenced by ug::maxheap< T >::create(), ug::maxheap< T >::height(), ug::maxheap< T >::insert_item(), ug::maxheap< T >::leftchild(), ug::maxheap< T >::maxheap(), ug::maxheap< T >::print(), ug::maxheap< T >::remove(), ug::maxheap< T >::remove_max(), ug::maxheap< T >::reset(), and ug::maxheap< T >::rightchild().
|
private |
Referenced by ug::maxheap< T >::create(), ug::maxheap< T >::downheap(), ug::maxheap< T >::insert_item(), ug::maxheap< T >::is_in(), ug::maxheap< T >::leftchild(), ug::maxheap< T >::maxheap(), ug::maxheap< T >::myswap(), ug::maxheap< T >::parent(), ug::maxheap< T >::remove(), ug::maxheap< T >::rightchild(), and ug::maxheap< T >::upheap().
|
private |