X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fhtable%2Fhtable.h;h=faaf541bd8ce8c5ee5f5ea64eab3649e1462fade;hb=fbac6a54735b5d153be4675d555e73713186cf69;hp=bdce920b552eb0e23a5bbe53877a146decf87d81;hpb=9e92552b1b2a1b631bde1c379b9f2950725b1245;p=ccan diff --git a/ccan/htable/htable.h b/ccan/htable/htable.h index bdce920b..faaf541b 100644 --- a/ccan/htable/htable.h +++ b/ccan/htable/htable.h @@ -24,11 +24,10 @@ struct htable { size_t (*rehash)(const void *elem, void *priv); void *priv; - unsigned int bits; + unsigned int bits, perfect_bitnum; size_t elems, deleted; /* These are the bits which are the same in all pointers. */ uintptr_t common_mask, common_bits; - uintptr_t perfect_bit; uintptr_t *table; }; @@ -50,7 +49,7 @@ struct htable { * static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL); */ #define HTABLE_INITIALIZER(name, rehash, priv) \ - { rehash, priv, 0, 0, 0, -1, 0, 0, &name.perfect_bit } + { rehash, priv, 0, 0, 0, 0, -1, 0, &name.common_bits } /** * htable_init - initialize an empty hash table. @@ -76,6 +75,15 @@ bool htable_init_sized(struct htable *ht, size_t (*rehash)(const void *elem, void *priv), void *priv, size_t size); +/** + * htable_count - count number of entries in a hash table. + * @ht: the hash table + */ +static inline size_t htable_count(const struct htable *ht) +{ + return ht->elems; +} + /** * htable_clear - empty a hash table. * @ht: the hash table to clear @@ -124,7 +132,7 @@ bool htable_copy_(struct htable *dst, const struct htable *src); * htable_add - add a pointer into a hash table. * @ht: the htable * @hash: the hash value of the object - * @p: the non-NULL pointer + * @p: the non-NULL pointer (also cannot be (void *)1). * * Also note that this can only fail due to allocation failure. Otherwise, it * returns true. @@ -259,6 +267,19 @@ void *htable_prev_(const struct htable *htable, struct htable_iter *i); htable_delval_(htable_debug(htable, HTABLE_LOC), i) void htable_delval_(struct htable *ht, struct htable_iter *i); +/** + * htable_pick - set iterator to a random valid entry. + * @ht: the htable + * @seed: a random number to use. + * @i: the htable_iter which is output (or NULL). + * + * Usually used with htable_delval to delete a random entry. Returns + * NULL iff the table is empty, otherwise a random entry. + */ +#define htable_pick(htable, seed, i) \ + htable_pick_(htable_debug(htable, HTABLE_LOC), seed, i) +void *htable_pick_(const struct htable *ht, size_t seed, struct htable_iter *i); + /** * htable_set_allocator - set calloc/free functions. * @alloc: allocator to use, must zero memory!