-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdict.cpp
More file actions
286 lines (229 loc) · 6.17 KB
/
Copy pathdict.cpp
File metadata and controls
286 lines (229 loc) · 6.17 KB
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
/**
This is a hash dictionary created by Qinjianbin. Based on the idea of odict.
date: 11-May-2009
Author: Jianbin Qin
Homepage: http://www.cse.unsw.edu.au/~jqin/
**/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <openssl/md5.h>
#include "dict.h"
#include <assert.h>
/**
Func: to Create a hash dict from a hash_number
In: hash number
Out: != NULL, success .
== NULL. Failed
**/
dict_t *dict_create(unsigned int hash_number){
dict_t *hdp;
hash_table_t *htp;
if((hdp = (dict_t *)malloc(sizeof(dict_t)))==NULL){
fprintf(stderr, "Fetal: Can not allocate memory for hash dict\n");
return NULL;
}
if((htp = (hash_table_t *)malloc(sizeof(hash_table_t)*hash_number))==NULL){
fprintf(stderr, "Fetal: Can not allocate memory for hash table\n");
free (hdp);
return NULL;
}
hdp->hash_num = hash_number;
hdp->node_num = 0;
hdp->hash_table = htp;
return hdp;
}
/**
Renew the hash dict
IN hashi dict pointer;
out Alwayers success.
**/
int dict_renew(dict_t *hdp){
unsigned int i;
dict_node_t *pnode, *pfree;
assert(hdp != NULL);
for(i=0;i<hdp->hash_num;i++){
pnode = hdp->hash_table[i].pnode;
while(pnode != NULL){
pfree = pnode;
pnode = pnode->next;
free(pfree);
}
hdp->hash_table[i].pnode= NULL;
}
return 0;
}
/**
Func: To destory a hash dict
IN: hash dict pointer.
Out: 0 Success. always success..
**/
int dict_destory(dict_t *dict)
{
assert(dict != NULL);
dict_renew(dict);
free (dict->hash_table);
free (dict);
return 0;
}
/**
Func: to do search on dict.
In: dict, search node info structure
Out: RT_HASH_DICT_SEARCH_SUCC, success find the search node.
RT_HASH_DICT_SEARCH_MISS, node not find.
-1, input failed finish the operation.
**/
int dict_search(dict_t *hdp, dict_node_t *snode)
{
unsigned int hkey;
dict_node_t *pt_node;
int ret;
assert(hdp != NULL);
assert(snode != NULL);
if( snode->sign1 == 0 && snode->sign2 == 0){
fprintf(stderr, "Fetal: Hash_dict insert node error \n");
return -1;
}
hkey = ( snode->sign1 + snode->sign2)%hdp->hash_num;
pt_node = hdp->hash_table[hkey].pnode;
while(pt_node != NULL) {
if ((pt_node->sign1 == snode->sign1) && (pt_node->sign2 == snode->sign2))
break;
pt_node = pt_node->next;
}
if(pt_node == NULL){ // if not find. clean the result
snode->pointer=0;
ret = RT_HASH_DICT_SEARCH_MISS;
}else{ // if find ,
snode->pointer=pt_node->pointer;
ret = RT_HASH_DICT_SEARCH_SUCC;
}
return ret;
}
/**
Func: to do add node on dict.
In: dict, add node info. is_overwrite.
Out: RT_HASH_DICT_ADD_ERROR: memory allocation error
RT_HASH_DICT_ADD_EXIST: the node is existed and without overwrite.
RT_HASH_DICT_ADD_SUCC: success to add a node to dict;
-1, input error the operation.
**/
int dict_add(dict_t *hdp, dict_node_t *snode, int is_overwrite)
{
unsigned int hkey;
dict_node_t *pt_node, *pfree = NULL;
int ret;
assert(hdp != NULL);
assert(snode != NULL);
if( snode->sign1 == 0 && snode->sign2 == 0)
{
fprintf(stderr, "Fetal: Hash_dict insert node error \n");
return -1;
}
hkey = ( snode->sign1 + snode->sign2)%hdp->hash_num;
pt_node = hdp->hash_table[hkey].pnode;
while(pt_node != NULL) {
if ((pt_node->sign1 == snode->sign1) && (pt_node->sign2 == snode->sign2)) {
break;
}
pfree = pt_node;
pt_node = pt_node->next;
}
if(pt_node == NULL){ // no exist node. craete a new node .
if((pfree = (dict_node_t *)malloc(sizeof(dict_node_t))) == NULL){
fprintf ( stderr, "Fatel: Hass node memory allocate error \n");
return RT_HASH_DICT_ADD_ERROR;
}
pfree->sign1 = snode->sign1;
pfree->sign2 = snode->sign2;
pfree->pointer = snode->pointer;
pfree->next = hdp->hash_table[hkey].pnode;
hdp->hash_table[hkey].pnode = pfree;
hdp->node_num ++;
ret = RT_HASH_DICT_ADD_SUCC;
}else{
if ( is_overwrite ){
pt_node -> pointer = snode->pointer;
ret = RT_HASH_DICT_ADD_SUCC;
}
else{
ret = RT_HASH_DICT_ADD_EXIST;
}
}
return ret;
}
/**
Func: to do del a node on dict.
In: dict, search node info.
Out: 0, success finished the opertaion.
RT_HASH_DICT_DEL_SUCC success del the node
RT_HASH_DICT_DEL_MISS did not find the node need to be del.
-1, input error.
**/
int dict_del(dict_t *hdp, dict_node_t *snode)
{
unsigned int hkey;
dict_node_t *pt_node, *pfree = NULL;
int ret;
assert(hdp != NULL);
assert(snode != NULL);
if( snode->sign1 == 0 && snode->sign2 == 0){
fprintf(stderr, "Fetal: Hash_dict insert node error \n");
return -1;
}
hkey = ( snode->sign1 + snode->sign2)%hdp->hash_num;
pt_node = hdp->hash_table[hkey].pnode;
while(pt_node != NULL) {
if ((pt_node->sign1 == snode->sign1) && (pt_node->sign2 == snode->sign2)) {
break;
}
pfree = pt_node;
pt_node = pt_node->next;
}
if ( pt_node != NULL){ // the node exist
if(pfree == NULL){
hdp->hash_table[hkey].pnode=NULL;
free(pt_node);
hdp->node_num--;
}else{
pfree->next=pt_node->next;
free(pt_node);
hdp->node_num--;
}
ret = RT_HASH_DICT_DEL_SUCC;
}else{
ret = RT_HASH_DICT_DEL_MISS;
}
return ret;
}
/**
Func: To create a md5 sign on the given string.
In: Key String, String len, Pointer to sign1, Pointer to sign2
Out: 0 success
1 Failed
**/
int create_sign_md5(char *str, unsigned int *sign1, unsigned int *sign2)
{
unsigned int md5res[4];
int len = strlen(str);
MD5((unsigned char*)str,(unsigned int)len,(unsigned char*)md5res);
*sign1=md5res[0]+md5res[1];
*sign2=md5res[2]+md5res[3];
return 0;
}
/**
Func: To create a bitwise sign on the given string.
In: Key String, String len, Pointer to sign1, Pointer to sign2
Out: 0 success
**/
int create_sign_bitwise(char *str, unsigned int *sign1, unsigned int *sign2){
unsigned int h1 = 1159241;
unsigned int h2 = 2000011;
while (*str) {
h1 ^= (h1 << 5) + (h1 >> 2) + *str++;
h2 ^= (h2 << 5) + (h2 >> 2) + *str++;
}
*sign1=h1&0x7fffffff;
*sign2=h2&0x7fffffff;
return 0;
}