-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashTable.c
More file actions
184 lines (152 loc) · 4.9 KB
/
Copy pathhashTable.c
File metadata and controls
184 lines (152 loc) · 4.9 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
#include "hashTable.h"
#include <math.h>
#include <time.h>
void freeList( hashTableEntry* pe );
void createAndInsert( long size, hashType type );
/* createTable
* input: long
* output: hashTable*
*
* Creates and returns a hashTable* with the specified size.
*/
hashTable* createTable( long tableSize ){
long i;
hashTable* ph = (hashTable*)malloc( sizeof(hashTable) );
ph->tableSize = tableSize;
ph->table = (hashTableEntry**)malloc( sizeof(hashTableEntry*)*ph->tableSize );
if( ph->table==NULL ){
printf("ERROR - createTable - Failed to malloc hash table\n");
exit(-1);
}
for( i=0; i<tableSize; i++)
ph->table[i] = NULL;
ph->A = (sqrt(5)-1)/2;
ph->type = FIBONACCI_HASH; /* Default to using the FIBONACCI_HASH */
ph->reportCollisions = false;
ph->numCollisions = 0;
return ph;
}
hashTable* createTableType( long tableSize, hashType type, bool report ){
long i;
hashTable* ph = (hashTable*)malloc( sizeof(hashTable) );
ph->tableSize = tableSize;
ph->table = (hashTableEntry**)malloc( sizeof(hashTableEntry*)*ph->tableSize );
if( ph->table==NULL ){
printf("ERROR - createTable - Failed to malloc hash table\n");
exit(-1);
}
for( i=0; i<tableSize; i++)
ph->table[i] = NULL;
ph->A = (sqrt(5)-1)/2;
ph->type = type;
ph->reportCollisions = report;
ph->numCollisions = 0;
return ph;
}
/* freeTable and freeList
* input: hashTable*
* output: N/A
*
* Frees all of the data in the hashTable.
*/
void freeTable( hashTable* ph ){
long i;
for( i=0; i<ph->tableSize; i++ )
if( ph->table[i] != NULL )
freeList( ph->table[i] );
if( ph->reportCollisions==true ){
printf("Number of hash table collisions = %ld\n", ph->numCollisions);
}
free( ph->table );
free( ph );
}
void freeList( hashTableEntry* pe ){
hashTableEntry* temp;
while( pe!=NULL ){
temp = pe->nextEntry;
free( pe );
pe = temp;
}
}
long hashCode( hashTable* ph, long key ){
if( ph->type == NAIVE_HASH )
return hashCodeNaive( ph, key );
else if( ph->type == FIBONACCI_HASH )
return hashCodeFibonacci( ph, key );
else /* if( ph->type == UNSORTEDLL_HASH ) */
return 0; /* All records collide in hash table */
}
long hashCodeNaive( hashTable* ph, long key ){
if(key%ph->tableSize<0)
return key%ph->tableSize + ph->tableSize;
return key%ph->tableSize;
}
long hashCodeFibonacci( hashTable* ph, long key ){
double product = key*ph->A;
double fraction = product - (long)product;
long slot = (long)(ph->tableSize*fraction);
return slot;
}
/* searchTable
* input: hashTable* ph, long key
* output: void*
*
* Searchs the hashTable based on the given key and returns data associated with this key
*/
void* searchTable( hashTable* ph, long key ){
hashTableEntry* pe = ph->table[ hashCode(ph, key) ];
while( pe!=NULL && pe->key!=key ){
pe = pe->nextEntry;
}
if( pe!=NULL )
return pe->data;
return NULL;
}
/* insertTable
* input: hashTable* ph, long key, void* data
* output: N/A
*
* Inserts the given void* into hashTable based on the given key
*/
void insertTable( hashTable* ph, long key, void* data ){
if( searchTable(ph,key)==NULL ){
hashTableEntry* pe = (hashTableEntry*)malloc( sizeof(hashTableEntry) );
if( pe==NULL ){
printf("ERROR - insertTable - Failed to malloc hash table entry\n");
exit(-1);
}
pe->key = key;
pe->data = data;
if( ph->table[hashCode(ph, key)]!=NULL )
ph->numCollisions++;
pe->nextEntry = ph->table[ hashCode(ph, key) ];
ph->table[ hashCode(ph, key) ] = pe;
}
else{
printf("ERROR - insertTable - Attempting to insert duplicate key.\n");
}
}
/*
//Functions to test hash table
int main( ){
clock_t start, end;
start = clock();
createAndInsert( 100000, UNSORTEDLL_HASH );
end = clock();
printf( "UNSORTEDLL_HASH testing on 10,000 inserts took %lf seconds\n\n", (double)(end - start)/ CLOCKS_PER_SEC );
start = clock();
createAndInsert( 10000000, NAIVE_HASH );
end = clock();
printf( "NAIVE_HASH testing on 10,000,000 inserts took %lf seconds\n\n", (double)(end - start)/ CLOCKS_PER_SEC );
start = clock();
createAndInsert( 10000000, FIBONACCI_HASH );
end = clock();
printf( "FIBONACCI_HASH testing on 10,000,000 inserts took %lf seconds\n\n", (double)(end - start)/ CLOCKS_PER_SEC );
}
void createAndInsert( long size, hashType type ){
long i;
hashTable* ph = createTable( size, type, true );
for( i = 10000000; i<size+10000000; i++ )
insertTable( ph, i, "Data" );
freeTable( ph );
}*/