make the hash table types opaque

This commit is contained in:
Bill Currie 2001-06-11 19:17:40 +00:00
parent fcd29459e4
commit 75fbb80b96
2 changed files with 22 additions and 18 deletions

View file

@ -32,19 +32,7 @@
#include <stdlib.h> // should be sys/types.h, but bc is stupid #include <stdlib.h> // should be sys/types.h, but bc is stupid
typedef struct hashlink_s { typedef struct hashtab_s hashtab_t;
struct hashlink_s *next;
struct hashlink_s **prev;
void *data;
} hashlink_t;
typedef struct hashtab_s {
size_t tab_size;
void *user_data;
const char *(*get_key)(void*,void*);
void (*free_ele)(void*,void*);
hashlink_t *tab[1]; // variable size
} hashtab_t;
hashtab_t *Hash_NewTable (int tsize, const char *(*gk)(void*,void*), hashtab_t *Hash_NewTable (int tsize, const char *(*gk)(void*,void*),
void (*f)(void*,void*), void *ud); void (*f)(void*,void*), void *ud);

View file

@ -37,9 +37,25 @@
# include <strings.h> # include <strings.h>
#endif #endif
#include "compat.h"
#include "QF/hash.h" #include "QF/hash.h"
#include "compat.h"
struct hashlink_s {
struct hashlink_s *next;
struct hashlink_s **prev;
void *data;
};
struct hashtab_s {
size_t tab_size;
void *user_data;
const char *(*get_key)(void*,void*);
void (*free_ele)(void*,void*);
struct hashlink_s *tab[1]; // variable size
};
static unsigned long static unsigned long
hash (const char *str) hash (const char *str)
{ {
@ -96,7 +112,7 @@ Hash_FlushTable (hashtab_t *tab)
for (i = 0; i < tab->tab_size; i++) { for (i = 0; i < tab->tab_size; i++) {
while (tab->tab[i]) { while (tab->tab[i]) {
hashlink_t *t = tab->tab[i]->next; struct hashlink_s *t = tab->tab[i]->next;
if (tab->free_ele) if (tab->free_ele)
tab->free_ele (tab->tab[i]->data, tab->user_data); tab->free_ele (tab->tab[i]->data, tab->user_data);
free (tab->tab[i]); free (tab->tab[i]);
@ -110,7 +126,7 @@ Hash_Add (hashtab_t *tab, void *ele)
{ {
unsigned long h = hash (tab->get_key(ele, tab->user_data)); unsigned long h = hash (tab->get_key(ele, tab->user_data));
size_t ind = h % tab->tab_size; size_t ind = h % tab->tab_size;
hashlink_t *lnk = malloc (sizeof (hashlink_t)); struct hashlink_s *lnk = malloc (sizeof (struct hashlink_s));
if (!lnk) if (!lnk)
return -1; return -1;
@ -128,7 +144,7 @@ Hash_Find (hashtab_t *tab, const char *key)
{ {
unsigned long h = hash (key); unsigned long h = hash (key);
size_t ind = h % tab->tab_size; size_t ind = h % tab->tab_size;
hashlink_t *lnk = tab->tab[ind]; struct hashlink_s *lnk = tab->tab[ind];
while (lnk) { while (lnk) {
if (strequal (key, tab->get_key (lnk->data, tab->user_data))) if (strequal (key, tab->get_key (lnk->data, tab->user_data)))
@ -143,7 +159,7 @@ Hash_Del (hashtab_t *tab, const char *key)
{ {
unsigned long h = hash (key); unsigned long h = hash (key);
size_t ind = h % tab->tab_size; size_t ind = h % tab->tab_size;
hashlink_t *lnk = tab->tab[ind]; struct hashlink_s *lnk = tab->tab[ind];
while (lnk) { while (lnk) {
if (strequal (key, tab->get_key (lnk->data, tab->user_data))) { if (strequal (key, tab->get_key (lnk->data, tab->user_data))) {