-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_table.c
64 lines (59 loc) · 1.82 KB
/
hash_table.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
#include <stdio.h>
#include "hash_table.h"
void hash_table_init(struct HashTable *ht, int capacity)
{
ht->capacity = capacity;
ht->addresses = (struct AddressSizePair *)mmap(
NULL, capacity * sizeof(struct AddressSizePair),
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
memset(ht->addresses, 0, capacity * sizeof(struct AddressSizePair));
}
void hash_table_insert(struct HashTable *ht, struct AddressSizePair address)
{
uint64_t hash =
((uint64_t)address.key * (uint64_t)11400954310080068983ULL) %
ht->capacity;
if (ht->addresses[hash].value == 0 || ht->addresses[hash].tombstone) {
ht->addresses[hash].key = address.key;
ht->addresses[hash].value = address.value;
ht->addresses[hash].tombstone = false;
} else {
uint64_t newhash = hash;
do {
newhash = (newhash + 1) % ht->capacity;
} while (ht->addresses[newhash].value != 0 || newhash != hash);
ht->addresses[newhash].key = address.key;
ht->addresses[newhash].value = address.value;
ht->addresses[newhash].tombstone = false;
}
// TODO: resize addresses region when capcaity reached.
}
struct AddressSizePair *hash_table_search(struct HashTable *ht, void *address)
{
uint64_t hash =
((uint64_t)address * (uint64_t)11400954310080068983ULL) %
ht->capacity;
struct AddressSizePair *pair = &ht->addresses[hash];
if (pair->key == address && !pair->tombstone) {
return pair;
} else {
uint64_t newhash = hash;
do {
newhash = (newhash + 1) % ht->capacity;
pair = &ht->addresses[newhash];
if (newhash == hash)
return NULL;
if (pair->tombstone)
continue;
} while (pair->key != address);
return pair;
}
return NULL;
}
void hash_table_remove(struct HashTable *ht, void *address)
{
struct AddressSizePair *pair = hash_table_search(ht, address);
if (pair)
pair->tombstone = true;
// TODO: error in case pair doesnt exist
}