wm: ticl

ref: c8499f76236656b20c24d0e2bc0690d4ae049b8f
dir: /htable.h/

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

#ifndef HTABLE_H
#define HTABLE_H

typedef unsigned int	(hash_fn)(void *key, unsigned int cap);
typedef int		(key_cmp_fn)(const void *key1, const void *key2);
typedef void		(key_free_fn)(void *key);
typedef void		(val_free_fn)(void *val);

struct htnode {
	void		*key;
	void		*val;
	struct htnode	*next;
};

struct htable {
	struct htnode	**nodes;
	unsigned int	  cap;
	unsigned int	  len;
	hash_fn		 *hash;
	key_cmp_fn	 *key_cmp;
	key_free_fn	 *key_free;
	val_free_fn	 *val_free;
};

struct htiter {
	unsigned int	 index;
	struct htnode	*node;
};

struct htable	*htcreate(hash_fn *hash,
			key_cmp_fn *key_cmp,
			key_free_fn *key_free,
			val_free_fn *val_free,
			unsigned int cap);
void		 htdestroy(struct htable *ht);
void		*htsearch(struct htable *ht, void *key);
int		 htinsert(struct htable *ht, void *key, void *val);
int		 htremove(struct htable *ht, void *key);
int		 htmodkey(struct htable *ht, void *oldkey, void *newkey);
int		 htmodval(struct htable *ht, void *key, void *newval);
struct htable	*htresize(struct htable *ht, unsigned int newcap);
void		 htiter_init(struct htiter *it);
int		 htiterate(struct htable *ht, struct htiter *it);

#endif /* HTABLE_H */