forked from frodosens/fsnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.c
295 lines (248 loc) · 6.04 KB
/
hash.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
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
//
// hash.c
// fsnet
//
// Created by Vincent on 14-5-22.
// Copyright (c) 2014年 Vincent. All rights reserved.
//
#include <stdio.h>
/*
* list.c
* Generic linked list implementation.
* cheungmine
* Sep. 22, 2007. All rights reserved.
*/
#include "fs_malloc.h"
#include "hash.h"
/* Appends a node to a list */
void
list_append_node(list_t *in_list, listnode_t *node)
{
node->next = NULL;
if (in_list->head)
{
in_list->tail->next = node;
in_list->tail = node;
}
else
in_list->head = in_list->tail = node;
in_list->size++;
}
/* Removes the first node from a list and returns it */
listnode_t*
list_remove_head(list_t *in_list)
{
listnode_t *node = NULL;
if (in_list->head)
{
node = in_list->head;
in_list->head = in_list->head->next;
if (in_list->head == NULL)
in_list->tail = NULL;
node->next = NULL;
in_list->size--;
}
return node;
}
/* Removes all nodes but for list itself */
void
list_remove_all(list_t *in_list, pfcb_list_node_free pf)
{
listnode_t *node;
while((node = list_remove_head(in_list))){
if (pf) (*pf)(node);
fs_free(node);
}
fs_assert (in_list->size==0, "");
}
/* Returns a copy of a list_t from heap */
list_t*
list_copy(list_t list)
{
list_t *newlist = (list_t*)fs_malloc (sizeof(list_t));
*newlist = list;
return newlist;
}
/* Concatenates two lists into first list */
void
list_concat(list_t *first, list_t *second)
{
if (first->head)
{
if (second->head)
{
first->tail->next = second->head;
first->tail = second->tail;
}
}
else
*first = *second;
second->head = second->tail = NULL;
first->size += second->size;
}
/* Allocates a new listnode_t from heap */
listnode_t*
list_node_create(void* data)
{
listnode_t *node = (listnode_t*)fs_malloc (sizeof(listnode_t));
node->next = NULL;
node->data = data;
return node;
}
listnode_t*
list_key_create(long key)
{
listnode_t *node = (listnode_t*)fs_malloc (sizeof(listnode_t));
node->next = NULL;
node->key = key;
return node;
}
/* Allocates a empty list_t from heap */
list_t*
list_create()
{
list_t *list = (list_t*)fs_malloc (sizeof(list_t));
list->size = 0;
list->head = list->tail = NULL;
return list;
}
/* Frees a empty list_t from heap */
void
list_destroy(list_t *in_list, pfcb_list_node_free pf)
{
list_remove_all(in_list, pf);
fs_free(in_list);
}
/* Gets count of nodes in the list */
size_t
list_size(const list_t* in_list)
{
return in_list->size;
}
/* Gets node by index 0-based. 0 is head */
listnode_t*
list_node_at(const list_t* in_list, int index)
{
int i=0;
listnode_t *node = in_list->head;
fs_assert(index >=0 && index < (int)in_list->size, "");
while (i < index)
{
node = node->next;
i++;
}
return node;
}
/*
* hashmap.c
* Generic hashmap implementation.
* a map for pair of key-value. key must be a null-end string, value is any type of data.
* cheungmine
* Sep. 22, 2007. All rights reserved.
*/
typedef struct _hash_map_t
{
size_t size;
listnode_t** key;
listnode_t** value;
}hash_map_t;
/* Hash a string, return a hash key */
static ulong hash_string(const char *s, int len)
{
ulong h = 0;
int i = 0;
assert (s);
if (len < 0)
len = (s? (int)strlen(s): 0);
while(i++ < len) { h = 17*h + *s++; }
return h;
}
static void _free_map_key(listnode_t* node)
{
listnode_t *old;
while(node)
{
old = node;
node = node->next;
fs_free(old->data);
fs_free (old);
}
}
static void _free_map_value(listnode_t* node, pfcb_hmap_value_free pfunc)
{
listnode_t *old;
while(node)
{
old = node;
node = node->next;
if (pfunc)
(*pfunc)(old->data);
fs_free (old);
}
}
/*=============================================================================
Public Functions
=============================================================================*/
/* Create before use */
void
hmap_create(hash_map *hmap, int size)
{
(*hmap) = (hash_map_t*) fs_malloc(sizeof(hash_map_t));
(*hmap)->size = size;
(*hmap)->key = (listnode_t**) fs_calloc(size, sizeof(listnode_t*));
(*hmap)->value = (listnode_t**) fs_calloc(size, sizeof(listnode_t*));
}
/* Destroy after use */
extern void
hmap_destroy(hash_map hmap, pfcb_hmap_value_free pfunc)
{
size_t i;
for(i=0; i<hmap->size; i++){
_free_map_key(hmap->key[i]);
_free_map_value(hmap->value[i], pfunc);
}
fs_free(hmap->key);
fs_free(hmap->value);
fs_free(hmap);
}
/* Insert a key-value into hash map. value is a pointer to callee-allocated memory */
void
hmap_insert(hash_map hmap, const char* key, int key_len, void* value)
{
listnode_t *node_key, *node_val;
ulong h;
char *s;
assert (key);
if (key_len<0) key_len = (int) strlen (key);
s = (char*) fs_malloc (key_len+1);
assert(s);
#pragma warning(push) /* C4996 */
#pragma warning( disable : 4996 )
strncpy (s, key, key_len);
#pragma warning(pop) /* C4996 */
s[key_len] = 0;
node_key = list_node_create ( (void*)s );
node_val = list_node_create ( value );
assert(node_key && node_val);
h = hash_string (s, key_len) % hmap->size;
node_key->next = hmap->key[h];
hmap->key[h] = node_key;
node_val->next = hmap->value[h];
hmap->value[h] = node_val;
}
/* Search a hash map for value of given key string */
void*
hmap_search(hash_map hmap, const char *key)
{
ulong h = hash_string (key, -1) % hmap->size;
listnode_t *pk = hmap->key[h];
listnode_t *pv = hmap->value[h];
while (pk)
{
if (strcmp(key, pk->str) == 0)
return pv->data;
pk = pk->next;
pv = pv->next;
}
return NULL;
}