From 6e6d58063e49850839264d1453558ce26315574e Mon Sep 17 00:00:00 2001 From: Stathis Kamperis Date: Tue, 23 Oct 2007 09:00:51 +0300 Subject: [PATCH] General cleanup * Use size_t instead of unsigned int * Use typedef's for function pointers (clarifies code a lot) --- genstructs/htable/htable.c | 34 +++++++++++++++++----------------- genstructs/htable/htable.h | 29 +++++++++++++++++------------ 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/genstructs/htable/htable.c b/genstructs/htable/htable.c index 732f833..05cc2a0 100644 --- a/genstructs/htable/htable.c +++ b/genstructs/htable/htable.c @@ -4,12 +4,12 @@ #include "htable.h" -htret_t htable_init(htable_t *htable, size_t size, unsigned int factor, - unsigned int (*myhashf)(const void *key), - int (*mycmpf)(const void *arg1, const void *arg2), - void (*myprintf)(const void *key, const void *data)) +htret_t htable_init(htable_t *htable, size_t size, size_t factor, + hashf_t *myhashf, + cmpf_t mycmpf, + printf_t *myprintf) { - unsigned int i; + size_t i; /* Allocate memory for `size' tailq headers */ if ((htable->ht_table = malloc(size * sizeof *htable->ht_table)) == NULL) @@ -36,7 +36,7 @@ void htable_free(htable_t *htable) { hhead_t *phead; hnode_t *pnode; - unsigned int i; + size_t i; for (i = 0; i < htable->ht_size; i++) { phead = &htable->ht_table[i]; @@ -54,7 +54,7 @@ htret_t htable_free_obj(htable_t *htable, void *key, htfree_t htfree) { hhead_t *phead; hnode_t *pnode; - unsigned int hash; + size_t hash; /* Calculate hash */ hash = htable->ht_hashf(key); @@ -82,7 +82,7 @@ void htable_free_all_obj(htable_t *htable, htfree_t htfree) { hhead_t *phead; hnode_t *pnode; - unsigned int i; + size_t i; for (i = 0; i < htable->ht_size; i++) { phead = &htable->ht_table[i]; @@ -99,7 +99,7 @@ htret_t htable_grow(htable_t *htable) { hhead_t *pcurhead, *pnewhead, *poldhead; hnode_t *pnode; - unsigned int i, newhash, newsize; + size_t i, newhash, newsize; /* Allocate memory for new hash table */ newsize = 2 * htable->ht_size; @@ -136,7 +136,7 @@ htret_t htable_insert(htable_t *htable, void *key, void *data) { hhead_t *phead; hnode_t *pnode; - unsigned int hash; + size_t hash; /* Calculate hash */ hash = htable->ht_hashf(key); @@ -166,7 +166,7 @@ htret_t htable_remove(htable_t *htable, const void *key) { hhead_t *phead; hnode_t *pnode; - unsigned int hash; + size_t hash; /* Calculate hash */ hash = htable->ht_hashf(key); @@ -190,7 +190,7 @@ void *htable_search(const htable_t *htable, const void *key) { const hhead_t *phead; const hnode_t *pnode; - unsigned int hash; + size_t hash; /* Calculate hash */ hash = htable->ht_hashf(key); @@ -207,7 +207,7 @@ void htable_print(const htable_t *htable) { const hhead_t *phead; const hnode_t *pnode; - unsigned int i; + size_t i; for (i = 0; i < htable->ht_size; i++) { phead = &htable->ht_table[i]; @@ -223,7 +223,7 @@ size_t htable_get_size(const htable_t *htable) return htable->ht_size; } -unsigned int htable_get_used(const htable_t *htable) +size_t htable_get_used(const htable_t *htable) { return htable->ht_used; } @@ -232,7 +232,7 @@ void htable_traverse(const htable_t *htable, void (*pfunc)(void *data)) { const hhead_t *phead; const hnode_t *pnode; - unsigned int i; + size_t i; for (i = 0; i < htable->ht_size; i++) { phead = &htable->ht_table[i]; @@ -241,10 +241,10 @@ void htable_traverse(const htable_t *htable, void (*pfunc)(void *data)) } } -const hnode_t *htable_get_next_elm(const htable_t *htable, unsigned int *pos, const hnode_t *pnode) +const hnode_t *htable_get_next_elm(const htable_t *htable, size_t *pos, const hnode_t *pnode) { const hhead_t *phead; - unsigned int i; + size_t i; /* Is pos out of bound ? If yes, return immediately */ if (*pos > (htable->ht_size - 1)) diff --git a/genstructs/htable/htable.h b/genstructs/htable/htable.h index da37757..3c47d32 100644 --- a/genstructs/htable/htable.h +++ b/genstructs/htable/htable.h @@ -10,14 +10,19 @@ typedef struct hnode { TAILQ_ENTRY(hnode) hn_next; } hnode_t; + +typedef size_t hashf_t(const void *key); +typedef int cmpf_t(const void *arg1, const void *arg2); +typedef void printf_t(const void *key, const void *data); + typedef struct htable { size_t ht_size; /* size must be a power of 2 */ - unsigned int ht_used; - unsigned int ht_factor; - unsigned int ht_limit; - unsigned int (*ht_hashf)(const void *key); - int (*ht_cmpf)(const void *arg1, const void *arg2); - void (*ht_printf)(const void *key, const void *data); + size_t ht_used; /* number of hash table entries */ + size_t ht_factor; + size_t ht_limit; /* limit = factor * size */ + hashf_t *ht_hashf; + cmpf_t *ht_cmpf; + printf_t *ht_printf; TAILQ_HEAD(htablehead, hnode) *ht_table; } htable_t; @@ -36,10 +41,10 @@ typedef enum { } htfree_t; /* Function prototypes */ -htret_t htable_init(htable_t *htable, size_t size, unsigned int factor, - unsigned int (*myhashf)(const void *key), - int (*mycmpf)(const void *arg1, const void *arg2), - void (*myprintf)(const void *key, const void *data)); +htret_t htable_init(htable_t *htable, size_t size, size_t factor, + hashf_t *myhashf, + cmpf_t *mycmpf, + printf_t *myprintf); void htable_free(htable_t *htable); htret_t htable_free_obj(htable_t *htable, void *key, htfree_t htfree); void htable_free_all_obj(htable_t *htable, htfree_t htfree); @@ -49,8 +54,8 @@ htret_t htable_remove(htable_t *htable, const void *key); void *htable_search(const htable_t *htable, const void *key); void htable_print(const htable_t *htable); size_t htable_get_size(const htable_t *htable); -unsigned int htable_get_used(const htable_t *htable); +size_t htable_get_used(const htable_t *htable); void htable_traverse(const htable_t *htable, void (*pfunc)(void *data)); -const hnode_t *htable_get_next_elm(const htable_t *htable, unsigned int *pos, const hnode_t *pnode); +const hnode_t *htable_get_next_elm(const htable_t *htable, size_t *pos, const hnode_t *pnode); #endif /* HTABLE_H */ -- 2.11.4.GIT