-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict.c
96 lines (85 loc) · 2.74 KB
/
dict.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "./spf/SuperFastHash.h"
#define DICTSIZE 8191
#define hash(key, keySize) (SuperFastHash(key, keySize) % DICTSIZE)
struct LLNode {
struct LLNode *next;
char *key, *val;
size_t keySize, valSize;
};
struct LLNode **getDict() {
return (struct LLNode **) malloc(sizeof(struct LLNode *) * DICTSIZE);
}
struct LLNode *_dictLookup(struct LLNode **dict, char *key, size_t keySize) {
struct LLNode *node = dict[hash(key, keySize)];
for (; node != NULL; node = node->next)
if (keySize == node->keySize && memcmp(key, node->key, keySize) == 0)
return node; /* found */
return NULL; /* not found */
}
void dictDestroyEntry(struct LLNode *entry) {
free((void *)entry->key);
free((void *)entry->val);
free((void *)entry);
}
void dictRemove(struct LLNode **dict, char *key, size_t keySize) {
struct LLNode *prev = NULL;
struct LLNode *node = dict[hash(key, keySize)];
while (node != NULL) {
prev = node;
node = node->next;
if (keySize == node->keySize && memcmp(key, node->key, keySize) == 0)
break; /* found */
}
if (node != NULL) {
prev->next = node->next;
dictDestroyEntry(node);
}
}
char *dictLookup(struct LLNode **dict, char *key, size_t keySize, size_t *valSize) {
struct LLNode *node = _dictLookup(dict, key, keySize);
if (node != NULL)
if (valSize != NULL)
*valSize = node->valSize;
return node->val;
return NULL;
}
void dictStore(struct LLNode **dict, char *key, size_t keySize, char *value, size_t valSize) {
struct LLNode *node = _dictLookup(dict, key, keySize);
if (node == NULL) {
node = (struct LLNode *) malloc(sizeof(*node));
size_t hashval = hash(key, keySize);
node->next = dict[hashval];
dict[hashval] = node;
node->keySize = keySize;
node->key = (char *) malloc(sizeof(char) * keySize);
memcpy(node->key, key, sizeof(char) * keySize);
}
else {
free((void *)node->val);
}
node->valSize = valSize;
node->val = (char *) malloc(sizeof(char) * valSize);
memcpy(node->val, value, sizeof(char) * valSize);
}
void dictDestroy(struct LLNode **dict) {
for (size_t i = 0; i < DICTSIZE; i++) {
while (dict[i] != NULL) {
struct LLNode *node = dict[i];
dict[i] = node->next;
dictDestroyEntry(node);
}
}
free((void *)dict);
}
int main(void) {
// your code goes here
struct LLNode **dict = NULL;
dict = getDict();
dictStore(dict, "abc", 3, "def", 3);
printf("\"abc\" = \"%s\"\n", dictLookup(dict, "abc", 3, NULL));
dictDestroy(dict);
return 0;
}