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
|
/*
* Simple hash table
* Copyright
* (C) 1992 Joseph H. Allen
*
* This file is part of JOE (Joe's Own Editor)
*/
#include "config.h"
#include "types.h"
__RCSID("$MirOS: contrib/code/jupp/hash.c,v 1.8 2020/04/07 11:56:40 tg Exp $");
#include <stdlib.h>
#include <string.h>
#include "hash.h"
#include "utils.h"
#define hnext(accu,c) (((accu) << 4) + ((accu) >> 28) + (c))
static HENTRY *freentry = NULL;
unsigned long hash(const unsigned char *s)
{
unsigned long accu = 0;
while (*s) {
accu = hnext(accu, *s++);
}
return accu;
}
HASH *htmk(int len)
{
HASH *t = malloc(sizeof(HASH));
t->len = len - 1;
t->tab = calloc(len, sizeof(HENTRY *));
return t;
}
void htrm(HASH *ht)
{
free(ht->tab);
free(ht);
}
void *htadd(HASH *ht, const unsigned char *name, void *val)
{
int idx = hash(name) & ht->len;
HENTRY *entry;
int x;
if (!freentry) {
entry = ralloc(64, sizeof(HENTRY));
for (x = 0; x != 64; ++x) {
entry[x].next = freentry;
freentry = entry + x;
}
}
entry = freentry;
freentry = entry->next;
entry->next = ht->tab[idx];
ht->tab[idx] = entry;
entry->name = name;
return entry->val = val;
}
void *htfind(HASH *ht, const unsigned char *name)
{
HENTRY *e;
for (e = ht->tab[hash(name) & ht->len]; e; e = e->next) {
if (!strcmp(e->name, name)) {
return e->val;
}
}
return NULL;
}
|