-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBHashTable.c
executable file
·297 lines (233 loc) · 7.62 KB
/
DBHashTable.c
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
DBHashTable* initialiseDBHashTable(int size) // Create a hash table of dtata bases with with size slots.
{
DBHashTable *dbht_ptr; /* New DBHashTable */
int i; /* Loop counter */
// Malloc the DBHashTable
if(!(dbht_ptr = malloc(sizeof(DBHashTable))))
{
printf("Error in initialiseDBHashTable: malloc failed.");
exit(0);
}
// Set the size and count
dbht_ptr->size = size;
dbht_ptr->count = 0;
// Malloc the table field of the DBHashTable
if(!(dbht_ptr->table = malloc(size * sizeof(DBHashEntry))))
{
printf("Error in initialiseDBHashTable: malloc failed.");
exit(0);
}
// Set every element in the table array to null
for (i = 0; i < size; i++)
{
dbht_ptr->table[i] = NULL;
}
return dbht_ptr;
}
int dbHashValue(DataBase* database_ptr, int size, int degree) // size < 2^{30}
{
int i; /* Loop counter */
int j; /* Loop counter */
int length = getDataBaseLength(degree);
UNINT dbhashvalue = 0; /* Hash value for the database in range 0 to size - 1 */
// Compute the hash value
for (i = 0; i < degree * (degree - 1); i++)
{
if (database_ptr->LocalGreater[i])
{
for (j = 0; j < length; j++)
{
dbhashvalue += database_ptr->LocalGreater[i][j] % size; // So now dbhashvalue < 2 ^ 31
// dbhashvalue += (database_ptr->LocalGreater[i][j] >> 32) % size; // Now dbhashvlalue < 2 ^ 32.
dbhashvalue = dbhashvalue % size; // Now dbhashvalue < size.
}
}
}
return (int) dbhashvalue;
}
DBHashEntry setDBHashEntry(DataBase* database_ptr, int degree)
// mallocs a dbhashentry, enters the database, and sets the next entry pointer to NULL.
{
DBHashEntry dbhashentry;
copyDataBase(database_ptr, &(dbhashentry.database), degree); // initialises dbhashentry.database
dbhashentry.next = NULL;
return dbhashentry;
}
DBHashTable* insertDBHashTable(DBHashTable* dbht_ptr, DataBase* database_ptr, int degree)
{
DBHashEntry *dbhe_one_ptr; /* A DBHashEntry to store database */
/* A DBHashEntry to traverse the linkedlist and check if the database is already
in the HashTable */
DBHashEntry *dbhe_two_ptr;
int hashvalue; /* The database's hash value */
int in_hash_table; /* Set to 1 if the database is in the HashTable */
// printf("Entering DBHashTable.\n");
// Find the database's hash value
hashvalue = dbHashValue(database_ptr,dbht_ptr->size, degree);
// Is this slot occupied?
if (dbht_ptr->table[hashvalue] == NULL)
{
// printf("Hash table place was not occupied.\n");
if (!(dbhe_one_ptr = malloc(sizeof(DBHashList)))) // typedef DBHashEntry* DBHashList
{
printf("Error in insertDBHashTable: malloc failed.");
exit(0);
}
*dbhe_one_ptr = setDBHashEntry(database_ptr, degree);
dbht_ptr->table[hashvalue] = dbhe_one_ptr;
dbht_ptr->count++;
}
else
{
// printf("Hash table place was occupied.\n");
dbhe_two_ptr = dbht_ptr->table[hashvalue];
while ((!(in_hash_table = equalDataBases(database_ptr,&(dbhe_two_ptr->database),degree)))&&
(dbhe_two_ptr->next != NULL))
{
// printf("in_hash_table = %d\n", in_hash_table);
dbhe_two_ptr = dbhe_two_ptr->next;
}
// printf("Got to head of queue.\n");
if(!in_hash_table)
{
// printf("Data base not in hash table.\n");
if (!(dbhe_one_ptr = malloc(sizeof(DBHashList))))
{
printf("Error in insertDBHashTable: malloc failed.");
exit(0);
}
*dbhe_one_ptr = setDBHashEntry(database_ptr, degree);
dbhe_two_ptr->next = dbhe_one_ptr;
dbht_ptr->count++;
// printf("Data base added to hash table.\n");
}
else
{
printf("in_hash_table returning true. Error\n");
printf("database:\n");
writeLaws(database_ptr, degree);
printf("dbhe_two_ptr->database:\n");
writeLaws(&(dbhe_two_ptr->database), degree);
exit(0);
}
}
// Check if the HashTable is 3/4 full
// ReHash if necessary and possible
if (dbht_ptr->count > 3 * (dbht_ptr->size/4))
{
printf("Rehashing.\n");
dbht_ptr = dbReHash(dbht_ptr,degree);
}
// printf("Leaving insertDBHashTable.\n");
return dbht_ptr;
}
void printDBHashTable(DBHashTable dbht, int degree)
{
int i; /* Loop counter */
int size = dbht.size; /* Number of slots in DBHashTable */
DBHashEntry *dbhe_ptr;
for (i = 0; i < size; i++)
{
dbhe_ptr = dbht.table[i];
while (dbhe_ptr != NULL)
{
writeDataBase(&(dbhe_ptr->database),degree);
dbhe_ptr = dbhe_ptr->next;
}
}
printf("Hashtable count: %d\n",dbht.count);
}
void fprintDBHashTable(DBHashTable *htp, int degree, FILE *fp)
{
int i;
int size = htp -> size;
DBHashEntry *hep;
for (i=0; i<size; i++) {
hep = htp -> table[i];
while (hep != NULL) {
fprintDBHashEntry(*hep, degree, fp);
hep = hep -> next;
}
}
}
void fprintDBHashEntry(DBHashEntry dbhe, int degree, FILE *file_ptr)
{
fprintDataBase(&(dbhe.database), degree, file_ptr);
}
DBHashTable* freadDBHashTable(DBHashTable* dbht_ptr, int degree, FILE* file_ptr)
// Read data bases from the file into the hash table.
{
DataBase database;
char ch;
if (isEmpty(file_ptr))
return dbht_ptr;
initialiseDataBase(&database,degree);
while ((!feof(file_ptr)) && (ch = fgetc(file_ptr)) != 'N')
{
freadDataBase(&database, degree, file_ptr);
dbht_ptr = insertDBHashTable(dbht_ptr, &database, degree);
}
return dbht_ptr;
}
int inDBHashTable(DBHashTable db_hashtable, DataBase* database_ptr, int degree)
// Is this database in the hash table?
{
DBHashEntry* db_he_ptr = db_hashtable.table[dbHashValue(database_ptr, db_hashtable.size, degree)];
while (db_he_ptr != NULL)
{
if (equalDataBases(database_ptr,&(db_he_ptr->database),degree))
return 1;
db_he_ptr = db_he_ptr->next;
}
return 0;
}
void freeDBLinkedList(DBHashEntry* db_he_ptr, int degree)
{
DBHashEntry* db_he_temp = db_he_ptr;
while (db_he_temp != NULL)
{
db_he_ptr = db_he_ptr->next;
freeDataBase(&(db_he_temp->database), degree);
free(db_he_temp);
db_he_temp = db_he_ptr;
}
}
void freeDBHashTable(DBHashTable* db_ht_ptr, int degree)
{
int i; /* Loop counter */
int size = db_ht_ptr->size; /* Number of slots in the data base */
// printf("Entering freeDBHashTable\n");
// Call freeDBLinkedList() for every entry in DBHashEntry array
for (i = 0; i < size; i++)
if (db_ht_ptr->table[i] != NULL)
freeDBLinkedList(db_ht_ptr->table[i],degree);
// printf("Leaving freeDBHashTable.\n");
}
DBHashTable* dbReHash(DBHashTable *old_dbht_ptr, int degree)
{
int i; /* Loop counter */
DBHashTable *new_dbht_ptr; /* A new DBHashTable */
DBHashEntry *db_he_ptr; /* DBHashEntry */
// Initialise new DBHashTable
if (old_dbht_ptr -> size >= 1 << 30)
{
printf("WARNING DBHashTable too full and too big to resize.\n");
return old_dbht_ptr;
}
new_dbht_ptr = initialiseDBHashTable((old_dbht_ptr -> size * 2) + 1);
new_dbht_ptr -> count = 0;
// Copy all DataBases to new DBHashTable
for (i = 0; i < old_dbht_ptr->size; i++)
{
db_he_ptr = old_dbht_ptr->table[i];
while (db_he_ptr != NULL)
{
new_dbht_ptr = insertDBHashTable(new_dbht_ptr,&(db_he_ptr->database),degree);
db_he_ptr = db_he_ptr -> next;
}
}
// Free the old DBHashTable
freeDBHashTable(old_dbht_ptr,degree);
// if (SafeToPrint) printf("\nMade a new hashtable of size %d.\n",newht_ptr->size);
return new_dbht_ptr;
}