-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.c
182 lines (149 loc) · 3.52 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Author: Gayan C. Karunarathna
* Github: github.com/gayanch
* Email: [email protected]
* License: GNU LGPL v3
*/
#include <stdlib.h>
#include <stdio.h>
#include "hashtable.h"
Hashtable* ht_new(int cap) {
if (cap <=0 ) {
printf("Invalid size.\n");
return NULL;
}
Hashtable *ht = malloc(sizeof *ht);
ht->cap = cap;
ht->size = 0;
ht->table = malloc(cap * sizeof *ht->table);
//associated function pointers
ht->put = &ht_put;
ht->get = &ht_get;
ht->delete = &ht_delete;
ht->contains_key = &ht_contains_key;
ht->load_factor = &ht_load_factor;
ht->clear = &ht_clear;
ht->dispose = &ht_dispose;
return ht;
}
//To-Do: implement a better hashing algorithm
int _ht_hash(int cap, int key) {
return key % cap;
}
void ht_put(Hashtable *ht, int key, int value) {
if (ht == NULL) {
printf("Error: null hashtable");
return;
}
int location = _ht_hash(ht->cap, key);
//new code
//now ht->table contains only references to heads of chains,
//ht->table's keys, values are ignored
Bucket *b = (&ht->table[location])->next;
Bucket *new = malloc(sizeof *new);
new->key = key;
new->value = value;
(&ht->table[location])->next = new;
new->next = b;
ht->size++;
}
int ht_get(Hashtable *ht, int key) {
if (ht == NULL) {
printf("Error: null hashtable");
return -1;
}
//redundant, marked for removal
//if (!ht_contains_key(ht, key)) return -1;
int location = _ht_hash(ht->cap, key);
//first bucket of the chain
Bucket *b = (&ht->table[location])->next;
//search linearly until a bucket with provided key found
while (b != NULL) {
if (b->key == key) {
return b->value;
}
b = b->next;
}
//key not found
return -1;
}
int ht_contains_key(Hashtable *ht, int key) {
if (ht == NULL) return 0;
int location = _ht_hash(ht->cap, key);
//getting first bucket of chain
Bucket *b = (&ht->table[location])->next;
//first bucket is null
if (b == NULL) return 0;
//search linearly for a matching bucket
while (b != NULL) {
if (b->key == key) {
return 1;
}
b = b->next;
}
//no bucket with provided key found
return 0;
}
double ht_load_factor(Hashtable *ht) {
return (double)ht->size/(double)ht->cap;
}
int ht_delete(Hashtable *ht, int key) {
//null hashtable provided, can not proceed
if (ht == NULL) return -1;
//key not found, nothing to delete
//redundant, if key found, marked for removal
//if (!ht_contains_key(ht, key)) return -1;
int location = _ht_hash(ht->cap, key);
Bucket *b = (&ht->table[location])->next;
while (b != NULL) {
//check buckets with provided key
if (b->key == key) {
int value = b->value;
//key found, check if it is in the middle of chain
if (b->next != NULL) {
//remove bucket and, reconnect links in chain
Bucket *tmp = b;
b = b->next;
free(tmp);
tmp = NULL;
ht->size--;
return value;
} else {
//last bucket, free up memory
free(b);
b = NULL;
ht->size--;
return value;
}
}
b = b->next;
}
//provided key is not found, nothing to delete
return -1;
}
void ht_clear(Hashtable *ht) {
if (ht == NULL) return;
//work around
//freeying & malloc'ing again does not seem to work
//freeying individual buckets in table
int cap = ht->cap;
int i;
for (i = 0; i < cap; i++) {
Bucket *b = (&ht->table[i])->next;
//access buckets in list and free them
while (b != NULL) {
Bucket *next = b->next;
free(b);
b = next;
}
}
ht->size = 0;
}
void ht_dispose(Hashtable *ht) {
//free up all buckets in chains
ht_clear(ht);
free(ht->table);
free(ht);
ht->table = NULL;
ht = NULL;
}