X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fhtable%2Fhtable_type.h;h=14011676903807fed252318ef5884b55f14e9556;hb=b7cb7dfdf27243696183698b2f82522003cc1216;hp=ba33f139d72a89387c73847b80bab73f47e1ce15;hpb=7b56762984ce66f38728b542ca347bcb6fe3e981;p=ccan diff --git a/ccan/htable/htable_type.h b/ccan/htable/htable_type.h index ba33f139..14011676 100644 --- a/ccan/htable/htable_type.h +++ b/ccan/htable/htable_type.h @@ -21,8 +21,9 @@ * * 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); * * Add function only fails if we run out of memory: * bool _add(struct *ht, const *e); @@ -63,15 +64,20 @@ { \ 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 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); \ @@ -84,13 +90,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, \ @@ -148,6 +158,9 @@ #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 */