int dic_resize(struct dic *m, size_t size) { //DEBUGPRINT("dic_resize\n"); struct dic newtbl; size_t n; struct hash_node *node,*next; newtbl.size = size; newtbl.hash_func = m->hash_func; if ((newtbl.nodes = (struct hash_node**)calloc(size, sizeof(struct hash_node*))) == NULL) { return -1; } for (n = 0; n<m->size; ++n) { for (node = m->nodes[n]; node; node = next) { next = node->next; dic_insert(&newtbl, node->key, node->data); dic_remove(m, node->key); } } free(m->nodes); m->size = newtbl.size; m->nodes = newtbl.nodes; return 0; }
// a - b struct dic *dic_minus(struct dic *a, const struct dic *b) { if ((a == NULL) || (b == NULL)) { return a; } struct array *keys = dic_keys(b); for (int i=0; i<keys->length; i++) { const void *key = array_get(keys, i); if (dic_has(a, key)) { dic_remove(a, key); } } array_del(keys); return a; }
int t_dic_remove__existing__removed() { dictionary *dic = dic_new_withint(str_duplicator, free); int key = 3; char str[] = "test1"; int retval, count; dic_add(dic, &key, str); retval = dic_remove(dic, &key); count = dic_count(dic); mu_assert_eq(retval, OK, "retval is not ok"); mu_assert_eq(count, 0, "number of elements is not correct"); dic_destroy(dic, NULL); mu_end; }