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
|
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "../libnetdata.h"
/* ------------------------------------------------------------------------- */
/*
* avl_insert(), avl_remove() and avl_search()
* are adaptations (by Costa Tsaousis) of the AVL algorithm found in libavl
* v2.0.3, so that they do not use any memory allocations and their memory
* footprint is optimized (by eliminating non-necessary data members).
*
* libavl - library for manipulation of binary trees.
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software
* Foundation, Inc.
*/
/* Search |tree| for an item matching |item|, and return it if found.
Otherwise return |NULL|. */
avl *avl_search(avl_tree_type *tree, avl *item) {
avl *p;
// assert (tree != NULL && item != NULL);
for (p = tree->root; p != NULL; ) {
int cmp = tree->compar(item, p);
if (cmp < 0)
p = p->avl_link[0];
else if (cmp > 0)
p = p->avl_link[1];
else /* |cmp == 0| */
return p;
}
return NULL;
}
/* Inserts |item| into |tree| and returns a pointer to |item|'s address.
If a duplicate item is found in the tree,
returns a pointer to the duplicate without inserting |item|.
*/
avl *avl_insert(avl_tree_type *tree, avl *item) {
avl *y, *z; /* Top node to update balance factor, and parent. */
avl *p, *q; /* Iterator, and parent. */
avl *n; /* Newly inserted node. */
avl *w; /* New root of rebalanced subtree. */
unsigned char dir; /* Direction to descend. */
unsigned char da[AVL_MAX_HEIGHT]; /* Cached comparison results. */
int k = 0; /* Number of cached results. */
// assert(tree != NULL && item != NULL);
z = (avl *) &tree->root;
y = tree->root;
dir = 0;
for (q = z, p = y; p != NULL; q = p, p = p->avl_link[dir]) {
int cmp = tree->compar(item, p);
if (cmp == 0)
return p;
if (p->avl_balance != 0)
z = q, y = p, k = 0;
da[k++] = dir = (unsigned char)(cmp > 0);
}
n = q->avl_link[dir] = item;
// tree->avl_count++;
n->avl_link[0] = n->avl_link[1] = NULL;
n->avl_balance = 0;
if (y == NULL) return n;
for (p = y, k = 0; p != n; p = p->avl_link[da[k]], k++)
if (da[k] == 0)
p->avl_balance--;
else
p->avl_balance++;
if (y->avl_balance == -2) {
avl *x = y->avl_link[0];
if (x->avl_balance == -1) {
w = x;
y->avl_link[0] = x->avl_link[1];
x->avl_link[1] = y;
x->avl_balance = y->avl_balance = 0;
}
else {
// assert (x->avl_balance == +1);
w = x->avl_link[1];
x->avl_link[1] = w->avl_link[0];
w->avl_link[0] = x;
y->avl_link[0] = w->avl_link[1];
w->avl_link[1] = y;
if (w->avl_balance == -1)
x->avl_balance = 0, y->avl_balance = +1;
else if (w->avl_balance == 0)
x->avl_balance = y->avl_balance = 0;
else /* |w->avl_balance == +1| */
x->avl_balance = -1, y->avl_balance = 0;
w->avl_balance = 0;
}
}
else if (y->avl_balance == +2) {
avl *x = y->avl_link[1];
if (x->avl_balance == +1) {
w = x;
y->avl_link[1] = x->avl_link[0];
x->avl_link[0] = y;
x->avl_balance = y->avl_balance = 0;
}
else {
// assert (x->avl_balance == -1);
w = x->avl_link[0];
x->avl_link[0] = w->avl_link[1];
w->avl_link[1] = x;
y->avl_link[1] = w->avl_link[0];
w->avl_link[0] = y;
if (w->avl_balance == +1)
x->avl_balance = 0, y->avl_balance = -1;
else if (w->avl_balance == 0)
x->avl_balance = y->avl_balance = 0;
else /* |w->avl_balance == -1| */
x->avl_balance = +1, y->avl_balance = 0;
w->avl_balance = 0;
}
}
else return n;
z->avl_link[y != z->avl_link[0]] = w;
// tree->avl_generation++;
return n;
}
/* Deletes from |tree| and returns an item matching |item|.
Returns a null pointer if no matching item found. */
avl *avl_remove(avl_tree_type *tree, avl *item) {
/* Stack of nodes. */
avl *pa[AVL_MAX_HEIGHT]; /* Nodes. */
unsigned char da[AVL_MAX_HEIGHT]; /* |avl_link[]| indexes. */
int k; /* Stack pointer. */
avl *p; /* Traverses tree to find node to delete. */
int cmp; /* Result of comparison between |item| and |p|. */
// assert (tree != NULL && item != NULL);
k = 0;
p = (avl *) &tree->root;
for(cmp = -1; cmp != 0; cmp = tree->compar(item, p)) {
unsigned char dir = (unsigned char)(cmp > 0);
pa[k] = p;
da[k++] = dir;
p = p->avl_link[dir];
if(p == NULL) return NULL;
}
item = p;
if (p->avl_link[1] == NULL)
pa[k - 1]->avl_link[da[k - 1]] = p->avl_link[0];
else {
avl *r = p->avl_link[1];
if (r->avl_link[0] == NULL) {
r->avl_link[0] = p->avl_link[0];
r->avl_balance = p->avl_balance;
pa[k - 1]->avl_link[da[k - 1]] = r;
da[k] = 1;
pa[k++] = r;
}
else {
avl *s;
int j = k++;
for (;;) {
da[k] = 0;
pa[k++] = r;
s = r->avl_link[0];
if (s->avl_link[0] == NULL) break;
r = s;
}
s->avl_link[0] = p->avl_link[0];
r->avl_link[0] = s->avl_link[1];
s->avl_link[1] = p->avl_link[1];
s->avl_balance = p->avl_balance;
pa[j - 1]->avl_link[da[j - 1]] = s;
da[j] = 1;
pa[j] = s;
}
}
// assert (k > 0);
while (--k > 0) {
avl *y = pa[k];
if (da[k] == 0) {
y->avl_balance++;
if (y->avl_balance == +1) break;
else if (y->avl_balance == +2) {
avl *x = y->avl_link[1];
if (x->avl_balance == -1) {
avl *w;
// assert (x->avl_balance == -1);
w = x->avl_link[0];
x->avl_link[0] = w->avl_link[1];
w->avl_link[1] = x;
y->avl_link[1] = w->avl_link[0];
w->avl_link[0] = y;
if (w->avl_balance == +1)
x->avl_balance = 0, y->avl_balance = -1;
else if (w->avl_balance == 0)
x->avl_balance = y->avl_balance = 0;
else /* |w->avl_balance == -1| */
x->avl_balance = +1, y->avl_balance = 0;
w->avl_balance = 0;
pa[k - 1]->avl_link[da[k - 1]] = w;
}
else {
y->avl_link[1] = x->avl_link[0];
x->avl_link[0] = y;
pa[k - 1]->avl_link[da[k - 1]] = x;
if (x->avl_balance == 0) {
x->avl_balance = -1;
y->avl_balance = +1;
break;
}
else x->avl_balance = y->avl_balance = 0;
}
}
}
else
{
y->avl_balance--;
if (y->avl_balance == -1) break;
else if (y->avl_balance == -2) {
avl *x = y->avl_link[0];
if (x->avl_balance == +1) {
avl *w;
// assert (x->avl_balance == +1);
w = x->avl_link[1];
x->avl_link[1] = w->avl_link[0];
w->avl_link[0] = x;
y->avl_link[0] = w->avl_link[1];
w->avl_link[1] = y;
if (w->avl_balance == -1)
x->avl_balance = 0, y->avl_balance = +1;
else if (w->avl_balance == 0)
x->avl_balance = y->avl_balance = 0;
else /* |w->avl_balance == +1| */
x->avl_balance = -1, y->avl_balance = 0;
w->avl_balance = 0;
pa[k - 1]->avl_link[da[k - 1]] = w;
}
else {
y->avl_link[0] = x->avl_link[1];
x->avl_link[1] = y;
pa[k - 1]->avl_link[da[k - 1]] = x;
if (x->avl_balance == 0) {
x->avl_balance = +1;
y->avl_balance = -1;
break;
}
else x->avl_balance = y->avl_balance = 0;
}
}
}
}
// tree->avl_count--;
// tree->avl_generation++;
return item;
}
/* ------------------------------------------------------------------------- */
// below are functions by (C) Costa Tsaousis
// ---------------------------
// traversing
int avl_walker(avl *node, int (*callback)(void * /*entry*/, void * /*data*/), void *data) {
int total = 0, ret = 0;
if(node->avl_link[0]) {
ret = avl_walker(node->avl_link[0], callback, data);
if(ret < 0) return ret;
total += ret;
}
ret = callback(node, data);
if(ret < 0) return ret;
total += ret;
if(node->avl_link[1]) {
ret = avl_walker(node->avl_link[1], callback, data);
if (ret < 0) return ret;
total += ret;
}
return total;
}
int avl_traverse(avl_tree_type *tree, int (*callback)(void * /*entry*/, void * /*data*/), void *data) {
if(tree->root)
return avl_walker(tree->root, callback, data);
else
return 0;
}
// ---------------------------
// locks
void avl_read_lock(avl_tree_lock *t) {
#ifndef AVL_WITHOUT_PTHREADS
#ifdef AVL_LOCK_WITH_MUTEX
netdata_mutex_lock(&t->mutex);
#else
netdata_rwlock_rdlock(&t->rwlock);
#endif
#endif /* AVL_WITHOUT_PTHREADS */
}
void avl_write_lock(avl_tree_lock *t) {
#ifndef AVL_WITHOUT_PTHREADS
#ifdef AVL_LOCK_WITH_MUTEX
netdata_mutex_lock(&t->mutex);
#else
netdata_rwlock_wrlock(&t->rwlock);
#endif
#endif /* AVL_WITHOUT_PTHREADS */
}
void avl_unlock(avl_tree_lock *t) {
#ifndef AVL_WITHOUT_PTHREADS
#ifdef AVL_LOCK_WITH_MUTEX
netdata_mutex_unlock(&t->mutex);
#else
netdata_rwlock_unlock(&t->rwlock);
#endif
#endif /* AVL_WITHOUT_PTHREADS */
}
// ---------------------------
// operations with locking
void avl_init_lock(avl_tree_lock *tree, int (*compar)(void * /*a*/, void * /*b*/)) {
avl_init(&tree->avl_tree, compar);
#ifndef AVL_WITHOUT_PTHREADS
int lock;
#ifdef AVL_LOCK_WITH_MUTEX
lock = netdata_mutex_init(&tree->mutex, NULL);
#else
lock = netdata_rwlock_init(&tree->rwlock);
#endif
if(lock != 0)
fatal("Failed to initialize AVL mutex/rwlock, error: %d", lock);
#endif /* AVL_WITHOUT_PTHREADS */
}
void avl_destroy_lock(avl_tree_lock *tree) {
#ifndef AVL_WITHOUT_PTHREADS
int lock;
#ifdef AVL_LOCK_WITH_MUTEX
lock = pthread_mutex_destroy(&tree->mutex);
#else
lock = pthread_rwlock_destroy(&tree->rwlock);
#endif
if(lock != 0)
fatal("Failed to destroy AVL mutex/rwlock, error: %d", lock);
#endif /* AVL_WITHOUT_PTHREADS */
}
avl *avl_search_lock(avl_tree_lock *tree, avl *item) {
avl_read_lock(tree);
avl *ret = avl_search(&tree->avl_tree, item);
avl_unlock(tree);
return ret;
}
avl * avl_remove_lock(avl_tree_lock *tree, avl *item) {
avl_write_lock(tree);
avl *ret = avl_remove(&tree->avl_tree, item);
avl_unlock(tree);
return ret;
}
avl *avl_insert_lock(avl_tree_lock *tree, avl *item) {
avl_write_lock(tree);
avl * ret = avl_insert(&tree->avl_tree, item);
avl_unlock(tree);
return ret;
}
int avl_traverse_lock(avl_tree_lock *tree, int (*callback)(void * /*entry*/, void * /*data*/), void *data) {
int ret;
avl_read_lock(tree);
ret = avl_traverse(&tree->avl_tree, callback, data);
avl_unlock(tree);
return ret;
}
void avl_init(avl_tree_type *tree, int (*compar)(void * /*a*/, void * /*b*/)) {
tree->root = NULL;
tree->compar = compar;
}
// ------------------
|