aboutsummaryrefslogtreecommitdiffstats
path: root/hash.h
blob: 56d950599c61377d4e42d1ae8e3b08bae3c1230f (plain) (blame)
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
/* $Id: hash.h,v 1.3 2001/11/24 02:01:26 ukai Exp $ */
#ifndef HASH_H
#define HASH_H

/* hash table */

#define defhash(keytype,type,sym) \
typedef struct HashItem_##sym { \
  keytype key; \
  type value; \
  struct HashItem_##sym *next; \
} HashItem_##sym;  \
typedef struct Hash_##sym { \
  int size; \
  struct HashItem_##sym **tab; \
} Hash_##sym; \
extern Hash_##sym *newHash_##sym(int size); \
extern void putHash_##sym(Hash_##sym *t, keytype key, type value); \
extern type getHash_##sym(Hash_##sym *t, keytype key, type failval);

defhash(char *, int, si)
defhash(char *, char *, ss)
defhash(char *, void *, hist)
#define defhashfunc(keytype,type,sym) \
Hash_##sym * \
newHash_##sym(int size)\
{\
  struct Hash_##sym *hash;\
  int i;\
\
  hash = (Hash_##sym*)GC_malloc(sizeof(Hash_##sym));\
  hash->size = size;\
  hash->tab = (HashItem_##sym**)GC_malloc(size*sizeof(HashItem_##sym*));\
  for (i = 0; i < size; i++)\
    hash->tab[i] = NULL;\
  return hash;\
}\
\
static HashItem_##sym* \
lookupHash_##sym(Hash_##sym *t, keytype key, int *hashval_return)\
{\
  HashItem_##sym *hi;\
\
  *hashval_return = hashfunc(key)%t->size;\
  for (hi = t->tab[*hashval_return]; hi != NULL; hi = hi->next) {\
    if (keycomp(hi->key,key))\
      return hi;\
  }\
  return NULL;\
}\
\
void \
putHash_##sym(Hash_##sym *t, keytype key, type value)\
{\
  int h;\
  HashItem_##sym *hi;\
\
  hi = lookupHash_##sym(t,key,&h);\
  if (hi) {\
    hi->value = value;\
    return;\
  }\
\
  hi = (HashItem_##sym*)GC_malloc(sizeof(HashItem_##sym));\
  hi->key = key;\
  hi->value = value;\
  hi->next = t->tab[h];\
  t->tab[h] = hi;\
}\
\
type \
getHash_##sym(Hash_##sym *t, keytype key, type failval)\
{\
  int h;\
  HashItem_##sym *hi;\
\
  hi = lookupHash_##sym(t,key,&h);\
  if (hi == NULL)\
    return failval;\
  return hi->value;\
}
#endif				/* not HASH_H */