forked from justin-lathrop/c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.c
168 lines (129 loc) · 3.62 KB
/
HashTable.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
/*
* @author: jelathro
* @date: 11/2/13
*
* Implementation file for HashTable.
*
* After ten collisions (ie. ten items in
* a particular hash items list) the hash
* table will expand to the maxCollision
* plus the growthFactor.
*/
#include <stdlib.h>
#include "HashTable.h"
HashTable * hashtable_initialize(size_t size, size_t mc, size_t gf, hash_function fn, compare_equal efn){
size_t i;
HashTable * ht = (HashTable *) malloc( sizeof(HashTable) );
if (ht == NULL) {
printf("\nERROR: Cannot create hash table. Terminating...");
exit(EXIT_FAILURE); // well... any other implementation could be added, instead of this.
}
ht->hf = fn;
ht->eq = efn;
ht->size = size;
ht->maxCollisions = mc;
ht->growthFactor = gf;
ht->table = (Item **) malloc(size * sizeof( Item * ));
if (ht->table == NULL) {
printf("\nERROR: Cannot create hash table. Terminating...");
exit(EXIT_FAILURE); // well... any other implementation could be added, instead of this.
}
for(i=0; i<size; i++){
ht->table[i] = 0;
}
return(ht);
}
void * hashtable_get(HashTable * ht, void * key){
size_t hash = ht->hf(key);
Item * next = ht->table[ hash % ht->size ];
while(next){
if(ht->eq( next->key, key )){
return( next->value );
}else{
next = next->next;
}
}
return( (void *)0 );
}
int hashtable_destroy(HashTable * ht){
// Avoiding runtime errors. Return a successful destroy when the
// hashtable has not been initialized using the 'hashtable_initialize'
// function.
if (ht == NULL)
return 1;
// From here we know the hashtable has been properly initialized so,
// we continue computations.
size_t i;
for(i=0; i<ht->size; i++){
free(ht->table[i]);
}
free(ht->table);
free(ht);
return(1);
}
int hashtable_resize(HashTable * ht, size_t size){
HashTable * newht = hashtable_initialize(size, ht->maxCollisions, ht->growthFactor, ht->hf, ht->eq);
int i;
Item * next;
// Re-enter all the items again into the new hashtable
// with the new size.
for(i=0; i<ht->size; i++){
if(ht->table[i]){
for(next=ht->table[i]; next; next=next->next){
hashtable_add(newht, next->key, next->value);
}
}
hashtable_remove(ht, ht->table[i]->key);
}
free(ht->table);
newht->size = ht->size;
newht->table = ht->table;
return(1);
}
int hashtable_add(HashTable * ht, void * key, void * value){
size_t hash = ht->hf(key);
Item * next = ht->table[ hash % ht->size ];
size_t i = 0;
while(next){
// Replace data if key is same
if(ht->eq( next->key, key )){
next->value = value;
return(1);
}
next = next->next;
i++;
}
next = (Item *)malloc( sizeof(Item) );
// Making sure that memory was properly allocated for 'next'.
if (next == NULL) {
printf("\nERROR: Insufficient memory. Terminating....");
exit(EXIT_FAILURE); // some other implementation could be used here.
}
next->key = key;
next->value = value;
next->next = ht->table[ hash % ht->size ];
ht->table[ hash % ht->size ] = next;
if(i >= ht->maxCollisions){
hashtable_resize(ht, ht->size + ht->growthFactor);
}
return(1);
}
int hashtable_remove(HashTable * ht, void * key){
size_t hash = ht->hf(key);
Item * next = ht->table[ hash % ht->size ];
Item * prev = 0;
while(next){
if(ht->eq( next->key, key )){
if(prev){
prev->next = next->next;
}else{
ht->table[ hash % ht->size ] = next->next;
}
free(next);
return(1);
}
prev = next;
next = next->next;
}
return(0);
}