summaryrefslogtreecommitdiff
path: root/dns_hash_table.c
blob: c4b7d2e986133b0570d9c63e125f8a8c38ab7860 (plain)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include<stdio.h>
#include "dns_hash_table.h"



static const unsigned int g_primes[] =
{
  11,
  19,
  37,
  73,
  109,
  163,
  251,
  367,
  557,
  823,
  1237,
  1861,
  2777,
  4177,
  6247,
  9371,
  14057,
  21089,
  31627,
  47431,
  71143,
  106721,
  160073,
  240101,
  360163,
  540217,
  810343,
  1215497,
  1823231,
  2734867,
  4102283,
  6153409,
  9230113,
  13845163,
};

static const unsigned int g_nprimes = sizeof (g_primes) / sizeof (g_primes[0]);

unsigned int
g_spaced_primes_closest (unsigned int num)
{
  int i;

  for (i = 0; i < g_nprimes; i++)
    if (g_primes[i] > num)
      return g_primes[i];

  return g_primes[g_nprimes - 1];
}


static void		hash_table_resize	 (struct HashTable	*hash_table);
static struct HashNode**	hash_table_lookup_node (struct HashTable	*hash_table,const void *	 key);
static struct HashNode*	hash_node_new		 (void *key, void *value);
static void		hash_node_destroy	 (  struct HashNode	*hash_node);
static void		hash_nodes_destroy	 (  struct HashNode	*hash_node);




struct HashTable* hash_table_new (HashFunc        hash_func,CompareFunc key_compare_func)
{
	struct HashTable *hash_table;
	hash_table = (struct HashTable *)calloc (sizeof(struct HashTable), 1);
	hash_table->size = HASH_TABLE_MIN_SIZE;
	hash_table->nnodes = 0;
	hash_table->frozen = 0;
	hash_table->hash_func = hash_func;
	hash_table->key_compare_func = key_compare_func;
	hash_table->nodes = (struct HashNode **)calloc (sizeof(struct HashNode*), hash_table->size);
	return hash_table;
}


void hash_table_destroy (struct HashTable *hash_table)
{
  unsigned int i;
  
  if(!hash_table)
  	return;
  for (i = 0; i < hash_table->size; i++)
    hash_nodes_destroy (hash_table->nodes[i]);

  free(hash_table->nodes);
  free (hash_table);
}


static inline struct HashNode** hash_table_lookup_node (struct HashTable	*hash_table, const void *	 key)
{
	struct HashNode **node;
	node = &hash_table->nodes[(* hash_table->hash_func) (key) % hash_table->size];
	
	if (hash_table->key_compare_func)
	  while (*node && !(*hash_table->key_compare_func) ((*node)->key, key))
		node = &(*node)->next;
	else
	  while (*node && (*node)->key != key)
		node = &(*node)->next;
	return node;

}

void *hash_table_lookup (struct HashTable	 *hash_table,	  const void * key)
{
	struct HashNode *node;
	if(!hash_table || !key)
		return NULL;
	node = *hash_table_lookup_node (hash_table, key);
	return node ? node->value : NULL;

}



void hash_table_insert (struct HashTable *hash_table,void *key,void *value)
{
	struct HashNode **node;
	if(!hash_table)
  		return;
	node = hash_table_lookup_node (hash_table, key);
  	if (*node)
	{
		(*node)->value = value;
    }
	else
	{
		*node = hash_node_new (key, value);
		hash_table->nnodes++;
		if (!hash_table->frozen)
			hash_table_resize (hash_table);
	}
}



void hash_table_remove (struct HashTable	     *hash_table,const void *    key)
{
	struct HashNode **node, *dest;
	if(!hash_table)
		return;
	node = hash_table_lookup_node (hash_table, key);

	if (*node)
	{
		dest = *node;
		(*node) = dest->next;
		hash_node_destroy (dest);
		hash_table->nnodes--;
		if (!hash_table->frozen)
			hash_table_resize (hash_table);
    }
}




