| 1139 | * In this way we smooth away the problem of different chain lengths. */ |
| 1140 | #define GETFAIR_NUM_ENTRIES 15 |
| 1141 | dictEntry *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 */ |
no test coverage detected