From 203d4148e404e0ddc6babbe25646420359dd4b11 Mon Sep 17 00:00:00 2001 From: Richard Nyberg Date: Tue, 12 Sep 2006 08:53:33 +0000 Subject: Add a hashtable implementation. --- misc/hashtable.h | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 misc/hashtable.h (limited to 'misc/hashtable.h') diff --git a/misc/hashtable.h b/misc/hashtable.h new file mode 100644 index 0000000..2dd227d --- /dev/null +++ b/misc/hashtable.h @@ -0,0 +1,80 @@ +#ifndef BTPD_HASHTABLE_H +#define BTPD_HASHTABLE_H + +struct htbl_iter { + struct _htbl *tbl; + size_t bi; + size_t cnt; + void *obj; +}; + +struct _htbl *_htbl_create(int (*equal)(const void *, const void *), + uint32_t (*hash)(const void *), size_t keyoff, size_t chainoff); +void _htbl_free(struct _htbl *tbl); +void _htbl_insert(struct _htbl *tbl, void *o); +void *_htbl_remove(struct _htbl *tbl, const void *key); +void *_htbl_find(struct _htbl *tbl, const void *key); +void _htbl_tov(struct _htbl *tb, void **v); +size_t _htbl_size(struct _htbl *tbl); +void _htbl_iter_init(struct _htbl *tbl, struct htbl_iter *it); +void *_htbl_iter_next(struct htbl_iter *it); + +#define HTBLTYPE(name, type, ktype, kname, cname) \ +__attribute__((always_inline)) static inline struct name * \ +name##_create(int (*equal)(const ktype *, const ktype *), \ + uint32_t (*hash)(const ktype *)) \ +{ \ + return (struct name *) \ + _htbl_create((int (*)(const void *, const void *))equal, \ + (uint32_t (*)(const void *))hash, offsetof(struct type, kname), \ + offsetof(struct type, cname)); \ +} \ +\ +__attribute__((always_inline)) static inline struct type * \ +name##_find(struct name *tbl, const ktype *key) \ +{ \ + return (struct type *)_htbl_find((struct _htbl *)tbl, key); \ +} \ +\ +__attribute__((always_inline)) static inline struct type * \ +name##_remove(struct name *tbl, const ktype *key) \ +{ \ + return (struct type *)_htbl_remove((struct _htbl *)tbl, key); \ +} \ +\ +__attribute__((always_inline)) static inline void \ +name##_free(struct name *tbl) \ +{ \ + _htbl_free((struct _htbl *)tbl); \ +} \ +\ +__attribute__((always_inline)) static inline void \ +name##_insert(struct name *tbl, struct type *o) \ +{ \ + _htbl_insert((struct _htbl *)tbl, o); \ +} \ +__attribute__((always_inline)) static inline void \ +name##_tov(struct name *tbl, struct type **v) \ +{ \ + _htbl_tov((struct _htbl *)tbl, (void **)v); \ +} \ +\ +__attribute__((always_inline)) static inline size_t \ +name##_size(struct name *tbl) \ +{ \ + return _htbl_size((struct _htbl *)tbl); \ +} \ +\ +__attribute__((always_inline)) static inline void \ +name##_iter_init(struct name *tbl, struct htbl_iter *it) \ +{ \ + _htbl_iter_init((struct _htbl *)tbl, it); \ +} \ +\ +__attribute__((always_inline)) static inline struct type * \ +name##_iter_next(struct htbl_iter *it) \ +{ \ + return (struct type *)_htbl_iter_next(it); \ +} + +#endif -- cgit 1.4.1