X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fhtable%2Fhtable_type.h;h=192824c2d9c80649026021b5cd745e6ed63d2326;hb=1cebc0895d236bfc5cd6797d03e02c55c773ddf1;hp=15a70fc28d039e31f9285f83a496e14faa09c71f;hpb=f3538fc7cbe97e7e0daa216b187596d77f189bf2;p=ccan diff --git a/ccan/htable/htable_type.h b/ccan/htable/htable_type.h index 15a70fc2..192824c2 100644 --- a/ccan/htable/htable_type.h +++ b/ccan/htable/htable_type.h @@ -21,8 +21,12 @@ * * It also defines initialization and freeing functions: * void _init(struct *); - * void _init_sized(struct *, size_t); + * 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); @@ -43,7 +47,9 @@ * 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. * @@ -62,15 +68,24 @@ { \ htable_init(&ht->raw, name##_hash, NULL); \ } \ - static inline UNNEEDED void name##_init_sized(struct name *ht, \ + static inline UNNEEDED bool name##_init_sized(struct name *ht, \ size_t s) \ { \ - htable_init_sized(&ht->raw, name##_hash, NULL, 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); \ @@ -83,13 +98,17 @@ 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 UNNEEDED type *name##_getmatch_(const struct name *ht, \ const HTABLE_KTYPE(keyof, type) k, \ @@ -128,6 +147,13 @@ return name##_del(ht, elem); \ return false; \ } \ + static inline UNNEEDED type *name##_pick(const struct name *ht, \ + size_t seed, \ + struct name##_iter *iter) \ + { \ + /* Note &iter->i == NULL iff iter is NULL */ \ + return htable_pick(&ht->raw, seed, &iter->i); \ + } \ static inline UNNEEDED type *name##_first(const struct name *ht, \ struct name##_iter *iter) \ { \ @@ -137,11 +163,19 @@ 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, type) typeof(keyof((const type *)NULL)) #else +/* Assumes keys are a pointer: if not, override. */ +#ifndef HTABLE_KTYPE #define HTABLE_KTYPE(keyof, type) void * #endif +#endif #endif /* CCAN_HTABLE_TYPE_H */