]> git.ozlabs.org Git - ccan/blobdiff - ccan/htable/htable.c
htable: htable_pick helper to select a random entry.
[ccan] / ccan / htable / htable.c
index d3c4f9b41187a9ef0481e332d710e95e42b29bea..cffd0619d338d8146365abd3f10e1b308ef4c4d4 100644 (file)
@@ -365,6 +365,20 @@ void htable_delval_(struct htable *ht, struct htable_iter *i)
        ht->deleted++;
 }
 
+void *htable_pick_(const struct htable *ht, size_t seed, struct htable_iter *i)
+{
+       void *e;
+       struct htable_iter unwanted;
+
+       if (!i)
+               i = &unwanted;
+       i->off = seed % ((size_t)1 << ht->bits);
+       e = htable_next(ht, i);
+       if (!e)
+               e = htable_first(ht, i);
+       return e;
+}
+
 struct htable *htable_check(const struct htable *ht, const char *abortstr)
 {
        void *p;