-
Notifications
You must be signed in to change notification settings - Fork 0
/
jhash.c
359 lines (248 loc) · 6.74 KB
/
jhash.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "jhash.h"
#include "hash_func.h"
#define DEBUG 1
hash_s *hash_init(void)
{
hash_s *h = (hash_s *)calloc(sizeof(hash_s), 1);
return h;
}
void free_hash_bucket(bucket_s *bucket)
{
if(bucket != NULL)
{
if(bucket->value != NULL)
{
free(bucket->value);
}
free(bucket);
}
}
#if 0
int hash_insert_strstr_kv(hash_s *hash, const char *key, const char *value)
{
if(key == NULL || value == NULL)
{
fprintf(stderr, "insert parameter is NULL.\n");
return -1;
}
long int index;
char *strkey_p = NULL;
bucket_s *first_bucket = NULL;
index = hash_string(key) % HASH_BUCKET_NUM;
//strkey_p = key;
first_bucket = &(hash->buckets[index]);
if(first_bucket->use_flag == 0 || strcmp(first_bucket->key, key) == 0)
{
// first bucket for one index unused or have the same key then overwrite it
strncpy(first_bucket->key, key, strlen(key));
if(first_bucket->value) free(first_bucket->value);
first_bucket->value = strdup(value);
first_bucket->use_flag = 1;
}
else
{
// key collision and handle key is the same
bucket_s *new_bucket;
bucket_s *p = first_bucket->zipper_head;
bucket_s *cur = NULL;
while(p != NULL)
{
cur = p;
if(strcmp(cur->key, key) == 0)
{
if(cur->value != NULL) free(cur->value);
cur->value = strdup(value);
return 0;
}
p = p->zipper_head;
}
new_bucket = (bucket_s *)calloc(sizeof(bucket_s), 1);
if(new_bucket == NULL)
{
fprintf(stderr, "calloc failed.\n");
return -1;
}
strncpy(new_bucket->key, key, strlen(key));
new_bucket->value = strdup(value);
// add link to new element
p->zipper_head = new_bucket;
}
}
#endif
// modify @20170110
int hash_insert_strint_kv(hash_s *hash, const char *key, int value)
{
if(key == NULL)
{
fprintf(stderr, "parameter key pointer is NULL at %s.\n", __FUNCTION__);
return -1;
}
long int index;
bucket_s *first_bucket = NULL;
index = hash_string(key) % HASH_BUCKET_NUM;
// the first element on list is statically allocated.
first_bucket = &(hash->buckets[index]);
if(first_bucket->use_flag == 0)
{
// unused
strncpy(first_bucket->key, key, strlen(key));
first_bucket->int_value = value;
first_bucket->use_flag = 1;
}
else if(strcmp(first_bucket->key, key) == 0)
{
//have the same key then overwrite it
first_bucket->int_value = value;
}
else
{
// key collision
bucket_s *new_bucket;
bucket_s *curr = first_bucket->next;
while(curr != NULL)
{
//next = cur->zipper_head;
if(strcmp(curr->key, key) == 0)
{
curr->int_value = value;
return 0;
}
curr = curr->next;
}
// not found in list
new_bucket = (bucket_s *)calloc(sizeof(bucket_s), 1);
if(new_bucket == NULL)
{
fprintf(stderr, "calloc failed.\n");
return -1;
}
strncpy(new_bucket->key, key, strlen(key));
new_bucket->int_value = value;
curr->next = new_bucket;
}
}
// return INT_MIN when can't or error
int hash_get_intvalue_by_str(hash_s *hash, char *key)
{
long int index;
index = hash_string(key) % HASH_BUCKET_NUM;
bucket_s *curr = &(hash->buckets[index]);
while(strcmp(curr->key, key))
{
curr = curr->next;
if(curr == NULL)
{
return INT_MIN;
}
}
if(curr->use_flag != -1) //not the deleted node
return curr->int_value;
return INT_MIN;
}
#if 0
const char *hash_get_strvalue_by_str(hash_s *hash, char *key)
{
long int index;
index = hash_string(key) % HASH_BUCKET_NUM;
#if DEBUG
printf("index : %ld \n", index);
#endif
bucket_s *next = &(hash->buckets[index]);
#if 0
if(next->use_flag == 0) // THIS CHAIN is empty
{
return NULL;
}
#endif
while(strcmp(next->key, key) != 0)
{
next = next->zipper_head;
if(next == NULL)
return NULL;
}
#if DEBUG
printf("the next use_flag %d at %s.\n", next->use_flag, __FUNCTION__);
#endif
if(next->use_flag != -1)
return next->value;
return NULL;
}
#endif
int hash_del_strkey(hash_s *hash, const char *key)
{
long int index;
index = hash_string(key) % HASH_BUCKET_NUM;
bucket_s *current = &(hash->buckets[index]);
if(strcmp(current->key, key) == 0)
{
hash->buckets[index].use_flag = -1; // deleted flag
return 0;
}
bucket_s *prev = current;
current = current->next;
while(current != NULL && strcmp(current->key, key))
{
prev = current; // keep previous element
current = current->next;
if(current == NULL)
return -1;
}
prev->next = current->next;
free_hash_bucket(current);
return 0;
}
int hash_del_intkey(hash_s *hash, int key)
{
}
void free_hash(hash_s *h)
{
if(h == NULL)
return;
int i = 0;
bucket_s *cur = NULL;
for(i = 0; i < HASH_BUCKET_NUM; i++)
{
cur = &(h->buckets[i]);
if(cur->value != NULL)
free(cur->value);
cur = cur->next;
while(cur)
{
bucket_s *curr_copy = cur;
cur = cur->next;
if(curr_copy->value !=NULL) free(curr_copy->value);
free(curr_copy);
}
}
free(h);
}
#if 0
int main()
{
hash_s *h = hash_init();
// str -> str
hash_insert(h, 0, "abc", "dddiii");
hash_insert(h, 0, "a", "ddd");
hash_insert(h, 0, "b", "ddd");
hash_insert(h, 0, "c", "ddd");
hash_insert(h, 1, "ll", (int []) { 5 });
const char *str_value;
str_value = hash_get_strvalue_by_str(h, "abc");
printf("the string value is %s.\n", str_value);
hash_del_strkey(h, "abc");
printf("after delete key.\n");
str_value = hash_get_strvalue_by_str(h, "abc");
printf("the string value is %s.\n", str_value);
int acont = 666;
hash_insert(h, 1, "h4h", &acont);
int value = hash_get_intvalue_by_str(h, "h4h");
printf("the int value : %d.\n", value);
value = hash_get_intvalue_by_str(h, "ll");
printf("the int value : %d.\n", value);
free_hash(h);
}
#endif