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_add(T_HashTable *H, T_HashTableEl *E, int *hint) { if (H->fill >= H->max) rehash(H); if (H->fill == H->size) return -1; /*out of memory error */ return _hash_add(H,E, hint); }
static int rehash(T_HashTable *H) { int size,i; T_HashTableEl *oldentries; /* resize the table */ size = H->size; oldentries = H->entries; if(alloc_ht(H,((H->inuse+1)*4+H->fill)/5)) return -1; for(i=0; i < size; i++){ if(oldentries[i] != &unallocated && oldentries[i] != &deleted) _hash_add(H, oldentries[i], 0); } Free(oldentries); return 0; }