NAME

strhash -- hash tables for pairs of strings


SYNOPSIS

   #include <afblib/strhash.h>
   typedef struct strhash_entry {
      char* key;
      char* value;
      struct strhash_entry* next;
   } strhash_entry;
   typedef struct strhash {
      unsigned int size, length;
      strhash_entry** bucket;
      unsigned int it_index;
      strhash_entry* it_entry;
   } strhash;
   int strhash_alloc(strhash* hash, unsigned int size);
   int strhash_add(strhash* hash, char* key, char* value);
   int strhash_remove(strhash* hash, char* key);
   unsigned int strhash_length(strhash* hash);
   int strhash_exists(strhash* hash, char* key);
   int strhash_lookup(strhash* hash, char* key, char** value);
   int strhash_start(strhash* hash);
   int strhash_next(strhash* hash, char** key);
   int strhash_free(strhash* hash);


DESCRIPTION

Objects of type strhash represent tables of string-valued key/value-pairs. Individual entries can be looked up by key. Multiple entries with an identical key are not permitted.

Each strhash object must be initialized first by invoking strhash_alloc and by specifying the size of the bucket table which depends on the expected number of to be stored pairs. Tables can accomodate any number of entries independent from this size but the performance degrades if the bucket table is too small.

Entries can be added by strhash_add. Entries with duplicate keys are, however, not accepted. Existing entries can be removed by strhash_remove. The number of entries within the hash table is returned by strhash_length. strhash_exists allows to test whether a pair with the given key exists within the table. strhash_lookup returns the pair with the given key, if existent.

strhash_start and strhash_next allow to iterate through all entries of a hash table.

strhash_free allows to free any memory associated with hash.


DIAGNOSTICS

All functions with the exception of strhash_exists return 1 in case of success and 0 otherwise.


AUTHOR

Andreas F. Borchert

(The hash function has been taken from Dan J. Bernstein.)