wm: ticl

ref: e09ff804102de75826409b6043619d2668d9cef8
dir: /htable.h/

View raw version
/*
 * This work is dedicated to the public domain.
 * See COPYING file for more information.
 */

typedef struct Htnode Htnode;
typedef struct Htable Htable;
typedef struct Htiter Htiter;

typedef size_t	(KeyLenFn)(const void *key);
typedef int	(KeyCmpFn)(const void *key1, const void *key2);
typedef void	(FreeKeyFn)(void *ptr);
typedef void	(FreeValFn)(void *ptr);

struct Htnode {
	void	*key;		/* key for the node */
	void	*val;		/* value for the node */
	Htnode	*next;		/* next node (open hash table) */
};

struct Htable {
	unsigned int	  cap;		/* capacity */
	unsigned int	  len;		/* length */
	Htnode		**nodes;	/* list of nodes */
	KeyLenFn	 *keylenfn;	/* key length function */
	KeyCmpFn	 *keycmpfn;	/* key compare function */
	FreeKeyFn	 *freekeyfn;
	FreeValFn	 *freevalfn;
};

struct Htiter {
	unsigned int	 index;
	Htnode		*node;
};

Htable	*htcreate(KeyLenFn *keylenfn, KeyCmpFn *keycmpfn, FreeKeyFn *freekeyfn, FreeValFn *freevalfn, unsigned int cap);
void	 htdestroy(Htable *ht);
void	*htsearch(Htable *ht, void *key);
int	 htinsert(Htable *ht, void *key, void *val);
int 	 htremove(Htable *ht, void *key);
int	 htsetkey(Htable *ht, void *oldkey, void *newkey);
void 	 htresize(Htable *ht, unsigned int ncap);
int	 htiterate(Htable *ht, Htiter *it);