From: Elias Fleckenstein Date: Thu, 14 Apr 2022 19:57:49 +0000 (+0200) Subject: Add key to add functions X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=d439328eed1446e6869a1fd23fe5c0a99350806d;p=dragonstd.git Add key to add functions --- diff --git a/bits/wrappers.h b/bits/wrappers.h index 62bce2b..a347090 100644 --- a/bits/wrappers.h +++ b/bits/wrappers.h @@ -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 c415ba6..2aa6a90 100644 --- 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 4a7333b..9e03023 100644 --- 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 81751b4..dabed94 100644 --- 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 eea5f0e..5d67657 100644 --- 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. diff --git a/test/test_list.c b/test/test_list.c index 3645c04..8f47d30 100644 --- a/test/test_list.c +++ b/test/test_list.c @@ -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); diff --git a/test/test_refcount_map.c b/test/test_refcount_map.c index fc3b5e4..6a8c039 100644 --- a/test/test_refcount_map.c +++ b/test/test_refcount_map.c @@ -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); diff --git a/test/test_tree.c b/test/test_tree.c index 005814b..44a8f72 100644 --- a/test/test_tree.c +++ b/test/test_tree.c @@ -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 5c27105..730f5dc 100644 --- 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.