-
Notifications
You must be signed in to change notification settings - Fork 1
/
HashTable.cpp
246 lines (186 loc) · 7.47 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
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
//Kanellaki Maria Anna - 1115201400060
#include <iostream>
#include "HashTable.h"
BucketEntry::BucketEntry(PatientRecord * rec, string myKey) //creates new bucket entry for disease/country from patient record
{
key = myKey;
nextEntry = NULL;
}
BucketEntry::~BucketEntry() //deletes one entry
{
nextEntry = NULL;
delete nextEntry;
}
Bucket::Bucket(int size) //initializes empty bucket
{
remainingSpace = size - 4 - 3*8; //subtracting the int (size) and the 3 pointers (tree, first and nextBucket)
tree = new Tree(); //initializes empty tree for this disease/country
first = NULL;
nextBucket = NULL;
}
Bucket::~Bucket() //deletes a bucket, the tree and its entries without touching the next Bucket
{
delete tree;
nextBucket = NULL;
delete nextBucket;
BucketEntry * temp, * entry = first;
while (entry)
{
temp = entry;
entry = entry->getNextEntry();
delete temp;
}
}
void Bucket::InsertInBucket(PatientRecord *rec, int bucketSize, string myKey, treeNode *nodeT) //creates new BucketEntry and inserts it in Bucket. creates new Bucket if needed.
{
BucketEntry * entry = new BucketEntry(rec, myKey);
this->tree->insertInTree(nodeT); //inserts node in Tree
if (!first)
{
first = entry;
remainingSpace -= sizeof(&entry);
return;
}
Bucket * temp = nextBucket;
if (remainingSpace - sizeof(&entry) < 0) //if there is not enough space creates new bucket and inserts entry there
{
while (temp->nextBucket)
{
if (remainingSpace - sizeof(&entry) < 0)
temp = temp->nextBucket;
else //if program finds a bucket where there is enough space, entry is inserted there
{
remainingSpace -= sizeof(&entry);
entry->setNextEntry(first); //insertion in the beginning of bucket for speed
first = entry;
return;
}
}
//no bucket with enough space: creates new bucket and inserts there
Bucket * newBucket = new Bucket(bucketSize);
newBucket->InsertInBucket(rec, bucketSize, myKey, nodeT);
nextBucket = newBucket;
}
else //enough space here
{
remainingSpace -= sizeof(&entry);
entry->setNextEntry(first); //insertion in the beginning of bucket for speed
first = entry;
}
}
HashTable::HashTable(int s, int bs) //initializes empty hash table with size and bucketsize given by the command line
{
size = s;
bucketSize = bs; //in bytes
table = new Bucket*[size];
for(int i = 0; i < size; i++)
table[i] = NULL;
}
HashTable::~HashTable() //deletes whole hash table
{
int i;
for (i=0; i<size; i++)
{
if (table[i])
delete table[i];
}
delete[] table;
}
void HashTable::InsertToTable(PatientRecord *rec, string myKey) //hashes rec key to find its index. inserts it in index's bucket. creates one if needed
{
Bucket * bucket;
int index = HashFunction(myKey); //country or disease
if (!table[index]) //creates new bucket if necessary
{
bucket = new Bucket(bucketSize);
table[index] = bucket;
}
else
bucket = table[index];
treeNode * nodeT = new treeNode(rec); //creates tree node from patient record
bucket->InsertInBucket(rec, bucketSize, myKey, nodeT); //calls function to insert rec in bucket
}
int HashTable::HashFunction(string id) //simple modular hash function
{
int i, multiplier=1;
long long int code=0;
//builds ascii code of id. stops before overflow if string too big
for (i=0; i<id.length(); i++)
{
char x = id.at(i);
if ((code*multiplier + int(x)) < 0)
return code % size;
code = code*multiplier + int(x);
multiplier *= 100;
}
return code % size; //simple modular hash function
}
int HashTable::CountAll(string disease, Date* date1, Date* date2) //prints count of a disease stored in the hash table through their trees
{
int value = HashFunction(disease);
int count = 0;
table[value]->getTree()->countDiseasedDates(table[value]->getTree()->getRoot(), date1, date2, &count);
return count;
//cout << disease <<" "<< count << endl;//" between "<<date1->getAsString()<<" and "<<date2->getAsString()<<"."<< endl;
}
int HashTable::CountAllCountry(string country, string disease, Date * date1, Date * date2) //prints count of a disease for a country stored in the hash table through their trees
{
int value = HashFunction(country);
int count = 0;
table[value]->getTree()->countDiseasedCountry(table[value]->getTree()->getRoot(), date1, date2, disease, &count);
//cout <<"Number of cases recorded in "<< country<<" for "<< disease <<": "<< count <<" between "<<date1->getAsString()<<" and "<<date2->getAsString()<<"."<< endl;
return count;
}
int HashTable::CountEnterOrExit(string disease, Date* date1, Date* date2, string status) //prints count of a disease stored in the hash table through their trees
{ //prints only entered or exited patients depending on status given
int value = HashFunction(disease);
int count = 0;
table[value]->getTree()->countEnterOrExit(table[value]->getTree()->getRoot(), date1, date2, &count, status);
return count;
//cout << disease <<" "<< count << endl;//" between "<<date1->getAsString()<<" and "<<date2->getAsString()<<"."<< endl;
}
int HashTable::CountEnterOrExitCountry(string country, string disease, Date * date1, Date * date2, string status) //prints count of a disease for a country stored in the hash table through their trees
{ //prints only entered or exited patients depending on status given
int value = HashFunction(country);
int count = 0;
table[value]->getTree()->countEnterOrExitCountry(table[value]->getTree()->getRoot(), date1, date2, disease, &count, status);
//cout <<"Number of cases recorded in "<< country<<" for "<< disease <<": "<< count <<" between "<<date1->getAsString()<<" and "<<date2->getAsString()<<"."<< endl;
return count;
}
//setters and getters for all classes related to HashTable
BucketEntry *BucketEntry::getNextEntry() const
{
return nextEntry;
}
void BucketEntry::setNextEntry(BucketEntry *nextEntry)
{
BucketEntry::nextEntry = nextEntry;
}
int HashTable::getSize() const
{
return size;
}
int HashTable::getBucketSize() const
{
return bucketSize;
}
BucketEntry *Bucket::getFirst() const
{
return first;
}
Bucket *Bucket::getNextBucket() const
{
return nextBucket;
}
const string &BucketEntry::getKey() const
{
return key;
}
Tree *Bucket::getTree() const
{
return tree;
}
Bucket **HashTable::getTable() const
{
return table;
}