lib/hmap.c
/* Chooses and returns a randomly selected node from 'hmap', which must not be
 * empty.
 *
 * I wouldn't depend on this algorithm to be fair, since I haven't analyzed it.
 * But it does at least ensure that any node in 'hmap' can be chosen. */
Chooses and returns a randomly selected node from 'hmap', which must not be
empty.

I wouldn't depend on this algorithm to be fair, since I haven't analyzed it.
But it does at least ensure that any node in 'hmap' can be chosen.
Chooses and returns a randomly selected node from ' hmap ' which must not be empty . I wouldn ' t depend on this algorithm to be fair since I haven ' t analyzed it . But it does at least ensure that any node in ' hmap ' can be chosen .
struct hmap_node *
hmap_random_node(const struct hmap *hmap)
{
    struct hmap_node *bucket, *node;
    size_t n, i;
    for (;;) {
        bucket = hmap->buckets[random_uint32() & hmap->mask];
        if (bucket) {
            break;
        }
    }
    n = 0;
    for (node = bucket; node; node = node->next) {
        n++;
    }
    i = random_range(n);
    for (node = bucket; i-- > 0; node = node->next) {
        continue;
    }
    return node;
}
struct hmap_node * hmap_random_node ( const struct hmap * hmap ) { struct hmap_node * bucket , * node ; size_t n , i ; for ( ; ; ) { bucket = hmap -> buckets [ random_uint32 ( ) & hmap -> mask ] ; if ( bucket ) { break ; } } n = 0 ; for ( node = bucket ; node ; node = node -> next ) { n ++ ; } i = random_range ( n ) ; for ( node = bucket ; i -- > 0 ; node = node -> next ) { continue ; } return node ; }
Chooses and returns a randomly selected node from 'hmap', which must not be
 empty.
Chooses and returns a randomly selected node from ' hmap ' which must not be empty .
/* Choose a random non-empty bucket. */ /* Count nodes in bucket. */ /* Choose random node from bucket. */