From: Elias Fleckenstein Date: Wed, 13 Apr 2022 19:06:57 +0000 (+0200) Subject: Add transformer to del X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=7710400cacc6ff407dcf69a34e95fd722d4713e5;p=dragonstd.git Add transformer to del --- diff --git a/bits/wrappers.h b/bits/wrappers.h index a278679..5985519 100644 --- a/bits/wrappers.h +++ b/bits/wrappers.h @@ -20,15 +20,15 @@ return trans ? trans((*node)->dat) : (*node)->dat; \ } \ \ - bool prefix ## del(Type *self, void *key, Comparator cmp, Callback call, void *arg) \ + bool prefix ## del(Type *self, void *key, Comparator cmp, Callback call, void *arg, Transformer trans) \ { \ Type ## Node **node = prefix ## nfd(self, key, cmp); \ \ if (!*node) \ return false; \ \ - if (call) \ - call((*node)->dat, arg); \ + if (call) \ + call(trans ? trans((*node)->dat) : (*node)->dat, arg); \ \ prefix ## nrm(self, node); \ return true; \ diff --git a/flag.c b/flag.c index e531546..c415ba6 100644 --- a/flag.c +++ b/flag.c @@ -26,7 +26,7 @@ void flag_sub(Flag *flag, pthread_cond_t *cnd) void flag_uns(Flag *flag, pthread_cond_t *cnd) { pthread_mutex_lock(&flag->mtx); - list_del(&flag->cvs, cnd, &cmp_ref, NULL, NULL); + list_del(&flag->cvs, cnd, &cmp_ref, NULL, NULL, NULL); pthread_mutex_unlock(&flag->mtx); } diff --git a/list.h b/list.h index 7a9e904..16fd46b 100644 --- a/list.h +++ b/list.h @@ -50,7 +50,7 @@ void *list_get(List *list, void *key, Comparator cmp, Transformer trans); The first matching element is returned, or NULL if none found. */ -bool list_del(List *list, void *key, Comparator cmp, Callback call, void *arg); +bool list_del(List *list, void *key, Comparator cmp, Callback call, void *arg, Transformer trans); /* Delete an element from the list if it is found. Return whether an element has been deleted. diff --git a/map.c b/map.c index 4917280..899d25c 100644 --- a/map.c +++ b/map.c @@ -65,12 +65,12 @@ void *map_get(Map *map, void *key, Comparator cmp, Transformer trans) return ret; } -bool map_del(Map *map, void *key, Comparator cmp, Callback call, void *arg) +bool map_del(Map *map, void *key, Comparator cmp, Callback call, void *arg, Transformer trans) { if (!get_lock(map, true)) return false; - bool ret = tree_del(&map->tre, key, cmp, call, arg); + bool ret = tree_del(&map->tre, key, cmp, call, arg, trans); pthread_rwlock_unlock(&map->tlk); return ret; } diff --git a/map.h b/map.h index 0c42ac8..3733b9b 100644 --- a/map.h +++ b/map.h @@ -67,7 +67,7 @@ void *map_get(Map *map, void *key, Comparator cmp, Transformer trans); Get an element from the map, or return NULL if none found. */ -bool map_del(Map *map, void *key, Comparator cmp, Callback call, void *arg); +bool map_del(Map *map, void *key, Comparator cmp, Callback call, void *arg, Transformer trans); /* [Thread Safe] Delete an element from the map if it is found. diff --git a/test/test_list.c b/test/test_list.c index baa5d91..5dba6d2 100644 --- a/test/test_list.c +++ b/test/test_list.c @@ -38,7 +38,7 @@ int main() assert(list_get(&list, &e, &cmp_int, NULL) == NULL); printf("testing del\n"); - assert(list_del(&list, &a, &cmp_int, NULL, NULL)); + assert(list_del(&list, &a, &cmp_int, NULL, NULL, NULL)); assert(list_get(&list, &a, &cmp_int, NULL) == NULL); printf("testing clr\n"); diff --git a/test/test_refcount_map.c b/test/test_refcount_map.c index bb369a2..9d717c1 100644 --- a/test/test_refcount_map.c +++ b/test/test_refcount_map.c @@ -95,7 +95,7 @@ static void *thread_delete(unsigned int *result) while (!cancel) { unsigned int id = rand_id(); - if (map_del(&map, &id, &data_object_compare_id, (void *) &refcount_drp, NULL)) + if (map_del(&map, &id, &data_object_compare_id, (void *) &refcount_drp, NULL, NULL)) (*result)++; } diff --git a/test/test_tree.c b/test/test_tree.c index b7e1391..dbe9899 100644 --- a/test/test_tree.c +++ b/test/test_tree.c @@ -51,7 +51,7 @@ int main() assert(tree_get(&tree, &e, &cmp_int, NULL) == NULL); printf("testing del\n"); - assert(tree_del(&tree, &a, &cmp_int, NULL, NULL)); + assert(tree_del(&tree, &a, &cmp_int, NULL, NULL, NULL)); assert(tree_get(&tree, &a, &cmp_int, NULL) == NULL); printf("testing clr\n"); diff --git a/tree.h b/tree.h index a55671f..2ce1b11 100644 --- a/tree.h +++ b/tree.h @@ -58,7 +58,7 @@ void *tree_get(Tree *tree, void *key, Comparator cmp, Transformer trans); Get an element from the tree, or return NULL if none found. */ -bool tree_del(Tree *tree, void *key, Comparator cmp, Callback call, void *arg); +bool tree_del(Tree *tree, void *key, Comparator cmp, Callback call, void *arg, Transformer trans); /* Delete an element from the tree if it is found. Return whether an element has been deleted.