summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/btree.c26
-rw-r--r--src/tree.c25
2 files changed, 30 insertions, 21 deletions
diff --git a/src/btree.c b/src/btree.c
index 21db8d6..4a8c4f6 100644
--- a/src/btree.c
+++ b/src/btree.c
@@ -115,13 +115,15 @@ zix_btree_new(const ZixComparator cmp, const void* const cmp_data)
}
static void
-zix_btree_free_children(ZixBTree* const t,
- ZixBTreeNode* const n,
- ZixDestroyFunc destroy)
+zix_btree_free_children(ZixBTree* const t,
+ ZixBTreeNode* const n,
+ const ZixDestroyFunc destroy,
+ const void* const destroy_user_data)
{
if (!n->is_leaf) {
for (ZixShort i = 0; i < n->n_vals + 1u; ++i) {
- zix_btree_free_children(t, zix_btree_child(n, i), destroy);
+ zix_btree_free_children(
+ t, zix_btree_child(n, i), destroy, destroy_user_data);
free(zix_btree_child(n, i));
}
}
@@ -129,30 +131,34 @@ zix_btree_free_children(ZixBTree* const t,
if (destroy) {
if (n->is_leaf) {
for (ZixShort i = 0u; i < n->n_vals; ++i) {
- destroy(n->data.leaf.vals[i]);
+ destroy(n->data.leaf.vals[i], destroy_user_data);
}
} else {
for (ZixShort i = 0u; i < n->n_vals; ++i) {
- destroy(n->data.inode.vals[i]);
+ destroy(n->data.inode.vals[i], destroy_user_data);
}
}
}
}
void
-zix_btree_free(ZixBTree* const t, ZixDestroyFunc destroy)
+zix_btree_free(ZixBTree* const t,
+ const ZixDestroyFunc destroy,
+ const void* const destroy_user_data)
{
if (t) {
- zix_btree_clear(t, destroy);
+ zix_btree_clear(t, destroy, destroy_user_data);
free(t->root);
free(t);
}
}
void
-zix_btree_clear(ZixBTree* const t, ZixDestroyFunc destroy)
+zix_btree_clear(ZixBTree* const t,
+ ZixDestroyFunc destroy,
+ const void* destroy_user_data)
{
- zix_btree_free_children(t, t->root, destroy);
+ zix_btree_free_children(t, t->root, destroy, destroy_user_data);
memset(t->root, 0, sizeof(ZixBTreeNode));
t->root->is_leaf = true;
diff --git a/src/tree.c b/src/tree.c
index 9faf13c..f26ff90 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -27,6 +27,7 @@ typedef struct ZixTreeNodeImpl ZixTreeNode;
struct ZixTreeImpl {
ZixTreeNode* root;
ZixDestroyFunc destroy;
+ const void* destroy_user_data;
ZixComparator cmp;
void* cmp_data;
size_t size;
@@ -69,15 +70,17 @@ ZixTree*
zix_tree_new(bool allow_duplicates,
ZixComparator cmp,
void* cmp_data,
- ZixDestroyFunc destroy)
+ ZixDestroyFunc destroy,
+ const void* destroy_user_data)
{
- ZixTree* t = (ZixTree*)malloc(sizeof(ZixTree));
- t->root = NULL;
- t->destroy = destroy;
- t->cmp = cmp;
- t->cmp_data = cmp_data;
- t->size = 0;
- t->allow_duplicates = allow_duplicates;
+ ZixTree* t = (ZixTree*)malloc(sizeof(ZixTree));
+ t->root = NULL;
+ t->destroy = destroy;
+ t->destroy_user_data = destroy_user_data;
+ t->cmp = cmp;
+ t->cmp_data = cmp_data;
+ t->size = 0;
+ t->allow_duplicates = allow_duplicates;
return t;
}
@@ -88,7 +91,7 @@ zix_tree_free_rec(ZixTree* t, ZixTreeNode* n)
zix_tree_free_rec(t, n->left);
zix_tree_free_rec(t, n->right);
if (t->destroy) {
- t->destroy(n->data);
+ t->destroy(n->data, t->destroy_user_data);
}
free(n);
}
@@ -451,7 +454,7 @@ zix_tree_remove(ZixTree* t, ZixTreeIter* ti)
if ((n == t->root) && !n->left && !n->right) {
t->root = NULL;
if (t->destroy) {
- t->destroy(n->data);
+ t->destroy(n->data, t->destroy_user_data);
}
free(n);
--t->size;
@@ -583,7 +586,7 @@ zix_tree_remove(ZixTree* t, ZixTreeIter* ti)
DUMP(t);
if (t->destroy) {
- t->destroy(n->data);
+ t->destroy(n->data, t->destroy_user_data);
}
free(n);