-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdHashTable.cpp
More file actions
405 lines (359 loc) · 17.2 KB
/
cdHashTable.cpp
File metadata and controls
405 lines (359 loc) · 17.2 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "cdHashTable.h"
#include <unistd.h>
#include <sys/types.h>
cdHashTable::cdHashTable(int sze, int bucketsize){
size = sze;
table = new chain_node *[size]; //ena dunamiko array apo deiktes se chain_node (buckets)
for(unsigned int i=0; i<size; i++){
table[i] = NULL; //arxika adeio olo
}
bucksize = bucketsize;
}
cdHashTable::~cdHashTable(){
for(unsigned int i=0; i<size; i++)
if(table[i] != NULL)
delete table[i];
delete[] table; //katastrofh tou pinaka apo chain nodes. To kathe chain node exei ton diko tou destructor kai kanei toso delete this oso kai delete next.
//h c++ frontizei kai ekshgw parakatw pws
}
//leitourgei kai gia disease k gia country
void cdHashTable::recordPatientExit(std::string disease_country, std::string status){
unsigned hval = hash_str(disease_country); //hasharei to diseaseID
hval = hval % size; //gia na pame sth swsth thesh pinaka
if(table[hval] == NULL){ //Auth h periptwsh de tha ginei pote sthn askhsh
std::cout << "Den yparxei h arrwstia/xwra (akoma)\n";
return;
}
else{
chain_node * currptr = table[hval];
while(currptr!= NULL){ //to psaxnei dieksodika wste na enhmerwthei o curr deikths
block_entry * buroku = currptr->block;
for(unsigned int i=0; i< currptr->block_size; i++){
if(buroku[i].dis_name_ptr == NULL)
continue;
if(*(buroku[i].dis_name_ptr) == disease_country){
if(status == "increase")
buroku[i].currval += 1; //ksanampainei nosokomeio, auksanetai o metrhths kai telos
if(status == "decrease")
buroku[i].currval -= 1; //phre eksithrio, meiwnetai o metrhths kai telos
return;
}
}//telos for gia block
currptr = currptr->next ;
}//telos while gia orizontia lista
}//telos else
std::cout << "De bre8hke h arrwstia/xwra\n";
return;
}
//dikh moy ektypwsh
void cdHashTable::print_contents(){
for(unsigned int i=0; i<size; i++){
std::cout << "anoigw thn " << i << " thesh tou pinaka\n";
if(table[i] == NULL) //kenh alusida
continue;
else{
chain_node * currptr = table[i];
int blockorder =1;
while(currptr!= NULL){ //paei sto teleutaio. ENDEIKTIKH EKTYPWSH. MONO MERIKA PEDIA ALLA MPORW KAI OLA
std::cout << "\t"<< "koitazw to block " << blockorder << "\n\t\t";
currptr->print_contents();
std::cout << "\n\n";
currptr = currptr->next ;
blockorder++;
}//telos while gia orizontia lista
}//telos else
}//telos for gia kathe alusida
}
//DISEASE
//derived parametrized constructor
diseaseHashTable::diseaseHashTable(int sze, int bucketsize) : cdHashTable(sze, bucketsize){
}
//h virtual gia disease
int diseaseHashTable::insert_record(record* rec){
unsigned hval = hash_str(rec->get_diseaseID()); //hasharei to diseaseID
hval = hval % size; //gia na pame sth swsth thesh pinaka
if(table[hval] == NULL){ //ean den yparxei alusida ekei, th ftiaxnoyme
table[hval] = new chain_node(bucksize);
table[hval]->insert_record(rec, rec->get_diseaseID());
return 0;
}
else{
chain_node * currptr = table[hval];
bool done = false;
while(!done){ //prospathei na to valei se kapoio ths alusidas. to kathena koitaei ton pinaka tou
done = currptr->insert_record(rec, rec->get_diseaseID());
if((done==false)&&(currptr->next == NULL)){ //an den exei kataferei na th valei kai den exoume epomeno komvo, ftiaxnume k paei ekei.
currptr->next = new chain_node(bucksize);
currptr->next->insert_record(rec, rec->get_diseaseID());
return 0;
}//telos if
currptr = currptr->next ;
}//telos while done eisagwghs
}//telos else
return 0;
}
//gia to diseaseFrequency xwris country
int diseaseHashTable::total_recs_for_cat(std::string diseasename, std::string date1, std::string date2){
unsigned hval = hash_str(diseasename); //hasharei to diseaseID
hval = hval % size; //gia na pame sth swsth thesh pinaka
if(table[hval] == NULL){ //Auth h periptwsh de tha ginei pote sthn askhsh
//std::cout << diseasename << " " << 0 << "\n";
return 0;
}
else{
chain_node * currptr = table[hval];
while(currptr!= NULL){ //to psaxnei dieksodika wste na brethei h astheneia
block_entry * buroku = currptr->block;
for(unsigned int i=0; i< currptr->block_size; i++){
if(buroku[i].dis_name_ptr == NULL)
continue;
if(*(buroku[i].dis_name_ptr) == diseasename){
//EDW GINETAI H DOULITSA ME TO DENDRO
search_containter querycontainer(buroku[i].totalval); //to megisto plhthos eggrafwn einai o sunolikos arithmos eggrafwn auths ths atheneias/xwras
//twra o container exei oles tis eggrafes ths astheneias/xwras me entrydate <= date2. H parakatw entolh ftiaxnei kai thn allh proypothesh
buroku[i].tree_ptr->collect_dated_reclists(buroku[i].tree_ptr, date2, &querycontainer); //o container exei tis eggrafes gia authn thn astheneia/xwra me entrydate <= Date2. Ekmetalleuetai th dendrikh domh gia kalyterh polyplokothta
int number_to_present = querycontainer.count_exit_limit(date1);
//std::cout << *(buroku[i].dis_name_ptr) << " " << number_to_present << "\n";
return number_to_present;
}
}//telos for gia block
currptr = currptr->next ;
}//telos while gia orizontia lista
}//telos else
//std::cout << diseasename << " " << 0 << "\n";
return 0;
}
//gia to diseaseFrequency ME country
int diseaseHashTable::total_recs_for_cat(std::string diseasename, std::string date1, std::string date2, std::string country){
unsigned hval = hash_str(diseasename); //hasharei to diseaseID
hval = hval % size; //gia na pame sth swsth thesh pinaka
if(table[hval] == NULL){ //Auth h periptwsh de tha ginei pote sthn askhsh
//std::cout << diseasename << " " << 0 << "\n";
return 0;
}
else{
chain_node * currptr = table[hval];
while(currptr!= NULL){ //to psaxnei dieksodika wste na brethei h astheneia
block_entry * buroku = currptr->block;
for(unsigned int i=0; i< currptr->block_size; i++){
if(buroku[i].dis_name_ptr == NULL)
continue;
if(*(buroku[i].dis_name_ptr) == diseasename){
//EDW GINETAI H DOULITSA ME TO DENDRO
search_containter querycontainer(buroku[i].totalval); //to megisto plhthos eggrafwn einai o sunolikos arithmos eggrafwn auths ths atheneias/xwras
//twra o container exei oles tis eggrafes ths astheneias/xwras me entrydate <= date2. H parakatw entolh ftiaxnei kai thn allh proypothesh
buroku[i].tree_ptr->collect_dated_reclists(buroku[i].tree_ptr, date2, &querycontainer); //o container exei tis eggrafes gia authn thn astheneia/xwra me entrydate <= Date2. Ekmetalleuetai th dendrikh domh gia kalyterh polyplokothta
int number_to_present = querycontainer.count_exit_limit(date1, country);
//std::cout << *(buroku[i].dis_name_ptr) << " " << number_to_present << "\n";
return number_to_present;
}
}//telos for gia block
currptr = currptr->next ;
}//telos while gia orizontia lista
}//telos else
//std::cout << diseasename << " " << 0 << "\n";
return 0;
}
//gia to admissions ME country
int diseaseHashTable::admissions(std::string diseasename, std::string date1, std::string date2, std::string country){
unsigned hval = hash_str(diseasename); //hasharei to diseaseID
hval = hval % size; //gia na pame sth swsth thesh pinaka
if(table[hval] == NULL){ //Auth h periptwsh de tha ginei pote sthn askhsh
//std::cout << diseasename << " " << 0 << "\n";
return 0;
}
else{
chain_node * currptr = table[hval];
while(currptr!= NULL){ //to psaxnei dieksodika wste na brethei h astheneia
block_entry * buroku = currptr->block;
for(unsigned int i=0; i< currptr->block_size; i++){
if(buroku[i].dis_name_ptr == NULL)
continue;
if(*(buroku[i].dis_name_ptr) == diseasename){
//EDW GINETAI H DOULITSA ME TO DENDRO
search_containter querycontainer(buroku[i].totalval); //to megisto plhthos eggrafwn einai o sunolikos arithmos eggrafwn auths ths atheneias/xwras
//twra o container exei oles tis eggrafes ths astheneias/xwras me entrydate <= date2. H parakatw entolh ftiaxnei kai thn allh proypothesh
buroku[i].tree_ptr->collect_dated_reclists(buroku[i].tree_ptr, date2, &querycontainer); //o container exei tis eggrafes gia authn thn astheneia/xwra me entrydate <= Date2. Ekmetalleuetai th dendrikh domh gia kalyterh polyplokothta
int number_to_present = querycontainer.count_entry_limit(date1, country);
//std::cout << *(buroku[i].dis_name_ptr) << " " << number_to_present << "\n";
return number_to_present;
}
}//telos for gia block
currptr = currptr->next ;
}//telos while gia orizontia lista
}//telos else
//std::cout << diseasename << " " << 0 << "\n";
return 0;
}
//gia to discharges ME country
int diseaseHashTable::discharges(std::string diseasename, std::string date1, std::string date2, std::string country){
unsigned hval = hash_str(diseasename); //hasharei to diseaseID
hval = hval % size; //gia na pame sth swsth thesh pinaka
if(table[hval] == NULL){ //Auth h periptwsh de tha ginei pote sthn askhsh
//std::cout << diseasename << " " << 0 << "\n";
return 0;
}
else{
chain_node * currptr = table[hval];
while(currptr!= NULL){ //to psaxnei dieksodika wste na brethei h astheneia
block_entry * buroku = currptr->block;
for(unsigned int i=0; i< currptr->block_size; i++){
if(buroku[i].dis_name_ptr == NULL)
continue;
if(*(buroku[i].dis_name_ptr) == diseasename){
//EDW GINETAI H DOULITSA ME TO DENDRO
search_containter querycontainer(buroku[i].totalval); //to megisto plhthos eggrafwn einai o sunolikos arithmos eggrafwn auths ths atheneias/xwras
//twra o container exei oles tis eggrafes ths astheneias/xwras me entrydate <= date2. H parakatw entolh ftiaxnei kai thn allh proypothesh
buroku[i].tree_ptr->collect_all_reclists(buroku[i].tree_ptr, &querycontainer); //o container exei tis eggrafes gia authn thn astheneia/xwra me entrydate <= Date2. Ekmetalleuetai th dendrikh domh gia kalyterh polyplokothta
int number_to_present = querycontainer.count_exit_limit2(date1, date2,country);
//std::cout << *(buroku[i].dis_name_ptr) << " " << number_to_present << "\n";
return number_to_present;
}
}//telos for gia block
currptr = currptr->next ;
}//telos while gia orizontia lista
}//telos else
//std::cout << diseasename << " " << 0 << "\n";
return 0;
}
//COUNTRY
//derived parametrized constructor
countryHashTable::countryHashTable(int sze, int bucketsize) : cdHashTable(sze, bucketsize){
}
//h virtual gia country
int countryHashTable::insert_record(record* rec){
unsigned hval = hash_str(rec->get_country()); //hasharei to countr
hval = hval % size; //gia na pame sth swsth thesh pinaka
if(table[hval] == NULL){ //ean den yparxei alusida ekei, th ftiaxnoyme
table[hval] = new chain_node(bucksize);
table[hval]->insert_record(rec, rec->get_country());
return 0;
}
else{
chain_node * currptr = table[hval];
bool done = false;
while(!done){ //prospathei na to valei se kapoio ths alusidas. to kathena koitaei ton pinaka tou
done = currptr->insert_record(rec, rec->get_country());
if((done==false)&&(currptr->next == NULL)){ //an den exei kataferei na th valei kai den exoume epomeno komvo, ftiaxnume k paei ekei.
currptr->next = new chain_node(bucksize);
currptr->next->insert_record(rec, rec->get_country());
return 0;
}//telos if
currptr = currptr->next ;
}//telos while done eisagwghs
}//telos else
return 0;
}
//omwnymo erwthma gia ta topk-askhshs 2 (age ranges)
int countryHashTable::topk_age_ranges(int k, std::string country, std::string disease, std::string date1, std::string date2, int * fetched, int * resu_array, float * fresu_array){
*fetched=0;//posa brhka
unsigned hval = hash_str(country); //hasharei to country
hval = hval % size; //gia na pame sth swsth thesh pinaka
if(table[hval] == NULL){ //ean den yparxei alusida ekei, den yparxei h xwra
//std::cout << "Country specified does not exist (yet).\n";
return -1;
}
else{ //h xwra yparxei
chain_node * currptr = table[hval];
while(currptr!= NULL){ //to psaxnei dieksodika wste na brethei h xwra
block_entry * buroku = currptr->block;
for(unsigned int i=0; i< currptr->block_size; i++){
if(buroku[i].dis_name_ptr == NULL)
continue;
if(*(buroku[i].dis_name_ptr) == country){
//extract eggrafes apo dentro
simple_cd_HT simpldiseaseht(4); //DES README gia topk
search_containter querycontainer(buroku[i].totalval); //to megisto plhthos eggrafwn einai o sunolikos arithmos eggrafwn auths ths atheneias/xwras
//me thn apo katw entolh o container exei oles tis eggrafes ths astheneias/xwras me entrydate <= date2.
buroku[i].tree_ptr->collect_dated_reclists(buroku[i].tree_ptr, date2, &querycontainer); //o container exei tis eggrafes gia authn thn astheneia/xwra me entrydate <= Date2. Ekmetalleuetai th dendrikh domh gia kalyterh polyplokothta
//twra gia date 1:disqualify dates passing date2 test but not date1 and pass them to simpleht ME ELEGXO GIA DISEASE MAZI
querycontainer.populate_simpleht(&simpldiseaseht, date1, disease); //twra oi komvoi tou simple ht exoun kathe astheneia sth xwra kai ton arithmo krousmatwn ths kai me ta swsta dates
maxBinaryHeap mheap(8); //ena maxheap gia krousmata astheneiwn
//gemizei o swros apo to dentro twn krousmatwn auths ths xwras
simpldiseaseht.populate_heap(&mheap);
//std::cout << "Top " << k << " diseases for " << country << " for dates provided :\n";
for(unsigned int ka=0; ka<k; ka++){
if(mheap.root == NULL){
//std::cout << "-\n"; //de ginontai alla extractions.
continue;
}
heapnode ophelia(mheap.extract() ); //eksagwgh rizas apo swro kai diathrhsh
//std::cout << ophelia.cat_name << " " << ophelia.krousmata << "\n";
resu_array[*fetched] = std::stoi(ophelia.cat_name); //krata hlikiakh kathgoria
fresu_array[(*fetched)] = ophelia.pososto; //krata pososto twn krousmatwn
*fetched +=1;
}//telos for ektypwshs topk
return 1;
}//telos if brethhke xwra
}//telos for gia block
currptr = currptr->next ;
}//telos while gia orizontia lista
}//telos else
//std::cout << "Country specified does not exist (yet).\n";
return -1;
}
//CHAIN NODE
chain_node::chain_node(int sz){
next = NULL;
//SUMPERILAMVENTAI STO BUCKETSIZE KAI O DEIKTHS STO NEXT KAI O AKERAIOS POY KRATAW
block_size = (sz-sizeof(chain_node *)-sizeof(int)) / sizeof(block_entry); //stathero megethos dioti exw deiktes sth domh block entry
if(block_size < 1)
block_size = 1; //toulaxiston ena element mesa
block = new block_entry[block_size]; //toses theseis oses leei h ekfwnhsh
}
chain_node::~chain_node(){
//std::cout << "ffOTO";
delete[] block;
delete next; //KATASTREFETAI ANADROMIKA. STH C++ H DELETE KALEI TON DESTRUCTOR EAN YPARXEI. THA KALESEI LOIPON GIA TO NEXT TON DESTRUCTOR O OPOIOS KALEI GIA TO NEXT KTL KTL.
//delete this; //to epitrepei h C++. To kalw mono edw giati ta chain nodes yparxoyn se pinaka (ton zhtoumeno ht) gia na glitwsw th loupa
}
bool chain_node::insert_record(record* rec, std::string key){
for(unsigned int i=0; i<block_size; i++){
if(block[i].dis_name_ptr == NULL){ //einai h 1h adeia thesh, valto ekei
block[i].insert_record(rec, key);
return true; //komple
}
if( *(block[i].dis_name_ptr) == key ){ //h astheneia/xwra yparxei. proxwrame edw.
block[i].insert_record(rec, key);
return true; //komple
}
}
return false;
}
void chain_node::print_contents(){
std::cout << "to block exei " << block_size << " theseis\n\t\t\t";
for(unsigned int i=0; i<block_size; i++){
if(block[i].dis_name_ptr == NULL)
continue;
std::cout << *(block[i].dis_name_ptr) << " curr " << block[i].currval << " total " << block[i].totalval << " ";
std::cout << "to dentro moy: ";
block[i].tree_ptr->print_contents();
std::cout << "--";
}
}
//BLOCK ENTRY
block_entry::block_entry(){
dis_name_ptr = NULL;
tree_ptr = NULL;
currval =0;
totalval =0;
}
block_entry::~block_entry(){
//std::cout << "watashi wa, BUROKU!";
delete dis_name_ptr;
delete tree_ptr; //KATASTREFETAI ANADROMIKA. STH C++ H DELETE KALEI TON DESTRUCTOR EAN EXEI ORISTEI. DES DESTRUCTOR BBST sto bbst.cpp
}
int block_entry::insert_record(record * rec, std::string key){
if(dis_name_ptr == NULL)
dis_name_ptr = new std::string(key);
//de xreiazetai else. an den einai null shmainei oti h astheneia/xwra yparxei hdh kai de xreiazetai ksana to idio string
//sunexizoume me aukshsh metrhth kai eisagwgh se dentro
if(rec->get_exitDate() == "-") //den exei bgei, increase current!
currval++;
totalval++;
if(tree_ptr == NULL) //den yparxei dentro, to ftiaxnw k eisagw eggrafh
tree_ptr = new BBST(rec); // o constructor basei orismatos tou bbst frontizei ta ypoloipa
else //yparxei dentro. eisagw eggrafh
tree_ptr->insert_record(rec);
return 0;
}