-
Notifications
You must be signed in to change notification settings - Fork 100
/
avl.h
439 lines (403 loc) · 10.2 KB
/
avl.h
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
* Vanitygen, vanity bitcoin address generator
* Copyright (C) 2011 <[email protected]>
*
* Vanitygen is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Vanitygen is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Vanitygen. If not, see <http://www.gnu.org/licenses/>.
*/
#if !defined (__VG_AVL_H__)
#define __VG_AVL_H__
#include <assert.h>
/*
* AVL tree implementation
*/
typedef enum { CENT = 1, LEFT = 0, RIGHT = 2 } avl_balance_t;
typedef struct _avl_item_s {
struct _avl_item_s *ai_left, *ai_right, *ai_up;
avl_balance_t ai_balance;
#ifndef NDEBUG
int ai_indexed;
#endif
} avl_item_t;
typedef struct _avl_root_s {
avl_item_t *ar_root;
} avl_root_t;
static INLINE void
avl_root_init(avl_root_t *rootp)
{
rootp->ar_root = NULL;
}
static INLINE int
avl_root_empty(avl_root_t *rootp)
{
return (rootp->ar_root == NULL) ? 1 : 0;
}
static INLINE void
avl_item_init(avl_item_t *itemp)
{
itemp->ai_left = NULL;
itemp->ai_right = NULL;
itemp->ai_up = NULL;
itemp->ai_balance = CENT;
#ifndef NDEBUG
itemp->ai_indexed = 0;
#endif
}
#define container_of(ptr, type, member) \
(((type*) (((unsigned char *)ptr) - \
(size_t)&(((type *)((unsigned char *)0))->member))))
#define avl_item_entry(ptr, type, member) \
container_of(ptr, type, member)
static INLINE void
_avl_rotate_ll(avl_root_t *rootp, avl_item_t *itemp)
{
avl_item_t *tmp;
tmp = itemp->ai_left;
itemp->ai_left = tmp->ai_right;
if (itemp->ai_left)
itemp->ai_left->ai_up = itemp;
tmp->ai_right = itemp;
if (itemp->ai_up) {
if (itemp->ai_up->ai_left == itemp) {
itemp->ai_up->ai_left = tmp;
} else {
assert(itemp->ai_up->ai_right == itemp);
itemp->ai_up->ai_right = tmp;
}
} else {
rootp->ar_root = tmp;
}
tmp->ai_up = itemp->ai_up;
itemp->ai_up = tmp;
}
static INLINE void
_avl_rotate_lr(avl_root_t *rootp, avl_item_t *itemp)
{
avl_item_t *rcp, *rlcp;
rcp = itemp->ai_left;
rlcp = rcp->ai_right;
if (itemp->ai_up) {
if (itemp == itemp->ai_up->ai_left) {
itemp->ai_up->ai_left = rlcp;
} else {
assert(itemp == itemp->ai_up->ai_right);
itemp->ai_up->ai_right = rlcp;
}
} else {
rootp->ar_root = rlcp;
}
rlcp->ai_up = itemp->ai_up;
rcp->ai_right = rlcp->ai_left;
if (rcp->ai_right)
rcp->ai_right->ai_up = rcp;
itemp->ai_left = rlcp->ai_right;
if (itemp->ai_left)
itemp->ai_left->ai_up = itemp;
rlcp->ai_left = rcp;
rlcp->ai_right = itemp;
rcp->ai_up = rlcp;
itemp->ai_up = rlcp;
}
static INLINE void
_avl_rotate_rr(avl_root_t *rootp, avl_item_t *itemp)
{
avl_item_t *tmp;
tmp = itemp->ai_right;
itemp->ai_right = tmp->ai_left;
if (itemp->ai_right)
itemp->ai_right->ai_up = itemp;
tmp->ai_left = itemp;
if (itemp->ai_up) {
if (itemp->ai_up->ai_right == itemp) {
itemp->ai_up->ai_right = tmp;
} else {
assert(itemp->ai_up->ai_left == itemp);
itemp->ai_up->ai_left = tmp;
}
} else {
rootp->ar_root = tmp;
}
tmp->ai_up = itemp->ai_up;
itemp->ai_up = tmp;
}
static INLINE void
_avl_rotate_rl(avl_root_t *rootp, avl_item_t *itemp)
{
avl_item_t *rcp, *rlcp;
rcp = itemp->ai_right;
rlcp = rcp->ai_left;
if (itemp->ai_up) {
if (itemp == itemp->ai_up->ai_right) {
itemp->ai_up->ai_right = rlcp;
} else {
assert(itemp == itemp->ai_up->ai_left);
itemp->ai_up->ai_left = rlcp;
}
} else {
rootp->ar_root = rlcp;
}
rlcp->ai_up = itemp->ai_up;
rcp->ai_left = rlcp->ai_right;
if (rcp->ai_left)
rcp->ai_left->ai_up = rcp;
itemp->ai_right = rlcp->ai_left;
if (itemp->ai_right)
itemp->ai_right->ai_up = itemp;
rlcp->ai_right = rcp;
rlcp->ai_left = itemp;
rcp->ai_up = rlcp;
itemp->ai_up = rlcp;
}
static void
avl_delete_fix(avl_root_t *rootp, avl_item_t *itemp, avl_item_t *parentp)
{
avl_item_t *childp;
if ((parentp->ai_left == NULL) &&
(parentp->ai_right == NULL)) {
assert(itemp == NULL);
parentp->ai_balance = CENT;
itemp = parentp;
parentp = itemp->ai_up;
}
while (parentp) {
if (itemp == parentp->ai_right) {
itemp = parentp->ai_left;
if (parentp->ai_balance == LEFT) {
/* Parent was left-heavy, now worse */
if (itemp->ai_balance == LEFT) {
/* If left child is also
* left-heavy, LL fixes it. */
_avl_rotate_ll(rootp, parentp);
itemp->ai_balance = CENT;
parentp->ai_balance = CENT;
parentp = itemp;
} else if (itemp->ai_balance == CENT) {
_avl_rotate_ll(rootp, parentp);
itemp->ai_balance = RIGHT;
parentp->ai_balance = LEFT;
break;
} else {
childp = itemp->ai_right;
_avl_rotate_lr(rootp, parentp);
itemp->ai_balance = CENT;
parentp->ai_balance = CENT;
if (childp->ai_balance == RIGHT)
itemp->ai_balance = LEFT;
if (childp->ai_balance == LEFT)
parentp->ai_balance = RIGHT;
childp->ai_balance = CENT;
parentp = childp;
}
} else if (parentp->ai_balance == CENT) {
parentp->ai_balance = LEFT;
break;
} else {
parentp->ai_balance = CENT;
}
} else {
itemp = parentp->ai_right;
if (parentp->ai_balance == RIGHT) {
if (itemp->ai_balance == RIGHT) {
_avl_rotate_rr(rootp, parentp);
itemp->ai_balance = CENT;
parentp->ai_balance = CENT;
parentp = itemp;
} else if (itemp->ai_balance == CENT) {
_avl_rotate_rr(rootp, parentp);
itemp->ai_balance = LEFT;
parentp->ai_balance = RIGHT;
break;
} else {
childp = itemp->ai_left;
_avl_rotate_rl(rootp, parentp);
itemp->ai_balance = CENT;
parentp->ai_balance = CENT;
if (childp->ai_balance == RIGHT)
parentp->ai_balance = LEFT;
if (childp->ai_balance == LEFT)
itemp->ai_balance = RIGHT;
childp->ai_balance = CENT;
parentp = childp;
}
} else if (parentp->ai_balance == CENT) {
parentp->ai_balance = RIGHT;
break;
} else {
parentp->ai_balance = CENT;
}
}
itemp = parentp;
parentp = itemp->ai_up;
}
}
static void
avl_insert_fix(avl_root_t *rootp, avl_item_t *itemp)
{
avl_item_t *childp, *parentp = itemp->ai_up;
itemp->ai_left = itemp->ai_right = NULL;
#ifndef NDEBUG
assert(!itemp->ai_indexed);
itemp->ai_indexed = 1;
#endif
while (parentp) {
if (itemp == parentp->ai_left) {
if (parentp->ai_balance == LEFT) {
/* Parent was left-heavy, now worse */
if (itemp->ai_balance == LEFT) {
/* If left child is also
* left-heavy, LL fixes it. */
_avl_rotate_ll(rootp, parentp);
itemp->ai_balance = CENT;
parentp->ai_balance = CENT;
break;
} else {
assert(itemp->ai_balance != CENT);
childp = itemp->ai_right;
_avl_rotate_lr(rootp, parentp);
itemp->ai_balance = CENT;
parentp->ai_balance = CENT;
if (childp->ai_balance == RIGHT)
itemp->ai_balance = LEFT;
if (childp->ai_balance == LEFT)
parentp->ai_balance = RIGHT;
childp->ai_balance = CENT;
break;
}
} else if (parentp->ai_balance == CENT) {
parentp->ai_balance = LEFT;
} else {
parentp->ai_balance = CENT;
return;
}
} else {
if (parentp->ai_balance == RIGHT) {
if (itemp->ai_balance == RIGHT) {
_avl_rotate_rr(rootp, parentp);
itemp->ai_balance = CENT;
parentp->ai_balance = CENT;
break;
} else {
assert(itemp->ai_balance != CENT);
childp = itemp->ai_left;
_avl_rotate_rl(rootp, parentp);
itemp->ai_balance = CENT;
parentp->ai_balance = CENT;
if (childp->ai_balance == RIGHT)
parentp->ai_balance = LEFT;
if (childp->ai_balance == LEFT)
itemp->ai_balance = RIGHT;
childp->ai_balance = CENT;
break;
}
} else if (parentp->ai_balance == CENT) {
parentp->ai_balance = RIGHT;
} else {
parentp->ai_balance = CENT;
break;
}
}
itemp = parentp;
parentp = itemp->ai_up;
}
}
static INLINE avl_item_t *
avl_first(avl_root_t *rootp)
{
avl_item_t *itemp = rootp->ar_root;
if (itemp) {
while (itemp->ai_left)
itemp = itemp->ai_left;
}
return itemp;
}
static INLINE avl_item_t *
avl_next(avl_item_t *itemp)
{
if (itemp->ai_right) {
itemp = itemp->ai_right;
while (itemp->ai_left)
itemp = itemp->ai_left;
return itemp;
}
while (itemp->ai_up && (itemp == itemp->ai_up->ai_right))
itemp = itemp->ai_up;
if (!itemp->ai_up)
return NULL;
return itemp->ai_up;
}
static void
avl_remove(avl_root_t *rootp, avl_item_t *itemp)
{
avl_item_t *relocp, *replacep, *parentp = NULL;
#ifndef NDEBUG
assert(itemp->ai_indexed);
itemp->ai_indexed = 0;
#endif
/* If the item is directly replaceable, do it. */
if ((itemp->ai_left == NULL) || (itemp->ai_right == NULL)) {
parentp = itemp->ai_up;
replacep = itemp->ai_left;
if (replacep == NULL)
replacep = itemp->ai_right;
if (replacep != NULL)
replacep->ai_up = parentp;
if (parentp == NULL) {
rootp->ar_root = replacep;
} else {
if (itemp == parentp->ai_left)
parentp->ai_left = replacep;
else
parentp->ai_right = replacep;
avl_delete_fix(rootp, replacep, parentp);
}
return;
}
/*
* Otherwise we do an indirect replacement with
* the item's leftmost right descendant.
*/
relocp = avl_next(itemp);
assert(relocp);
assert(relocp->ai_up != NULL);
assert(relocp->ai_left == NULL);
replacep = relocp->ai_right;
relocp->ai_left = itemp->ai_left;
if (relocp->ai_left != NULL)
relocp->ai_left->ai_up = relocp;
if (itemp->ai_up == NULL)
rootp->ar_root = relocp;
else {
if (itemp == itemp->ai_up->ai_left)
itemp->ai_up->ai_left = relocp;
else
itemp->ai_up->ai_right = relocp;
}
if (relocp == relocp->ai_up->ai_left) {
assert(relocp->ai_up != itemp);
relocp->ai_up->ai_left = replacep;
parentp = relocp->ai_up;
if (replacep != NULL)
replacep->ai_up = relocp->ai_up;
relocp->ai_right = itemp->ai_right;
} else {
assert(relocp->ai_up == itemp);
relocp->ai_right = replacep;
parentp = relocp;
}
if (relocp->ai_right != NULL)
relocp->ai_right->ai_up = relocp;
relocp->ai_up = itemp->ai_up;
relocp->ai_balance = itemp->ai_balance;
avl_delete_fix(rootp, replacep, parentp);
}
#endif /* !defined (__VG_AVL_H__) */