-
Notifications
You must be signed in to change notification settings - Fork 7
/
WeightedDiscretePDF.cpp
312 lines (255 loc) · 5.5 KB
/
WeightedDiscretePDF.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// $Id: WeightedDiscretePDF.cpp,v 1.4 2006/07/07 05:54:31 zr Exp $
#include <stdexcept>
//
template <class T>
WDPDF_Node<T>::WDPDF_Node(T key_, float weight_, WDPDF_Node<T> *parent_)
{
m_mark = false;
key = key_;
weight = weight_;
sumWeights = 0;
left = right = 0;
parent = parent_;
}
template <class T>
WDPDF_Node<T>::~WDPDF_Node()
{
if (left) delete left;
if (right) delete right;
}
//
template <class T>
WeightedDiscretePDF<T>::WeightedDiscretePDF()
{
m_root = 0;
}
template <class T>
WeightedDiscretePDF<T>::~WeightedDiscretePDF()
{
if (m_root) delete m_root;
}
template <class T>
void WeightedDiscretePDF<T>::insert(T item, float weight)
{
WDPDF_Node<T> *p=0, *n=m_root;
while (n) {
if (n->leftIsRed() && n->rightIsRed())
split(n);
p = n;
if (n->key==item) {
throw std::domain_error("insert: argument(item) already in tree");
} else {
n = (item<n->key)?n->left:n->right;
}
}
n = new WDPDF_Node<T>(item, weight, p);
if (!p) {
m_root = n;
} else {
if (item<p->key) {
p->left = n;
} else {
p->right = n;
}
split(n);
}
propogateSumsUp(n);
}
template <class T>
void WeightedDiscretePDF<T>::remove(T item)
{
WDPDF_Node<T> **np = lookup(item, 0);
WDPDF_Node<T> *child, *n = *np;
if (!n) {
throw std::domain_error("remove: argument(item) not in tree");
} else {
if (n->left) {
WDPDF_Node<T> **leftMaxp = &n->left;
while ((*leftMaxp)->right)
leftMaxp = &(*leftMaxp)->right;
n->key = (*leftMaxp)->key;
n->weight = (*leftMaxp)->weight;
np = leftMaxp;
n = *np;
}
// node now has at most one child
child = n->left?n->left:n->right;
*np = child;
if (child) {
child->parent = n->parent;
if (n->isBlack()) {
lengthen(child);
}
}
propogateSumsUp(n->parent);
n->left = n->right = 0;
delete n;
}
}
template <class T>
void WeightedDiscretePDF<T>::update(T item, float weight)
{
WDPDF_Node<T> *n = *lookup(item, 0);
if (!n) {
throw std::domain_error("update: argument(item) not in tree");
} else {
float delta = weight - n->weight;
n->weight = weight;
for (; n; n=n->parent) {
n->sumWeights += delta;
}
}
}
template <class T>
T WeightedDiscretePDF<T>::choose(float p)
{
if (p<0.0 || p>=1.0) {
throw std::domain_error("choose: argument(p) outside valid range");
} else if (!m_root) {
throw std::logic_error("choose: choose() called on empty tree");
} else {
float w = m_root->sumWeights * p;
WDPDF_Node<T> *n = m_root;
while (1) {
if (n->left) {
if (w<n->left->sumWeights) {
n = n->left;
continue;
} else {
w -= n->left->sumWeights;
}
}
if (w<n->weight || !n->right) {
break; // !n->right condition shouldn't be necessary, just sanity check
}
w -= n->weight;
n = n->right;
}
return n->key;
}
}
template <class T>
bool WeightedDiscretePDF<T>::inTree(T item)
{
WDPDF_Node<T> *n = *lookup(item, 0);
return !!n;
}
//
template <class T>
WDPDF_Node<T> **WeightedDiscretePDF<T>::lookup(T item, WDPDF_Node<T> **parent_out)
{
WDPDF_Node<T> *n, *p=0, **np=&m_root;
while ((n = *np)) {
if (n->key==item) {
break;
} else {
p = n;
if (item<n->key) {
np = &n->left;
} else {
np = &n->right;
}
}
}
if (parent_out)
*parent_out = p;
return np;
}
template <class T>
void WeightedDiscretePDF<T>::split(WDPDF_Node<T> *n)
{
if (n->left) n->left->markBlack();
if (n->right) n->right->markBlack();
if (n->parent) {
WDPDF_Node<T> *p = n->parent;
n->markRed();
if (p->isRed()) {
p->parent->markRed();
// not same direction
if (!( (n==p->left && p==p->parent->left) ||
(n==p->right && p==p->parent->right))) {
rotate(n);
p = n;
}
rotate(p);
p->markBlack();
}
}
}
template <class T>
void WeightedDiscretePDF<T>::rotate(WDPDF_Node<T> *n)
{
WDPDF_Node<T> *p=n->parent, *pp=p->parent;
n->parent = pp;
p->parent = n;
if (n==p->left) {
p->left = n->right;
n->right = p;
if (p->left) p->left->parent = p;
} else {
p->right = n->left;
n->left = p;
if (p->right) p->right->parent = p;
}
n->setSum();
p->setSum();
if (!pp) {
m_root = n;
} else {
if (p==pp->left) {
pp->left = n;
} else {
pp->right = n;
}
}
}
template <class T>
void WeightedDiscretePDF<T>::lengthen(WDPDF_Node<T> *n)
{
if (n->isRed()) {
n->markBlack();
} else if (n->parent) {
WDPDF_Node<T> *sibling = n->sibling();
if (sibling && sibling->isRed()) {
n->parent->markRed();
sibling->markBlack();
rotate(sibling); // node sibling is now old sibling child, must be black
sibling = n->sibling();
}
// sibling is black
if (!sibling) {
lengthen(n->parent);
} else if (sibling->leftIsBlack() && sibling->rightIsBlack()) {
if (n->parent->isBlack()) {
sibling->markRed();
lengthen(n->parent);
} else {
sibling->markRed();
n->parent->markBlack();
}
} else {
if (n==n->parent->left && sibling->rightIsBlack()) {
rotate(sibling->left); // sibling->left must be red
sibling->markRed();
sibling->parent->markBlack();
sibling = sibling->parent;
} else if (n==n->parent->right && sibling->leftIsBlack()) {
rotate(sibling->right); // sibling->right must be red
sibling->markRed();
sibling->parent->markBlack();
sibling = sibling->parent;
}
// sibling is black, and sibling's far child is red
rotate(sibling);
if (n->parent->isRed()) sibling->markRed();
sibling->left->markBlack();
sibling->right->markBlack();
}
}
}
template <class T>
void WeightedDiscretePDF<T>::propogateSumsUp(WDPDF_Node<T> *n)
{
for (; n; n=n->parent)
n->setSum();
}