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 #ifndef UCX_TREE_H
37 #define UCX_TREE_H
38
39 #include "common.h"
40
41 #include "collection.h"
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 typedef struct cx_tree_iterator_s {
64
65
66
67 CX_ITERATOR_BASE;
68
69
70
71 bool skip;
72
73
74
75
76 bool visit_on_exit;
77
78
79
80 bool exiting;
81
82
83
84 ptrdiff_t loc_children;
85
86
87
88 ptrdiff_t loc_next;
89
90
91
92 size_t counter;
93
94
95
96
97
98 void *node;
99
100
101
102
103 void *node_next;
104
105
106
107
108
109
110
111 void **stack;
112
113
114
115 size_t stack_capacity;
116 union {
117
118
119
120 size_t stack_size;
121
122
123
124 size_t depth;
125 };
126 } CxTreeIterator;
127
128
129
130
131 struct cx_tree_visitor_queue_s {
132
133
134
135 void *node;
136
137
138
139 size_t depth;
140
141
142
143 struct cx_tree_visitor_queue_s *next;
144 };
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 typedef struct cx_tree_visitor_s {
168
169
170
171 CX_ITERATOR_BASE;
172
173
174
175 bool skip;
176
177
178
179 ptrdiff_t loc_children;
180
181
182
183 ptrdiff_t loc_next;
184
185
186
187 size_t counter;
188
189
190
191
192
193 void *node;
194
195
196
197 size_t depth;
198
199
200
201 struct cx_tree_visitor_queue_s *queue_next;
202
203
204
205 struct cx_tree_visitor_queue_s *queue_last;
206 } CxTreeVisitor;
207
208
209
210
211
212 cx_attr_nonnull
213 static inline
void cxTreeIteratorDispose(CxTreeIterator *iter) {
214 free(iter->stack);
215 iter->stack =
NULL;
216 }
217
218
219
220
221
222 cx_attr_nonnull
223 static inline
void cxTreeVisitorDispose(CxTreeVisitor *visitor) {
224 struct cx_tree_visitor_queue_s *q = visitor->queue_next;
225 while (q !=
NULL) {
226 struct cx_tree_visitor_queue_s *next = q->next;
227 free(q);
228 q = next;
229 }
230 }
231
232
233
234
235
236
237
238 #define cxTreeIteratorContinue(iterator) (iterator).skip = true;
continue
239
240
241
242
243
244
245
246 #define cxTreeVisitorContinue(visitor) cxTreeIteratorContinue(visitor)
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 cx_attr_nonnull
266 void cx_tree_link(
267 void *parent,
268 void *node,
269 ptrdiff_t loc_parent,
270 ptrdiff_t loc_children,
271 ptrdiff_t loc_last_child,
272 ptrdiff_t loc_prev,
273 ptrdiff_t loc_next
274 );
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290 cx_attr_nonnull
291 void cx_tree_unlink(
292 void *node,
293 ptrdiff_t loc_parent,
294 ptrdiff_t loc_children,
295 ptrdiff_t loc_last_child,
296 ptrdiff_t loc_prev,
297 ptrdiff_t loc_next
298 );
299
300
301
302
303 #define CX_TREE_SEARCH_INFINITE_DEPTH 0
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 cx_attr_nonnull
332 typedef int (*cx_tree_search_data_func)(
const void *node,
const void *data);
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 cx_attr_nonnull
362 typedef int (*cx_tree_search_func)(
const void *node,
const void *new_node);
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 cx_attr_nonnull
389 cx_attr_access_w(
5)
390 int cx_tree_search_data(
391 const void *root,
392 size_t depth,
393 const void *data,
394 cx_tree_search_data_func sfunc,
395 void **result,
396 ptrdiff_t loc_children,
397 ptrdiff_t loc_next
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 cx_attr_nonnull
425 cx_attr_access_w(
5)
426 int cx_tree_search(
427 const void *root,
428 size_t depth,
429 const void *node,
430 cx_tree_search_func sfunc,
431 void **result,
432 ptrdiff_t loc_children,
433 ptrdiff_t loc_next
434 );
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456 cx_attr_nodiscard
457 CxTreeIterator cx_tree_iterator(
458 void *root,
459 bool visit_on_exit,
460 ptrdiff_t loc_children,
461 ptrdiff_t loc_next
462 );
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482 cx_attr_nodiscard
483 CxTreeVisitor cx_tree_visitor(
484 void *root,
485 ptrdiff_t loc_children,
486 ptrdiff_t loc_next
487 );
488
489
490
491
492
493
494
495
496
497
498
499 cx_attr_nonnull_arg(
1)
500 typedef void *(*cx_tree_node_create_func)(
const void *,
void *);
501
502
503
504
505
506
507
508 extern unsigned int cx_tree_add_look_around_depth;
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 cx_attr_nonnull_arg(
1,
3,
4,
6,
7)
549 cx_attr_access_w(
6)
550 size_t cx_tree_add_iter(
551 struct cx_iterator_base_s *iter,
552 size_t num,
553 cx_tree_search_func sfunc,
554 cx_tree_node_create_func cfunc,
555 void *cdata,
556 void **failed,
557 void *root,
558 ptrdiff_t loc_parent,
559 ptrdiff_t loc_children,
560 ptrdiff_t loc_last_child,
561 ptrdiff_t loc_prev,
562 ptrdiff_t loc_next
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 cx_attr_nonnull_arg(
1,
4,
5,
7,
8)
603 cx_attr_access_w(
7)
604 size_t cx_tree_add_array(
605 const void *src,
606 size_t num,
607 size_t elem_size,
608 cx_tree_search_func sfunc,
609 cx_tree_node_create_func cfunc,
610 void *cdata,
611 void **failed,
612 void *root,
613 ptrdiff_t loc_parent,
614 ptrdiff_t loc_children,
615 ptrdiff_t loc_last_child,
616 ptrdiff_t loc_prev,
617 ptrdiff_t loc_next
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 cx_attr_nonnull_arg(
1,
2,
3,
5,
6)
666 cx_attr_access_w(
5)
667 int cx_tree_add(
668 const void *src,
669 cx_tree_search_func sfunc,
670 cx_tree_node_create_func cfunc,
671 void *cdata,
672 void **cnode,
673 void *root,
674 ptrdiff_t loc_parent,
675 ptrdiff_t loc_children,
676 ptrdiff_t loc_last_child,
677 ptrdiff_t loc_prev,
678 ptrdiff_t loc_next
679 );
680
681
682
683
684
685 typedef struct cx_tree_class_s cx_tree_class;
686
687
688
689
690 struct cx_tree_node_base_s {
691
692
693
694 struct cx_tree_node_base_s *parent;
695
696
697
698 struct cx_tree_node_base_s *children;
699
700
701
702 struct cx_tree_node_base_s *last_child;
703
704
705
706 struct cx_tree_node_base_s *prev;
707
708
709
710 struct cx_tree_node_base_s *next;
711 };
712
713
714
715
716 struct cx_tree_s {
717
718
719
720 const cx_tree_class *cl;
721
722
723
724
725 const CxAllocator *allocator;
726
727
728
729
730
731
732 void *root;
733
734
735
736
737
738
739
740
741
742 cx_tree_node_create_func node_create;
743
744
745
746
747 cx_destructor_func simple_destructor;
748
749
750
751
752 cx_destructor_func2 advanced_destructor;
753
754
755
756
757 void *destructor_data;
758
759
760
761
762 cx_tree_search_func search;
763
764
765
766
767 cx_tree_search_data_func search_data;
768
769
770
771
772 size_t size;
773
774
775
776
777 ptrdiff_t loc_parent;
778
779
780
781
782 ptrdiff_t loc_children;
783
784
785
786
787
788 ptrdiff_t loc_last_child;
789
790
791
792
793 ptrdiff_t loc_prev;
794
795
796
797
798 ptrdiff_t loc_next;
799 };
800
801
802
803
804
805
806
807
808
809 #define CX_TREE_NODE_BASE(type) \
810 type *parent; \
811 type *children;\
812 type *last_child;\
813 type *prev;\
814 type *next
815
816
817
818
819
820
821
822
823
824 #define cx_tree_node_base_layout \
825 offsetof(
struct cx_tree_node_base_s, parent),\
826 offsetof(
struct cx_tree_node_base_s, children),\
827 offsetof(
struct cx_tree_node_base_s, last_child),\
828 offsetof(
struct cx_tree_node_base_s, prev), \
829 offsetof(
struct cx_tree_node_base_s, next)
830
831
832
833
834 struct cx_tree_class_s {
835
836
837
838
839
840
841 int (*insert_element)(
842 struct cx_tree_s *tree,
843 const void *data
844 );
845
846
847
848
849
850
851
852 size_t (*insert_many)(
853 struct cx_tree_s *tree,
854 struct cx_iterator_base_s *iter,
855 size_t n
856 );
857
858
859
860
861 void *(*find)(
862 struct cx_tree_s *tree,
863 const void *subtree,
864 const void *data,
865 size_t depth
866 );
867 };
868
869
870
871
872 typedef struct cx_tree_s CxTree;
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896 cx_attr_nonnull
897 void cxTreeDestroySubtree(CxTree *tree,
void *node);
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917 #define cxTreeClear(tree) cxTreeDestroySubtree(tree, tree->root)
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935 void cxTreeFree(CxTree *tree);
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 cx_attr_nonnull_arg(
2,
3,
4)
962 cx_attr_nodiscard
963 cx_attr_malloc
964 cx_attr_dealloc(cxTreeFree,
1)
965 CxTree *cxTreeCreate(
966 const CxAllocator *allocator,
967 cx_tree_node_create_func create_func,
968 cx_tree_search_func search_func,
969 cx_tree_search_data_func search_data_func,
970 ptrdiff_t loc_parent,
971 ptrdiff_t loc_children,
972 ptrdiff_t loc_last_child,
973 ptrdiff_t loc_prev,
974 ptrdiff_t loc_next
975 );
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994 #define cxTreeCreateSimple(\
995 allocator, create_func, search_func, search_data_func \
996 ) cxTreeCreate(allocator, create_func, search_func, search_data_func, \
997 cx_tree_node_base_layout)
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021 cx_attr_nonnull_arg(
2)
1022 cx_attr_nodiscard
1023 cx_attr_malloc
1024 cx_attr_dealloc(cxTreeFree,
1)
1025 CxTree *cxTreeCreateWrapped(
1026 const CxAllocator *allocator,
1027 void *root,
1028 ptrdiff_t loc_parent,
1029 ptrdiff_t loc_children,
1030 ptrdiff_t loc_last_child,
1031 ptrdiff_t loc_prev,
1032 ptrdiff_t loc_next
1033 );
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047 cx_attr_nonnull
1048 static inline
int cxTreeInsert(
1049 CxTree *tree,
1050 const void *data
1051 ) {
1052 return tree->cl->insert_element(tree, data);
1053 }
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067 cx_attr_nonnull
1068 static inline
size_t cxTreeInsertIter(
1069 CxTree *tree,
1070 struct cx_iterator_base_s *iter,
1071 size_t n
1072 ) {
1073 return tree->cl->insert_many(tree, iter, n);
1074 }
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089 cx_attr_nonnull
1090 static inline
size_t cxTreeInsertArray(
1091 CxTree *tree,
1092 const void *data,
1093 size_t elem_size,
1094 size_t n
1095 ) {
1096 if (n ==
0)
return 0;
1097 if (n ==
1)
return 0 == cxTreeInsert(tree, data) ?
1 :
0;
1098 CxIterator iter = cxIterator(data, elem_size, n);
1099 return cxTreeInsertIter(tree, cxIteratorRef(iter), n);
1100 }
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113 cx_attr_nonnull
1114 cx_attr_nodiscard
1115 static inline
void *cxTreeFind(
1116 CxTree *tree,
1117 const void *data
1118 ) {
1119 return tree->cl->find(tree, tree->root, data,
0);
1120 }
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141 cx_attr_nonnull
1142 cx_attr_nodiscard
1143 static inline
void *cxTreeFindInSubtree(
1144 CxTree *tree,
1145 const void *data,
1146 void *subtree_root,
1147 size_t max_depth
1148 ) {
1149 return tree->cl->find(tree, subtree_root, data, max_depth);
1150 }
1151
1152
1153
1154
1155
1156
1157
1158
1159 cx_attr_nonnull
1160 cx_attr_nodiscard
1161 size_t cxTreeSubtreeSize(CxTree *tree,
void *subtree_root);
1162
1163
1164
1165
1166
1167
1168
1169
1170 cx_attr_nonnull
1171 cx_attr_nodiscard
1172 size_t cxTreeSubtreeDepth(CxTree *tree,
void *subtree_root);
1173
1174
1175
1176
1177
1178
1179
1180 cx_attr_nonnull
1181 cx_attr_nodiscard
1182 size_t cxTreeDepth(CxTree *tree);
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196 cx_attr_nonnull
1197 cx_attr_nodiscard
1198 static inline CxTreeIterator cxTreeIterateSubtree(
1199 CxTree *tree,
1200 void *node,
1201 bool visit_on_exit
1202 ) {
1203 return cx_tree_iterator(
1204 node, visit_on_exit,
1205 tree->loc_children, tree->loc_next
1206 );
1207 }
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219 cx_attr_nonnull
1220 cx_attr_nodiscard
1221 static inline CxTreeVisitor cxTreeVisitSubtree(CxTree *tree,
void *node) {
1222 return cx_tree_visitor(
1223 node, tree->loc_children, tree->loc_next
1224 );
1225 }
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236 cx_attr_nonnull
1237 cx_attr_nodiscard
1238 static inline CxTreeIterator cxTreeIterate(
1239 CxTree *tree,
1240 bool visit_on_exit
1241 ) {
1242 return cxTreeIterateSubtree(tree, tree->root, visit_on_exit);
1243 }
1244
1245
1246
1247
1248
1249
1250
1251
1252 cx_attr_nonnull
1253 cx_attr_nodiscard
1254 static inline CxTreeVisitor cxTreeVisit(CxTree *tree) {
1255 return cxTreeVisitSubtree(tree, tree->root);
1256 }
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269 cx_attr_nonnull
1270 void cxTreeSetParent(
1271 CxTree *tree,
1272 void *parent,
1273 void *child
1274 );
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291 cx_attr_nonnull
1292 void cxTreeAddChildNode(
1293 CxTree *tree,
1294 void *parent,
1295 void *child
1296 );
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315 cx_attr_nonnull
1316 int cxTreeAddChild(
1317 CxTree *tree,
1318 void *parent,
1319 const void *data
1320 );
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334 cx_attr_nonnull
1335 typedef void (*cx_tree_relink_func)(
1336 void *node,
1337 const void *old_parent,
1338 const void *new_parent
1339 );
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355 cx_attr_nonnull_arg(
1,
2)
1356 int cxTreeRemoveNode(
1357 CxTree *tree,
1358 void *node,
1359 cx_tree_relink_func relink_func
1360 );
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373 cx_attr_nonnull
1374 void cxTreeRemoveSubtree(CxTree *tree,
void *node);
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394 cx_attr_nonnull_arg(
1,
2)
1395 int cxTreeDestroyNode(
1396 CxTree *tree,
1397 void *node,
1398 cx_tree_relink_func relink_func
1399 );
1400
1401 #ifdef __cplusplus
1402 }
1403 #endif
1404
1405 #endif
1406