MCPcopy Create free account
hub / github.com/F-Stack/f-stack / dictGetFairRandomKey

Function dictGetFairRandomKey

app/redis-6.2.6/src/dict.c:779–789  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

777 * In this way we smooth away the problem of different chain lengths. */
778#define GETFAIR_NUM_ENTRIES 15
779dictEntry *dictGetFairRandomKey(dict *d) {
780 dictEntry *entries[GETFAIR_NUM_ENTRIES];
781 unsigned int count = dictGetSomeKeys(d,entries,GETFAIR_NUM_ENTRIES);
782 /* Note that dictGetSomeKeys() may return zero elements in an unlucky
783 * run() even if there are actually elements inside the hash table. So
784 * when we get zero, we call the true dictGetRandomKey() that will always
785 * yield the element if the hash table has at least one. */
786 if (count == 0) return dictGetRandomKey(d);
787 unsigned int idx = rand() % count;
788 return entries[idx];
789}
790
791/* Function to reverse bits. Algorithm from:
792 * http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel */

Callers 6

hashTypeRandomElementFunction · 0.85
dbRandomKeyFunction · 0.85
setTypeRandomElementFunction · 0.85
zsetTypeRandomElementFunction · 0.85

Calls 2

dictGetSomeKeysFunction · 0.85
dictGetRandomKeyFunction · 0.85

Tested by

no test coverage detected