summary refs log tree commit diff
path: root/misc/hashtable.h
diff options
context:
space:
mode:
authorRichard Nyberg <rnyberg@murmeldjur.se>2009-02-06 20:03:56 +0100
committerRichard Nyberg <rnyberg@murmeldjur.se>2009-02-06 20:03:56 +0100
commite5332f1aa9226a0934ae616828e1dbf603d50d91 (patch)
treeec3e38cb780a1bcc07b6756f98ec93e5cbc1d0b1 /misc/hashtable.h
parent432be0a10329c3209610a3af932209819b5999b5 (diff)
downloadbtpd-e5332f1aa9226a0934ae616828e1dbf603d50d91.tar.gz
btpd-e5332f1aa9226a0934ae616828e1dbf603d50d91.zip
Hashtable tweaks.
o Added ability to set the ratio items:buckets.
o One can remove items while iterating.
o _htbl_tov now allocates the result array, _htbl_fillv acts as the old
  _htbl_tov did.
Diffstat (limited to 'misc/hashtable.h')
-rw-r--r--misc/hashtable.h36
1 files changed, 25 insertions, 11 deletions
diff --git a/misc/hashtable.h b/misc/hashtable.h
index 84a8e0b..adc4229 100644
--- a/misc/hashtable.h
+++ b/misc/hashtable.h
@@ -5,30 +5,34 @@ struct htbl_iter {
     struct _htbl *tbl;
     size_t bi;
     size_t cnt;
+    struct _any **ptr;
     struct _any *obj;
 };
 
-struct _htbl *_htbl_create(int (*equal)(const void *, const void *),
+struct _htbl *_htbl_create(float ratio,
+    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, struct _any *o);
 struct _any *_htbl_remove(struct _htbl *tbl, const void *key);
 struct _any *_htbl_find(struct _htbl *tbl, const void *key);
-void _htbl_tov(struct _htbl *tb, struct _any **v);
+void _htbl_fillv(struct _htbl *tbl, struct _any **v);
+struct _any **_htbl_tov(struct _htbl *tbl);
 size_t _htbl_size(struct _htbl *tbl);
-void _htbl_iter_init(struct _htbl *tbl, struct htbl_iter *it);
+struct _any *_htbl_iter_first(struct _htbl *tbl, struct htbl_iter *it);
 struct _any *_htbl_iter_next(struct htbl_iter *it);
+struct _any *_htbl_iter_del(struct htbl_iter *it);
 
 #define HTBL_ENTRY(name) struct _any *name
 
 #define HTBL_TYPE(name, type, ktype, kname, cname) \
 __attribute__((always_inline)) static inline struct name * \
-name##_create(int (*equal)(const void *, const void *), \
+name##_create(float ratio, int (*equal)(const void *, const void *), \
     uint32_t (*hash)(const void *)) \
 { \
     return (struct name *) \
-      _htbl_create(equal, hash, offsetof(struct type, kname), \
-        offsetof(struct type, cname)); \
+        _htbl_create(ratio, equal, hash, offsetof(struct type, kname), \
+            offsetof(struct type, cname)); \
 } \
 \
 __attribute__((always_inline)) static inline struct type * \
@@ -54,10 +58,15 @@ name##_insert(struct name *tbl, struct type *o) \
 { \
     _htbl_insert((struct _htbl *)tbl, (struct _any *)o); \
 } \
+__attribute__((always_inline)) static inline struct type ** \
+name##_tov(struct name *tbl) \
+{ \
+    return (struct type **) _htbl_tov((struct _htbl *)tbl); \
+} \
 __attribute__((always_inline)) static inline void \
-name##_tov(struct name *tbl, struct type **v) \
+name##_fillv(struct name *tbl, struct type **v) \
 { \
-    _htbl_tov((struct _htbl *)tbl, (struct _any **)v); \
+    _htbl_fillv((struct _htbl *)tbl, (struct _any **)v); \
 } \
 \
 __attribute__((always_inline)) static inline size_t \
@@ -66,13 +75,18 @@ 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) \
+__attribute__((always_inline)) static inline struct type * \
+name##_iter_first(struct name *tbl, struct htbl_iter *it) \
 { \
-    _htbl_iter_init((struct _htbl *)tbl, it); \
+    return (struct type *)_htbl_iter_first((struct _htbl *)tbl, it); \
 } \
 \
 __attribute__((always_inline)) static inline struct type * \
+name##_iter_del(struct htbl_iter *it) \
+{ \
+    return (struct type *)_htbl_iter_del(it); \
+} \
+__attribute__((always_inline)) static inline struct type * \
 name##_iter_next(struct htbl_iter *it) \
 { \
     return (struct type *)_htbl_iter_next(it); \