Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef unsigned long long integer;
- struct my_hash_map
- {
- vector<integer> q;
- vector<char> used;
- int cnt;
- my_hash_map(int sz = 1)
- {
- q.assign(sz, 0);
- used.assign(sz, 0);
- cnt = 0;
- }
- void rehash(int sz)
- {
- if (sz <= sz(q))
- return;
- vector<integer> q_(sz, 0);
- vector<char> used_(sz, 0);
- for (int i = 0; i < sz(q); i++)
- if (used[i])
- {
- int pos = q[i] % (integer)sz;
- while (used_[pos])
- pos = (pos + 1 == sz ? 0 : pos + 1);
- used_[pos] = 1;
- q_[pos] = q[i];
- }
- swap(q, q_);
- swap(used, used_);
- }
- void insert(integer val)
- {
- int pos = val % (integer)sz(q);
- while (used[pos])
- {
- if (q[pos] == val)
- return;
- pos = (pos + 1 == sz(q) ? 0 : pos + 1);
- }
- used[pos] = 1;
- q[pos] = val;
- cnt++;
- if (cnt * 2 > sz(q))
- rehash(2 * sz(q) + 3); // random (lol)
- }
- void erase(integer val)
- {
- int pos = val % (integer)sz(q);
- while (used[pos])
- {
- if (q[pos] == val)
- {
- cnt--;
- used[pos] = 0;
- return;
- }
- pos = (pos + 1 == sz(q) ? 0 : pos + 1);
- }
- return;
- }
- int count(integer val)
- {
- int pos = val % (integer)sz(q);
- while (used[pos])
- {
- if (q[pos] == val)
- return 1;
- pos = (pos + 1 == sz(q) ? 0 : pos + 1);
- }
- return 0;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement