erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

73 řádky
1.3 KiB

před 5 roky
  1. //-----------------------------------------------------------------------------
  2. // MurmurHash2, by Austin Appleby
  3. // Note - This code makes a few assumptions about how your machine behaves -
  4. // 1. We can read a 4-byte value from any address without crashing
  5. // 2. sizeof(int) == 4
  6. // And it has a few limitations -
  7. // 1. It will not wo
  8. //
  9. // rk incrementally.
  10. // 2. It will not produce the same results on little-endian and big-endian
  11. // machines.
  12. unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed )
  13. {
  14. // 'm' and 'r' are mixing constants generated offline.
  15. // They're not really 'magic', they just happen to work well.
  16. const unsigned int m = 0x5bd1e995;
  17. const int r = 24;
  18. // Initialize the hash to a 'random' value
  19. unsigned int h = seed ^ len;
  20. // Mix 4 bytes at a time into the hash
  21. const unsigned char * data = (const unsigned char *)key;
  22. while(len >= 4)
  23. {
  24. unsigned int k = *(unsigned int *)data;
  25. k *= m;
  26. k ^= k >> r;
  27. k *= m;
  28. h *= m;
  29. h ^= k;
  30. data += 4;
  31. len -= 4;
  32. }
  33. // Handle the last few bytes of the input array
  34. switch(len)
  35. {
  36. case 3: h ^= data[2] << 16;
  37. case 2: h ^= data[1] << 8;
  38. case 1: h ^= data[0];
  39. h *= m;
  40. };
  41. // Do a few final mixes of t
  42. //
  43. //
  44. //
  45. // he hash to ensure the last few
  46. // bytes are well-incorporated.
  47. h ^= h >> 13;
  48. h *= m;
  49. h ^= h >> 15;
  50. return h;
  51. }