-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashmap.c
149 lines (116 loc) · 3.52 KB
/
hashmap.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
/*
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2023 Mathew Jenkins <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
hashmap.c: What do you think
Credits to lenny's shitty hashmap
https://github.com/idkso/datastructs/blob/master/hashmap.c
*/
#include "hashmap.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static uint64_t
hashn(const char *str, int len) {
uint64_t out = 7;
for (int i = 0; i < len; i++) {
out = (out * 31) + str[i];
}
return out;
}
static uint64_t
hash(const char *str) {
return hashn(str, strlen(str));
}
void
hashtable_new(hashtable_t *ht, int size) {
ht->table = calloc(size, sizeof(hashtable_node_t));
ht->size = size;
}
hashtable_node_t *
hashtable_rnfind(hashtable_t *ht, const char *key, int key_len) {
hashtable_node_t *n = ht->table+(hashn(key, key_len) % ht->size);
lbl:
if (n == NULL) return NULL;
if (n->key_len == 0) return NULL;
if (n->key_len != key_len) {
n = n->next;
goto lbl;
}
if (memcmp(n->key, key, key_len) != 0) {
n = n->next;
goto lbl;
}
return n;
}
hashtable_node_t *
hashtable_nfind(hashtable_t *ht, const char *key, int key_len) {
hashtable_node_t *n = ht->table + (hashn(key, key_len) % ht->size);
lbl:
if (n->key_len == 0) {
n->key_len = key_len;
n->key = malloc(key_len);
memcpy((void*)n->key, key, key_len);
return n;
}
if (n->key_len != key_len) {
if (n->next == NULL) goto new;
n = n->next;
goto lbl;
}
if (memcmp(n->key, key, key_len) != 0) {
if (n->next == NULL) goto new;
n = n->next;
goto lbl;
} else {
return n;
}
new:
n->next = calloc(1, sizeof(hashtable_node_t));
goto lbl;
}
hashtable_node_t *
hashtable_ninsert(hashtable_t *ht, const char *key,
int key_len, htdata_t value) {
hashtable_node_t *n = hashtable_nfind(ht, key, key_len);
n->data = value;
return n;
}
hashtable_node_t *
hashtable_insert(hashtable_t *ht, const char *key, htdata_t value) {
return hashtable_ninsert(ht, key, strlen(key), value);
}
htdata_t *
hashtable_nget(hashtable_t *ht, const char *key, int key_len) {
hashtable_node_t *n = hashtable_rnfind(ht, key, key_len);
if (n == NULL) return NULL;
return &n->data;
}
htdata_t *
hashtable_get(hashtable_t *ht, const char *key) {
return hashtable_nget(ht, key, strlen(key));
}
/*
int main(void) {
int *tmp;
struct hashtable tbl;
const char *strings[] = {"balls balls", "balls", "sex", "jimmy", "george", "lenny is the coolest"};
const int values[] = {69, 420, 421, 469, 42, 22};
hashtable_new(&tbl, 64);
struct timespec t1 = {0}, t2 = {0};
for (int i = 0; i < 6; i++) {
hashtable_insert(&tbl, strings[i], values[i]);
printf("insert '%s' -> %d took %luns\n", strings[i], values[i], t2.tv_nsec-t1.tv_nsec);
}
for (int i = 0; i < 6; i++) {
tmp = qget(&tbl, strings[i]);
printf("get '%s' -> %d took %luns\n", strings[i], *tmp, t2.tv_nsec-t1.tv_nsec);
}
}*/