-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashlist.c
More file actions
237 lines (219 loc) · 6.4 KB
/
Copy pathhashlist.c
File metadata and controls
237 lines (219 loc) · 6.4 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
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "hashlist.h"
/********* slist operation
************/
int slist_add_head (slist_head_t *p_head, slist_node_t *p_node)
{
p_node->p_next = p_head->p_next;
p_head->p_next = p_node;
return 0;
}
slist_node_t *slist_next_get (slist_head_t *p_head, slist_node_t *p_pos)
{
if (p_pos) {
return p_pos->p_next;
}
return NULL;
}
slist_node_t *slist_begin_get (slist_head_t *p_head)
{
return slist_next_get(p_head, p_head);
}
slist_node_t *slist_end_get (slist_head_t *p_head)
{
return NULL;
}
typedef int (*slist_node_process_t) (void *p_arg, slist_node_t *p_node);
int slist_foreach( slist_head_t *p_head, slist_node_process_t pfn_node_process, void *p_arg)
{
slist_node_t *p_tmp, *p_end;
int ret;
if ((p_head == NULL) || (pfn_node_process == NULL)){
return -1;
}
p_tmp = slist_begin_get(p_head);
p_end = slist_end_get(p_head);
while (p_tmp != p_end){
ret = pfn_node_process(p_arg, p_tmp);
if (ret < 0) return ret; // fail and over
p_tmp = slist_next_get(p_head, p_tmp);
}
return 0;
}
slist_node_t *slist_prev_get (slist_head_t *p_head, slist_node_t *p_pos)
{
slist_node_t *p_tmp = p_head;
while ((p_tmp != NULL) && (p_tmp->p_next != p_pos)) {
p_tmp = p_tmp->p_next;
}
return p_tmp;
}
slist_node_t *slist_tail_get (slist_head_t *p_head)
{
return slist_prev_get(p_head, NULL);
}
int slist_del (slist_head_t *p_head, slist_node_t *p_node)
{
slist_node_t *p_prev = slist_prev_get(p_head, p_node);
if (p_prev) {
p_prev->p_next = p_node->p_next;
p_node->p_next = NULL;
return 0;
}
return -1;
}
int slist_init (slist_head_t *p_head)
{
if (p_head == NULL){
return -1;
}
p_head -> p_next = NULL;
return 0;
}
/****** hash operation group
********/
int hash_db_init (hash_db_t *p_hash, unsigned int size, unsigned int key_len,
unsigned int value_len, hash_func_t pfn_hash)
{
int i;
if ((p_hash == NULL) || (pfn_hash == NULL)){
return -1;
}
p_hash->p_head = (slist_head_t *)malloc(size * sizeof(slist_head_t));
if (p_hash -> p_head == NULL) {
return -1;
}
for (i = 0; i < size; i++){
slist_init(&p_hash -> p_head[i]);
}
p_hash -> size = size;
p_hash -> key_len = key_len;
p_hash -> value_len = value_len;
p_hash -> pfn_hash = pfn_hash;
return 0;
}
int hash_db_add (hash_db_t *p_hash, const void *key, const void *value)
{
int idx = p_hash -> pfn_hash(key); //get index via pfn_hash function
char *p_mem = (char *)malloc(sizeof(slist_node_t) + p_hash -> key_len + p_hash -> value_len);
if (p_mem == NULL) {
return -1;
}
memcpy(p_mem + sizeof(slist_node_t), key, p_hash -> key_len);//storage to key[]
memcpy(p_mem + sizeof(slist_node_t) + p_hash->key_len, value, p_hash->value_len); //storage the slist node
return slist_add_head(&p_hash -> p_head[idx], (slist_node_t *)p_mem); //add to slist-head
}
struct _node_find_ctx {
void *key;
unsigned int key_len;
slist_node_t *p_result;
};
static int __hash_db_node_find (void *p_arg, slist_node_t *p_node)
{
struct _node_find_ctx *p_info = (struct _node_find_ctx *)p_arg; //user para as the context of node
char *p_mem = (char *)p_node + sizeof(slist_node_t); //get the key store address
if (memcmp(p_mem, p_info->key, p_info->key_len) == 0) {//compare -> find the node
p_info->p_result = p_node;
return -1;
}
return 0;
}
int hash_db_search(hash_db_t *p_hash, const void *key, void *value)
{
int idx = p_hash->pfn_hash(key); //generate index from key id[]
struct _node_find_ctx info = {(void*)key, p_hash->key_len, NULL}; //initiate the des_node context
slist_foreach(&p_hash->p_head[idx], __hash_db_node_find, &info);//cb function __hash_db_node_find is hidden
if (info.p_result != NULL) {
memcpy(value, (char *)info.p_result, sizeof(slist_node_t)+p_hash->key_len+p_hash->value_len);
return 0;
}
return -1;
}
int hash_db_del (hash_db_t *p_hash, const void *key)
{
int idx = p_hash->pfn_hash(key);
struct _node_find_ctx info = {(void*)key, p_hash->key_len, NULL};
slist_foreach(&p_hash->p_head[idx], __hash_db_node_find, &info);
if (info.p_result != NULL) {
slist_del(&p_hash->p_head[idx], info.p_result);
free(info.p_result);
return 0;
}
return -1;
}
int hash_db_deinit (hash_db_t *p_hash)
{
int i;
slist_node_t *p_node;
for (i = 0; i < p_hash->size; i++) {
while (slist_begin_get(&p_hash->p_head[i]) != slist_end_get(&p_hash->p_head[i])) {
p_node = slist_begin_get(&p_hash->p_head[i]);
slist_del(&p_hash->p_head[i], p_node);
free(p_node);
}
}
free(p_hash->p_head);
return 0;
}
/*************** application level operation
************/
typedef struct _student{
char name[10];
char sex;
float height, weight;
} student_t;
int db_id_to_idx (unsigned char id[6])
{
int i;
int sum = 0;
for (i = 0; i < 6; i++){
sum += id[0];
}
return sum % 250;
}
int student_info_generate (unsigned char *p_id, student_t *p_student)
{
int i;
for (i = 0; i < 6; i++) {
p_id[i] = rand();
}
for (i = 0; i < 9; i++) {
p_student->name[i] = (rand() % ('z' - 'a')) + 'a';
}
p_student->name[i]= '\0';
p_student->sex = (rand() & 0x01) ? 'F' : 'M';
p_student->height = (float)rand() / rand();
p_student->weight = (float)rand() / rand();
return 0;
}
int main ()
{
student_t stu;
unsigned char id[6];
int i;
hash_db_t hash_students;
hash_db_init(&hash_students, 250, 6, sizeof(student_t), (hash_func_t)db_id_to_idx);
for (i = 0; i < 100; i++) {
student_info_generate(id, &stu); //radom 100 value_t
if (hash_db_search(&hash_students, id, &stu) == 0) {
printf("ID already exist \n");
continue;
}
printf("Add record ID : %02x%02x%02x%02x%02x%02x\n",id[0],id[1],id[2],id[3],id[4],id[5]);
printf("it's message %s %c %.2f %.2f\n", stu.name, stu.sex, stu.height, stu.weight);
if (hash_db_add(&hash_students, id, &stu) != 0) {
printf("Add Failed");
}
}
printf("Check ID: %02x%02x%02x%02x%02x%02x\n",id[0],id[1],id[2],id[3],id[4],id[5]);
if (hash_db_search(&hash_students, id, &stu) == 0) {
printf("student info: %s %c %.2f %.2f\n", stu.name, stu.sex, stu.height, stu.weight);
}
else {
printf("Not illegal ID \r\n");
}
hash_db_deinit(&hash_students);
return 0;
}