consistent_hashing.h 773 B

12345678910111213141516
  1. #pragma once
  2. #include <util/system/defaults.h>
  3. /*
  4. * Maps random ui64 x (in fact hash of some string) to n baskets/shards.
  5. * Output value is id of a basket. 0 <= ConsistentHashing(x, n) < n.
  6. * Probability of all baskets must be equal. Also, it should be consistent
  7. * in terms, that with different n_1 < n_2 probability of
  8. * ConsistentHashing(x, n_1) != ConsistentHashing(x, n_2) must be equal to
  9. * (n_2 - n_1) / n_2 - the least possible with previous conditions.
  10. * It requires O(1) memory and cpu to calculate. So, it is faster than classic
  11. * consistent hashing algos with points on circle.
  12. */
  13. size_t ConsistentHashing(ui64 x, size_t n); // Works good for n < 65536
  14. size_t ConsistentHashing(ui64 lo, ui64 hi, size_t n); // Works good for n < 4294967296