MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / dictGetFairRandomKey

Function dictGetFairRandomKey

src/dict.cpp:1141–1151  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1139 * In this way we smooth away the problem of different chain lengths. */
1140#define GETFAIR_NUM_ENTRIES 15
1141dictEntry *dictGetFairRandomKey(dict *d) {
1142 dictEntry *entries[GETFAIR_NUM_ENTRIES];
1143 unsigned int count = dictGetSomeKeys(d,entries,GETFAIR_NUM_ENTRIES);
1144 /* Note that dictGetSomeKeys() may return zero elements in an unlucky
1145 * run() even if there are actually elements inside the hash table. So
1146 * when we get zero, we call the true dictGetRandomKey() that will always
1147 * yield the element if the hash table has at least one. */
1148 if (count == 0) return dictGetRandomKey(d);
1149 unsigned int idx = rand() % count;
1150 return entries[idx];
1151}
1152
1153/* Function to reverse bits. Algorithm from:
1154 * http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel */

Callers 5

hashTypeRandomElementFunction · 0.85
zsetTypeRandomElementFunction · 0.85
setTypeRandomElementFunction · 0.85

Calls 2

dictGetSomeKeysFunction · 0.85
dictGetRandomKeyFunction · 0.85

Tested by

no test coverage detected