-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.c
433 lines (349 loc) · 7.56 KB
/
tree.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* compile command:
* cc tree.c -o tree -Wall -Imycommon -Lmycommon -lmycommon
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "mycommon.h"
typedef struct info {
char szData[ 128 ];
} ST_INFO;
typedef struct tree {
uint16_t nIdx;
struct tree *pLeft;
struct tree *pRight;
ST_INFO stInfo; // tree$B9=B$$GJ];}$9$k<g%G!<%?(B
} ST_TREE;
static uint32_t gnCntTree;
static ST_TREE *gpstTreeRoot;
static ST_TREE *gpstSearchBefore; // $B%5!<%ACf(B $BJ];}%G!<%?(B
static BOOL g_isLeftSearchBefore; // $B%5!<%ACf(B $BJ];}%G!<%?(B $B:8$+1&$+(B
void InitTree( void ); // extern
BOOL AddTree( ST_INFO *pstInfo, size_t nArgSize ); // extern
static BOOL AddTreeFromIdx( uint16_t nIdx, ST_INFO *pstInfo );
static BOOL AddTreeImp( ST_TREE *pstAdd );
BOOL DelTree( uint16_t nIdx ); // extern
ST_TREE *SearchTree( uint16_t nIdx ); // extern
static void RefTree( ST_TREE *pstArg );
void RefAllTree(); // extern
static void DestroyTree( ST_TREE *pstArg );
void DestroyAllTree(); // extern
static uint16_t GenIdx( const uint16_t *pArg, size_t nArgSize );
/**
* $B=i4|2=(B
*/
void InitTree( void )
{
DestroyAllTree();
gnCntTree = 0;
gpstTreeRoot = NULL;
gpstSearchBefore = NULL;
g_isLeftSearchBefore = FALSE;
}
/**
* $BMWAGDI2C(B
*/
BOOL AddTree( ST_INFO *pstInfo, size_t nArgSize )
{
if (gnCntTree >= 0xffff) {
return FALSE;
}
uint32_t nIdx = GenIdx( (const uint16_t*)pstInfo, nArgSize );
/* index$B=EJ#3NG'$9$k(B */
while (1) {
if (!SearchTree( (uint16_t)nIdx )) {
break;
}
nIdx ++;
nIdx &= 0xffff;
}
if (!AddTreeFromIdx( (uint16_t)nIdx, pstInfo )) {
return FALSE;
}
gnCntTree ++;
return TRUE;
}
/**
* $BMWAGDI2C(B
* index$B$h$j(B
*/
static BOOL AddTreeFromIdx( uint16_t nIdx, ST_INFO *pstInfo )
{
ST_TREE *pstAdd = NULL;
pstAdd = (ST_TREE*)malloc( sizeof(ST_TREE) );
if (!pstAdd) {
PERROR( "malloc()" );
return FALSE;
}
pstAdd->nIdx = nIdx;
memcpy( &(pstAdd->stInfo), pstInfo, sizeof(ST_INFO) );
pstAdd->pLeft = NULL;
pstAdd->pRight = NULL;
/* $B=i2s(B */
if (!gpstTreeRoot) {
gpstTreeRoot = pstAdd;
return TRUE;
}
/* $B=i2s0J9_$N>l9g$O$3$3$K(B */
if (!AddTreeImp( pstAdd )) {
return FALSE;
}
return TRUE;
}
/**
* $BMWAGDI2C(B
* $B8!:w$7$FDI2C(B
*/
static BOOL AddTreeImp( ST_TREE *pstAdd )
{
BOOL bRtn = TRUE;
ST_TREE *pstWork = gpstTreeRoot;
if (!pstAdd) {
return FALSE;
}
/* root$B$,6u(B */
if (!pstWork) {
return FALSE;
}
while (1) {
if (pstWork->nIdx > pstAdd->nIdx) {
if (!(pstWork->pLeft)) {
pstWork->pLeft = pstAdd;
break;
} else {
/* $BB39T(B */
pstWork = pstWork->pLeft;
}
} else if (pstWork->nIdx < pstAdd->nIdx) {
if (!(pstWork->pRight)) {
pstWork->pRight = pstAdd;
break;
} else {
/* $BB39T(B */
pstWork = pstWork->pRight;
}
} else {
/*
* index$B$,=EJ#$7$?>l9g(B
* $B$3$3$K$O$3$J$$$O$:(B
*/
bRtn = FALSE;
LOG_E( "index duplicate.\n" );
break;
}
}
return bRtn;
}
/**
* $B:o=|(B
* $BMWAG$r(B1$B$D:o=|(B
*/
BOOL DelTree( uint16_t nIdx )
{
ST_TREE *pstWork = NULL;
ST_TREE *pstWorkLeft = NULL;
ST_TREE *pstWorkRight = NULL;
pstWork = SearchTree( nIdx );
if (!pstWork) {
LOG_W( "index not found.\n" );
return FALSE;
}
/* $B?F$N%]%$%s%?$r@Z$jN%$9(B */
if (g_isLeftSearchBefore) {
gpstSearchBefore->pLeft = NULL;
} else {
gpstSearchBefore->pRight = NULL;
}
/* $B%^%k%A%9%l%C%I;~$O(B $B%5!<%A3+;O$+$i$3$3$^$GGSB>$9$Y$-(B */
pstWorkLeft = pstWork->pLeft;
pstWorkRight = pstWork->pRight;
free( pstWork );
pstWork = NULL;
gnCntTree --;
//puts("ref in del");
//RefAllTree();
/* $B0J2<(B $B;R$NMWAG$r:F9=@.(B */
if (pstWorkLeft) {
if (!AddTreeImp( pstWorkLeft )) {
return FALSE;
}
}
if (pstWorkRight) {
if (!AddTreeImp( pstWorkRight )) {
return FALSE;
}
}
return TRUE;
}
/**
* $B8!:w(B
*/
ST_TREE *SearchTree( uint16_t nIdx )
{
ST_TREE *pstWork = gpstTreeRoot;
ST_TREE *pstRtn = NULL;
/* root$B$,6u(B */
if (!pstWork) {
return NULL;
}
while (1) {
if (pstWork->nIdx > nIdx) {
if (!(pstWork->pLeft)) {
pstRtn = NULL;
break;
} else {
/* $BB39T(B */
/* 1$B$DA0$rJ]B8(B */
gpstSearchBefore = pstWork;
g_isLeftSearchBefore = TRUE;
pstWork = pstWork->pLeft;
}
} else if (pstWork->nIdx < nIdx) {
if (!(pstWork->pRight)) {
pstRtn = NULL;
break;
} else {
/* $BB39T(B */
/* 1$B$DA0$rJ]B8(B */
gpstSearchBefore = pstWork;
g_isLeftSearchBefore = FALSE;
pstWork = pstWork->pRight;
}
} else {
/* index$B$,0lCW$7$?(B */
pstRtn = pstWork;
break;
}
}
return pstRtn;
}
/**
* $B;2>H(B
* $B0z?t$G;XDj$7$?MWAG$H$=$N;R0J2<$r;2>H$9$k(B
*/
static void RefTree( ST_TREE *pstArg )
{
if (!pstArg) {
return;
}
LOG_I (
"addr:[%p] nIdx:[%d] pLeft:[%p] pRight:[%p] data:[%s]\n",
pstArg,
pstArg->nIdx,
pstArg->pLeft,
pstArg->pRight,
pstArg->stInfo.szData
);
if (pstArg->pLeft) {
RefTree( pstArg->pLeft );
}
if (pstArg->pRight) {
RefTree( pstArg->pRight );
}
}
/**
* $BA4%G!<%?;2>H(B
*/
void RefAllTree( void )
{
RefTree( gpstTreeRoot );
}
/**
* $B%G!<%?GK4~(B
* $B0z?t$G;XDj$7$?MWAG$H$=$N;R0J2<$rGK4~$9$k(B
*/
static void DestroyTree( ST_TREE *pstArg )
{
if (!pstArg) {
return;
}
LOG_I( "free(%p)\n", pstArg );
if (pstArg->pLeft) {
DestroyTree( pstArg->pLeft );
}
if (pstArg->pRight) {
DestroyTree( pstArg->pRight );
}
free( pstArg );
pstArg = NULL;
}
/**
* $BA4%G!<%?GK4~(B
*/
void DestroyAllTree( void )
{
DestroyTree( gpstTreeRoot );
gpstTreeRoot = NULL;
gnCntTree = 0;
}
/**
* Index$B@8@.(B
* 0$B!A(B65535 $B$N%i%s%@%`(B?$B$JCM$rJV$9(B
* ($B$H$$$&$3$H$O(B $B$3$N(Btree$B9=B$$N%G!<%?$O:GBg(B65536$B8D(B)
* IP$B%A%'%C%/%5%`$r;29M(B
*/
static uint16_t GenIdx( const uint16_t *pArg, size_t nArgSize )
{
uint32_t nIdx = 0;
while (nArgSize > 1) {
nIdx += *pArg;
if (nIdx & 0x10000) {
/* 16bit$BL\$,N)$C$F$$$?$i%*!<%P!<%U%m!<J,$rB-$9(B */
nIdx = ( nIdx >> 16 ) + ( nIdx & 0xffff );
}
nArgSize -= 2;
pArg ++;
}
/* $BM>$j$N(B1byte$BJ,(B */
if (nArgSize == 1) {
nIdx += *pArg;
if (nIdx & 0x10000) {
/* 16bit$BL\$,N)$C$F$$$?$i%*!<%P!<%U%m!<J,$rB-$9(B */
nIdx = ( nIdx >> 16 ) + ( nIdx & 0xffff );
}
}
return (uint16_t)nIdx;
}
int main( void )
{
ST_INFO stInfo;
InitTree();
memset( &stInfo, 0x00, sizeof(stInfo) );
strcpy( stInfo.szData, "aaa" );
AddTree( &stInfo, sizeof(stInfo) );
memset( &stInfo, 0x00, sizeof(stInfo) );
strcpy( stInfo.szData, "bbb" );
AddTree( &stInfo, sizeof(stInfo) );
memset( &stInfo, 0x00, sizeof(stInfo) );
strcpy( stInfo.szData, "ccc" );
AddTree( &stInfo, sizeof(stInfo) );
memset( &stInfo, 0x00, sizeof(stInfo) );
strcpy( stInfo.szData, "aaa" );
AddTree( &stInfo, sizeof(stInfo) );
memset( &stInfo, 0x00, sizeof(stInfo) );
strcpy( stInfo.szData, "egehhryjyrj" );
AddTree( &stInfo, sizeof(stInfo) );
memset( &stInfo, 0x00, sizeof(stInfo) );
strcpy( stInfo.szData, "234346576uhy5g" );
AddTree( &stInfo, sizeof(stInfo) );
memset( &stInfo, 0x00, sizeof(stInfo) );
strcpy( stInfo.szData, "dfergk4k-5v,.aa" );
AddTree( &stInfo, sizeof(stInfo) );
RefAllTree();
DelTree( 25284 );
puts("del idx:25284 after");
RefAllTree();
DestroyAllTree();
puts("destroy after");
memset( &stInfo, 0x00, sizeof(stInfo) );
strcpy( stInfo.szData, "eee" );
AddTree( &stInfo, sizeof(stInfo) );
memset( &stInfo, 0x00, sizeof(stInfo) );
strcpy( stInfo.szData, "ggg" );
AddTree( &stInfo, sizeof(stInfo) );
RefAllTree();
exit( EXIT_SUCCESS );
}