#ifndef _HTAB_H #define _HTAB_H #define KEYLEN 128 #define KEY_INT 1 #define KEY_STR 2 struct htab_key { int type; union { char as_str[KEYLEN]; int as_int; } key; }; typedef struct htab_key htab_key_t; struct htab_entry { struct htab_entry *next; htab_key_t *key; void *data; }; struct htab_table { int len; struct htab_entry **ent; }; /* Allocate and return a pointer to a key. Type is KEY_INT or KEY_STR. * key is a NUL-terminated string if type is KEY_STR, it will be truncated * to KEYLEN bytes on overflow. */ htab_key_t *htab_mk_key(void *key, int type); /* Return 1 if k1 and k2 are equal keys */ int htab_keycmp(htab_key_t *k1, htab_key_t *k2); /* Return an allocated string containing the textual representation of k */ char *htab_key2str(htab_key_t *k); int htab_init(struct htab_table *tab, int len); /* Grow the table with grow_by */ int htab_grow(struct htab_table *tab, int grow_by); int htab_insert(struct htab_table *tab, htab_key_t *key, void *data); /* Returns the data pointer if found, NULL otherwise */ void *htab_lookup(struct htab_table *tab, htab_key_t *key); int htab_remove(struct htab_table *tab, htab_key_t *key); /* Removes the first found allocated element in the table, returning * a pointer to the data. This allows you to free all data before * freeing the table. */ void *htab_remove_next(struct htab_table *tab); void htab_free(struct htab_table *tab); void htab_prettyprint(struct htab_table *tab); /* Return the items in the table one for each call. * Reset counter when argument is NULL. * Return NULL when all items have been returned. * Modifying the table may cause undefined results. */ void *htab_iter(struct htab_table *tab); #endif