Pagini recente » Cod sursa (job #1156759) | Cod sursa (job #2937621) | Cod sursa (job #1029655) | Cod sursa (job #3217703) | Cod sursa (job #2417896)
#include <fstream>
#ifndef HASHTABLE_SEPARATE_CHAINING_WITH_LINKED_LISTS_H
#define HASHTABLE_SEPARATE_CHAINING_WITH_LINKED_LISTS_H
#include <iostream>
#include <vector>
#include <list>
#define ULL unsigned long long int
#define PRIME_CAPACITY_FOR_HASH 666013
template <typename Tkey, typename Tvalue>
struct info {
Tkey key;
Tvalue value;
};
template <typename Tkey, typename Tvalue>
class Hashtable_SCLL {
private:
std::vector<std::list<struct info<Tkey, Tvalue>>> hash_table_;
int size_;
int capacity_;
ULL (*hash_)(Tkey);
public:
Hashtable_SCLL(int capacity, ULL (*h)(Tkey));
~Hashtable_SCLL();
bool lookup(const Tkey&);
void set(const Tkey&, const Tvalue&);
void remove(const Tkey&);
Tvalue operator[](const Tkey&);
int getSize();
int getCapacity();
void printHashTable();
};
template <typename Tkey, typename Tvalue>
Hashtable_SCLL<Tkey, Tvalue>::Hashtable_SCLL(int capacity, ULL (*h)(Tkey)):
hash_table_(capacity),
size_(0), capacity_(capacity), hash_(h) {}
template <typename Tkey, typename Tvalue>
Hashtable_SCLL<Tkey, Tvalue>::~Hashtable_SCLL() {}
template <typename Tkey, typename Tvalue>
bool Hashtable_SCLL<Tkey, Tvalue>::lookup(const Tkey& key) {
int index = hash_(key) % capacity_;
for (auto it = hash_table_[index].begin(); it != hash_table_[index].end(); ++it) {
if ((*it).key == key) {
return true;
}
}
return false;
}
template <typename Tkey, typename Tvalue>
void Hashtable_SCLL<Tkey, Tvalue>::set(const Tkey& key, const Tvalue& value) {
info<Tkey, Tvalue> data = {key, value};
remove(key);
hash_table_[hash_(key) % capacity_].push_back(data);
}
template <typename Tkey, typename Tvalue>
void Hashtable_SCLL<Tkey, Tvalue>::remove(const Tkey& key) {
int index = hash_(key) % capacity_;
for (auto it = hash_table_[index].begin(); it != hash_table_[index].end(); ++it) {
if ((*it).key == key) {
hash_table_[index].erase(it);
break;
}
}
}
template <typename Tkey, typename Tvalue>
Tvalue Hashtable_SCLL<Tkey, Tvalue>::operator[](const Tkey& key) {
int index = hash_(key) % capacity_;
for (auto it = hash_table_[index].begin(); it != hash_table_[index].end(); ++it) {
if ((*it).key == key) {
return (*it).value;
}
}
//std::cerr << "Standard exception: key not found\n";
return Tvalue();
}
template <typename Tkey, typename Tvalue>
int Hashtable_SCLL<Tkey, Tvalue>::getSize() {
return size_;
}
template <typename Tkey, typename Tvalue>
int Hashtable_SCLL<Tkey, Tvalue>::getCapacity() {
return capacity_;
}
template <typename Tkey, typename Tvalue>
void Hashtable_SCLL<Tkey, Tvalue>::printHashTable() {
for (unsigned int i = 0; i < hash_table_.size(); ++i) {
for (auto it = hash_table_[i].begin(); it != hash_table_[i].end(); ++it) {
std::cout << "<" << i << ", " << (*it).key << ", " << (*it).value << "> ";
}
}
std::cout << "\n\n";
}
#endif // HASHTABLE_SEPARATE_CHAINING_WITH_LINKED_LISTS_H
unsigned long long int try_hash(int number) {
return number;
}
int main() {
std::ifstream fin("hashuri.in");
std::ofstream fout("hashuri.out");
Hashtable_SCLL<int, int> ht(PRIME_CAPACITY_FOR_HASH, try_hash);
int N, q, x;
fin >> N;
for (int i = 0; i < N; ++i) {
fin >> q >> x;
switch(q) {
case 1:
ht.set(x, 1);
break;
case 2:
ht.remove(x);
break;
default:
fout << ht.lookup(x) << '\n';
}
}
fin.close();
fout.close();
return 0;
}