void hash_set(Hash *tab, const char *key, int value) { int h = _hash_func(key); HashNode *node = _hash_get(tab, h, key); if (value == HASH_NULL) { if (node) { _hash_del(tab, h, node); _hash_list_del(tab, node); free(node); node = NULL; } } else { if (node) { node->value = value; } else { node = HASH_CREATE(HashNode); node->key = key; node->value = value; _hash_add(tab, h, node); _hash_list_add(tab, node); } } }
int hash_get(Hash *tab, const char *key) { int h = _hash_func(key); HashNode *node = _hash_get(tab, h, key); if (node) return node->value; else return HASH_NULL; }
void test_htable_probing() { htable *ht = htable_create(); size_t capacity = htable_capacity(ht); assert(_hash_func("one", capacity) == _hash_func("seven", capacity) && "Make sure that the keys gives the same key"); htable_insert(ht, "one", "1st value"); htable_insert(ht, "seven", "7th value"); assert(0 == strcmp("1st value", htable_get(ht, "one")) && "First value should be equal to 1st value"); assert(0 == strcmp("7th value", htable_get(ht, "seven")) && "Seventh value should be equal to 7th value"); htable_destroy(ht); }