ug4
ug::maxheap< T > Class Template Reference

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
 

Detailed Description

template<typename T>
class ug::maxheap< T >

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

Note
a radix heap would be faster for some applications.
Parameters
Ttype of elements in the maxheap. Have to support bool operator <(const T &a, const T &b)

Constructor & Destructor Documentation

◆ maxheap() [1/4]

template<typename T >
ug::maxheap< T >::maxheap ( const maxheap< T > *  other)
private

◆ maxheap() [2/4]

template<typename T >
ug::maxheap< T >::maxheap ( )
inline

◆ maxheap() [3/4]

template<typename T >
ug::maxheap< T >::maxheap ( size_t  n,
const T *  arr_ 
)
inline

constructor

Parameters
nmaximal 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.

◆ maxheap() [4/4]

template<typename T >
ug::maxheap< T >::maxheap ( const std::vector< T > &  v)
inline

◆ ~maxheap()

template<typename T >
ug::maxheap< T >::~maxheap ( )
inline

deconstructor

Member Function Documentation

◆ arr_size()

template<typename T >
size_t ug::maxheap< T >::arr_size ( ) const
inline

returns size of external array

References ug::maxheap< T >::m_size.

◆ create() [1/2]

template<typename T >
void ug::maxheap< T >::create ( const std::vector< T > &  v)
inline

◆ create() [2/2]

template<typename T >
void ug::maxheap< T >::create ( size_t  n,
const T *  arr_ 
)
inline

◆ downheap()

template<typename T >
void ug::maxheap< T >::downheap ( int  i)
inlineprivate

◆ get_max()

template<typename T >
int ug::maxheap< T >::get_max ( ) const
inline

returns the index in m_arr of maximal element of the heap

References ug::maxheap< T >::m_heap.

◆ height()

template<typename T >
size_t ug::maxheap< T >::height ( ) const
inline

returns nr of elements in the heap.

References ug::maxheap< T >::m_height.

◆ insert_item()

template<typename T >
void ug::maxheap< T >::insert_item ( int  i)
inline

insertItem inserts Item m_arr[i] (see constructor) into the heap

Parameters
iindex 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().

◆ is_in()

template<typename T >
bool ug::maxheap< T >::is_in ( size_t  i)
inline

◆ leftchild()

template<typename T >
int ug::maxheap< T >::leftchild ( int  index) const
inlineprivate

returns the index in m_arr of the left child of i all indices in m_arr, NOT in m_heap

Parameters
indexindex in m_arr of element
Returns
returns index in m_arr of the left child

References ug::maxheap< T >::m_heap, ug::maxheap< T >::m_height, ug::maxheap< T >::m_posinheap, and p.

Referenced by ug::maxheap< T >::downheap().

◆ myswap()

template<typename T >
void ug::maxheap< T >::myswap ( int  i,
int  j 
)
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().

◆ parent()

template<typename T >
int ug::maxheap< T >::parent ( int  index) const
inlineprivate

returns the index in m_arr of the parent of i all indices in m_arr, NOT in m_heap

Parameters
indexindex in m_arr of element
Returns
returns index in m_arr of the parent

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().

◆ print()

template<typename T >
void ug::maxheap< T >::print ( ) const
inline

◆ remove()

◆ remove_max()

template<typename T >
int 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.

◆ reset()

template<typename T >
void ug::maxheap< T >::reset ( )
inline

reset set m_height 0 (= remove all items from the heap)

References ug::maxheap< T >::m_height.

◆ rightchild()

template<typename T >
int ug::maxheap< T >::rightchild ( int  index) const
inlineprivate

returns the index in m_arr of the right child of i all indices in m_arr, NOT in m_heap

Parameters
indexindex in m_arr of element
Returns
returns index in m_arr of the right child

References ug::maxheap< T >::m_heap, ug::maxheap< T >::m_height, ug::maxheap< T >::m_posinheap, and p.

Referenced by ug::maxheap< T >::downheap().

◆ update()

template<typename T >
void ug::maxheap< T >::update ( int  i)
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.

Parameters
iindex 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().

◆ upheap()

template<typename T >
void ug::maxheap< T >::upheap ( int  i)
inlineprivate

Member Data Documentation

◆ m_arr

◆ m_heap

◆ m_height

◆ m_posinheap

◆ m_size


The documentation for this class was generated from the following file: