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(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;
170 prev = &(((htable->content)[(*(htable->fn_hfunc))(key) % htable->size]));
172 fn_cmp_key =htable->fn_cmp_key;
174 for(hassoc = *prev; hassoc; hassoc = hassoc->next)
176 if((*fn_cmp_key)((hassoc->key),key))
178 *prev = hassoc->next;
179 val = (void*)hassoc->val;
181 if((rv = allocator_dealloc(htable->allocator,hassoc)))
184 if(htable->fn_finalize)
186 if((rv = (*(htable->fn_finalize))(&val)))
193 prev = &(hassoc->next);
200 htable_free(htable_t* htableptr)
208 htable = (htable_t)(*htableptr);
210 if(htable->fn_finalize)
213 register hassoc_t hassoc;
218 content = htable->content;
221 for(pos = 0; pos < size; pos++)
223 for(hassoc = content[pos]; hassoc; hassoc = hassoc->next)
225 val = (void*)hassoc->val;
226 if((rv = (*(htable->fn_finalize))(&val)))
232 free(htable->content);
234 if((rv = allocator_free(&(htable->allocator))))
244 htable_clear(htable_t htable)
247 register hassoc_t hassoc;
257 content = htable->content;
260 if(htable->fn_finalize)
262 for(pos = 0; pos < size; pos++)
264 for(hassoc = content[pos]; hassoc; hassoc = hassoc->next)
266 val = (void*)hassoc->val;
267 if((rv = (*(htable->fn_finalize))(&val)))
276 for(pos = 0; pos < size; pos++)
280 return allocator_clear(htable->allocator);
284 htable_get_size(htable_t htable)
293 return allocator_get_size(htable->allocator);
297 htable_is_empty(htable_t htable)
305 return allocator_get_size(htable->allocator);
309 htable_is_autodelete(htable_t htable)
318 return (NULL != htable->fn_finalize);