ht.c (844B)
1 #include "ht.h" 2 3 void ht_init_size(ht_t *t, const size_t alloc) { 4 t->nalloc = alloc; 5 t->nelts = 0; 6 t->entries = calloc(t->nalloc, sizeof(*t->entries)); 7 8 size_t i; 9 for (i = 0; i < t->nalloc; i++) 10 stack_sd_init_size(&t->entries[i], HT_DEFAULT_STACK_SIZE); 11 } 12 13 void ht_init(ht_t *t) { ht_init_size(t, HT_DEFAULT_SIZE); } 14 15 void ht_clear(ht_t *t) { 16 size_t i; 17 for (i = 0; i < t->nalloc; i++) 18 stack_sd_clear(&t->entries[i]); 19 20 t->nalloc = 0; 21 t->nelts = 0; 22 free(t->entries); 23 t->entries = NULL; 24 } 25 26 int ht_lookup(ht_t *t, sd_t d) { 27 uint64_t h = sd_hash(&d); 28 size_t i = h % t->nalloc; 29 return stack_sd_lookup(&t->entries[i], d); 30 } 31 32 void ht_insert(ht_t *t, sd_t d) { 33 if (ht_lookup(t, d) != STACK_SD_LOOKUP_NOT_FOUND) 34 return; 35 36 size_t i = sd_hash(&d) % t->nalloc; 37 stack_sd_push(&t->entries[i], d); 38 39 t->nelts++; 40 }