-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhash.c
253 lines (223 loc) · 6.29 KB
/
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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "tintin.h"
#include "protos/glob.h"
#include "protos/utils.h"
#define DELETED_HASHENTRY ((char*)-1)
// Jenkins hash
static uint32_t hash(const char *key)
{
uint32_t h = 0;
while (*key)
{
h += *key++;
h += h << 10;
h ^= h >> 6;
}
h += h << 3;
h ^= h >> 11;
h += h << 15;
return h;
}
/**********************************/
/* initialize an empty hash table */
/**********************************/
struct hashtable* init_hash(void)
{
struct hashtable *h=TALLOC(struct hashtable);
h->size=8;
h->nent=0;
h->nval=0;
h->tab=CALLOC(8, struct hashentry);
return h;
}
/*********************/
/* free a hash table */
/*********************/
void kill_hash(struct hashtable* h)
{
if (h->nval)
for (int i=0; i<h->size; i++)
{
if (h->tab[i].left && (h->tab[i].left!=DELETED_HASHENTRY))
{
SFREE(h->tab[i].left);
SFREE(h->tab[i].right);
}
}
CFREE(h->tab, h->size, struct hashentry);
TFREE(h, struct hashtable);
}
void kill_hash_nostring(struct hashtable* h)
{
if (h->nval)
for (int i=0; i<h->size; i++)
{
if (h->tab[i].left && (h->tab[i].left!=DELETED_HASHENTRY))
SFREE(h->tab[i].left);
}
CFREE(h->tab, h->size, struct hashentry);
TFREE(h, struct hashtable);
}
static inline void add_hash_value(struct hashtable *h, char *left, char *right)
{
int i=hash(left)%h->size;
while (h->tab[i].left)
{
if (!i)
i=h->size-1;
i--;
}
h->tab[i].left = left;
h->tab[i].right= right;
}
static inline void rehash(struct hashtable *h, int s)
{
struct hashentry *gt=h->tab;
int gs=h->size;
h->tab=CALLOC(s, struct hashentry);
h->nent=h->nval;
h->size=s;
for (int i=0;i<gs;i++)
{
if (gt[i].left && gt[i].left!=DELETED_HASHENTRY)
add_hash_value(h, gt[i].left, gt[i].right);
}
CFREE(gt, gs, struct hashentry);
}
/********************************************************************/
/* add a (key,value) pair to the hash table, rehashing if necessary */
/********************************************************************/
void set_hash(struct hashtable *h, const char *key, const char *value)
{
if (h->nent*5 > h->size*4)
rehash(h, h->nval*3);
int i=hash(key)%h->size;
while (h->tab[i].left)
{
if (h->tab[i].left==DELETED_HASHENTRY)
goto found_tombstone;
if (!strcmp(h->tab[i].left, key))
{
SFREE(h->tab[i].right);
h->tab[i].right=mystrdup(value);
return;
}
if (!i)
i=h->size;
i--;
}
h->nent++;
found_tombstone:
h->tab[i].left = mystrdup(key);
h->tab[i].right= mystrdup(value);
h->nval++;
}
void set_hash_nostring(struct hashtable *h, const char *key, const char *value)
{
if (h->nent*5 > h->size*4)
rehash(h, h->nval*3);
int i=hash(key)%h->size;
while (h->tab[i].left)
{
if (h->tab[i].left==DELETED_HASHENTRY)
goto found_tombstone;
if (!strcmp(h->tab[i].left, key))
{
h->tab[i].right=(char*)value;
return;
}
if (!i)
i=h->size;
i--;
}
h->nent++;
found_tombstone:
h->tab[i].left = mystrdup(key);
h->tab[i].right= (char*)value;
h->nval++;
}
/****************************************************/
/* get the value for a given key, or 0 if not found */
/****************************************************/
char* get_hash(struct hashtable *h, const char *key)
{
int i=hash(key)%h->size;
while (h->tab[i].left)
{
if (h->tab[i].left!=DELETED_HASHENTRY&&(!strcmp(h->tab[i].left, key)))
{
return h->tab[i].right;
}
if (!i)
i=h->size;
i--;
}
return 0;
}
/****************************************************/
/* delete the key and its value from the hash table */
/****************************************************/
bool delete_hash(struct hashtable *h, const char *key)
{
int i=hash(key)%h->size;
while (h->tab[i].left)
{
if (h->tab[i].left!=DELETED_HASHENTRY&&(!strcmp(h->tab[i].left, key)))
{
SFREE(h->tab[i].left);
SFREE(h->tab[i].right);
h->tab[i].left=DELETED_HASHENTRY;
h->nval--;
if (h->nval*5<h->size)
rehash(h, h->size/2);
return true;
}
if (!i)
i=h->size;
i--;
}
return false;
}
/**************************************************************************/
/* create a sorted llist containing all entries of the table matching pat */
/**************************************************************************/
/* Rationale: hash tables are by definition unsorted. When listing or */
/* deleting from a list, we should show the entries in a sorted order, */
/* however screen output is slow anyways, so we can sort it on the fly. */
/**************************************************************************/
static int paircmp(const void *a, const void *b)
{
return strcmp(((struct pair*)a)->left, ((struct pair*)b)->left);
}
typedef int (*compar_t)(const void*, const void*);
struct pairlist* hash2list(struct hashtable *h, const char *pat)
{
int n = h->nval;
struct pairlist *pl = MALLOC((n*2+1)*sizeof(void*));
struct pair *p = &pl->pairs[0];
for (int i=0;i<h->size;i++)
if (h->tab[i].left && (h->tab[i].left!=DELETED_HASHENTRY)
&& (!pat || match(pat, h->tab[i].left)))
{
p->left = h->tab[i].left;
p->right= h->tab[i].right;
p++;
}
n = p-&pl->pairs[0];
qsort(&pl->pairs[0], n, sizeof(struct pair), paircmp);
pl->size = n;
return pl;
}
/**************************/
/* duplicate a hash table */
/**************************/
struct hashtable* copy_hash(struct hashtable *h)
{
struct hashtable *g=init_hash();
CFREE(g->tab, g->size, struct hashentry);
g->size=(h->nval>4) ? (h->nval*2) : 8;
g->tab=CALLOC(g->size, struct hashentry);
for (int i=0; i<h->size; i++)
if (h->tab[i].left && h->tab[i].left!=DELETED_HASHENTRY)
set_hash(g, h->tab[i].left, h->tab[i].right);
return g;
}