Pagini recente » Cod sursa (job #576956) | Cod sursa (job #2902535) | Cod sursa (job #897043) | Cod sursa (job #1830986) | Cod sursa (job #2418049)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("hashuri.in");
ofstream t ("hashuri.out");
template <class _key, class _information>
class hashtable {
public:
/// makes no sense to implement rule of three
hashtable(unsigned expected_number_of_elements = 1e6,
double load_factor = 0.75) {
MOD = 1LL * (expected_number_of_elements * (1.0 / load_factor));
storage = new data_cell[MOD + 10];
}
[[gnu::hot]] void add(_key key, _information info) {
//uint16_t normalised_key = hash_key(key);
int normalised_key = key;
uint64_t start = (1LL * normalised_key * prime1) % MOD,
step = (1LL * normalised_key * prime2) % MOD,
index = start;
for (; storage[index].free == false && storage[index].key != key;
index = (index + step) % MOD) {}
storage[index].free = false,
storage[index].key = key,
storage[index].info = info;
}
[[gnu::hot]] bool add_unique(_key key, _information info) {
//uint16_t normalised_key = hash_key(key);
int normalised_key = key;
uint64_t start = (1LL * normalised_key * prime1) % MOD,
step = (1LL * normalised_key * prime2) % MOD,
index = start;
for (; storage[index].free == false && storage[index].key != key;
index = (index + step) % MOD) {}
if (storage[index].free) {
storage[index].free = false,
storage[index].key = key,
storage[index].info = info;
return true;
} else {
return false;
}
}
[[gnu::hot]] void remove(_key key) {
//uint16_t normalised_key = hash_key(key);
int normalised_key = key;
uint64_t start = (1LL * normalised_key * prime1) % MOD,
step = (1LL * normalised_key * prime2) % MOD,
index = start;
for (; storage[index].free == false && storage[index].key != key;
index = (index + step) % MOD) {}
storage[index].free = true;
}
[[gnu::hot]] _information find(_key key) {
//uint16_t normalised_key = hash_key(key);
int normalised_key = key;
uint64_t start = (1LL * normalised_key * prime1) % MOD,
step = (1LL * normalised_key * prime2) % MOD,
index = start;
for (; storage[index].free == false && storage[index].key != key;
index = (index + step) % MOD) {}
return storage[index].info;
}
bool check(_key key) {
//uint16_t normalised_key = hash_key(key);
int normalised_key = key;
uint64_t start = (1LL * normalised_key * prime1) % MOD,
step = (1LL * normalised_key * prime2) % MOD,
index = start;
for (; storage[index].free == false && storage[index].key != key;
index = (index + step) % MOD) {}
return (storage[index].free ^ 1);
}
~hashtable() {
delete[] storage;
}
private:
struct data_cell{
bool free = true;
_key key;
_information info;
} *storage;
/*
uint16_t hash_key(_key target) {
uint16_t projection = 0xC00F; /// random seed
std::hash<_key> xoring;
return projection ^ xoring(target);
}
*/
static const unsigned prime1 = 6397,
prime2 = 8761;
uint64_t MOD;
};
int main() {
int n, q, x;
f >> n;
hashtable<int, int> h;
for(int i = 0; i < n; ++i) {
f >> q >> x;
if (q == 1)
h.add(x, x);
else if(q == 2)
h.remove(x);
else
t << h.check(x) << '\n';
}
return 0;
}