35 template <
typename TValue,
typename TWeight>
54 #define HEAPMODE_MAXHEAP 0x0001 56 #define HEAPMODE_MINHEAP 0x0002 60 #define wbHeap_PARENT(i) ((i)/2) 61 #define wbHeap_RIGHT(i) (2*(i)+1) 63 #define wbHeap_LEFT(i) (2*(i)) 74 template <
typename TValue,
typename TWeight>
78 _wb_HEAP_MODE_ m_mode;
82 bool(*m_funpCompare)(TWeight, TWeight);
86 m_mode(mode), m_aUnits(size)
92 m_funpCompare = Heap::MaxHeapCompare;
94 m_funpCompare = Heap::MinHeapCompare;
111 tempUnit = m_pBuf[i];
112 m_pBuf[i] = m_pBuf[j];
113 m_pBuf[j] = tempUnit;
125 if (m_funpCompare(m_pBuf[i].
w, m_pBuf[nParent].w))
135 if (i > m_aUnits.
m_nTop / 2)
142 if (nLeft <= m_aUnits.
m_nTop && m_funpCompare(m_pBuf[nLeft].
w, m_pBuf[nLarge].w))
144 if (nRight <= m_aUnits.
m_nTop && m_funpCompare(m_pBuf[nRight].w, m_pBuf[nLarge].w))
166 bool GetTop(TValue &p_value, TWeight &p_w)
171 p_value = m_aUnits[1].value;
178 m_aUnits[1].value = p_value;
184 bool OutTop(TValue &p_value, TWeight &p_w)
189 p_value = m_aUnits[1].value;
192 m_aUnits[1] = m_aUnits.
End();
202 inline static bool MaxHeapCompare(TWeight p_n, TWeight p_nParent) {
return p_n > p_nParent; }
204 inline static bool MinHeapCompare(TWeight p_n, TWeight p_nParent) {
return p_n < p_nParent; }
Heap(_wb_HEAP_MODE_ mode=HEAPMODE_MAXHEAP, int size=1)
int m_nTop
Record the top of the array.
unsigned short _wb_HEAP_MODE_
void Swap(int i, int j)
exchange
bool GetTop(TValue &p_value, TWeight &p_w)
get the value at the top of heap
T & End()
Get the value at the tail of array.
T * GetBuffer(int i=0) const
get the buffer pointer
void operator=(_wb_HEAP_UNIT_ &unit)
void Insert(TValue p_value, TWeight p_w)
insert a value
#define wbHeap_RIGHT(i)
heap Right index
void Update(int i)
update the data at position i and then heapify
void Heapify(int i)
heapify
_wb_HEAP_UNIT_(TValue p_v, TWeight p_w)
= opeartion
#define HEAPMODE_MAXHEAP
max heap, used to sort from large to small
void Clean()
Clean the array. Just set the top of array to -1 and donot release the memory.
void SetTop(TValue p_value, TWeight p_w)
set the value at top and heapify
int GetNum() const
Get Array number.
void Add(T t)
Add a value to the tail of array.
#define wbHeap_LEFT(i)
heap Left index
#define wbHeap_PARENT(i)
heap get father index
bool OutTop(TValue &p_value, TWeight &p_w)
out the top
#define HEAPMODE_MINHEAP
min heap, used to sort from samll to large
define all the code written by Bin Wang.
Defination of simple dynamic array/stack/queue and so on.