free (n);
}
-static avl_node_t *search (avl_tree_t *t, void *key)
+static avl_node_t *search (avl_tree_t *t, const void *key)
{
avl_node_t *n;
int cmp;
return (r);
}
-static void *_remove (avl_tree_t *t, avl_node_t *n)
+static int _remove (avl_tree_t *t, avl_node_t *n)
{
- void *ret;
-
assert ((t != NULL) && (n != NULL));
- ret = n->value;
-
if ((n->left == NULL) && (n->right == NULL))
{
/* Deleting a leave is easy */
_remove (t, r);
}
- return (ret);
+ return (0);
} /* void *_remove */
/*
return (0);
} /* int avl_insert */
-void *avl_remove (avl_tree_t *t, void *key)
+int avl_remove (avl_tree_t *t, void *key, void **rkey, void **rvalue)
{
avl_node_t *n;
n = search (t, key);
if (n == NULL)
- return (NULL);
+ return (-1);
+
+ if (rkey != NULL)
+ *rkey = n->key;
+ if (rvalue != NULL)
+ *rvalue = n->value;
return (_remove (t, n));
} /* void *avl_remove */
-void *avl_get (avl_tree_t *t, void *key)
+int avl_get (avl_tree_t *t, const void *key, void **value)
{
avl_node_t *n;
+ assert (value != NULL);
+
n = search (t, key);
if (n == NULL)
- return (NULL);
+ return (-1);
- return (n->value);
+ *value = n->value;
+
+ return (0);
}
avl_iterator_t *avl_get_iterator (avl_tree_t *t)
struct avl_iterator_s;
typedef struct avl_iterator_s avl_iterator_t;
+struct avl_keyval_s
+{
+ void *key;
+ void *value;
+};
+
avl_tree_t *avl_create (int (*compare) (const void *, const void *));
void avl_destroy (avl_tree_t *t);
int avl_insert (avl_tree_t *t, void *key, void *value);
-void *avl_remove (avl_tree_t *t, void *key);
+int avl_remove (avl_tree_t *t, void *key, void **rkey, void **rvalue);
-void *avl_get (avl_tree_t *t, void *key);
+int avl_get (avl_tree_t *t, const void *key, void **value);
avl_iterator_t *avl_get_iterator (avl_tree_t *t);
void *avl_iterator_next (avl_iterator_t *iter);