X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fhtable%2Fhtable_type.h;h=15a70fc28d039e31f9285f83a496e14faa09c71f;hb=f3538fc7cbe97e7e0daa216b187596d77f189bf2;hp=ad3974c5e446dc74be147371d74dafb09feea631;hpb=64b0c5cdeea5bc00ce3b9ef89fd3aade9cb0cd2c;p=ccan diff --git a/ccan/htable/htable_type.h b/ccan/htable/htable_type.h index ad3974c5..15a70fc2 100644 --- a/ccan/htable/htable_type.h +++ b/ccan/htable/htable_type.h @@ -2,6 +2,7 @@ #ifndef CCAN_HTABLE_TYPE_H #define CCAN_HTABLE_TYPE_H #include +#include #include "config.h" /** @@ -30,9 +31,15 @@ * bool _del(struct *ht, const *e); * bool _delkey(struct *ht, const *k); * - * Find function return the matching element, or NULL: + * Find and return the (first) matching element, or NULL: * type *_get(const struct @name *ht, const *k); * + * Find and return all matching elements, or NULL: + * type *_getfirst(const struct @name *ht, const *k, + * struct _iter *i); + * type *_getnext(const struct @name *ht, const *k, + * struct _iter *i); + * * Iteration over hashtable is also supported: * type *_first(const struct *ht, struct _iter *i); * type *_next(const struct *ht, struct _iter *i); @@ -48,17 +55,19 @@ struct name##_iter { struct htable_iter i; }; \ static inline size_t name##_hash(const void *elem, void *priv) \ { \ + (void)priv; \ return hashfn(keyof((const type *)elem)); \ } \ - static inline void name##_init(struct name *ht) \ + static inline UNNEEDED void name##_init(struct name *ht) \ { \ htable_init(&ht->raw, name##_hash, NULL); \ } \ - static inline void name##_init_sized(struct name *ht, size_t s) \ + static inline UNNEEDED void name##_init_sized(struct name *ht, \ + size_t s) \ { \ htable_init_sized(&ht->raw, name##_hash, NULL, s); \ } \ - static inline void name##_clear(struct name *ht) \ + static inline UNNEEDED void name##_clear(struct name *ht) \ { \ htable_clear(&ht->raw); \ } \ @@ -66,12 +75,13 @@ { \ return htable_add(&ht->raw, hashfn(keyof(elem)), elem); \ } \ - static inline bool name##_del(struct name *ht, const type *elem) \ + static inline UNNEEDED bool name##_del(struct name *ht, \ + const type *elem) \ { \ return htable_del(&ht->raw, hashfn(keyof(elem)), elem); \ } \ - static inline type *name##_get(const struct name *ht, \ - const HTABLE_KTYPE(keyof) k) \ + static inline UNNEEDED type *name##_get(const struct name *ht, \ + const HTABLE_KTYPE(keyof, type) k) \ { \ /* Typecheck for eqfn */ \ (void)sizeof(eqfn((const type *)NULL, \ @@ -81,28 +91,57 @@ (bool (*)(const void *, void *))(eqfn), \ k); \ } \ - static inline bool name##_delkey(struct name *ht, \ - const HTABLE_KTYPE(keyof) k) \ + static inline UNNEEDED type *name##_getmatch_(const struct name *ht, \ + const HTABLE_KTYPE(keyof, type) k, \ + size_t h, \ + type *v, \ + struct name##_iter *iter) \ + { \ + while (v) { \ + if (eqfn(v, k)) \ + break; \ + v = htable_nextval(&ht->raw, &iter->i, h); \ + } \ + return v; \ + } \ + static inline UNNEEDED type *name##_getfirst(const struct name *ht, \ + const HTABLE_KTYPE(keyof, type) k, \ + struct name##_iter *iter) \ + { \ + size_t h = hashfn(k); \ + type *v = htable_firstval(&ht->raw, &iter->i, h); \ + return name##_getmatch_(ht, k, h, v, iter); \ + } \ + static inline UNNEEDED type *name##_getnext(const struct name *ht, \ + const HTABLE_KTYPE(keyof, type) k, \ + struct name##_iter *iter) \ + { \ + size_t h = hashfn(k); \ + type *v = htable_nextval(&ht->raw, &iter->i, h); \ + return name##_getmatch_(ht, k, h, v, iter); \ + } \ + static inline UNNEEDED bool name##_delkey(struct name *ht, \ + const HTABLE_KTYPE(keyof, type) k) \ { \ type *elem = name##_get(ht, k); \ if (elem) \ return name##_del(ht, elem); \ return false; \ } \ - static inline type *name##_first(const struct name *ht, \ + static inline UNNEEDED type *name##_first(const struct name *ht, \ struct name##_iter *iter) \ { \ return htable_first(&ht->raw, &iter->i); \ } \ - static inline type *name##_next(const struct name *ht, \ + static inline UNNEEDED type *name##_next(const struct name *ht, \ struct name##_iter *iter) \ { \ return htable_next(&ht->raw, &iter->i); \ } #if HAVE_TYPEOF -#define HTABLE_KTYPE(keyof) typeof(keyof(NULL)) +#define HTABLE_KTYPE(keyof, type) typeof(keyof((const type *)NULL)) #else -#define HTABLE_KTYPE(keyof) void * +#define HTABLE_KTYPE(keyof, type) void * #endif #endif /* CCAN_HTABLE_TYPE_H */