Alex_tz307

Hash cu pointeri

Apr 26th, 2021 (edited)
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. typedef struct element {
  2.   int val, freq;
  3.   element *next;
  4. } *lista;
  5.  
  6. const int MASK = (1 << 23) - 1;
  7. const int mod = 196613;
  8. bitset<MASK + 1> filter;
  9. lista h[mod];
  10.  
  11. void Insert(int x) {
  12.   filter[x & MASK] = true;
  13.   int key = x % mod;
  14.   lista p;
  15.   for (p = h[key]; p; p = p->next)
  16.     if (p->val == x) {
  17.       ++p->freq;
  18.       return;
  19.     }
  20.   p = new element;
  21.   p->val = x;
  22.   p->freq = 1;
  23.   p->next = h[key];
  24.   h[key] = p;
  25. }
  26.  
  27. void Erase(int x) {
  28.   if (!filter[x & MASK])
  29.     return;
  30.   int key = x % mod;
  31.   for (lista p = h[key]; p; p = p->next)
  32.     if (p->val == x) {
  33.       --p->freq;
  34.       return;
  35.     }
  36. }
  37.  
  38. int Find(int x) {
  39.   if (!filter[x & MASK])
  40.     return 0;
  41.   int key = x % mod;
  42.   for (lista p = h[key]; p; p = p->next)
  43.     if (p->val == x)
  44.       return p->freq;
  45.   return 0;
  46. }
Add Comment
Please, Sign In to add comment