]> git.lizzy.rs Git - dragonstd.git/commitdiff
Add key to add functions
authorElias Fleckenstein <eliasfleckenstein@web.de>
Thu, 14 Apr 2022 19:57:49 +0000 (21:57 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Thu, 14 Apr 2022 19:57:49 +0000 (21:57 +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 62bce2b7f67f6ed2cc2b54b5044e4b5e736f7c02..a34709021c816c8d01f92b5b30e72a5fa04bbc55 100644 (file)
@@ -1,9 +1,9 @@
 #include "callback.h" // for Transformer, Callback
 
 #define WRAP_NODE_FUNCTIONS(Type, prefix) \
-       bool prefix ## add(Type *self, void *dat, void *cmp, void *trans) \
+       bool prefix ## add(Type *self, void *key, void *dat, void *cmp, void *trans) \
        { \
-               Type ## Node **node = prefix ## nfd(self, dat, cmp); \
+               Type ## Node **node = prefix ## nfd(self, key, cmp); \
  \
                if (*node) \
                        return false; \
diff --git a/flag.c b/flag.c
index c415ba60e206a5d39c70cd99fe2a9ce0203ddf66..2aa6a90112000cc17285df6349e01e5b2b163ca1 100644 (file)
--- a/flag.c
+++ b/flag.c
@@ -19,7 +19,7 @@ void flag_dst(Flag *flag)
 void flag_sub(Flag *flag, pthread_cond_t *cnd)
 {
        pthread_mutex_lock(&flag->mtx);
-       list_add(&flag->cvs, cnd, &cmp_ref, NULL);
+       list_add(&flag->cvs, cnd, cnd, &cmp_ref, NULL);
        pthread_mutex_unlock(&flag->mtx);
 }
 
diff --git a/list.h b/list.h
index 4a7333b26a8e62a9be1450997506cd9bfca73d3a..9e03023e2cf48cad86b072c9ebc356c7693822f6 100644 (file)
--- a/list.h
+++ b/list.h
@@ -34,7 +34,7 @@ void list_ini(List *list);
        This function should be called before any other function is called on the list.
 */
 
-bool list_add(List *list, void *dat, void *cmp, void *trans);
+bool list_add(List *list, void *key, void *dat, void *cmp, void *trans);
 /*
        Add an element to the list.
 
diff --git a/map.c b/map.c
index 81751b481f149f50d26449965bc02cd09afc087c..dabed94b5a6f46a7787f1107cf7cdee44fe7d2af 100644 (file)
--- a/map.c
+++ b/map.c
@@ -46,12 +46,12 @@ void map_cnl(Map *map, void *iter, void *arg, void *trans, TreeTraversionOrder o
        pthread_rwlock_unlock(&map->tlk);
 }
 
-bool map_add(Map *map, void *dat, void *cmp, void *trans)
+bool map_add(Map *map, void *key, void *dat, void *cmp, void *trans)
 {
        if (!get_lock(map, true))
                return false;
 
-       bool ret = tree_add(&map->tre, dat, cmp, trans);
+       bool ret = tree_add(&map->tre, key, dat, cmp, trans);
        pthread_rwlock_unlock(&map->tlk);
        return ret;
 }
diff --git a/map.h b/map.h
index eea5f0ef4abbe46ac9821ec300bcc73c390031a9..5d676578c26a04a0a6a5b98771aac842b3337377 100644 (file)
--- a/map.h
+++ b/map.h
@@ -51,7 +51,7 @@ void map_cnl(Map *map, void *iter, void *arg, void *trans, TreeTraversionOrder o
        If no callback is given, the traversion order is irrelevant.
 */
 
-bool map_add(Map *map, void *dat, void *cmp, void *trans);
+bool map_add(Map *map, void *key, void *dat, void *cmp, void *trans);
 /*
        [Thread Safe]
        Add an element to the map.
index 3645c048a200dc48eafb29fafad3da1332b521b7..8f47d303b9f036e696a4f485d8b08265200a6f2f 100644 (file)
@@ -25,10 +25,10 @@ int main()
        int e = 3;
 
        printf("testing add\n");
-       assert(list_add(&list, &a, &cmp_int, NULL));
-       assert(list_add(&list, &b, &cmp_int, NULL));
-       assert(list_add(&list, &c, &cmp_int, NULL));
-       assert(!list_add(&list, &d, &cmp_int, NULL));
+       assert(list_add(&list, &a, &a, &cmp_int, NULL));
+       assert(list_add(&list, &b, &b, &cmp_int, NULL));
+       assert(list_add(&list, &c, &c, &cmp_int, NULL));
+       assert(!list_add(&list, &d, &d, &cmp_int, NULL));
 
        printf("testing get\n");
        assert(list_get(&list, &a, &cmp_int, NULL) == &a);
index fc3b5e4596cc63d9a5537380c06a0b26db63b551..6a8c03998220d06cda367bc4c49e3286fd0435b1 100644 (file)
@@ -32,11 +32,6 @@ int cmp_obj(const Refcount *rc, const int *id)
        return ((DataObject *) rc->obj)->id - *id;
 }
 
-int cmp_obj_sym(const Refcount *rc1, const Refcount *rc2)
-{
-       return cmp_obj(rc1, &((DataObject *) rc2->obj)->id);
-}
-
 static void *thread_create(unsigned int *result)
 {
        while (!cancel) {
@@ -45,7 +40,7 @@ static void *thread_create(unsigned int *result)
 
                refcount_ini(&obj->rc, obj, &delete_obj);
 
-               if (map_add(&map, &obj->rc, &cmp_obj_sym, &refcount_inc))
+               if (map_add(&map, &obj->id, &obj->rc, &cmp_obj, &refcount_inc))
                        (*result)++;
 
                refcount_drp(&obj->rc);
index 005814b64de29d3c7501d1db1ed49b4be7f8f0d6..44a8f728b166498e7c24bcc3f21664fa369e763a 100644 (file)
@@ -38,10 +38,10 @@ int main()
        int e = 3;
 
        printf("testing add\n");
-       assert(tree_add(&tree, &a, &cmp_int, NULL));
-       assert(tree_add(&tree, &b, &cmp_int, NULL));
-       assert(tree_add(&tree, &c, &cmp_int, NULL));
-       assert(!tree_add(&tree, &d, &cmp_int, NULL));
+       assert(tree_add(&tree, &a, &a, &cmp_int, NULL));
+       assert(tree_add(&tree, &b, &b, &cmp_int, NULL));
+       assert(tree_add(&tree, &c, &c, &cmp_int, NULL));
+       assert(!tree_add(&tree, &d, &d, &cmp_int, NULL));
 
        printf("testing get\n");
        assert(tree_get(&tree, &a, &cmp_int, NULL) == &a);
@@ -63,7 +63,7 @@ int main()
                int *n = malloc(sizeof *n);
                *n = rand();
 
-               if (!tree_add(&tree, n, &cmp_int, NULL))
+               if (!tree_add(&tree, n, n, &cmp_int, NULL))
                        free(n);
        }
 
diff --git a/tree.h b/tree.h
index 5c27105c79b87b164b49aec57a9203561685237c..730f5dc2721b6e308552897195e41287489ebba7 100644 (file)
--- a/tree.h
+++ b/tree.h
@@ -44,7 +44,7 @@ void tree_ini(Tree *tree);
        This function should be called before any other function is called on the tree.
 */
 
-bool tree_add(Tree *tree, void *dat, void *cmp, void *trans);
+bool tree_add(Tree *tree, void *key, void *dat, void *cmp, void *trans);
 /*
        Add an element to the tree.