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 __attribute__((__nonnull__))
213 static inline
void cxTreeIteratorDispose(CxTreeIterator *iter) {
214 free(iter->stack);
215 iter->stack =
NULL;
216 }
217
218
219
220
221
222 __attribute__((__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 __attribute__((__nonnull__))
266 void cx_tree_link(
267 void *restrict parent,
268 void *restrict 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 __attribute__((__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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324 typedef int (*cx_tree_search_data_func)(
const void *node,
const void *data);
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 typedef int (*cx_tree_search_func)(
const void *node,
const void *new_node);
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 __attribute__((__nonnull__))
377 int cx_tree_search_data(
378 const void *root,
379 const void *data,
380 cx_tree_search_data_func sfunc,
381 void **result,
382 ptrdiff_t loc_children,
383 ptrdiff_t loc_next
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 __attribute__((__nonnull__))
410 int cx_tree_search(
411 const void *root,
412 const void *node,
413 cx_tree_search_func sfunc,
414 void **result,
415 ptrdiff_t loc_children,
416 ptrdiff_t loc_next
417 );
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439 CxTreeIterator cx_tree_iterator(
440 void *root,
441 bool visit_on_exit,
442 ptrdiff_t loc_children,
443 ptrdiff_t loc_next
444 );
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464 CxTreeVisitor cx_tree_visitor(
465 void *root,
466 ptrdiff_t loc_children,
467 ptrdiff_t loc_next
468 );
469
470
471
472
473
474
475
476
477
478
479
480 typedef void *(*cx_tree_node_create_func)(
const void *,
void *);
481
482
483
484
485
486
487
488 extern unsigned int cx_tree_add_look_around_depth;
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 __attribute__((__nonnull__(
1,
3,
4,
6,
7)))
529 size_t cx_tree_add_iter(
530 struct cx_iterator_base_s *iter,
531 size_t num,
532 cx_tree_search_func sfunc,
533 cx_tree_node_create_func cfunc,
534 void *cdata,
535 void **failed,
536 void *root,
537 ptrdiff_t loc_parent,
538 ptrdiff_t loc_children,
539 ptrdiff_t loc_last_child,
540 ptrdiff_t loc_prev,
541 ptrdiff_t loc_next
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 __attribute__((__nonnull__(
1,
4,
5,
7,
8)))
582 size_t cx_tree_add_array(
583 const void *src,
584 size_t num,
585 size_t elem_size,
586 cx_tree_search_func sfunc,
587 cx_tree_node_create_func cfunc,
588 void *cdata,
589 void **failed,
590 void *root,
591 ptrdiff_t loc_parent,
592 ptrdiff_t loc_children,
593 ptrdiff_t loc_last_child,
594 ptrdiff_t loc_prev,
595 ptrdiff_t loc_next
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 __attribute__((__nonnull__(
1,
2,
3,
5,
6)))
644 int cx_tree_add(
645 const void *src,
646 cx_tree_search_func sfunc,
647 cx_tree_node_create_func cfunc,
648 void *cdata,
649 void **cnode,
650 void *root,
651 ptrdiff_t loc_parent,
652 ptrdiff_t loc_children,
653 ptrdiff_t loc_last_child,
654 ptrdiff_t loc_prev,
655 ptrdiff_t loc_next
656 );
657
658
659
660
661
662 typedef struct cx_tree_class_s cx_tree_class;
663
664
665
666
667 struct cx_tree_node_base_s {
668
669
670
671 struct cx_tree_node_base_s *parent;
672
673
674
675 struct cx_tree_node_base_s *children;
676
677
678
679 struct cx_tree_node_base_s *last_child;
680
681
682
683 struct cx_tree_node_base_s *prev;
684
685
686
687 struct cx_tree_node_base_s *next;
688 };
689
690
691
692
693 struct cx_tree_s {
694
695
696
697 const cx_tree_class *cl;
698
699
700
701
702 const CxAllocator *allocator;
703
704
705
706
707
708
709 void *root;
710
711
712
713
714
715
716
717
718
719 cx_tree_node_create_func node_create;
720
721
722
723
724 cx_destructor_func simple_destructor;
725
726
727
728
729 cx_destructor_func2 advanced_destructor;
730
731
732
733
734 void *destructor_data;
735
736
737
738
739 cx_tree_search_func search;
740
741
742
743
744 cx_tree_search_data_func search_data;
745
746
747
748
749 size_t size;
750
751
752
753
754 ptrdiff_t loc_parent;
755
756
757
758
759 ptrdiff_t loc_children;
760
761
762
763
764
765 ptrdiff_t loc_last_child;
766
767
768
769
770 ptrdiff_t loc_prev;
771
772
773
774
775 ptrdiff_t loc_next;
776 };
777
778
779
780
781
782 #define CX_TREE_NODE_BASE(type) \
783 type *parent; \
784 type *children;\
785 type *last_child;\
786 type *prev;\
787 type *next
788
789
790
791
792 #define cx_tree_node_base_layout \
793 offsetof(
struct cx_tree_node_base_s, parent),\
794 offsetof(
struct cx_tree_node_base_s, children),\
795 offsetof(
struct cx_tree_node_base_s, last_child),\
796 offsetof(
struct cx_tree_node_base_s, prev), \
797 offsetof(
struct cx_tree_node_base_s, next)
798
799
800
801
802 #define cx_tree_node_layout(tree) \
803 (tree)->loc_parent,\
804 (tree)->loc_children,\
805 (tree)->loc_last_child,\
806 (tree)->loc_prev, \
807 (tree)->loc_next
808
809
810
811
812 struct cx_tree_class_s {
813
814
815
816
817
818
819 void (*destructor)(
struct cx_tree_s *);
820
821
822
823
824
825
826
827 int (*insert_element)(
828 struct cx_tree_s *tree,
829 const void *data
830 );
831
832
833
834
835
836
837
838 size_t (*insert_many)(
839 struct cx_tree_s *tree,
840 struct cx_iterator_base_s *iter,
841 size_t n
842 );
843
844
845
846
847 void *(*find)(
848 struct cx_tree_s *tree,
849 const void *subtree,
850 const void *data
851 );
852
853
854
855
856 CxTreeIterator (*iterator)(
857 struct cx_tree_s *tree,
858 bool visit_on_exit
859 );
860
861
862
863
864 CxTreeVisitor (*visitor)(
struct cx_tree_s *tree);
865 };
866
867
868
869
870 typedef struct cx_tree_s CxTree;
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 __attribute__((__nonnull__, __warn_unused_result__))
896 CxTree *cxTreeCreate(
897 const CxAllocator *allocator,
898 cx_tree_node_create_func create_func,
899 cx_tree_search_func search_func,
900 cx_tree_search_data_func search_data_func,
901 ptrdiff_t loc_parent,
902 ptrdiff_t loc_children,
903 ptrdiff_t loc_last_child,
904 ptrdiff_t loc_prev,
905 ptrdiff_t loc_next
906 );
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925 __attribute__((__nonnull__, __warn_unused_result__))
926 static inline CxTree *cxTreeCreateSimple(
927 const CxAllocator *allocator,
928 cx_tree_node_create_func create_func,
929 cx_tree_search_func search_func,
930 cx_tree_search_data_func search_data_func
931 ) {
932 return cxTreeCreate(
933 allocator,
934 create_func,
935 search_func,
936 search_data_func,
937 cx_tree_node_base_layout
938 );
939 }
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961 __attribute__((__nonnull__, __warn_unused_result__))
962 CxTree *cxTreeCreateWrapped(
963 const CxAllocator *allocator,
964 void *root,
965 ptrdiff_t loc_parent,
966 ptrdiff_t loc_children,
967 ptrdiff_t loc_last_child,
968 ptrdiff_t loc_prev,
969 ptrdiff_t loc_next
970 );
971
972
973
974
975
976
977
978
979
980
981
982 __attribute__((__nonnull__))
983 static inline
void cxTreeDestroy(CxTree *tree) {
984 tree->cl->destructor(tree);
985 }
986
987
988
989
990
991
992
993
994
995
996
997
998 __attribute__((__nonnull__))
999 static inline
int cxTreeInsert(
1000 CxTree *tree,
1001 const void *data
1002 ) {
1003 return tree->cl->insert_element(tree, data);
1004 }
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018 __attribute__((__nonnull__))
1019 static inline
size_t cxTreeInsertIter(
1020 CxTree *tree,
1021 struct cx_iterator_base_s *iter,
1022 size_t n
1023 ) {
1024 return tree->cl->insert_many(tree, iter, n);
1025 }
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040 __attribute__((__nonnull__))
1041 static inline
size_t cxTreeInsertArray(
1042 CxTree *tree,
1043 const void *data,
1044 size_t elem_size,
1045 size_t n
1046 ) {
1047 if (n ==
0)
return 0;
1048 if (n ==
1)
return 0 == cxTreeInsert(tree, data) ?
1 :
0;
1049 CxIterator iter = cxIterator(data, elem_size, n);
1050 return cxTreeInsertIter(tree, cxIteratorRef(iter), n);
1051 }
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064 __attribute__((__nonnull__))
1065 static inline
void *cxTreeFind(
1066 CxTree *tree,
1067 const void *data
1068 ) {
1069 return tree->cl->find(tree, tree->root, data);
1070 }
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087 __attribute__((__nonnull__))
1088 static inline
void *cxTreeFindInSubtree(
1089 CxTree *tree,
1090 const void *data,
1091 void *subtree_root
1092 ) {
1093 return tree->cl->find(tree, subtree_root, data);
1094 }
1095
1096
1097
1098
1099
1100
1101
1102
1103 __attribute__((__nonnull__))
1104 size_t cxTreeSubtreeSize(CxTree *tree,
void *subtree_root);
1105
1106
1107
1108
1109
1110
1111
1112
1113 __attribute__((__nonnull__))
1114 size_t cxTreeSubtreeDepth(CxTree *tree,
void *subtree_root);
1115
1116
1117
1118
1119
1120
1121
1122 __attribute__((__nonnull__))
1123 size_t cxTreeDepth(CxTree *tree);
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134 __attribute__((__nonnull__, __warn_unused_result__))
1135 static inline CxTreeIterator cxTreeIterator(
1136 CxTree *tree,
1137 bool visit_on_exit
1138 ) {
1139 return tree->cl->iterator(tree, visit_on_exit);
1140 }
1141
1142
1143
1144
1145
1146
1147
1148
1149 __attribute__((__nonnull__, __warn_unused_result__))
1150 static inline CxTreeVisitor cxTreeVisitor(CxTree *tree) {
1151 return tree->cl->visitor(tree);
1152 }
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165 __attribute__((__nonnull__))
1166 static inline
void cxTreeAddChildNode(
1167 CxTree *tree,
1168 void *parent,
1169 void *child) {
1170 cx_tree_link(parent, child, cx_tree_node_layout(tree));
1171 tree->size++;
1172 }
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191 __attribute__((__nonnull__))
1192 int cxTreeAddChild(
1193 CxTree *tree,
1194 void *parent,
1195 const void *data
1196 );
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209 typedef void (*cx_tree_relink_func)(
1210 void *node,
1211 const void *old_parent,
1212 const void *new_parent
1213 );
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229 __attribute__((__nonnull__(
1,
2)))
1230 int cxTreeRemove(
1231 CxTree *tree,
1232 void *node,
1233 cx_tree_relink_func relink_func
1234 );
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247 __attribute__((__nonnull__))
1248 void cxTreeRemoveSubtree(CxTree *tree,
void *node);
1249
1250 #ifdef __cplusplus
1251 }
1252 #endif
1253
1254 #endif
1255