Cod sursa(job #1489500)

Utilizator deea101Andreea deea101 Data 21 septembrie 2015 12:04:04
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.67 kb
enum State {full, empty, del};

template <typename T, typename U>
struct entry {
	T key;
	U value;
	State state;
	entry () : key(), value() { state=empty; }
};

template <typename T, typename U>
class Hash {
	
	entry <T,U> *table;
	
	int probe(int h, int i) {
		return (h+i)%size;
	}
	int find(const T &key) {
		int h = hash(key), i, p;
		for(i = 0; i<size; ++i) {
			p = probe(h,i);
			if(table[p].state == empty || table[p].key == key)
				return p;
		}
		//resize();
	}
	
public:
	Hash() : size(1000013) {
		table = new entry<T,U> [size];
	}
	U get(const T&);
	void put(const T&, const U&);
	void remove(const T&);
	
protected:
	virtual int hash(const T&) = 0;
	int size;
};

template <typename T, typename U>
U Hash<T,U>::get(const T &key) {
	int p = find(key);
	
	if(table[p].state == full) 
		return table[p].value;
	else 
		return U();
}

template <typename T, typename U>
void Hash<T,U>::put(const T &key, const U &value) {
	int p = find(key);
	
	if(table[p].state == empty)
		table[p].key = key;
	
	table[p].value = value;
	table[p].state = full;
}

template <typename T, typename U>
void Hash<T,U>::remove(const T &key) {
	int p = find(key);
	
	if(table[p].state == full)
		table[p].state = del;
}

template <typename U>
class IntHash : public Hash <int, U> {
	int hash(const int &key) {
		return key%size;
	}
};

#include <fstream>
std::ifstream f("hashuri.in");
std::ofstream g("hashuri.out");
int main() {
	
	IntHash<bool> h;
	
	int c, q, x;
	f>>c;
	while(c--)
	{
		f>>q>>x;
		switch(q) {
		case 1: h.put(x, true); break;
		case 2: h.remove(x); break;
		case 3: g<<h.get(x)<<'\n'; break;
		}
	}
}