struct Hash_Table* init_Hash_Table(); void* get_hash(Hash_Table, key); bool put_hash(Hash_Table, key, object); void* next_hash(Hash_Table, firstp); struct Hash_Table *Hash_Table; string key; void *object; bool firstp;
put_hash stores the information: it uses the pointers to the key and object you supply, which must hence be in safe memory, not local variables. It returns FALSE if for some reason the key could not be stored (a common cause would be if the key had already been entered).
get_hash returns a pointer to the object requested by name key. It returns NULL if no data found.
next_hash iterated over all objects, in the order as stored in the hash table. It MUST be called the first time with firstp set to TRUE. It can be iterated until NULL is returned.
typedef struct keyval { char *key; char *val; } keyval; keyval x[] = { "in", "???", "out", "???", "n", "10", "k", "2", "x", "2.342", "pi", "PI", NULL, NULL, }; struct Hash_Table *h; keyval *xp; int i; h = init_Hash_Table(); /* init */ for (xp=x; xp->key; xp++) { /* store */ if (!put_hash(h,xp->key, xp->val)) printf("### error put_hash0); } for (i=1; i<ac; i++) /* retrieve */ printf("%s -> %s0,av[i],(char *)get_hash(h,av[i]));
Permission is granted to any individual or institution to use, copy, modify, and distribute this software, provided that this complete copyright and permission notice is maintained, intact, in all copies and supporting documentation.
Texas Instruments Incorporated provides this software "as is" without express or implied warranty.
#define BUCKET_SIZE 8 struct hash_pair { char* key; void* value; }; struct bucket { struct hash_pair data[BUCKET_SIZE]; }; struct Hash_Table { struct bucket* table; unsigned char* items_in_buckets; int entry_count; int bucket_index; int current_bucket; int current_index; }
30-mar-89 Initial design and implementation. LGO 28-apr-92 Adapted from the TI cpp for NEMO PJT