forked from JerryZhou/aoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoi.c
2274 lines (1944 loc) · 59.6 KB
/
aoi.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
Area Of Interest In Game Developing
Copyright [email protected]
Licence: Apache 2.0
Project: https://github.com/JerryZhou/aoi.git
Purpose: Resolve the AOI problem in game developing
with high run fps
with minimal memory cost,
Please see examples for more details.
*/
#include "aoi.h"
// 日志开关
#define open_log_code (0) // 关于编码生成的日志
#define open_log_gencode (0)
#define open_log_node (0) // 关于节点生成的日志
#define open_log_node_get (0) // 关于节点查找的日志
#define open_log_unit (0)
#define open_log_unit_update (0)
#define open_log_filter (0)
#define open_log_unit_add (0)
#define open_log_unit_remove (0)
#define open_log_profile (0)
// 常用的宏
#define iunused(v) (void)(v)
#define ilog(...) printf(__VA_ARGS__)
// 状态操作宏
#define _state_add(value, state) value |= state
#define _state_remove(value, state) value &= ~state
#define _state_is(value, state) (value & state)
#if iimeta
// 内存统计
int64_t gcallocsize = 0;
int64_t gfreesize = 0;
int64_t gholdsize = 0;
#undef __ideclaremeta
#define __ideclaremeta(type, cap) {.name=#type, .size=sizeof(type), .current=0, .alloced=0, .freed=0, .cache={.root=NULL, .length=0, .capacity=cap}}
// 所有类型的元信息系统
imeta gmetas[] = {__iallmeta,
__ideclaremeta(imeta, 0)
};
// 所有自定义的类型原系统
imeta gmetasuser[IMaxMetaCountForUser] = {};
#undef __ideclaremeta
#define __ideclaremeta(type, cap) EnumMetaTypeIndex_##type
#ifndef __countof
#define __countof(array) (sizeof(array)/sizeof(array[0]))
#endif
#ifndef __max
#define __max(a, b) ((a) > (b) ? (a) : (b))
#endif
// 内置的meta个数
static int const gmetacount = __countof(gmetas);
static int gmetacountuser = 0;
// 获取类型的元信息
imeta *imetaget(int idx) {
if (idx < 0) {
return NULL;
}
if (idx < gmetacount) {
return &gmetas[idx];
}
idx -= gmetacount;
if (idx < gmetacountuser ) {
return &gmetasuser[idx];
}
return NULL;
}
// 也可以手动注册一个元信息来管理自己的对象: 然后就可以通过 iobjmallocuser 进行对象内存管理
int imetaregister(const char* name, int size, int capacity) {
gmetasuser[gmetacountuser].name = name;
gmetasuser[gmetacountuser].size = size;
gmetasuser[gmetacountuser].cache.capacity = capacity;
return gmetacount + gmetacountuser++;
}
// 尝试从缓冲区拿对象
iobj *imetapoll(imeta *meta) {
iobj *obj = NULL;
if (meta->cache.length) {
obj = meta->cache.root;
meta->cache.root = meta->cache.root->next;
obj->next = NULL;
--meta->cache.length;
memset(obj->addr, 0, obj->meta->size);
}else {
int newsize = sizeof(iobj) + meta->size;
obj = (iobj*)icalloc(1, newsize);
obj->meta = meta;
obj->size = newsize;
obj->meta->alloced += newsize;
obj->meta->current += newsize;
gcallocsize += newsize;
gholdsize += newsize;
}
return obj;
}
// 释放对象
void imetaobjfree(iobj *obj) {
obj->meta->current -= obj->size;
obj->meta->freed += obj->size;
gfreesize += obj->size;
gholdsize -= obj->size;
ifree(obj);
}
// Meta 的缓冲区管理
void imetapush(iobj *obj) {
if (obj->meta->cache.length < obj->meta->cache.capacity) {
obj->next = obj->meta->cache.root;
obj->meta->cache.root = obj;
++obj->meta->cache.length;
} else {
imetaobjfree(obj);
}
}
// 获取响应的内存:会经过Meta的Cache
void *iaoicalloc(imeta *meta) {
iobj *obj = imetapoll(meta);
return obj->addr;
}
// 偏移一下获得正确的内存对象
#define __iobj(p) (iobj*)((char*)(p) - sizeof(iobj))
// 释放内存:会经过Meta的Cache
void iaoifree(void *p) {
iobj *newp = __iobj(p);
imetapush(newp);
}
// 尽可能的释放Meta相关的Cache
void iaoicacheclear(imeta *meta) {
icheck(meta->cache.length);
iobj *next = NULL;
iobj *cur = meta->cache.root;
while (cur) {
next = cur->next;
imetaobjfree(cur);
cur = next;
}
meta->cache.root = NULL;
meta->cache.length = 0;
}
// 打印当前内存状态
void iaoimemorystate() {
ilog("[AOI-Memory] *************************************************************** Begin\n");
ilog("[AOI-Memory] Total---> new: %lld, free: %lld, hold: %lld \n", gcallocsize, gfreesize, gholdsize);
int count = __countof(gmetas);
int i;
for (i=0; i<count; ++i) {
ilog("[AOI-Memory] Obj: (%s, %d) ---> alloc: %lld, free: %lld, hold: %lld - count: %lld\n",
gmetas[i].name, gmetas[i].size,
gmetas[i].alloced, gmetas[i].freed, gmetas[i].current,
gmetas[i].current/(gmetas[i].size+sizeof(iobj)));
if (gmetas[i].cache.capacity) {
ilog("[AOI-Memory] Obj: (%s, %d) ---> cache: (%d/%d) \n",
gmetas[i].name, gmetas[i].size,
gmetas[i].cache.length, gmetas[i].cache.capacity);
}
}
ilog("[AOI-Memory] *************************************************************** End\n");
}
// 获取指定对象的meta信息
imeta *iaoigetmeta(void *p) {
icheckret(p, NULL);
iobj *obj = __iobj(p);
return obj->meta;
}
// 指定对象是响应的类型
int iaoiistype(void *p, const char* type) {
icheckret(type, iino);
imeta *meta = iaoigetmeta(p);
icheckret(meta, iino);
if (strncmp(type, meta->name, __max(strlen(meta->name), strlen(type))) == 0) {
return iiok;
}
return iino;
}
#else
void iaoimemorystate() {
ilog("[AOI-Memory] Not Implement IMeta \n");
}
#endif
// 获取当前系统的纳秒数
int64_t igetcurnano() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec*1000*1000 + tv.tv_usec;
}
// 获取当前系统的毫秒数
int64_t igetcurtick() {
return igetcurnano()/1000;
}
// 获取系统的下一个唯一的事件纳秒数
int64_t igetnextnano(){
static int64_t gseq = 0;
int64_t curseq = igetcurnano();
if (curseq > gseq) {
gseq = curseq;
}else {
++gseq;
}
return gseq;
}
// 计算距离的平方
ireal idistancepow2(ipos *p, ipos *t) {
ireal dx = p->x - t->x;
ireal dy = p->y - t->y;
return dx*dx + dy*dy;
}
// 判断矩形包含关系
int irectcontains(irect *con, irect *r) {
icheckret(con, iino);
icheckret(r, iiok);
if (con->pos.x <= r->pos.x && con->pos.y <= r->pos.y
&& con->pos.x + con->size.w >= r->pos.x + r->size.w
&& con->pos.y + con->size.h >= r->pos.y + r->size.h) {
return iiok;
}
return iino;
}
// 判断矩形与点的包含关系
int irectcontainspoint(irect *con, ipos *p) {
icheckret(con, iino);
icheckret(p, iiok);
if (con->pos.x <= p->x && con->pos.y <= p->y
&& con->pos.x + con->size.w >= p->x
&& con->pos.y + con->size.h >= p->y) {
return iiok;
}
return iino;
}
// 圆形相交: iiok, iino
int icircleintersect(icircle *con, icircle *c) {
icheckret(con, iino);
icheckret(c, iiok);
ireal ds = (con->radis + c->radis);
if (idistancepow2(&con->pos, &c->pos) <= ds * ds) {
return iiok;
}
return iino;
}
// 圆形包含: iiok, iino
int icirclecontains(icircle *con, icircle *c) {
icheckret(con, iino);
icheckret(c, iiok);
ireal ds = (con->radis - c->radis);
icheckret(ds >= 0, iino);
if (idistancepow2(&con->pos, &c->pos) <= ds * ds) {
return iiok;
}
return iino;
}
// 圆形包含: iiok, iino
int icirclecontainspoint(icircle *con, ipos *p) {
icheckret(con, iino);
icheckret(p, iiok);
ireal ds = (con->radis);
icheckret(ds >= 0, iino);
if (idistancepow2(&con->pos, p) <= ds * ds) {
return iiok;
}
return iino;
}
// 圆形的关系: EnumCircleRelationBContainsA(c包含con),
// EnumCircleRelationAContainsB(con包含c),
// EnumCircleRelationIntersect(相交),
// EnumCircleRelationNoIntersect(相离)
int icirclerelation(icircle *con, icircle *c) {
icheckret(con, EnumCircleRelationBContainsA);
icheckret(c, EnumCircleRelationAContainsB);
ireal minusds = con->radis - c->radis;
ireal addds = con->radis + c->radis;
ireal ds = idistancepow2(&con->pos, &c->pos);
if (ds <= minusds * minusds) {
if (minusds >= 0) {
return EnumCircleRelationAContainsB;
}else {
return EnumCircleRelationBContainsA;
}
} else if (ds <= addds *addds) {
return EnumCircleRelationIntersect;
} else {
return EnumCircleRelationNoIntersect;
}
}
// 一堆时间宏,常用来做性能日志
#define __ProfileThreashold 10
#define __ShouldLog(t) (t > __ProfileThreashold)
#define __Millis igetcurtick()
#define __Nanos igetcurnano()
#define __Since(t) (__Nanos - t)
#define iplogwhen(t, when, ...) do { if(open_log_profile && t > when) {printf("[PROFILE] Take %lld nanos ", t); printf(__VA_ARGS__); } } while (0)
#define iplog(t, ...) iplogwhen(t, __ProfileThreashold, __VA_ARGS__)
// iref 转换成 target
#define icast(type, v) ((type*)(v))
// 转换成iref
#define irefcast(v) icast(iref, v)
// 增加营养计数
int irefretain(iref *ref) {
return ++ref->ref;
}
// 释放引用计数
void irefrelease(iref *ref) {
--ref->ref;
// 通知 watch
if (ref->watch) {
ref->watch(ref);
}
// 没有引用了,析构对象
if (ref->ref == 0) {
if (ref->free) {
ref->free(ref);
}else {
iobjfree(ref);
}
}
}
// 申请自动释放池子
irefautoreleasepool * irefautoreleasebegin() {
return iobjmalloc(irefautoreleasepool);
}
// 自动释放
iref* irefautorelease(irefautoreleasepool *pool, iref *ref) {
icheckret(pool, NULL);
if(!pool->list) {
pool->list = ireflistmake();
}
ireflistadd(pool->list, ref);
return ref;
}
// 结束自动释放
void irefautoreleaseend(irefautoreleasepool *pool) {
icheck(pool);
icheck(pool->list);
irefjoint *joint = ireflistfirst(pool->list);
while(joint) {
irelease(joint->value);
joint = joint->next;
}
ireflistfree(pool->list);
iobjfree(pool);
}
// 返回值本身的引用retain
iref *irefassistretain(iref *ref) {
iretain(ref);
return ref;
}
// 列表操作
#define list_add_front(root, node) \
do {\
if (root == NULL) { \
root = node; \
}else { \
node->next = root; \
root->pre = node; \
root = node; \
}\
}while(0)
#define list_remove(root, node) \
do {\
if (root == node) { \
root = node->next;\
}\
if (node->next) {\
node->next->pre = node->pre;\
}\
\
if (node->pre) {\
node->pre->next = node->next;\
}\
\
node->pre = NULL;\
node->next = NULL;\
}while(0)
// 构造列表节点
irefjoint* irefjointmake(iref *value) {
irefjoint *joint = iobjmalloc(irefjoint);
iassign(joint->value, value);
return joint;
}
// 释放列表节点
void irefjointfree(irefjoint* joint) {
icheck(joint);
irelease(joint->value);
iobjfree(joint);
}
// 创建列表
ireflist *ireflistmake() {
return iobjmalloc(ireflist);
}
// 获取列表长度
int ireflistlen(ireflist *list) {
icheckret(list, 0);
return list->length;
}
// 获取第一个节点
irefjoint* ireflistfirst(ireflist *list) {
icheckret(list, NULL);
return list->root;
}
// 从列表里面查找第一个满足要求的节点
irefjoint* ireflistfind(ireflist *list, iref *value) {
irefjoint* joint = ireflistfirst(list);
while(joint) {
if (joint->value == value) {
break;
}
joint = joint->next;
}
return joint;
}
// 往列表增加节点: 前置节点
irefjoint* ireflistaddjoint(ireflist *list, irefjoint * joint) {
joint->list = list;
list_add_front(list->root, joint);
++list->length;
return joint;
}
// 往列表增加节点: 前置节点(会增加引用计数)
irefjoint* ireflistadd(ireflist *list, iref *value) {
icheckret(list, NULL);
irefjoint *joint = irefjointmake(value);
return ireflistaddjoint(list, joint);
}
// 从节点里面移除节点 , 并且释放当前节点, 并且返回下一个节点
static irefjoint* _ireflistremovejointwithfree(ireflist *list, irefjoint *joint, bool withfree) {
irefjoint *next = NULL;
icheckret(list, next);
icheckret(joint, next);
icheckret(joint->list == list, next);
joint->list = NULL;
next = joint->next;
list_remove(list->root, joint);
--list->length;
if (withfree) {
irefjointfree(joint);
}
return next;
}
// 从节点里面移除节点 , 并且释放当前节点, 并且返回下一个节点
irefjoint* ireflistremovejointandfree(ireflist *list, irefjoint *joint) {
return _ireflistremovejointwithfree(list, joint, true);
}
// 从节点里面移除节点
irefjoint * ireflistremovejoint(ireflist *list, irefjoint *joint) {
return _ireflistremovejointwithfree(list, joint, false);
}
// 从节点里面移除节点: 并且会释放节点
irefjoint* ireflistremove(ireflist *list, iref *value) {
icheckret(list, NULL);
irefjoint *joint = ireflistfind(list, value);
return _ireflistremovejointwithfree(list, joint, true);
}
// 释放所有节点
void ireflistremoveall(ireflist *list) {
icheck(list);
irefjoint *joint = ireflistfirst(list);
irefjoint *next;
while(joint) {
next = joint->next;
irefjointfree(joint);
joint = next;
}
list->root = NULL;
list->length = 0;
}
// 释放列表
void ireflistfree(ireflist *list) {
icheck(list);
// 释放列表节点
ireflistremoveall(list);
// 释放list 本身
iobjfree(list);
}
// cache 的 绑定在 ref 上的回调
void _ientrywatch_cache(iref *ref) {
icheck(ref->ref == 0);
// only move ref to live cache
irefcache *cache = ref->cache;
int len = ireflistlen(cache->cache);
if (len < cache->capacity) {
ireflistadd(cache->cache, ref);
}else if (cache->envicted){
cache->envicted(ref);
}
}
// 创造一个cache
irefcache *irefcachemake(int capacity, icachenewentry newentry) {
irefcache *cache = iobjmalloc(irefcache);
cache->cache = ireflistmake();
cache->capacity = capacity;
cache->newentry = newentry;
return cache;
}
// 从缓存里面取一个
iref *irefcachepoll(irefcache *cache) {
iref *ref = NULL;
irefjoint* joint = ireflistfirst(cache->cache);
if (joint) {
ireflistremovejoint(cache->cache, joint);
iassign(ref, joint->value);
irefjointfree(joint);
if (ref->ref != 1) {
ilog("[IMAP-RefCache] Poll - %d\n", ref->ref);
}
return ref;
}
ref = cache->newentry();
ref->cache = cache;
ref->watch = _ientrywatch_cache;
return ref;
}
// 释放到缓存里面
void irefcachepush(irefcache *cache, iref *ref) {
icheck(ref);
// just make sure we should be cacheable
if (ref->cache != cache) {
ref->cache = cache;
if (ref->cache) {
ref->watch = _ientrywatch_cache;
} else {
ref->watch = NULL;
}
}
// release the ref
irelease(ref);
}
// 清理缓冲区
void irefcacheclear(irefcache *cache) {
icheck(cache);
int oldcapacity = cache->capacity;
cache->capacity = 0;
ireflistremoveall(cache->cache);
cache->capacity = oldcapacity;
}
// 释放缓存
void irefcachefree(irefcache *cache) {
icheck(cache);
// 因为Cache会劫持对象的释放,所以释放对象的时候应该停掉Cache的劫持
cache->capacity = 0;
// 释放缓存的引用对象
ireflistfree(cache->cache);
// 释放缓存自己
iobjfree(cache);
}
// 当前缓冲区的存储的对象个数
int irefcachesize(irefcache *cache) {
return ireflistlen(cache->cache);
}
// 用宏处理缓存接口: 拿
#define icache(cache, type) ((type*)irefcachepoll(cache))
// 用宏处理缓存接口: 放回
#define icacheput(cache, ref) irefcachepush(cache, (iref*)(ref))
// 拷贝一个ABCD编码
#define copycode(dst, src, count) \
do { \
memset(dst.code, 0, IMaxDivide); \
memcpy(dst.code, src.code, count); \
} while(0)
// 释放几个基本单元
iunit * imakeunit(iid id, ireal x, ireal y) {
iunit *unit = iobjmalloc(iunit);
iretain(unit);
unit->id = id;
unit->pos.x = x;
unit->pos.y = y;
return unit;
}
// 释放一个基本单元
void ifreeunit(iunit *unit) {
irelease(unit);
}
// 释放链表
void ifreeunitlist(iunit *unit) {
icheck(unit);
iunit *next = NULL;
while (unit) {
next = unit->next;
ifreeunit(unit);
unit = next;
}
}
// 维度索引
#define DimensionW 0
#define DimensionH 1
// 节点内存管理
inode * imakenode(){
inode *node = iobjmalloc(inode);
iretain(node);
return node;
}
// 释放节点
void ifreenode(inode *node){
irelease(node);
}
// 释放节点包含的单元
void ifreenodekeeper(inode *node) {
ifreeunitlist(node->units);
ifreenode(node);
}
// 释放节点组成的树
void ifreenodetree(inode *node) {
icheck(node);
for (int i=0; i<IMaxChilds; ++i) {
ifreenodetree(node->childs[i]);
}
ifreenodekeeper(node);
}
// 增加叶子节点
void justaddleaf(imap *map, inode *node) {
icheck(node);
icheck(node->pre == NULL && node->next == NULL);
list_add_front(map->leaf, node);
iretain(node);
}
// 移除叶子节点
void justremoveleaf(imap *map, inode *node) {
icheck(node);
icheck(node->pre != NULL || node->next != NULL || node == map->leaf);
list_remove(map->leaf, node);
irelease(node);
}
// 把单元加入叶子节点
int justaddunit(imap *map, inode *node, iunit *unit){
icheckret(node && unit, iino);
icheckret(unit->node == NULL, iino);
icheckret(node->level == map->divide, iino);
unit->node = node;
unit->tick = igetnextnano();
node->unitcnt++;
node->tick = unit->tick;
#if open_node_utick
node->utick = unit->tick;
#endif
#if open_log_unit
ilog("[IMAP-Unit] Add Unit (%lld, %s) To Node (%d, %s)\n",
unit->id, unit->code.code, node->level, node->code.code);
#endif
list_add_front(node->units, unit);
// add ref as we save units in list
iretain(unit);
return iiok;
}
// 从叶子节点移除单元
int justremoveunit(imap *map, inode *node, iunit *unit) {
icheckret(node && unit, iino);
icheckret(unit->node == node, iino);
icheckret(node->level == map->divide, iino);
#if open_log_unit
ilog("[IMAP-Unit] Remove Unit (%lld, %s) From Node (%d, %s)\n",
unit->id, unit->code.code, node->level, node->code.code);
#endif
node->unitcnt--;
node->tick = igetnextnano();
#if open_node_utick
node->utick = node->tick;
#endif
unit->node = NULL;
unit->tick = node->tick;
list_remove(node->units, unit);
irelease(unit);
return iiok;
}
// 打印节点信息
#define _print_node(node) \
do {\
ilog("[IMAP-Node] Node (%d, %s, %p)\n", \
node->level, node->code.code, node);\
}while(0)
// 打印单元加入节点的操作
#define _print_unit_add(node, unit, idx) \
do {\
ilog("[IMAP-Unit-Add] Unit(%lld, %s, x: %.3f, y: %.3f) To Node (%d, %s, %p) \n", \
unit->id, unit->code.code, unit->code.pos.x, unit->code.pos.y, node->level, node->code.code, node);\
}while(0)
// 打印单元移除出节点的操作
#define _print_unit_remove(node, unit, idx) \
do {\
ilog("[IMAP-Unit-Remove] Unit(%lld, %s, x: %.3f, y: %.3f) From Node (%d, %s, %p) \n", \
unit->id, unit->code.code, unit->code.pos.x, unit->code.pos.y, node->level, node->code.code, node);\
}while(0)
// 加入新的节点
inode* addnodetoparent(imap *map, inode *node, int codei, int idx, icode *code) {
// 创造一个节点
inode* child = icache(map->nodecache, inode);
child->level = idx+1;
child->parent = node;
child->codei = codei;
// 生成节点编码信息
copycode(child->code, (*code), child->level);
imapgenpos(map, &child->code.pos, &child->code);
// 加入父节点
node->childcnt++;
node->childs[codei] = child;
#if open_log_node
ilog("[IMAP-Node] Add Node (%d, %s, %p) To Node (%d, %s, %p) in %d\n",
child->level, child->code.code, child,
node->level, node->code.code, node, codei);
#endif
// 增加节点统计
++map->state.nodecount;
// 增加叶子节点统计
if (child->level == map->divide) {
// 叶子节点出现
justaddleaf(map, child);
// 叶子节点统计
++map->state.leafcount;
}
return child;
}
// 移除节点
int removenodefromparent(imap *map, inode *node) {
icheckret(node, iino);
icheckret(node->parent, iino);
// 减少节点统计
--map->state.nodecount;
// 减少叶子节点统计
if (node->level == map->divide) {
// 叶子节点移除
justremoveleaf(map, node);
// 叶子节点统计
--map->state.leafcount;
}
// 移除出父节点
node->parent->childcnt--;
node->parent->childs[node->codei] = NULL;
node->parent = NULL;
// reset
node->level = 0;
node->codei = 0;
node->code.code[0] = 0;
node->tick = 0;
#if open_node_utick
node->utick = 0;
#endif
// 释放节点
icacheput(map->nodecache, node);
return iiok;
}
// 节点加入地图
int imapaddunitto(imap *map, inode *node, iunit *unit, int idx) {
icheckret(idx <= map->divide, iino);
char code;
int codei;
int ok = iino;
inode *child = NULL;
// 如果节点不能附加单元到上面则直接返回失败
if (_state_is(node->state, EnumNodeStateNoUnit)) {
return ok;
}
code = unit->code.code[idx];
#if open_log_code
ilog("[IMAP-Code] (%lld, %s, %p) Code %d, Idx: %d, Divide: %d\n", unit->id, unit->code.code, unit, code, idx, map->divide);
#endif
// 节点不需要查找了,或者以及达到最大层级
if (code == 0 || idx >= map->divide) {
// add unit
justaddunit(map, node, unit);
// log it
#if _print_unit_add
_print_unit_add(node, unit, idx);
i // log it
#endif
++map->state.unitcount;
ok = iiok;
}else {
// 定位节点所在的子节点
codei = code - 'A';
icheckret(codei>=0 && codei<IMaxChilds, iino);
child = node->childs[codei];
#if open_log_node_get
ilog("[IMAP-Node-Get] Get Node %p from (%d, %s, %p) in %d\n",
child, node->level, node->code.code, node, codei);
#endif
if (child == NULL) {
// 创造一个节点
child = addnodetoparent(map, node, codei, idx, &unit->code);
}
ok = imapaddunitto(map, child, unit, ++idx);
}
// 更新节点信息
if (ok == iiok) {
if (child) {
node->tick = child->tick;
#if open_node_utick
node->utick = child->tick;
#endif
}else {
// 已经在 justaddunit 更新了节点时间戳
}
}
return ok;
}
// 从地图上移除
int imapremoveunitfrom(imap *map, inode *node, iunit *unit, int idx, inode *stop) {
icheckret(idx <= map->divide, iino);
icheckret(node, iino);
char code;
int codei;
inode *child = NULL;
int ok = iino;
code = unit->code.code[idx];
if (code == 0 || idx >= map->divide) {
// log it
#if open_log_unit_remove
_print_unit_remove(node, unit, idx);
#endif
// 移除单元
justremoveunit(map, node, unit);
// 移除单元统计
--map->state.unitcount;
// 移除成功
ok = iiok;
}else {
codei = code - 'A';
if (codei >= 0 && codei<IMaxChilds) {
child = node->childs[codei];
}
ok = imapremoveunitfrom(map, child, unit, ++idx, stop);
}
if (ok == iiok){
// 更新时间
if (child) {
node->tick = child->tick;
#if open_node_utick
node->utick = child->tick;
#endif
}else {
// 已经在 justremoveunit 更新了时间戳
}
// 回收可能的节点
if (node != stop // 不是停止节点
&& !_state_is(node->state, EnumNodeStateStatic) // 不是静态节点
&& node->childcnt == 0 // 孩子节点为0
&& node->unitcnt == 0 // 上面绑定的单元节点也为空
) {
removenodefromparent(map, node);
}
}
return ok;