int hash_table_lookup_extended (struct HashTable	*hash_table,const void *	 lookup_key,void * *orig_key,void *		*value)
{
	struct HashNode *node;
	if(!hash_table)
		return 0;
	node = *hash_table_lookup_node (hash_table, lookup_key);
	
	if (node)
	{
		if (orig_key)
			*orig_key = node->key;
		if (value)
			*value = node->value;
		return 1;
	}
	else
	return 0;
}


void hash_table_freeze (struct HashTable *hash_table)
{
	if(!hash_table)
		return;
	hash_table->frozen++;
}



void hash_table_thaw (struct HashTable *hash_table)
{
	if(!hash_table)
		return;
	if (hash_table->frozen)
		if (!(--hash_table->frozen))
			hash_table_resize (hash_table);
}



int hash_table_foreach_remove (struct HashTable	*hash_table,HRFunc	 func,void *	 user_data)
{
	struct HashNode *node, *prev;
	unsigned int i;
	int deleted = 0;
  
	if(!hash_table)
		return 0;

  	for (i = 0; i < hash_table->size; i++)
	{
		restart:
		prev = NULL;
		for (node = hash_table->nodes[i]; node; prev = node, node = node->next)
		{
			if ((* func) (node->key, node->value, user_data))
			{
				deleted += 1;
				hash_table->nnodes -= 1;
				if (prev)
				{
					prev->next = node->next;
					hash_node_destroy (node);
					node = prev;
				}
				else
				{
					hash_table->nodes[i] = node->next;
					hash_node_destroy (node);
					goto restart;
				}
			}
		}
	}
	if (!hash_table->frozen)
		hash_table_resize (hash_table);
	return deleted;
}

void hash_table_foreach (struct HashTable *hash_table,HFunc	func,void *user_data)
{
	struct HashNode *node;
	int i;
	if(!hash_table || !func)
		return;
	for (i = 0; i < hash_table->size; i++)
		for (node = hash_table->nodes[i]; node; node = node->next)
		{
			(* func) (node->key, node->value, user_data);
		}
}



static void hash_table_resize (struct HashTable *hash_table)
{
	struct HashNode **new_nodes;
	struct HashNode *node;
	struct HashNode *next;
	float nodes_per_list;
	unsigned int hash_val;
	int new_size;
	int i;
	nodes_per_list = (float) hash_table->nnodes / (float) hash_table->size;
	if ((nodes_per_list > 0.3 || hash_table->size <= HASH_TABLE_MIN_SIZE) &&
		(nodes_per_list < 3.0 || hash_table->size >= HASH_TABLE_MAX_SIZE))
		return;
	new_size = CLAMP(g_spaced_primes_closest (hash_table->nnodes),
		HASH_TABLE_MIN_SIZE,
		HASH_TABLE_MAX_SIZE);

	new_nodes = (struct HashNode **)calloc (sizeof(struct HashNode), new_size);
	for (i = 0; i < hash_table->size; i++)
		for (node = hash_table->nodes[i]; node; node = next)
		{
			next = node->next;
			hash_val = (* hash_table->hash_func) (node->key) % new_size;
			node->next = new_nodes[hash_val];
			new_nodes[hash_val] = node;
		}
	free (hash_table->nodes);
	hash_table->nodes = new_nodes;
	hash_table->size = new_size;
}

static struct HashNode* hash_node_new (void * key,void * value)
{

	struct HashNode *hash_node = malloc(sizeof(struct HashNode));
	memset(hash_node,0,sizeof(struct HashNode));
	hash_node->key = key;
	hash_node->value = value;
	hash_node->next = NULL;
	return hash_node;
}



static void hash_node_destroy (struct HashNode *hash_node)
{
	if(hash_node){
		free(hash_node);
	}
}


static void hash_nodes_destroy (struct HashNode *hash_node)
{
	if (hash_node)
	{
		struct HashNode *node,*node0;
		node = hash_node;
		while (node)
		{
			node0 = node;
			node = node->next;
			free(node0);
		}
    }
}