about summary refs log tree commit diff
path: root/misc/hashtable.h
diff options
context:
space:
mode:
authorRichard Nyberg <rnyberg@murmeldjur.se>2006-09-12 08:53:33 +0000
committerRichard Nyberg <rnyberg@murmeldjur.se>2006-09-12 08:53:33 +0000
commit203d4148e404e0ddc6babbe25646420359dd4b11 (patch)
tree40e38b8539fd099c4462a03a3fb983c7ac323d20 /misc/hashtable.h
parentc9ea09b1ad72ccb342802622943c98f0fa465799 (diff)
downloadbtpd-203d4148e404e0ddc6babbe25646420359dd4b11.tar.gz
btpd-203d4148e404e0ddc6babbe25646420359dd4b11.zip
Add a hashtable implementation.
Diffstat (limited to 'misc/hashtable.h')
-rw-r--r--misc/hashtable.h80
1 files changed, 80 insertions, 0 deletions
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