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
93 size_t counter;
94
95
96
97
98
99 void *node;
100
101
102
103
104 void *node_next;
105
106
107
108
109
110
111
112 void **stack;
113
114
115
116 size_t stack_capacity;
117 union {
118
119
120
121 size_t stack_size;
122
123
124
125
126 size_t depth;
127 };
128 } CxTreeIterator;
129
130
131
132
133 struct cx_tree_visitor_queue_s {
134
135
136
137 void *node;
138
139
140
141
142 size_t depth;
143
144
145
146 struct cx_tree_visitor_queue_s *next;
147 };
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 typedef struct cx_tree_visitor_s {
171
172
173
174 CX_ITERATOR_BASE;
175
176
177
178 bool skip;
179
180
181
182 ptrdiff_t loc_children;
183
184
185
186 ptrdiff_t loc_next;
187
188
189
190
191 size_t counter;
192
193
194
195
196
197 void *node;
198
199
200
201 size_t depth;
202
203
204
205 struct cx_tree_visitor_queue_s *queue_next;
206
207
208
209 struct cx_tree_visitor_queue_s *queue_last;
210 } CxTreeVisitor;
211
212
213
214
215
216 cx_attr_nonnull
217 CX_EXPORT void cxTreeIteratorDispose(CxTreeIterator *iter);
218
219
220
221
222
223 cx_attr_nonnull
224 CX_EXPORT void cxTreeVisitorDispose(CxTreeVisitor *visitor);
225
226
227
228
229
230
231
232 #define cxTreeIteratorContinue(iterator) (iterator).skip = true;
continue
233
234
235
236
237
238
239
240 #define cxTreeVisitorContinue(visitor) cxTreeIteratorContinue(visitor)
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 cx_attr_nonnull
260 CX_EXPORT void cx_tree_link(
void *parent,
void *node,
261 ptrdiff_t loc_parent,
ptrdiff_t loc_children,
ptrdiff_t loc_last_child,
262 ptrdiff_t loc_prev,
ptrdiff_t loc_next);
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 cx_attr_nonnull
279 CX_EXPORT void cx_tree_unlink(
void *node,
280 ptrdiff_t loc_parent,
ptrdiff_t loc_children,
ptrdiff_t loc_last_child,
281 ptrdiff_t loc_prev,
ptrdiff_t loc_next);
282
283
284
285
286 #define CX_TREE_SEARCH_INFINITE_DEPTH 0
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 typedef int (*cx_tree_search_data_func)(
const void *node,
const void *data);
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 typedef int (*cx_tree_search_func)(
const void *node,
const void *new_node);
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 cx_attr_nonnull cx_attr_access_w(
5)
370 CX_EXPORT int cx_tree_search_data(
const void *root,
size_t depth,
371 const void *data, cx_tree_search_data_func sfunc,
372 void **result,
ptrdiff_t loc_children,
ptrdiff_t loc_next);
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 cx_attr_nonnull cx_attr_access_w(
5)
399 CX_EXPORT int cx_tree_search(
const void *root,
size_t depth,
400 const void *node, cx_tree_search_func sfunc,
401 void **result,
ptrdiff_t loc_children,
ptrdiff_t loc_next);
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423 cx_attr_nodiscard
424 CX_EXPORT CxTreeIterator cx_tree_iterator(
void *root, bool visit_on_exit,
425 ptrdiff_t loc_children,
ptrdiff_t loc_next);
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445 cx_attr_nodiscard
446 CX_EXPORT CxTreeVisitor cx_tree_visitor(
void *root,
447 ptrdiff_t loc_children,
ptrdiff_t loc_next);
448
449
450
451
452
453
454
455
456
457
458
459 typedef void *(*cx_tree_node_create_func)(
const void *,
void *);
460
461
462
463
464
465
466
467 CX_EXPORT extern unsigned int cx_tree_add_look_around_depth;
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 cx_attr_nonnull_arg(
1,
3,
4,
6,
7) cx_attr_access_w(
6)
508 CX_EXPORT size_t cx_tree_add_iter(
struct cx_iterator_base_s *iter,
size_t num,
509 cx_tree_search_func sfunc, cx_tree_node_create_func cfunc,
510 void *cdata,
void **failed,
void *root,
511 ptrdiff_t loc_parent,
ptrdiff_t loc_children,
ptrdiff_t loc_last_child,
512 ptrdiff_t loc_prev,
ptrdiff_t loc_next);
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 cx_attr_nonnull_arg(
1,
4,
5,
7,
8) cx_attr_access_w(
7)
552 CX_EXPORT size_t cx_tree_add_array(
const void *src,
size_t num,
size_t elem_size,
553 cx_tree_search_func sfunc, cx_tree_node_create_func cfunc,
554 void *cdata,
void **failed,
void *root,
555 ptrdiff_t loc_parent,
ptrdiff_t loc_children,
ptrdiff_t loc_last_child,
556 ptrdiff_t loc_prev,
ptrdiff_t loc_next);
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 cx_attr_nonnull_arg(
1,
2,
3,
5,
6) cx_attr_access_w(
5)
604 CX_EXPORT int cx_tree_add(
const void *src,
605 cx_tree_search_func sfunc, cx_tree_node_create_func cfunc,
606 void *cdata,
void **cnode,
void *root,
607 ptrdiff_t loc_parent,
ptrdiff_t loc_children,
ptrdiff_t loc_last_child,
608 ptrdiff_t loc_prev,
ptrdiff_t loc_next);
609
610
611
612
613
614 typedef struct cx_tree_class_s cx_tree_class;
615
616
617
618
619 struct cx_tree_node_base_s {
620
621
622
623 struct cx_tree_node_base_s *parent;
624
625
626
627 struct cx_tree_node_base_s *children;
628
629
630
631 struct cx_tree_node_base_s *last_child;
632
633
634
635 struct cx_tree_node_base_s *prev;
636
637
638
639 struct cx_tree_node_base_s *next;
640 };
641
642
643
644
645 struct cx_tree_s {
646 CX_COLLECTION_BASE;
647
648
649
650 const cx_tree_class *cl;
651
652
653
654
655
656
657 void *root;
658
659
660
661
662
663
664
665
666
667 cx_tree_node_create_func node_create;
668
669
670
671
672 cx_tree_search_func search;
673
674
675
676
677 cx_tree_search_data_func search_data;
678
679
680
681
682 ptrdiff_t loc_parent;
683
684
685
686
687 ptrdiff_t loc_children;
688
689
690
691
692
693 ptrdiff_t loc_last_child;
694
695
696
697
698 ptrdiff_t loc_prev;
699
700
701
702
703 ptrdiff_t loc_next;
704 };
705
706
707
708
709
710
711
712
713
714 #define CX_TREE_NODE_BASE(type) \
715 type *parent; \
716 type *children;\
717 type *last_child;\
718 type *prev;\
719 type *next
720
721
722
723
724
725
726
727
728
729 #define cx_tree_node_base_layout \
730 offsetof(
struct cx_tree_node_base_s, parent),\
731 offsetof(
struct cx_tree_node_base_s, children),\
732 offsetof(
struct cx_tree_node_base_s, last_child),\
733 offsetof(
struct cx_tree_node_base_s, prev), \
734 offsetof(
struct cx_tree_node_base_s, next)
735
736
737
738
739 struct cx_tree_class_s {
740
741
742
743
744
745
746 int (*insert_element)(
struct cx_tree_s *tree,
const void *data);
747
748
749
750
751
752
753
754 size_t (*insert_many)(
struct cx_tree_s *tree,
struct cx_iterator_base_s *iter,
size_t n);
755
756
757
758
759 void *(*find)(
struct cx_tree_s *tree,
const void *subtree,
const void *data,
size_t depth);
760 };
761
762
763
764
765 typedef struct cx_tree_s CxTree;
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789 cx_attr_nonnull
790 CX_EXPORT void cxTreeDestroySubtree(CxTree *tree,
void *node);
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810 #define cxTreeClear(tree) cxTreeDestroySubtree(tree, tree->root)
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828 CX_EXPORT void cxTreeFree(CxTree *tree);
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 cx_attr_nonnull_arg(
2,
3,
4) cx_attr_nodiscard cx_attr_malloc cx_attr_dealloc(cxTreeFree,
1)
855 CX_EXPORT CxTree *cxTreeCreate(
const CxAllocator *allocator, cx_tree_node_create_func create_func,
856 cx_tree_search_func search_func, cx_tree_search_data_func search_data_func,
857 ptrdiff_t loc_parent,
ptrdiff_t loc_children,
ptrdiff_t loc_last_child,
858 ptrdiff_t loc_prev,
ptrdiff_t loc_next);
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877 #define cxTreeCreateSimple(allocator, create_func, search_func, search_data_func) \
878 cxTreeCreate(allocator, create_func, search_func, search_data_func, cx_tree_node_base_layout)
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902 cx_attr_nonnull_arg(
2) cx_attr_nodiscard cx_attr_malloc cx_attr_dealloc(cxTreeFree,
1)
903 CX_EXPORT CxTree *cxTreeCreateWrapped(
const CxAllocator *allocator,
void *root,
904 ptrdiff_t loc_parent,
ptrdiff_t loc_children,
ptrdiff_t loc_last_child,
905 ptrdiff_t loc_prev,
ptrdiff_t loc_next);
906
907
908
909
910
911
912
913
914
915
916
917
918
919 cx_attr_nonnull
920 CX_EXPORT int cxTreeInsert(CxTree *tree,
const void *data);
921
922
923
924
925
926
927
928
929
930
931
932
933
934 cx_attr_nonnull
935 CX_EXPORT size_t cxTreeInsertIter(CxTree *tree, CxIteratorBase *iter,
size_t n);
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950 cx_attr_nonnull
951 CX_EXPORT size_t cxTreeInsertArray(CxTree *tree,
const void *data,
size_t elem_size,
size_t n);
952
953
954
955
956
957
958
959
960
961
962
963
964 cx_attr_nonnull cx_attr_nodiscard
965 CX_EXPORT void *cxTreeFind(CxTree *tree,
const void *data);
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986 cx_attr_nonnull cx_attr_nodiscard
987 CX_EXPORT void *cxTreeFindInSubtree(CxTree *tree,
const void *data,
void *subtree_root,
size_t max_depth);
988
989
990
991
992
993
994
995
996 cx_attr_nonnull cx_attr_nodiscard
997 CX_EXPORT size_t cxTreeSubtreeSize(CxTree *tree,
void *subtree_root);
998
999
1000
1001
1002
1003
1004
1005
1006 cx_attr_nonnull cx_attr_nodiscard
1007 CX_EXPORT size_t cxTreeSubtreeDepth(CxTree *tree,
void *subtree_root);
1008
1009
1010
1011
1012
1013
1014
1015 cx_attr_nonnull cx_attr_nodiscard
1016 CX_EXPORT size_t cxTreeSize(CxTree *tree);
1017
1018
1019
1020
1021
1022
1023
1024 cx_attr_nonnull cx_attr_nodiscard
1025 CX_EXPORT size_t cxTreeDepth(CxTree *tree);
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039 cx_attr_nonnull cx_attr_nodiscard
1040 CX_EXPORT CxTreeIterator cxTreeIterateSubtree(CxTree *tree,
void *node, bool visit_on_exit);
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052 cx_attr_nonnull cx_attr_nodiscard
1053 CX_EXPORT CxTreeVisitor cxTreeVisitSubtree(CxTree *tree,
void *node);
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064 cx_attr_nonnull cx_attr_nodiscard
1065 CX_EXPORT CxTreeIterator cxTreeIterate(CxTree *tree, bool visit_on_exit);
1066
1067
1068
1069
1070
1071
1072
1073
1074 cx_attr_nonnull cx_attr_nodiscard
1075 CX_EXPORT CxTreeVisitor cxTreeVisit(CxTree *tree);
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088 cx_attr_nonnull
1089 CX_EXPORT void cxTreeSetParent(CxTree *tree,
void *parent,
void *child);
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106 cx_attr_nonnull
1107 CX_EXPORT void cxTreeAddChildNode(CxTree *tree,
void *parent,
void *child);
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126 cx_attr_nonnull
1127 CX_EXPORT int cxTreeAddChild(CxTree *tree,
void *parent,
const void *data);
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141 typedef void (*cx_tree_relink_func)(
1142 void *node,
1143 const void *old_parent,
1144 const void *new_parent
1145 );
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161 cx_attr_nonnull_arg(
1,
2)
1162 CX_EXPORT int cxTreeRemoveNode(CxTree *tree,
void *node, cx_tree_relink_func relink_func);
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175 cx_attr_nonnull
1176 CX_EXPORT void cxTreeRemoveSubtree(CxTree *tree,
void *node);
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196 cx_attr_nonnull_arg(
1,
2)
1197 CX_EXPORT int cxTreeDestroyNode(CxTree *tree,
void *node, cx_tree_relink_func relink_func);
1198
1199 #ifdef __cplusplus
1200 }
1201 #endif
1202
1203 #endif
1204