projects
/
collectd.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge pull request #1308 from mfournier/openldap-persistent-connection
[collectd.git]
/
src
/
daemon
/
utils_avltree.c
diff --git
a/src/daemon/utils_avltree.c
b/src/daemon/utils_avltree.c
index
04e5403
..
1680c41
100644
(file)
--- a/
src/daemon/utils_avltree.c
+++ b/
src/daemon/utils_avltree.c
@@
-146,6
+146,9
@@
static c_avl_node_t *rotate_right (c_avl_tree_t *t, c_avl_node_t *x)
c_avl_node_t *y;
c_avl_node_t *b;
c_avl_node_t *y;
c_avl_node_t *b;
+ assert (x != NULL);
+ assert (x->left != NULL);
+
p = x->parent;
y = x->left;
b = y->right;
p = x->parent;
y = x->left;
b = y->right;
@@
-170,7
+173,7
@@
static c_avl_node_t *rotate_right (c_avl_tree_t *t, c_avl_node_t *x)
y->height = calc_height (y);
return (y);
y->height = calc_height (y);
return (y);
-} /* void rotate_
lef
t */
+} /* void rotate_
righ
t */
/*
* (x) (y)
/*
* (x) (y)
@@
-187,6
+190,9
@@
static c_avl_node_t *rotate_left (c_avl_tree_t *t, c_avl_node_t *x)
c_avl_node_t *y;
c_avl_node_t *b;
c_avl_node_t *y;
c_avl_node_t *b;
+ assert (x != NULL);
+ assert (x->right != NULL);
+
p = x->parent;
y = x->right;
b = y->left;
p = x->parent;
y = x->right;
b = y->left;
@@
-239,7
+245,7
@@
static void rebalance (c_avl_tree_t *t, c_avl_node_t *n)
{
assert (n->right != NULL);
b_bottom = BALANCE (n->right);
{
assert (n->right != NULL);
b_bottom = BALANCE (n->right);
- assert ((b_bottom >= -1)
||
(b_bottom <= 1));
+ assert ((b_bottom >= -1)
&&
(b_bottom <= 1));
if (b_bottom == 1)
n = rotate_right_left (t, n);
else
if (b_bottom == 1)
n = rotate_right_left (t, n);
else
@@
-249,7
+255,7
@@
static void rebalance (c_avl_tree_t *t, c_avl_node_t *n)
{
assert (n->left != NULL);
b_bottom = BALANCE (n->left);
{
assert (n->left != NULL);
b_bottom = BALANCE (n->left);
- assert ((b_bottom >= -1)
||
(b_bottom <= 1));
+ assert ((b_bottom >= -1)
&&
(b_bottom <= 1));
if (b_bottom == -1)
n = rotate_left_right (t, n);
else
if (b_bottom == -1)
n = rotate_left_right (t, n);
else
@@
-479,7
+485,7
@@
c_avl_tree_t *c_avl_create (int (*compare) (const void *, const void *))
if (compare == NULL)
return (NULL);
if (compare == NULL)
return (NULL);
- if ((t =
(c_avl_tree_t *) malloc (sizeof (c_avl_tree_
t))) == NULL)
+ if ((t =
malloc (sizeof (*
t))) == NULL)
return (NULL);
t->root = NULL;
return (NULL);
t->root = NULL;
@@
-503,7
+509,7
@@
int c_avl_insert (c_avl_tree_t *t, void *key, void *value)
c_avl_node_t *nptr;
int cmp;
c_avl_node_t *nptr;
int cmp;
- if ((new =
(c_avl_node_t *) malloc (sizeof (c_avl_node_t
))) == NULL)
+ if ((new =
malloc (sizeof (*new
))) == NULL)
return (-1);
new->key = key;
return (-1);
new->key = key;
@@
-617,10
+623,18
@@
int c_avl_pick (c_avl_tree_t *t, void **key, void **value)
n = t->root;
while ((n->left != NULL) || (n->right != NULL))
{
n = t->root;
while ((n->left != NULL) || (n->right != NULL))
{
- int height_left = (n->left == NULL) ? 0 : n->left->height;
- int height_right = (n->right == NULL) ? 0 : n->right->height;
+ if (n->left == NULL)
+ {
+ n = n->right;
+ continue;
+ }
+ else if (n->right == NULL)
+ {
+ n = n->left;
+ continue;
+ }
- if (
height_left > height_r
ight)
+ if (
n->left->height > n->right->he
ight)
n = n->left;
else
n = n->right;
n = n->left;
else
n = n->right;
@@
-638,6
+652,7
@@
int c_avl_pick (c_avl_tree_t *t, void **key, void **value)
*value = n->value;
free_node (n);
*value = n->value;
free_node (n);
+ --t->size;
rebalance (t, p);
return (0);
rebalance (t, p);
return (0);
@@
-650,10
+665,9
@@
c_avl_iterator_t *c_avl_get_iterator (c_avl_tree_t *t)
if (t == NULL)
return (NULL);
if (t == NULL)
return (NULL);
- iter =
(c_avl_iterator_t *) malloc (sizeof (c_avl_iterator_t
));
+ iter =
calloc (1, sizeof (*iter
));
if (iter == NULL)
return (NULL);
if (iter == NULL)
return (NULL);
- memset (iter, '\0', sizeof (c_avl_iterator_t));
iter->tree = t;
return (iter);
iter->tree = t;
return (iter);