-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathads_hash.h
254 lines (211 loc) · 7.07 KB
/
ads_hash.h
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
#include <stdlib.h> //malloc()
#include <string.h> //memset()
#include <stdio.h>
#include <stdbool.h>
#include <signal.h> //signal()
#include <stdint.h> //uint32_t
#define HASH_TABLE_SIZE 61
//static char str_FILE[]="/tmp/ads.data";
/*=====================================================================
HASH DATA STRUCT DEFINITION
=====================================================================*/
typedef struct _STRUCT_DATA
{
uint32_t int_ICAO24; //int data; //-----------------!!!! int_ICAO24 is a key word !!!!!!!
char str_CALL_SIGN[9];
}STRUCT_DATA;
typedef struct _NODE //--------- define a data link, memset to 0 during init.
{
STRUCT_DATA ads_data; //---must no be a Pointer!!!
struct _NODE* next;
}NODE; //-----!!!!!!!! end of struct token ;;;;;;;;; !!!!!
typedef struct _HASH_TABLE
{
NODE* HashTbl_item[HASH_TABLE_SIZE]; //--------- hash table with total HASH_TABLE_SIZE HashTbl_items (links)
}HASH_TABLE;
/*=====================================================================
HASH FUNCTIONS DEFINITION
=====================================================================*/
//---------create and initilize a hash table ---------------
HASH_TABLE* create_hash_table()
{
HASH_TABLE* pHashTbl=(HASH_TABLE*)malloc(sizeof(HASH_TABLE));
memset(pHashTbl,0,sizeof(HASH_TABLE));
return pHashTbl;
}
//------ calculate hash table item number --------
int get_hashtbl_itemnum(uint32_t int_ICAO24)
{
int tbnum;
// tbnum=((int_ICAO24&0xf0000000)>>24)+(int_ICAO24&0x0000000f);
tbnum=(int_ICAO24&0x000000ff);
// printf("Hash Table Item Num =%tbnum\n");
return tbnum%HASH_TABLE_SIZE;
}
//---- find data in which link node -----
NODE* find_data_in_hash(HASH_TABLE* pHashTbl,uint32_t int_ICAO24)
{
NODE* pNode;
int hash_num;
hash_num=get_hashtbl_itemnum(int_ICAO24); //--get hash table number
if(NULL == pHashTbl) //--hash table is null
return NULL;
if(NULL == (pNode=pHashTbl->HashTbl_item[hash_num])) //--HashTbl_item(link node) is null
return NULL;
while(pNode) //---while pNode is not null, search each link node for the data
{
if(int_ICAO24 == pNode->ads_data.int_ICAO24)
return pNode;
pNode=pNode->next;
}
return NULL;
}
//================== insert data into hash ===================
bool insert_data_into_hash(HASH_TABLE* pHashTbl,const STRUCT_DATA* _ads_data)
{
NODE* pNode;
int hash_num;
hash_num=get_hashtbl_itemnum(_ads_data->int_ICAO24); //--get hash table item number
if(NULL == pHashTbl) //---hash table is null
return false;
if(NULL == pHashTbl->HashTbl_item[hash_num]) //--corresponding HashTbl_item (link node) is null
{
pNode=(NODE*)malloc(sizeof(NODE));
memset(pNode,0,sizeof(NODE));
//------push in ICAO and Call-sign
memcpy(&(pNode->ads_data),_ads_data,sizeof(STRUCT_DATA));
pHashTbl->HashTbl_item[hash_num]=pNode;
return true;
}
if(NULL!= find_data_in_hash(pHashTbl,_ads_data->int_ICAO24)) //--if data exist
return false;
//-----if corresponding hash-table already existes, then put pnode to the end of the link
pNode=pHashTbl->HashTbl_item[hash_num]; //-- get pnode head
while(NULL!=pNode->next)
pNode=pNode->next; //---move to end of the link
//---add a link node and put data into
pNode->next=(NODE*)malloc(sizeof(NODE));
memset(pNode->next,0,sizeof(NODE));
//----push ICAO and CALL-SIGN
pNode->next->ads_data.int_ICAO24=_ads_data->int_ICAO24;
strcpy(&(pNode->next->ads_data.str_CALL_SIGN[0]),&(_ads_data->str_CALL_SIGN[0]));
return true;
}
//===================== delete data from hash ======================
bool delete_data_from_hash(HASH_TABLE* pHashTbl,uint32_t int_ICAO24)
{
NODE* pHead;
NODE* pNode;
int hash_num;
hash_num=get_hashtbl_itemnum(int_ICAO24); //--get hash table number
if(NULL==pHashTbl || NULL==pHashTbl->HashTbl_item[hash_num]) //--hash table or HashTbl_item doesn't exist
return false;
if(NULL==(pNode=find_data_in_hash(pHashTbl,int_ICAO24))) //--the data haven't been hashed in before
return false;
if(pNode==pHashTbl->HashTbl_item[hash_num]) //--if it's the first node in found hash table HashTbl_item
{
pHashTbl->HashTbl_item[hash_num]=pNode->next; //--adjust the head node for hash HashTbl_item, prepare for deleting.
goto final;
}
pHead=pHashTbl->HashTbl_item[hash_num];
while(pNode!=pHead->next)
pHead=pHead->next;
pHead->next=pNode->next; //--bridge two pnodes beside the wanted pnode,preapare for deleting .
final:
free(pNode); //--delete the pnode
return true;
}
int count_hash_data(HASH_TABLE* pHashTbl)
{
int i,cnt=0;
NODE *pTemp;
for(i=0;i<HASH_TABLE_SIZE;i++)
{
if(pHashTbl->HashTbl_item[i]!=NULL) //
{
cnt++;
pTemp=pHashTbl->HashTbl_item[i];
while(pTemp->next!=NULL)
{
pTemp=pTemp->next;
cnt++;
}//-while
}//-if
}//-for
return cnt;
}
//========================= save hash data to file ==================
void save_hash_data(char* str_FILE,HASH_TABLE* pHashTbl)
{
FILE *fp;
int i;
int ncount=0;
NODE* pHead;
if((fp=fopen(str_FILE,"w+"))==NULL) //--to overwrite original file
{
printf("fail to open %s\n",str_FILE);
return;
}
ncount=count_hash_data(pHashTbl);
fwrite(&ncount,sizeof(int), 1,fp);
for(i=0;i<HASH_TABLE_SIZE;i++)
{
if(pHashTbl->HashTbl_item[i]!=NULL) //
{
pHead=pHashTbl->HashTbl_item[i];
while(pHead)
{
fwrite((char*)&(pHead->ads_data),sizeof(STRUCT_DATA),1,fp); // Is it necessary to convert to char* ??
pHead=pHead->next;
}//-end of while
}//-end of if
}//-end of for
fclose(fp);
}
//----------------- restore hash data ----------------------
void restore_hash_data(char* str_FILE,HASH_TABLE* pHashTbl)
{
FILE *fp;
int i;
int ncount=0;
STRUCT_DATA sdata;
if((fp=fopen(str_FILE,"r"))==NULL) //
{
printf("fail to open %s\n",str_FILE);
return;
}
fread(&ncount,sizeof(int), 1,fp);
printf("restore ncount=%d\n",ncount);
for(i=0;i<ncount;i++)
{
fread((char*)&sdata,sizeof(STRUCT_DATA),1,fp);
if(insert_data_into_hash(pHashTbl,&sdata))
printf("restore DATA[%d]: %06X %s \n",i,sdata.int_ICAO24,sdata.str_CALL_SIGN);
}
fclose(fp);
}
//-------------------release hash and free memory ------------------
void release_hash_table(HASH_TABLE* pHashTbl)
{
int i;
NODE *pHead,*pTemp;
for(i=0;i<HASH_TABLE_SIZE;i++)
{
if(pHashTbl->HashTbl_item[i]!=NULL) //
{
printf("------- Free hash table item[%d] -------\n",i);
pHead=pHashTbl->HashTbl_item[i];
while(pHead)
{
pTemp=pHead; //---two pnodes, one for deleting and one for pointing.
pHead=pHead->next;
if(pTemp!=NULL)
{
printf("free a node with int_ICAO24=%06X CALLSIGN:%s \n",pTemp->ads_data.int_ICAO24,pTemp->ads_data.str_CALL_SIGN);
free(pTemp);
}
}//-end of while
}//-end of if
}//-end of for
}
//====================================== HASH FUNCTION DEFINITION FINISH ========================