Cod sursa(job #2417754)

Utilizator horiacoolNedelcu Horia Alexandru horiacool Data 1 mai 2019 04:18:24
Problema Hashuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.17 kb
#include <fstream>
 
#ifndef HASHTABLE_H_
#define HASHTABLE_H_

#include <list>
#include <iterator>
#include <iostream>

#define PRIME_FOR_HASH 666013

/**
 * Hashtable implementation.
 */
template <typename Tkey, typename Tvalue> struct info {
    Tkey key;
    Tvalue value;
};

template <typename Tkey, typename Tvalue> class Hashtable {
 private:
    info<Tkey, Tvalue> H_[PRIME_FOR_HASH];
    char type_H_[PRIME_FOR_HASH];
    int size_;
    int capacity_;
    unsigned long (*hash_)(Tkey);

 public:
    // Constructor
    Hashtable(unsigned long (*h)(Tkey));

    // Copy Constructor
    Hashtable(const Hashtable<Tkey, Tvalue>&);

    // Destructor
    ~Hashtable();

    // Copy assignment operator
    Hashtable<Tkey, Tvalue>& operator=(const Hashtable<Tkey, Tvalue>&);

    void put(Tkey key, Tvalue value);

    void remove(Tkey key);

    Tvalue operator[](Tkey key);

    bool hasKey(Tkey key);

    int getSize();

    int getCapacity();
};

template <typename Tkey, typename Tvalue>
Hashtable<Tkey, Tvalue>::Hashtable(unsigned long (*h)(Tkey)): size_(0), capacity_(PRIME_FOR_HASH), hash_(h) {
    //for (int i = 0; i < PRIME_FOR_HASH; ++i) {
    //    type_H_[i] = 0;
    //}
}

template <typename Tkey, typename Tvalue>
Hashtable<Tkey, Tvalue>::Hashtable(const Hashtable<Tkey, Tvalue>& other) {
    for (int i = 0; i < PRIME_FOR_HASH; ++i) {
        H_[i] = other.H_[i];
        type_H_[i] = other.type_H_[i];
    }

    size_ = other.size_;
    hash_ = other.hash_;
}

template <typename Tkey, typename Tvalue>
Hashtable<Tkey, Tvalue>::~Hashtable() {}

template <typename Tkey, typename Tvalue> Hashtable<Tkey, Tvalue>&
Hashtable<Tkey, Tvalue>::operator=(const Hashtable<Tkey, Tvalue>& other) {
    for (int i = 0; i < PRIME_FOR_HASH; ++i) {
        H_[i] = other.H_[i];
        type_H_[i] = other.type_H_[i];
    }

    size_ = other.size_;
    hash_ = other.hash_;
}

template <typename Tkey, typename Tvalue>
void Hashtable<Tkey, Tvalue>::put(Tkey key, Tvalue value) {
    info<Tkey, Tvalue> data = {key, value};

    unsigned int i = 0, index = hash_(key);

    while (type_H_[(index + i) % capacity_] != 0 && type_H_[(index + i) % capacity_] != 1) {
        ++i;
    }

    type_H_[(index + i) % capacity_] = 2;
    H_[(index + i) % capacity_] = data;
}

template <typename Tkey, typename Tvalue>
void Hashtable<Tkey, Tvalue>::remove(Tkey key) {
    unsigned int i = 0, index = hash_(key);

    while (type_H_[(index + i) % capacity_] != 0 && H_[(index + i) % capacity_].key != key) {
        ++i;
    }

    if (type_H_[(index + i) % capacity_] == 2 && H_[(index + i) % capacity_].key == key) {
        type_H_[(index + i) % capacity_] = 1;   
    }
}

template <typename Tkey, typename Tvalue>
Tvalue Hashtable<Tkey, Tvalue>::operator[](Tkey key) {
    unsigned int i = 0, index = hash_(key) % capacity_;

    while (type_H_[(index + i) % capacity_] != 0 && H_[(index + i) % capacity_].key != key) {
        ++i;
    }

    if (type_H_[(index + i) % capacity_] == 2 && H_[(index + i) % capacity_].key == key) {
        return H_[(index + i) % capacity_].value;
    }

    std::cerr << "Standard exception: key not found\n";
    return Tvalue();
}

template <typename Tkey, typename Tvalue>
bool Hashtable<Tkey, Tvalue>::hasKey(Tkey key) {
    unsigned int i = 0, index = hash_(key) % capacity_;

    while (type_H_[(index + i) % capacity_] != 0 && H_[(index + i) % capacity_].key != key) {
        ++i;
    }

    if (type_H_[(index + i) % capacity_] == 2 && H_[(index + i) % capacity_].key == key) {
        return true;
    }

    return false;
}

template <typename Tkey, typename Tvalue>
int Hashtable<Tkey, Tvalue>::getSize() {
    return size_;
}

template <typename Tkey, typename Tvalue>
int Hashtable<Tkey, Tvalue>::getCapacity() {
    return capacity_;
}

#endif // HASHTABLE_H_

 
unsigned long int_hash(int number) {
  return number;
}
 
int main() {
	std::ifstream fin("hashuri.in");
	std::ofstream fout("hashuri.out");
 
	Hashtable<int, int> ht(int_hash);
	int N, q, x;
 
	fin >> N;
	for (int i = 0; i < N; ++i) {
		fin >> q >> x;
 
		switch(q) {
			case 1:
				ht.put(x, 1);
				break;
			case 2:
				ht.remove(x);
				break;
			default:
				fout << ht.hasKey(x) << '\n';
		}
	}
 
	fin.close();
	fout.close();
 
	return 0;
}