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