-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabellahash.cpp
More file actions
152 lines (121 loc) · 3.03 KB
/
tabellahash.cpp
File metadata and controls
152 lines (121 loc) · 3.03 KB
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
#include "tabellahash.h"
TabellaHash::TabellaHash()
{
size=10;
termini=0;
hashtable.resize(size);
}
TabellaHash::~TabellaHash()
{}
int TabellaHash::hash1(char *v,int m)
{
int h=0;
for(; *v!=0;v++)
h=h+*v;
h=h%m;
return h;
}
int TabellaHash::hash2(char *v,int m)
{
int h=0;
for(; *v != 0; v++)
h=h+*v;
h=(h%(m-2))+1;
return h;
}
int TabellaHash::hashingdoppio(string par,int i)
{
int h1=hash1((char*)par.c_str(),size);
int h2=hash2((char*)par.c_str(),size);
int hd=(h1+(i*h2))%size;
return hd;
}
void TabellaHash::stampatable()
{
for(int i=0;i<grandezza();i++)
{ if(hashtable.at(i).getP() != "")
cout<<hashtable.at(i).getP()<<endl;
}
}
bool TabellaHash::inserisci(PAROLE par)
{
int i=0;
char scelta;
if ( termini*2 >= size ) sizepiu();
int hd=hashingdoppio(par.getP(),i);
while(hashtable.at(hd).getP() != "")
{
if(hashtable.at(hd).getP() == par.getP() )
{ cout<<"Il termine inserito esiste gia' ed ha questa definizione:"<<endl<<endl;
cout<<hashtable.at(hd).getD()<<endl<<endl;
cout<<"Se vuoi aggiornare la descrizione digita Y altrimenti digita N"<<endl;
cin>>scelta;
if ( scelta == 'Y' || scelta == 'y') {
hashtable.at(hd).UP_D(par.getD()); cout<<"Descrizione aggiornata!"<<endl;}
return false;
}
i++;
hd=hashingdoppio(par.getP(),i);
}
termini++;
hashtable.at(hd)=par;
return true;
}
bool TabellaHash::cerca(string par)
{
int i=0;
int hd=hashingdoppio(par,i);
while(hashtable.at(hd).getP() != "" )
{
if(hashtable.at(hd).getP() == par)
{
cout<<"La definizione e': "<<hashtable.at(hd).getD()<<endl;
return true;
}
else{
i++;
hd=hashingdoppio(par,i);
}
}
return false;
}
bool TabellaHash::rimuovi(string par)
{
int i=0;
int hd=hashingdoppio(par,i);
while(hashtable.at(hd).getP() != "" )
{
if(hashtable.at(hd).getP() == par)
{
while(hashtable.at(hashingdoppio(par,i+1)).getP() != "" )
{
hashtable.at(hd).UP_P( hashtable.at(hashingdoppio(par,i+1)).getP() );
hashtable.at(hd).UP_D( hashtable.at(hashingdoppio(par,i+1)).getD() );
i++;
}
hashtable.at(hd).UP_P("");
hashtable.at(hd).UP_D("");
cout<<"Ho eliminato il termine!"<<endl;
return true;
}
i++; hd=hashingdoppio(par,i);
} return false;
}
int TabellaHash::grandezza()
{ return size;}
void TabellaHash::sizepiu()
{
vector<PAROLE> supporto;
supporto.clear();
supporto.resize(size);
for(int i=0;i<size;i++)
supporto.at(i) = hashtable.at(i);
int vecchiosize=size;
size=size*2;
hashtable.clear();
hashtable.resize(size);
for(int i=0;i<vecchiosize;i++)
if (supporto.at(i).getP() != "") inserisci(supporto.at(i));
}
vector<PAROLE> TabellaHash::testa()
{ return hashtable; }