-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearHashImplementation.c
More file actions
202 lines (153 loc) · 5.02 KB
/
LinearHashImplementation.c
File metadata and controls
202 lines (153 loc) · 5.02 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
#include <stdio.h>
#include <stdlib.h>
#include "HashTableTypes.h"
#include "HashTableInterface.h"
int L=0;
int p=0;
double load=0.0;
int entries=0;
int buckets=INITIALSIZE;
/*Allocate memory for starting buckets and initialize them as empty*/
Table makeTable(Table T){
int i;
T=malloc(INITIALSIZE*sizeof(TableEntry));
for(i=0;i<INITIALSIZE;i++){
T[i].Key=EmptyKey;
T[i].Info=EmptyInfo;
T[i].next=NULL;
}
return T;
}
/* Our hash function */
int h(KeyType Key,int L) {
int c = 3;
int M = 1048583;
int result = ((Key*c)%M)%(INITIALSIZE << L);
return result;
}
/*Search function*/
int Search(Table T,KeyType Key){
int addr;
TableEntry* temp;
addr=h(Key,L); /*Compute address of round L*/
if(addr<p) /*If that bucket is already rehashed*/
addr=h(Key,L+1); /*Address should be the result for round L+1*/
temp=&(T[addr]); /*Pointer to the start of the bucket*/
while(temp){ /*Traverse bucket*/
if(temp->Key==Key) /*Did we find it?*/
return 1; /*Yes*/
temp=temp->next; /*No, next one then*/
}
return 0; /*Failure*/
}
/*Insert function*/
Table Insert(Table T,KeyType Key,int mode){
int hashresult;
TableEntry* temp;
TableEntry* newentry;
if(mode==HASH){
entries++;
load=(double)entries/buckets;
hashresult = h(Key,L);
if(hashresult<p)
hashresult = h(Key,L+1);
}
else{ /*mode==REHASH*/
hashresult = h(Key,L+1);
}
if(T[hashresult].Key==EmptyKey) /*If bucket is empty*/
T[hashresult].Key = Key; /*Just copy the key to it*/
else{
temp = &(T[hashresult]); /*pointer to the start of the bucket*/
while(temp->next){ /*traverse to the end of the bucket*/
temp = temp->next;
}
newentry=malloc(sizeof(TableEntry)); /*allocate memory*/
temp->next=newentry; /*make the last element point to the new one*/
newentry->Key=Key; /*initialize new element*/
newentry->Info=0;
newentry->next=NULL;
}
if(mode==HASH){
if(load>LOADLIMIT){ /*Should we rehash?*/
if(p==0) /*Do we need new memory for that?*/
T=extendTable(T);
buckets++; /*One new bucket*/
Rehash(T); /*Rehash bucket p */
p++; /*update p to the next bucket*/
if(p%(INITIALSIZE<<L)==0){ /*Are we finished with this round?*/
L++; /*Yes,update L and p*/
p=0;
}
}
}
return T;
}
/*Printing function for the hash table*/
void printTable(Table T){
TableEntry* temp;
int i,count=0,empty=0;
for(i=0;i<buckets;i++){
printf("%d:",i);
temp=&(T[i]);
while(temp){
printf("%d,",temp->Key);
if(temp->Key==0)
empty++;
count++;
temp=temp->next;
}
printf("\b \n");
}
printf("I printed %d items.\n",count);
printf("Empty buckets are %d.\n",empty);
}
/*Function that doubles allocated memory when needed*/
Table extendTable(Table T){
int i;
T=realloc(T,(INITIALSIZE<<(L+1))*sizeof(TableEntry));
for(i=(INITIALSIZE<<L);i<(INITIALSIZE<<(L+1));i++){
T[i].Key=EmptyKey;
T[i].Info=EmptyInfo;
T[i].next=NULL;
}
return T;
}
/*Rehashing function*/
void Rehash(Table T){
TableEntry* temp;
TableEntry* temp2;
temp=&(T[p]); /*Pointer to the start of the bucket to be rehashed*/
/*TREATMENT FOR THE FIRST ELEMENT*/
while(h(temp->Key,L+1)>((INITIALSIZE<<L)-1)){ /*Is the current element hashed to the new bucket?*/
Insert(T,temp->Key,REHASH); /*then insert it*/
if(temp->next==NULL){ /*If this was the first and only element of the bucket*/
temp->Key=EmptyKey; /*Make the bucket empty again*/
}
else{
temp->Key=temp->next->Key; /*else update list and free the entry that was moved*/
temp2=temp->next;
temp->next=temp->next->next;
free(temp2);
}
}
/*TREATMENT FOR ALL OTHER ELEMENTS*/
temp=temp->next;
while(temp){
temp2=temp->next;
if(h(temp->Key,L+1)>((INITIALSIZE<<L)-1)){
Insert(T,temp->Key,REHASH);
Remove(&T[p],temp);
}
temp=temp2;
}
}
/*List removal function for all elements of a bucket EXCEPT FOR the first one*/
void Remove(TableEntry* list,TableEntry* node){
TableEntry* temp;
temp = list;
while(temp->next!=node)
temp=temp->next;
temp->next=temp->next->next;
free(node);
}