X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fhtable%2Fhtable_type.h;h=0aacb7f334925ffe58d2c8cb489a9ae0efe36b66;hp=03cc46fc5836c8003e2c1e325c13a5ec6df5db47;hb=HEAD;hpb=60cc720d0797fc49325437ea36a9ffd909c75ed0 diff --git a/ccan/htable/htable_type.h b/ccan/htable/htable_type.h index 03cc46fc..0aacb7f3 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" /** @@ -20,7 +21,12 @@ * * It also defines initialization and freeing functions: * void _init(struct *); + * bool _init_sized(struct *, size_t); * void _clear(struct *); + * bool _copy(struct *dst, const struct *src); + * + * Count entries: + * size_t _count(const struct *ht); * * Add function only fails if we run out of memory: * bool _add(struct *ht, const *e); @@ -29,13 +35,24 @@ * bool _del(struct *ht, const *e); * bool _delkey(struct *ht, const *k); * - * Find function return the matching element, or NULL: + * Delete by iterator: + * bool _delval(struct *ht, struct _iter *i); + * + * 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); - * + * type *_prev(const struct *ht, struct _iter *i); + * type *_pick(const struct *ht, size_t seed, + * struct _iter *i); * It's currently safe to iterate over a changing hashtable, but you might * miss an element. Iteration isn't very efficient, either. * @@ -47,57 +64,125 @@ 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##_clear(struct name *ht) \ + static inline UNNEEDED bool name##_init_sized(struct name *ht, \ + size_t s) \ + { \ + return htable_init_sized(&ht->raw, name##_hash, NULL, s); \ + } \ + static inline UNNEEDED size_t name##_count(const struct name *ht) \ + { \ + return htable_count(&ht->raw); \ + } \ + static inline UNNEEDED void name##_clear(struct name *ht) \ { \ htable_clear(&ht->raw); \ } \ + static inline UNNEEDED bool name##_copy(struct name *dst, \ + const struct name *src) \ + { \ + return htable_copy(&dst->raw, &src->raw); \ + } \ static inline bool name##_add(struct name *ht, const type *elem) \ { \ 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, \ - keyof((const type *)NULL))); \ - return htable_get(&ht->raw, \ - hashfn(k), \ - (bool (*)(const void *, void *))(eqfn), \ - k); \ + struct htable_iter i; \ + size_t h = hashfn(k); \ + void *c; \ + \ + for (c = htable_firstval(&ht->raw,&i,h); \ + c; \ + c = htable_nextval(&ht->raw,&i,h)) { \ + if (eqfn(c, k)) \ + return c; \ + } \ + return NULL; \ } \ - 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 void name##_delval(struct name *ht, \ + struct name##_iter *iter) \ + { \ + htable_delval(&ht->raw, &iter->i); \ + } \ + static inline UNNEEDED type *name##_pick(const struct name *ht, \ + size_t seed, \ + struct name##_iter *iter) \ + { \ + return htable_pick(&ht->raw, seed, iter ? &iter->i : NULL); \ + } \ + 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); \ + } \ + static inline UNNEEDED type *name##_prev(const struct name *ht, \ + struct name##_iter *iter) \ + { \ + return htable_prev(&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 * +/* Assumes keys are a pointer: if not, override. */ +#ifndef HTABLE_KTYPE +#define HTABLE_KTYPE(keyof, type) void * +#endif #endif #endif /* CCAN_HTABLE_TYPE_H */