-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathintmap.c
95 lines (79 loc) · 2.17 KB
/
intmap.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
#include <stdint.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "intmap.h"
#define INTMAP_MAX_PROBING_TRIES 10
static void err(int status, const char * const message) {
fputs(message, stderr);
exit(status);
}
intmap_t *
intmap_new(const size_t size) {
intmap_t *hm = (intmap_t *) malloc(sizeof(intmap_t));
if (!hm) {
err(EXIT_FAILURE, "could not allocate memory for hashmap");
}
// make sure capacity is power of 2
hm->cap = 16384;
while (hm->cap < size) {
hm->cap *= 2;
}
hm->entries = (intmap_entry_t *) calloc(hm->cap, sizeof(intmap_entry_t));
if (!hm->entries) {
err(EXIT_FAILURE, "could not allocate memory for hashmap entries");
}
return hm;
}
static inline size_t
hash_func (size_t key) {
key = ((key >> 16) ^ key) * 0x45d9f3b;
key = (key >> 16) ^ key;
return key;
}
int64_t
intmap_get(intmap_t *hm, const size_t key) {
size_t index = hash_func(key) & (hm->cap - 1);
uint8_t tries = 0;
while (hm->entries[index].key != key) {
// exhausted linear probing attempts
if (++tries > INTMAP_MAX_PROBING_TRIES) {
// assume item was not in hashmap yet
return 0;
}
// wrap around
if (++index >= hm->cap) {
index = 0;
}
}
return hm->entries[index].value;
}
int64_t
intmap_set(intmap_t *hm, const size_t key, const int64_t value) {
size_t index = hash_func(key) & (hm->cap - 1);
uint8_t tries = 0;
while (hm->entries[index].key != 0) {
if (hm->entries[index].key == key) {
int64_t old_value = hm->entries[index].value;
hm->entries[index].value = value;
return old_value;
}
// did we exchaust # of tries?
if (++tries > INTMAP_MAX_PROBING_TRIES) {
err(EXIT_FAILURE, "exhausted linear probing attempts");
}
// wrap around if we reached end of entries
if (++index >= hm->cap) {
index = 0;
}
}
// add to values
hm->entries[index].key = key;
hm->entries[index].value = value;
return 0;
}
void
intmap_free(intmap_t *m) {
free(m->entries);
free(m);
}