-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtbl.c
53 lines (49 loc) · 1.25 KB
/
hashtbl.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
#include "hashtbl.h"
#include <stdio.h>
unsigned int hash(const char *key)
{
unsigned int count = 0;
for(unsigned int i = 0; key[i];count += key[i], i++);
return count;
}
void hashtbl_insert(struct hashtbl *table, const char *key, void *value)
{
int offset = hash(key) % 32;
if (table->keys[offset]) {
if (strcmp(table->keys[offset], key) == 0 ) {
table->values[offset] = value;
} else {
if (!table->next)
{
table->next = malloc(sizeof(struct hashtbl));
*table->next = (struct hashtbl){.next = NULL};
}
hashtbl_insert(table->next, key, value);
}
} else {
table->values[offset] = value;
int key_len = strlen(key);
printf("length of %s is %d", key, key_len);
(table->keys[offset]) = malloc(key_len+1);
snprintf(table->keys[offset], key_len+1, "%s", key);
printf("NEW KEY %s\n", table->keys[offset]);
}
}
void *hashtbl_find(struct hashtbl *table, const char *key)
{
int offset = hash(key) % 32;
if (!table) {
return NULL;
}
if (table->keys[offset]) {
if (strcmp(table->keys[offset], key) == 0 ) {
return table->values[offset];
} else {
if (table->next)
{
return hashtbl_find(table->next, key);
}
}
}
return NULL;
}