- Notifications
You must be signed in to change notification settings - Fork4
/
Copy pathmtab.c
259 lines (217 loc) · 4.66 KB
/
mtab.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
254
255
256
257
258
#include<stdlib.h>
#include"mtab.h"
/*
* mtab.c - a hash table implementation designed for use as node link storage
* in cp_trie objects. performance benefits compared to cp_hashtable result
* from cutting the overhead of dereferencing generic hash and compare
* functions and saving on checks for collection mode.
*/
/* an array of prime numbers for hash table sizes */
staticintsizes[]= {1,3,5,7,11,19,29,37,47,59,
71,89,107,127,151,181,211,239,257,281 };
staticintsizes_len=20;
#defineMIN_FILL_FACTOR 30
#defineMAX_FILL_FACTOR 100
#defineDOWNSIZE_RATIO 2
staticunsigned longmt_abs(longx)
{
if (x<0)return-x;
returnx;
}
/*
* performs a binary search on the sizes array to choose the first entry larger
* than the requested size. the sizes array is initialized with prime numbers.
*/
staticintchoose_size(intsize)
{
intnew_size;
if (sizes[sizes_len-1]<size)
{
for (new_size=sizes[sizes_len-1];
new_size<size;
new_size=new_size*2+1);
}
else
{
intmin=-1;
intmax=sizes_len-1;
intpos;
while (max>min+1)
{
pos= (max+min+1) /2;
if (sizes[pos]<size)
min=pos;
else
max=pos;
}
new_size=sizes[max];
}
returnnew_size;
}
staticvoidresize_table(mtab*t,intsize)
{
mtab_node**table= (mtab_node**)calloc(size,sizeof(mtab_node*));
if (table)
{
inti;
mtab_node*ni;
mtab_node**nf;
for (i=0;i<t->size;i++)
{
ni=t->table[i];
while (ni)
{
nf=&table[ni->key %size];
while (*nf)nf=&(*nf)->next;
*nf=ni;
ni=ni->next;
(*nf)->next=NULL;
}
}
free(t->table);
t->table=table;
t->size=size;
}
}
mtab_node*mtab_node_new(unsignedcharkey,void*value,void*attr)
{
mtab_node*node=calloc(1,sizeof(mtab_node));
if (node)
{
node->key=key;
node->value=value;
node->attr=attr;
}
returnnode;
}
mtab*mtab_new(intsize)
{
mtab*t=calloc(1,sizeof(mtab));
if (t)
{
t->size=choose_size(size);
t->table=calloc(t->size,sizeof(mtab_node*));
if (t->table==NULL)
{
free(t);
t=NULL;
}
}
returnt;
}
/*
* the 'owner' pointer is used by cp_trie deletion code to pass a pointer to the
* trie being deleted for access to mode settings etc.
*/
voidmtab_delete_custom(mtab*t,
void*owner,
mtab_dtrdtr)
{
while (t->size--)
{
mtab_node*curr=t->table[t->size];
mtab_node*tmp;
while (curr)
{
tmp=curr;
curr=curr->next;
(*dtr)(owner,tmp);
free(tmp);
}
}
free(t->table);
free(t);
}
voidmtab_delete(mtab*t)
{
while (t->size--)
{
mtab_node*curr=t->table[t->size];
mtab_node*tmp;
while (curr)
{
tmp=curr;
curr=curr->next;
free(tmp);
}
}
free(t->table);
free(t);
}
/*
* mtab doesn't support collection modes. inserting a new value for an
* key already present silently replaces the existing value.
*/
mtab_node*mtab_put(mtab*t,unsignedcharkey,void*value,void*attr)
{
mtab_node**loc;
if ((t->items+1)*100>t->size*MAX_FILL_FACTOR)
resize_table(t,choose_size(t->size+1));
loc=&t->table[key %t->size];
while (*loc&& (*loc)->key!=key)loc=&(*loc)->next;
if (*loc==NULL)
{
t->items++;
*loc=mtab_node_new(key,value,attr);
}
else
{
(*loc)->value=value;/* replace */
if ((*loc)->attr)free((*loc)->attr);
(*loc)->attr=attr;
}
return*loc;
}
mtab_node*mtab_get(mtab*t,unsignedcharkey)
{
mtab_node*node=t->table[key %t->size];
while (node&&key!=node->key)
node=node->next;
returnnode;
}
/*
* mtab_remove performs a table resize if table density drops below the 'fill
* ratio'. this is done to keep cp_trie memory usage low.
*/
void*mtab_remove(mtab*t,unsignedcharkey)
{
mtab_node**node;
void*res=NULL;
node=&t->table[key %t->size];
while ((*node)&&key!= (*node)->key)
node=&(*node)->next;
if (*node)
{
mtab_node*rm=*node;
*node=rm->next;/* unlink */
res=rm->value;
if (rm->attr)free(rm->attr);
free(rm);
t->items--;
}
if (t->items>0&&t->items*100<t->size*MIN_FILL_FACTOR)
resize_table(t,choose_size(t->size /DOWNSIZE_RATIO));
returnres;
}
intmtab_count(mtab*t)
{
returnt->items;
}
intmtab_callback(mtab*t,cp_callback_fnfn,void*prm)
{
inti;
mtab_node*n;
intcount=0;
intres=0;
for (i=0;i<t->size&&count<t->items;i++)
{
n=t->table[i];
while (n)
{
count++;
if ((res= (*fn)(n,prm))!=0)returnres;
n=n->next;
}
}
return0;
}