TRF Language Model
wb-heap.h
Go to the documentation of this file.
1 // You may obtain a copy of the License at
2 //
3 // http://www.apache.org/licenses/LICENSE-2.0
4 //
5 // Unless required by applicable law or agreed to in writing, software
6 // distributed under the License is distributed on an "AS IS" BASIS,
7 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8 // See the License for the specific language governing permissions and
9 // limitations under the License.
10 //
11 // Copyright 2014-2015 Tsinghua University
12 // Author: wb.th08@gmail.com (Bin Wang), ozj@tsinghua.edu.cn (Zhijian Ou)
13 //
14 // All h, cpp, cc, and script files (e.g. bat, sh, pl, py) should include the above
15 // license declaration. Different coding language may use different comment styles.
16 
17 
18 
19 
26 #ifndef _WB_HEAP_H_
27 #define _WB_HEAP_H_
28 #include "wb-vector.h"
29 
30 namespace wb
31 {
35  template <typename TValue, typename TWeight>
38  {
39  public:
40  TValue value;
41  TWeight w;
43  _wb_HEAP_UNIT_(TValue p_v, TWeight p_w) :value(p_v), w(p_w){}
46  {
47  value = unit.value;
48  w = unit.w;
49  }
50  };
53  typedef unsigned short _wb_HEAP_MODE_;
54 #define HEAPMODE_MAXHEAP 0x0001
56 #define HEAPMODE_MINHEAP 0x0002
58 
60 #define wbHeap_PARENT(i) ((i)/2)
61 #define wbHeap_RIGHT(i) (2*(i)+1)
63 #define wbHeap_LEFT(i) (2*(i))
65 
69 
74  template <typename TValue, typename TWeight>
75  class Heap
76  {
77  private:
78  _wb_HEAP_MODE_ m_mode;
81 
82  bool(*m_funpCompare)(TWeight, TWeight);
83 
84  public:
85  Heap(_wb_HEAP_MODE_ mode = HEAPMODE_MAXHEAP, int size = 1) :
86  m_mode(mode), m_aUnits(size)
87  {
88  m_pBuf = m_aUnits.GetBuffer();
89  m_aUnits.m_nTop = 0; //������һ�����ݣ���1��ʼ����
90 
91  if (mode & HEAPMODE_MAXHEAP)
92  m_funpCompare = Heap::MaxHeapCompare;
93  else if (mode & HEAPMODE_MINHEAP)
94  m_funpCompare = Heap::MinHeapCompare;
95  }
96 
98  {
99 
100  }
101 
102  void Clean() { m_aUnits.Clean(); m_aUnits.m_nTop = 0; }
103  int GetNum() { return m_aUnits.GetNum() - 1; }
104  bool IsEmpty() { return m_aUnits.m_nTop < 1; }
106  inline void Swap(int i, int j)
107  {
109 
110  //ʹ��m_pBuf��Ѱַ�����ﱣ֤�����ڴ�Խ��
111  tempUnit = m_pBuf[i];
112  m_pBuf[i] = m_pBuf[j];
113  m_pBuf[j] = tempUnit;
114  }
115 
117  void Update(int i)
118  {
119  if (i == 1) //���Ǵ�1��ʼʹ�õ�
120  return;
121 
122  int nParent = wbHeap_PARENT(i);
123 
124  //������ǰ�ڵ�͸��׽ڵ�
125  if (m_funpCompare(m_pBuf[i].w, m_pBuf[nParent].w))
126  {
127  Swap(i, nParent);
128 
129  Update(nParent);
130  }
131  }
133  void Heapify(int i)
134  {
135  if (i > m_aUnits.m_nTop / 2) //��Ҷ�ӽڵ����
136  return;
137 
138  int nLarge = i;
139  int nLeft = wbHeap_LEFT(i);
140  int nRight = wbHeap_RIGHT(i);
141 
142  if (nLeft <= m_aUnits.m_nTop && m_funpCompare(m_pBuf[nLeft].w, m_pBuf[nLarge].w))
143  nLarge = nLeft;
144  if (nRight <= m_aUnits.m_nTop && m_funpCompare(m_pBuf[nRight].w, m_pBuf[nLarge].w))
145  nLarge = nRight;
146 
147  if (nLarge != i)
148  {
149  //���� i �� nLarge
150  Swap(i, nLarge);
151 
152  Heapify(nLarge);
153  }
154  }
155 
157  void Insert(TValue p_value, TWeight p_w)
158  {
159  m_aUnits.Add(_wb_HEAP_UNIT_<TValue, TWeight>(p_value, p_w));
160  m_pBuf = m_aUnits.GetBuffer();
161 
162  Update(m_aUnits.m_nTop);
163  }
164 
166  bool GetTop(TValue &p_value, TWeight &p_w)
167  {
168  if (IsEmpty())
169  return false;
170 
171  p_value = m_aUnits[1].value;
172  p_w = m_aUnits[1].w;
173  return true;
174  }
176  void SetTop(TValue p_value, TWeight p_w)
177  {
178  m_aUnits[1].value = p_value;
179  m_aUnits[1].w = p_w;
180  Heapify(1);
181  }
182 
184  bool OutTop(TValue &p_value, TWeight &p_w)
185  {
186  if (IsEmpty())
187  return false;
188 
189  p_value = m_aUnits[1].value;
190  p_w = m_aUnits[1].w;
191 
192  m_aUnits[1] = m_aUnits.End();
193  m_aUnits.m_nTop--;
194 
195  Heapify(1);
196 
197  return true;
198  }
199 
200  private:
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; }
205  };
207 }
208 
209 #endif
TValue value
value
Definition: wb-heap.h:40
Heap(_wb_HEAP_MODE_ mode=HEAPMODE_MAXHEAP, int size=1)
Definition: wb-heap.h:85
int m_nTop
Record the top of the array.
Definition: wb-vector.h:208
unsigned short _wb_HEAP_MODE_
Definition: wb-heap.h:53
void Clean()
Definition: wb-heap.h:102
void Swap(int i, int j)
exchange
Definition: wb-heap.h:106
heap unit
Definition: wb-heap.h:37
TWeight w
weight
Definition: wb-heap.h:41
bool GetTop(TValue &p_value, TWeight &p_w)
get the value at the top of heap
Definition: wb-heap.h:166
int GetNum()
Definition: wb-heap.h:103
T & End()
Get the value at the tail of array.
Definition: wb-vector.h:256
T * GetBuffer(int i=0) const
get the buffer pointer
Definition: wb-vector.h:97
void operator=(_wb_HEAP_UNIT_ &unit)
Definition: wb-heap.h:45
void Insert(TValue p_value, TWeight p_w)
insert a value
Definition: wb-heap.h:157
#define wbHeap_RIGHT(i)
heap Right index
Definition: wb-heap.h:62
~Heap()
Definition: wb-heap.h:97
void Update(int i)
update the data at position i and then heapify
Definition: wb-heap.h:117
void Heapify(int i)
heapify
Definition: wb-heap.h:133
_wb_HEAP_UNIT_(TValue p_v, TWeight p_w)
= opeartion
Definition: wb-heap.h:43
#define HEAPMODE_MAXHEAP
max heap, used to sort from large to small
Definition: wb-heap.h:55
void Clean()
Clean the array. Just set the top of array to -1 and donot release the memory.
Definition: wb-vector.h:258
bool IsEmpty()
Definition: wb-heap.h:104
void SetTop(TValue p_value, TWeight p_w)
set the value at top and heapify
Definition: wb-heap.h:176
int GetNum() const
Get Array number.
Definition: wb-vector.h:240
void Add(T t)
Add a value to the tail of array.
Definition: wb-vector.h:242
#define wbHeap_LEFT(i)
heap Left index
Definition: wb-heap.h:64
#define wbHeap_PARENT(i)
heap get father index
Definition: wb-heap.h:60
bool OutTop(TValue &p_value, TWeight &p_w)
out the top
Definition: wb-heap.h:184
heap
Definition: wb-heap.h:75
#define HEAPMODE_MINHEAP
min heap, used to sort from samll to large
Definition: wb-heap.h:57
define all the code written by Bin Wang.
Definition: wb-file.cpp:21
Dynamic array.
Definition: wb-vector.h:205
Defination of simple dynamic array/stack/queue and so on.