Advertisement
tranxuanbach

Hashing use for converting from Themis to Polygon

Nov 28th, 2020 (edited)
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. array <int, 4> mod;
  2.  
  3. struct elem{
  4.     int type; // 1 = number, 2 = string
  5.     ll number1;
  6.     string string2;
  7.  
  8.     void checkMod(){
  9.         if (!mod[0]){
  10.             mod[0] = 998244353; mod[1] = 999995993; mod[2] = 999996989; mod[3] = 999999067;
  11.         }
  12.     }
  13.  
  14.     elem(): type(0) {
  15.         checkMod();
  16.     }
  17.  
  18.     elem(ll number1): type(1), number1(number1) {
  19.         checkMod();
  20.     }
  21.  
  22.     elem(string string2): type(2), string2(string2) {
  23.         checkMod();
  24.     }
  25.  
  26.     array <int, 4> hashValue(){
  27.         assert(type != 0);
  28.         array <int, 4> ansHashValue;
  29.         if (type == 1){
  30.             For(i, 0, 4){
  31.                 ansHashValue[i] = number1 % mod[i];
  32.                 if (ansHashValue[i] < 0) ansHashValue[i] += mod[i];
  33.             }
  34.         }
  35.         else if (type == 2){
  36.             For(i, 0, 4){
  37.                 ansHashValue[i] = 0;
  38.                 For(j, 0, isz(string2)){
  39.                     ansHashValue[i] = ((ll)ansHashValue[i] * 256 + string2[j]) % mod[i];
  40.                 }
  41.             }
  42.         }
  43.         return ansHashValue;
  44.     }
  45. };
  46.  
  47. array <int, 4> hashValue(elem a){
  48.     return a.hashValue();
  49. }
  50.  
  51. array <int, 4> hashValue(vector <elem> a){
  52.     array <int, 4> ansHashValue;
  53.     vector <array <int, 4>> aHashValue;
  54.     For(j, 0, isz(a)){
  55.         aHashValue[j] = a[j].hashValue();
  56.     }
  57.     For(i, 0, 4){
  58.         ansHashValue[i] = 0;
  59.         For(j, 0, isz(a)){
  60.             ansHashValue[i] ^= aHashValue[j][i] * j;
  61.         }
  62.     }
  63.     return ansHashValue;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement