Pagini recente » Cod sursa (job #1968968) | Cod sursa (job #2152698) | Cod sursa (job #2753036) | Cod sursa (job #1108567) | Cod sursa (job #1490492)
#include <fstream>
#include <vector>
using namespace std;
template <typename T, typename U>
struct node {
T key;
U value;
node(const T &k, const U &v) : key(k), value(v) {}
};
template <typename T, typename U>
class Hash {
vector < node<T,U> > *table;
pair <unsigned int, int> find(const T &key) {
unsigned int h = hash(key), i;
for(i = 0; i<table[h].size(); i++)
if(table[h][i].key == key)
return make_pair(h, i);
return make_pair(h, -1);
}
public:
Hash (unsigned int s) : size(s) {
table = new vector < node <T,U> > [size];
}
void insert(const T &, const U &);
void remove(const T &);
bool isIn(const T &);
U get(const T&);
protected:
unsigned int size;
virtual unsigned int hash(const T&) = 0;
};
template<typename T, typename U>
void Hash<T,U>::insert(const T& key, const U& value) {
pair<unsigned int, int> p = find(key);
if(p.second == -1)
table[p.first].push_back(node<T,U>(key, value));
else
table[p.first][p.second].value = value;
}
template <typename T, typename U>
void Hash<T,U>::remove(const T& key) {
pair<unsigned int, int> p = find(key);
if(p.second == -1)
return;
else {
swap(table[p.first][p.second], table[p.first].back());
table[p.first].pop_back();
}
}
template <typename T, typename U>
bool Hash<T,U>::isIn(const T& key) {
pair<unsigned int, int> p = find(key);
return !(p.second == -1);
}
template <typename T, typename U>
U Hash<T,U>::get(const T& key) {
pair<unsigned int, int> p = find(key);
if(p.second != -1)
return table[p.first][p.second].value;
else
throw throw out_of_range("bad");
}
template <typename U>
class IntHash : public Hash<int, U> {
unsigned int hash (const int &key) {
return (key % Hash<int,U>::size);
}
public:
IntHash(unsigned int s) : Hash<int,U>::Hash(s) {}
};
ifstream f("hashuri.in");
ofstream g("hashuri.out");
int main() {
IntHash<bool> h(1000013);
int c, q, x;
f>>c;
while(c--)
{
f>>q>>x;
switch(q) {
case 1: h.insert(x, true); break;
case 2: h.remove(x); break;
case 3: g<<h.isIn(x)<<'\n'; break;
}
}
}