add some docs for the hash table api and move the include of stdlib.h to

hash.c (no longer needed in hash.h).
This commit is contained in:
Bill Currie 2001-06-11 19:37:25 +00:00
parent 75fbb80b96
commit f4969d5d67
2 changed files with 54 additions and 2 deletions

View file

@ -30,16 +30,66 @@
#ifndef __hash_h #ifndef __hash_h
#define __hash_h #define __hash_h
#include <stdlib.h> // should be sys/types.h, but bc is stupid
typedef struct hashtab_s hashtab_t; typedef struct hashtab_s hashtab_t;
/*
create a new hash table:
tsize: table size. larger values will give better distribution, but
use more memory.
gk: a function that returns a string to be used as the key for
inserting or finding the element. First parameter is a pointer
to the element from which to extract the key, the second is
the user data pointer.
f: a function to free the element. Only ever called from
Hash_FlushTable and Hash_DelTable. The first parameter is the
element to be freed and the second is the user data pointer.
ud: user data pointer. set to whatever you want, it will be passed
to the get key and free functions as the second parameter.
returns a pointer to the hash table (to be passed to the other functions)
or 0 on error.
multiple inserions of the same key are fine; later insertions override
previous ones until the later one is removed (Hash_Del).
*/
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);
/*
delete a hash table:
tab: the table to be deleted
*/
void Hash_DelTable (hashtab_t *tab); void Hash_DelTable (hashtab_t *tab);
/*
clean out all the entries from a hash table, starting over again:
tab: the table to be cleared
*/
void Hash_FlushTable (hashtab_t *tab); void Hash_FlushTable (hashtab_t *tab);
/*
add an entry to a hash table:
tab: the table to be added to
ele: the element to add to the table
returns 0 for success, -1 for error.
*/
int Hash_Add (hashtab_t *tab, void *ele); int Hash_Add (hashtab_t *tab, void *ele);
/*
find an element within a hash table:
tab: the table to search
key: the key string identifying the element being searched for
returns a pointer to the element if found, otherwise 0.
*/
void *Hash_Find (hashtab_t *tab, const char *key); void *Hash_Find (hashtab_t *tab, const char *key);
/*
delete an element from a hash table:
tab: the table to remove the element from
key: the key string identifying the element to be deleted
returns 0 on success, -1 if the element could not be found.
Does /NOT/ call the free element function. That is the caller's
responsibility.
*/
int Hash_Del (hashtab_t *tab, const char *key); int Hash_Del (hashtab_t *tab, const char *key);
#endif // __hash_h #endif // __hash_h

View file

@ -37,6 +37,8 @@
# include <strings.h> # include <strings.h>
#endif #endif
#include <stdlib.h> // should be sys/types.h, but bc is stupid
#include "QF/hash.h" #include "QF/hash.h"
#include "compat.h" #include "compat.h"