Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <array>
  4. #include <cmath>
  5. #include <set>
  6. #include <random>
  7. using namespace std;
  8.  
  9. std::array<std::uint32_t, 8> P = {
  10.     2,
  11.     59,
  12.     137,
  13.     10321,
  14.     434981,
  15.     2002547,
  16.     1676902,
  17.     1073676287
  18. };
  19.  
  20. std::int64_t hash1(std::int32_t x) {
  21.     std::int64_t result = 0;
  22.  
  23.     for (std::size_t i = 0; i < P.size(); ++i) {
  24.         std::int64_t left = (x % P[i]);
  25.         std::int64_t right = std::floor((x / (std::pow(16.0f, i) - 1.0f)));
  26.  
  27.         //std::cout << "P[" << i << "]: " << P[i] << std::endl;
  28.         //std::cout << "left:  " << left << std::endl;
  29.         //std::cout << "right: " << right << std::endl;
  30.         result += left * right;
  31.     }
  32.     return result;
  33. }
  34.  
  35. std::int64_t hash2(std::int32_t x) {
  36.     std::int64_t result = 0;
  37.  
  38.     for (std::size_t i = 0; i < P.size(); ++i) {
  39.         result += std::pow((x >> i) ^ (x & P[i]), 2);
  40.     }
  41.  
  42.     return result;
  43. }
  44.  
  45. std::set<std::int64_t> hash1_values;
  46. std::set<std::int64_t> hash2_values;
  47.  
  48. int main() {
  49.     std::mt19937 gen{std::random_device{}()};
  50.  
  51.     for (std::int32_t i = 0; i < 2000000; ++i) {
  52.         auto h = hash1(gen());
  53.         hash1_values.insert(h);
  54.     }
  55.  
  56.     for (std::int32_t i = 0; i < 2000000; ++i) {
  57.         auto h = hash2(gen());
  58.         hash2_values.insert(h);
  59.     }
  60.  
  61.     std::cout << hash1_values.size() << std::endl;
  62.     std::cout << hash2_values.size() << std::endl;
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement