wm: ticl

ref: 6bbc66e7c0951d7f9ec054179867c4a0d839bb26
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);