Newer
Older
Ítalo Epifânio de Lima e Silva
committed
#ifndef _HASH_TABLE_HPP
#define _HASH_TABLE_HPP
#include <string>
#include <vector>
#include <list>
#include <iostream>
Ítalo Epifânio de Lima e Silva
committed
template <typename T>
class HashTable {
private:
int tamanho;
std::vector<std::list<T>> buckets;
public:
Ítalo Epifânio de Lima e Silva
committed
this->buckets.resize(tamanho);
Ítalo Epifânio de Lima e Silva
committed
}
bool vazio(){
return ! this->tamanho;
}
int hash(T valor){
return valor >> 1;
}
void print(){
for(auto& bucket : buckets){
std::cout << "[";
for(auto valor : bucket){
std::cout << " " << valor << " ";
Ítalo Epifânio de Lima e Silva
committed
}
Ítalo Epifânio de Lima e Silva
committed
std::cout << std::endl;
}
}
bool find(T valor){
int idx = hash(valor) % getCapacidade();
Ítalo Epifânio de Lima e Silva
committed
Ítalo Epifânio de Lima e Silva
committed
buckets[idx].begin(),
buckets[idx].end(),
valor
);
return posicao != buckets[idx].end();
}
void adicionar(T valor) {
int idx = hash(valor) % getCapacidade();
Ítalo Epifânio de Lima e Silva
committed
buckets[idx].push_back(valor);
}
// Gets e sets
int getTamanho(){
return tamanho;
}
int getCapacidade(){
return buckets.size();
}
};
#endif