forked from universal-ctags/ctags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htable.c
206 lines (171 loc) · 4.02 KB
/
htable.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
/*
*
* Copyright (c) 2014, Red Hat, Inc.
* Copyright (c) 2014, Masatake YAMATO
*
* This source code is released for free distribution under the terms of the
* GNU General Public License.
*
* Defines hashtable
*/
#include "htable.h"
#ifndef MAIN
#include <stdio.h>
#include "routines.h"
#else
#include <stdlib.h>
#ifndef xCalloc
#define xCalloc(n,Type) (Type *)calloc((size_t)(n), sizeof (Type))
#endif
#ifndef xMalloc
#define xMalloc(n,Type) (Type *)malloc((size_t)(n) * sizeof (Type))
#endif
#ifndef eFree
#define eFree(x) free(x)
#endif
#endif /* MAIN */
typedef struct sHashEntry hentry;
struct sHashEntry {
void *key;
void *value;
hentry *next;
};
struct sHashTable {
hentry** table;
unsigned int size;
hashTableHashFunc hashfn;
hashTableEqualFunc equalfn;
hashTableFreeFunc keyfreefn;
hashTableFreeFunc valfreefn;
};
static hentry* entry_new (void *key, void *value, hentry* next)
{
hentry* entry = xMalloc (1, hentry);
entry->key = key;
entry->value = value;
entry->next = next;
return entry;
}
static hentry* entry_destroy (hentry* entry,
hashTableFreeFunc keyfreefn,
hashTableFreeFunc valfreefn)
{
hentry* tmp;
keyfreefn (entry->key);
valfreefn (entry->value);
entry->key = NULL;
entry->value = NULL;
tmp = entry->next;
eFree (entry);
return tmp;
}
static void entry_reclaim (hentry* entry,
hashTableFreeFunc keyfreefn,
hashTableFreeFunc valfreefn)
{
while (entry)
entry = entry_destroy (entry, keyfreefn, valfreefn);
}
static void *entry_find (hentry* entry, void* key, hashTableEqualFunc equalfn)
{
while (entry)
{
if (equalfn( key, entry->key))
return entry->value;
}
return NULL;
}
static boolean entry_delete (hentry **entry, void *key, hashTableEqualFunc equalfn,
hashTableFreeFunc keyfreefn, hashTableFreeFunc valfreefn)
{
while (*entry)
{
if (equalfn (key, (*entry)->key))
{
*entry = entry_destroy (*entry, keyfreefn, valfreefn);
return TRUE;
}
}
return FALSE;
}
static void entry_foreach (hentry *entry, hashTableForeachFunc proc, void *user_data)
{
while (entry)
{
proc (entry->key, entry->value, user_data);
entry = entry->next;
}
}
extern hashTable *hashTableNew (unsigned int size,
hashTableHashFunc hashfn,
hashTableEqualFunc equalfn,
hashTableFreeFunc keyfreefn,
hashTableFreeFunc valfreefn)
{
hashTable *htable;
htable = xMalloc (1, hashTable);
htable->size = size;
htable->table = xCalloc (size, hentry*);
htable->hashfn = hashfn;
htable->equalfn = equalfn;
htable->keyfreefn = keyfreefn;
htable->valfreefn = valfreefn;
return htable;
}
extern void hashTableDelete (hashTable *htable)
{
unsigned int i;
if (!htable)
return;
for (i = 0; i < htable->size; i++)
{
hentry *entry;
entry = htable->table[i];
entry_reclaim (entry, htable->keyfreefn, htable->valfreefn);
htable->table[i] = NULL;
}
eFree (htable);
}
extern void hashTablePutItem (hashTable *htable, void *key, void *value)
{
unsigned int i;
i = htable->hashfn (key) % htable->size;
htable->table[i] = entry_new(key, value, htable->table[i]);
}
extern void* hashTableGetItem (hashTable *htable, void *key)
{
unsigned int i;
i = htable->hashfn (key) % htable->size;
return entry_find(htable->table[i], key, htable->equalfn);
}
extern boolean hashTableDeleteItem (hashTable *htable, void *key)
{
unsigned int i;
i = htable->hashfn (key) % htable->size;
return entry_delete(&htable->table[i], key,
htable->equalfn, htable->keyfreefn, htable->valfreefn);
}
extern boolean hashTableHasItem (hashTable *htable, void *key)
{
return hashTableGetItem (htable, key)? TRUE: FALSE;
}
extern void hashTableForeachItem (hashTable *htable, hashTableForeachFunc proc, void *user_data)
{
unsigned int i;
for (i = 0; i < htable->size; i++)
entry_foreach(htable->table[i], proc, user_data);
}
unsigned int hash_ptrhash (void * x)
{
union {
void *ptr;
unsigned int ui;
} v;
v.ui = 0;
v.ptr = x;
return v.ui;
}
boolean hash_ptreq (void *a, void *b)
{
return (a == b)? TRUE: FALSE;
}