Advertisement
hxrussia

Multihash

Nov 16th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. const long long P = 41;
  2. const int hashes_count = 2;
  3. const long long mods[hashes_count] = {1000000087, 1000000093};
  4.  
  5. class Hashes {
  6.     long long hashes[hashes_count];
  7.    
  8.     inline static long long mod(long long x, long long m) {
  9.         long long res = x % m;
  10.         return res < 0 ? res + m : res;
  11.     }
  12. public:
  13.     Hashes() {
  14.         for (int i = 0; i < hashes_count; i++)
  15.             hashes[i] = 0;
  16.     }
  17.    
  18.     Hashes operator =(const Hashes &other) {
  19.         for (int i = 0; i < hashes_count; i++)
  20.             hashes[i] = other.hashes[i];
  21.         return *this;
  22.     }
  23.    
  24.     Hashes(const Hashes &other) {
  25.         for (int i = 0; i < hashes_count; i++)
  26.             hashes[i] = other.hashes[i];
  27.     }
  28.    
  29.     Hashes operator +=(int value) {
  30.         for (int i = 0; i < hashes_count; i++)
  31.             hashes[i] = mod(hashes[i] + value, mods[i]);
  32.         return *this;
  33.     }
  34.    
  35.     Hashes operator +=(const Hashes &other) {
  36.         for (int i = 0; i < hashes_count; i++)
  37.             hashes[i] = mod(hashes[i] + other.hashes[i], mods[i]);
  38.         return *this;
  39.     }
  40.    
  41.     Hashes operator *=(int value) {
  42.         for (int i = 0; i < hashes_count; i++)
  43.             hashes[i] = mod(hashes[i] * value, mods[i]);
  44.         return *this;
  45.     }
  46.    
  47.     friend bool operator <(const Hashes &a, const Hashes &b);
  48.     friend bool operator ==(const Hashes &a, const Hashes &b);
  49. };
  50.  
  51. bool operator <(const Hashes &a, const Hashes &b) {
  52.     for (int i = 0; i < hashes_count; i++)
  53.         if (a.hashes[i] != b.hashes[i])
  54.             return a.hashes[i] < b.hashes[i];
  55.     return false;
  56. }
  57.  
  58. bool operator ==(const Hashes &a, const Hashes &b) {
  59.     for (int i = 0; i < hashes_count; i++)
  60.         if (a.hashes[i] != b.hashes[i])
  61.             return false;
  62.     return true;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement