5 #include <stdlib.h> /* calloc() free() */
7 #define __DEFAULT_BLOCK_CAPACITY ((int)512)
8 #define __DEFAULT_TABLE_SIZE ((int)256)
12 get_assoc(htable_t htable, const void* key, unsigned int* hval)
14 register hassoc_t hassoc;
15 hassoc_t* content = htable->content;
16 fn_cmp_key_t fn_cmp_key = htable->fn_cmp_key;
18 *hval = (*(htable->fn_hfunc))(key) % htable->size;
20 for (hassoc = content[*hval]; hassoc; hassoc = hassoc->next)
21 if((*fn_cmp_key)(hassoc->key,key))
33 fn_cmp_key_t fn_cmp_key,
34 fn_finalize_t fn_finalize
39 if((block_capacity < 0) || (size < 0) ||!fn_hfunc || !fn_cmp_key)
46 if(!(htable = (htable_t)calloc(1, sizeof(s_htable_t))))
49 if(!(htable->content = (hassoc_t*)calloc(size ? size : __DEFAULT_TABLE_SIZE, sizeof(hassoc_t))))
55 if(!(htable->allocator = allocator_new(block_capacity ? block_capacity : __DEFAULT_BLOCK_CAPACITY, sizeof(s_hassoc_t),NULL)))
57 free(htable->content);
63 htable->fn_hfunc = fn_hfunc;
64 htable->fn_cmp_key = fn_cmp_key;
65 htable->fn_finalize = fn_finalize;
71 htable_set(htable_t htable, const void* key, const void* val)
76 if(!htable || !key || !val)
80 if(!(hassoc = get_assoc(htable, key, &hval)))
82 if(!(hassoc = (hassoc_t)allocator_alloc(htable->allocator)))
88 hassoc->next = (htable->content)[hval];
89 (htable->content)[hval] = hassoc;
99 htable_lookup(htable_t htable, const void* key)
110 if(!(hassoc = get_assoc(htable, key, &hval)))
116 return (void*)(hassoc->val);
121 htable_remove(htable_t htable, const void* key)
123 register hassoc_t hassoc;
125 fn_cmp_key_t fn_cmp_key;
134 prev = &(((htable->content)[(*(htable->fn_hfunc))(key) % htable->size]));
135 fn_cmp_key =htable->fn_cmp_key;
137 for (hassoc = *prev; hassoc; hassoc = hassoc->next)
139 if((*fn_cmp_key)((hassoc->key),key))
141 *prev = hassoc->next;
142 val = (void*)hassoc->val;
144 if((errno = allocator_dealloc(htable->allocator,hassoc)))
150 prev = &(hassoc->next);
159 htable_erase(htable_t htable, const void* key)
161 register hassoc_t hassoc;
163 fn_cmp_key_t fn_cmp_key;
169 prev = &(((htable->content)[(*(htable->fn_hfunc))(key) % htable->size]));
171 fn_cmp_key =htable->fn_cmp_key;
173 for(hassoc = *prev; hassoc; hassoc = hassoc->next)
175 if((*fn_cmp_key)((hassoc->key),key))
177 *prev = hassoc->next;
178 val = (void*)hassoc->val;
180 if((errno = allocator_dealloc(htable->allocator,hassoc)))
183 if(htable->fn_finalize)
185 if((errno = (*(htable->fn_finalize))(&val)))
192 prev = &(hassoc->next);
199 htable_free(htable_t* htableptr)
206 htable = (htable_t)(*htableptr);
208 if(htable->fn_finalize)
211 register hassoc_t hassoc;
216 content = htable->content;
219 for(pos = 0; pos < size; pos++)
221 for(hassoc = content[pos]; hassoc; hassoc = hassoc->next)
223 val = (void*)hassoc->val;
224 if((errno = (*(htable->fn_finalize))(&val)))
230 free(htable->content);
232 if((errno = allocator_free(&(htable->allocator))))
242 htable_clear(htable_t htable)
245 register hassoc_t hassoc;
254 content = htable->content;
257 if(htable->fn_finalize)
259 for(pos = 0; pos < size; pos++)
261 for(hassoc = content[pos]; hassoc; hassoc = hassoc->next)
263 val = (void*)hassoc->val;
264 if((errno = (*(htable->fn_finalize))(&val)))
273 for(pos = 0; pos < size; pos++)
277 return allocator_clear(htable->allocator);
281 htable_get_size(htable_t htable)
290 return allocator_get_size(htable->allocator);
294 htable_is_empty(htable_t htable)
302 return allocator_get_size(htable->allocator);
306 htable_is_autodelete(htable_t htable)
315 return (NULL != htable->fn_finalize);