-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaodbm_hash.c
120 lines (106 loc) · 3.13 KB
/
aodbm_hash.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
/*
Copyright (C) 2011 aodbm authors,
This file is part of aodbm.
aodbm is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
aodbm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define AODBM_HASH_BUCKETS 5
#include "aodbm_hash.h"
#include "stdlib.h"
struct aodbm_hash {
void **data;
unsigned int sz;
unsigned int (*hash_function)(void *);
bool (*eq)(void *, void *);
};
static uint32_t hash(uint32_t a){
a = (a+0x7ed55d16) + (a<<12);
a = (a^0xc761c23c) ^ (a>>19);
a = (a+0x165667b1) + (a<<5);
a = (a+0xd3a2646c) ^ (a<<9);
a = (a+0xfd7046c5) + (a<<3);
a = (a^0xb55a4f09) ^ (a>>16);
return a;
}
aodbm_hash *aodbm_new_hash(unsigned int sz,
unsigned int (*hash_function)(void *),
bool (*eq)(void *, void *)) {
if (sz == 0) {
sz = 16;
}
aodbm_hash *hash = malloc(sizeof(aodbm_hash));
hash->sz = sz;
hash->hash_function = hash_function;
hash->eq = eq;
hash->data = malloc(sizeof(void *) * sz);
unsigned int i;
for (i = 0; i < sz; ++i) {
hash->data[i] = NULL;
}
return hash;
}
void aodbm_hash_insert(aodbm_hash *ht, void *val) {
unsigned int key = ht->hash_function(val);
unsigned int orig_key = key;
unsigned int i;
for (i = 0; i < AODBM_HASH_BUCKETS; ++i) {
void **b = &ht->data[key % ht->sz];
if (*b == NULL) {
*b = val;
return;
}
key = hash(key);
}
void **old = ht->data;
ht->data = malloc(sizeof(void *) * ht->sz * 2);
unsigned int old_sz = ht->sz;
ht->sz = ht->sz * 2;
for (i = 0; i < ht->sz; ++i) {
ht->data[i] = NULL;
}
for (i = 0; i < old_sz; ++i) {
void *b = old[i];
if (b != NULL) {
aodbm_hash_insert(ht, b);
}
}
free(old);
}
void aodbm_hash_del(aodbm_hash *ht, void *data) {
unsigned int key = ht->hash_function(data);
unsigned int orig_key = key;
unsigned int i;
for (i = 0; i < AODBM_HASH_BUCKETS; ++i) {
void **b = &ht->data[key % ht->sz];
if (ht->eq(data, *b)) {
*b = NULL;
return;
}
key = hash(key);
}
}
void *aodbm_hash_get(aodbm_hash *ht, void *data) {
unsigned int key = ht->hash_function(data);
unsigned int orig_key = key;
unsigned int i;
for (i = 0; i < AODBM_HASH_BUCKETS; ++i) {
void *b = ht->data[key % ht->sz];
if (ht->eq(data, b)) {
return b;
}
key = hash(key);
}
return NULL;
}
void aodbm_free_hash(aodbm_hash *ht) {
free(ht->data);
free(ht);
}