-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.c
118 lines (89 loc) · 2.74 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashtable.h"
/**
* Add the new entry to the head of the linked list of the hash bucket.
*/
node_t *add_hash_entry(hash_table_t *hashtable, char* key, char *value) {
if (hashtable == NULL) {
fprintf(stderr, "No hash table\n");
exit(1);
}
size_t hashval = hash(hashtable, key);
node_t *new_node;
if ((new_node = (node_t *) malloc(sizeof(node_t))) == NULL) {
fprintf(stderr, "Could not allocate memory for new node\n");
exit(2);
}
new_node->key = strdup(key);
new_node->value = strdup(value);
new_node->next = hashtable->table[hashval];
hashtable->table[hashval] = new_node;
// printf("%d\n", hashval);
return new_node;
}
/**
* Allocate memory for the hash table and its elements and initialize each element.
*/
hash_table_t *create_hashtable(size_t size) {
if (size < 1) {
fprintf(stderr, "Size must be greater than zero\n");
exit(1);
}
size_t i;
hash_table_t *new_table;
if ((new_table = (hash_table_t *) malloc(sizeof(hash_table_t))) == NULL) {
fprintf(stderr, "Could not allocate memory for hash table\n");
exit(2);
}
if ((new_table->table = (node_t **) malloc(sizeof(node_t) * size)) == NULL) {
fprintf(stderr, "Could not allocate memory for hash table elements\n");
exit(3);
}
for (i = 0; i < size; ++i)
new_table->table[i] = NULL;
new_table->size = size;
return new_table;
}
void free_hashtable(hash_table_t *hashtable) {
int size = hashtable->size, i;
node_t *node, *tmp;
for (i = 0; i < size; ++i) {
// Remove the nodes in each linked list.
node = hashtable->table[i];
while (node) {
node->key = NULL;
node->value = NULL;
tmp = node;
node = node->next;
free(tmp);
}
}
free(hashtable->table);
free(hashtable);
}
// TODO: Need a (much) better hashing algo! This is just to keep moving along.
size_t hash(hash_table_t *hashtable, char *key) {
if (hashtable == NULL) {
fprintf(stderr, "No hash table\n");
exit(1);
}
size_t hashval = 0;
for (; *key != '\0'; ++key)
hashval = *key + (hashval << 5) - hashval;
return hashval % hashtable->size;
}
node_t *lookup_hash_entry(hash_table_t *hashtable, char *key) {
if (hashtable == NULL) {
fprintf(stderr, "No hash table\n");
exit(1);
}
size_t hashval = hash(hashtable, key);
node_t *bucket = hashtable->table[hashval];
if (bucket == NULL)
return NULL;
while (bucket && strncmp(bucket->key, key, strlen(key)) != 0)
bucket = bucket->next;
return bucket;
}