Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. #define BRACKETS 1000003
  6.  
  7. uint32_t cdb_hashadd(uint32_t h, unsigned char c) {
  8.     h += (h << 5);
  9.     return h ^ c;
  10. }
  11.  
  12. uint32_t cdb_hash(const unsigned char *buf, int len) {
  13.     uint32_t h;
  14.  
  15.     h = 5381;
  16.     while (len) {
  17.         h = cdb_hashadd(h, *buf++);
  18.         --len;
  19.     }
  20.     return h;
  21. }
  22.  
  23. uint32_t fnv_hash_1a_32(const unsigned char *p, int len) {
  24.     unsigned h = 0x811c9dc5;
  25.     int i;
  26.  
  27.     for (i = 0; i < len; i++)
  28.         h = (h ^ p[i]) * 0x01000193;
  29.  
  30.     return h;
  31. }
  32.  
  33. uint32_t djb2(const unsigned char *p, int len)
  34. {
  35.     unsigned long hash = 5381;
  36.  
  37.     for (int i = 0; i < len; i++)
  38.         hash = ((hash << 5) + hash) + *p++; /* hash * 33 + c */
  39.  
  40.     return hash;
  41. }
  42.  
  43. uint32_t djb2a(const unsigned char *p, int len)
  44. {
  45.     unsigned long hash = 5381;
  46.  
  47.     for (int i = 0; i < len; i++)
  48.         hash = ((hash << 5) + hash) ^ (*p++); /* hash * 33 ^ c */
  49.  
  50.     return hash;
  51. }
  52.  
  53. uint32_t murmur3_32(const uint8_t* key, int len)
  54. {
  55.     uint32_t h = 0x12345678;
  56.     if (len > 3) {
  57.         size_t i = len >> 2;
  58.         do {
  59.             uint32_t k;
  60.             memcpy(&k, key, sizeof(uint32_t));
  61.             key += sizeof(uint32_t);
  62.             k *= 0xcc9e2d51;
  63.             k = (k << 15) | (k >> 17);
  64.             k *= 0x1b873593;
  65.             h ^= k;
  66.             h = (h << 13) | (h >> 19);
  67.             h = h * 5 + 0xe6546b64;
  68.         } while (--i);
  69.     }
  70.     if (len & 3) {
  71.         size_t i = len & 3;
  72.         uint32_t k = 0;
  73.         do {
  74.             k <<= 8;
  75.             k |= key[i - 1];
  76.         } while (--i);
  77.         k *= 0xcc9e2d51;
  78.         k = (k << 15) | (k >> 17);
  79.         k *= 0x1b873593;
  80.         h ^= k;
  81.     }
  82.     h ^= len;
  83.     h ^= h >> 16;
  84.     h *= 0x85ebca6b;
  85.     h ^= h >> 13;
  86.     h *= 0xc2b2ae35;
  87.     h ^= h >> 16;
  88.     return h;
  89. }
  90.  
  91. void testhash(const char *name, std::vector<std::string> &words, uint32_t func(const unsigned char *p, int len)) {
  92.  
  93.     std::vector<int> counts(BRACKETS, 0);
  94.     for (std::string &word: words) {
  95.         uint32_t hash = func(reinterpret_cast<const unsigned char *>(word.c_str()), word.size());
  96.         counts[hash % BRACKETS]++;
  97.     }
  98.  
  99.     std::vector<int> bracketMap(1000, 0);
  100.     for (int c: counts) {
  101.         if (c < bracketMap.size()) bracketMap[c]++;
  102.     }
  103.  
  104.     printf("%s:\n", name);
  105.     for (int i = 1; i < bracketMap.size(); i++) {
  106.         if (bracketMap[i] == 0) continue;
  107.  
  108.         printf("%d: %d\n", i, bracketMap[i]);
  109.     }
  110.     printf("\n");
  111. }
  112.  
  113. int main() {
  114.     std::vector<std::string> words;
  115.  
  116.     std::ifstream input("../words.txt");
  117.     for (std::string line; getline(input, line);) {
  118.         words.emplace_back(line);
  119.     }
  120. //    words.resize(10000);
  121.  
  122.     printf("Total words: %d\nBrackets: %d\n\n", words.size(), BRACKETS);
  123.  
  124.     testhash("cdb_hash", words, cdb_hash);
  125.     testhash("fnv_hash_1a_32", words, fnv_hash_1a_32);
  126.     testhash("djb2", words, djb2);
  127.     testhash("djb2a", words, djb2a);
  128.     testhash("murmur3_32", words, murmur3_32);
  129.  
  130.  
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement