]> git.lizzy.rs Git - dragonstd.git/commitdiff
Add transformer to del
authorElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 13 Apr 2022 19:06:57 +0000 (21:06 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 13 Apr 2022 19:06:57 +0000 (21:06 +0200)
bits/wrappers.h
flag.c
list.h
map.c
map.h
test/test_list.c
test/test_refcount_map.c
test/test_tree.c
tree.h

index a2786791875e0c31a34dd664136b2d526330ddb4..59855193c487bfe1320f9f77e427e8b024b1db88 100644 (file)
                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 e53154611dffbe4765c26c2c9673688929ea4b76..c415ba60e206a5d39c70cd99fe2a9ce0203ddf66 100644 (file)
--- 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 7a9e904d92040722f8e6452ce05d7603aa50f96e..16fd46b94029bf9b81ce1d5203b4f8fb5e1a551f 100644 (file)
--- 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 491728025e7e6d0706f6dd3d340b2ae9588581e2..899d25c3486511ebe312ce28382e3a880ea68f6b 100644 (file)
--- 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 0c42ac8c4cc9463c55cc8f5851b6e2a9a0f1cfd0..3733b9bd31292a7d3630bb66f94a0508b2c862c8 100644 (file)
--- 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.
index baa5d91bd1324b683f7ccba47aa3a71678b8acb1..5dba6d2d7689bc4f9a9e23f7564295cf0e481fc9 100644 (file)
@@ -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");
index bb369a26b3a10bde7edde2408a7d7f18109bc62a..9d717c184f81aab1c8ec8175689872319d0c6c8b 100644 (file)
@@ -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)++;
        }
 
index b7e139121e2b1e6388118f191d5dd2596f82d601..dbe98998d09861ddd0fabd6f467a42c098cae043 100644 (file)
@@ -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 a55671f3dd5481272bb04b97591a0736f7f7bd62..2ce1b116bc57655cadf5b0a65d9028fea10c9411 100644 (file)
--- 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.