-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashtable.cpp
164 lines (161 loc) · 4.39 KB
/
Hashtable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <string>
using namespace std;
class Musica{
public:
int t, total;
int id;
string nome;
Musica(){
nome = " ";
t = -1;
total = 0;
id = 0;
}
Musica(string n, int temp){
nome = n;
t = temp;
total = 0;
id = 0;
}
string obterNome(){
return nome;
}
int obterTempo(){
return t;
}
int obterTotal(){
return total;
}
int obterId(){
return id;
}
void incrementaTempo(){
total = total+t;
}
void calcularId(string nome){
for (int i = 0; i < nome.size(); i++)
id += (int)nome[i]*i;
}
};
class Hash{
public:
int max_itens;
int max_posicoes;
int quant_itens;
Musica* estrutura;
int funcaoHash (Musica musica){
return(musica.obterId()%max_posicoes);
}
int funcaoHashBusca (Musica musica){
musica.calcularId(musica.obterNome());
return(musica.obterId()%max_posicoes);
}
Hash(int n){
quant_itens = 0;
max_itens = n*0.5;
max_posicoes = n;
estrutura = new Musica[n];
//condicao de aumento de vetor eh qnt_itens >= max itens;
}
~Hash(){
delete [] estrutura;
}
bool estaCheio(){
return(quant_itens >= (max_itens)+1);
}
int oberTamanhoAtual(){
return(quant_itens);
}
void inserir(Musica musica){
int local = funcaoHash(musica);
while(estrutura[local].obterTempo() > 0){
local = (local+1) % max_posicoes;
}
estrutura[local] = musica;
quant_itens++;
cout << musica.obterNome() << " " << local << endl;
}
void rehash(Musica musica){
int local = funcaoHash(musica);
while(estrutura[local].obterTempo() > 0){
local = (local+1) % max_posicoes;
}
estrutura[local] = musica;
quant_itens++;
}
void buscaIncrementa(Musica& musica){
int local = funcaoHashBusca(musica);
while (estrutura[local].obterTempo() != -1){
if (estrutura[local].obterNome()==musica.obterNome()){
musica = estrutura[local];
musica.incrementaTempo();
estrutura[local] = musica;
cout << estrutura[local].obterNome() << " " << estrutura[local].obterTotal() << endl;
break;
}
local = (local+1) % max_posicoes;
}
}
void buscaImprime(Musica musica){//
int local = funcaoHashBusca(musica);//
while (estrutura[local].obterTempo() != -1){
if (estrutura[local].obterNome()==musica.obterNome()){
cout << estrutura[local].obterNome() << " " << estrutura[local].obterTotal() << endl;
break;
}
local = (local+1) % max_posicoes;
}
}
};
int main(){
int n;
string comando, nome;
int t;
cin >> n;
Hash hashtable(n);
do{
cin >> comando;
if (comando=="END"){
continue;
}
else if(comando=="ADD"){
if (hashtable.estaCheio()){
int num = hashtable.oberTamanhoAtual();
Musica* aux = new Musica[num];
int pos = 0;
for (int i = 0; i < n; i++){
if(hashtable.estrutura[i].t!=-1){
aux[pos] = hashtable.estrutura[i];
pos++;
}
}
delete [] hashtable.estrutura;
n = (n*2)+1;
hashtable.quant_itens = 0;
hashtable.max_itens = n*0.5;
hashtable.max_posicoes = n;
hashtable.estrutura = new Musica[n];
//reharshing
for (int i = 0; i < num; i++){
hashtable.rehash(aux[i]);
}
delete [] aux;
}
cin >> nome >> t;
Musica musica(nome, t);
musica.calcularId(musica.obterNome());
hashtable.inserir(musica);
}
else if(comando=="PLAY"){
cin >> nome;
Musica musica(nome, 0);
hashtable.buscaIncrementa(musica);
}
else if(comando=="TIME"){
cin >> nome;
Musica musica(nome, 0);
hashtable.buscaImprime(musica);
}
} while(comando!="END");
}