4 #define bst_declare(name, type, idx_type) struct { \
13 #define bst_init(name) do { \
17 #define bst_node_type(name) typeof(*((name).head))
19 #define bst_insert(name, _elem, _idx) do { \
20 bst_node_type(name) **cur = NULL; \
22 for (cur = &((name).head); ;) { \
23 assert(cur != NULL); \
26 (*cur) = malloc(sizeof(bst_node_type(name))); \
27 assert(*cur != NULL); \
29 (*cur)->elem = _elem; \
30 (*cur)->left = NULL; \
31 (*cur)->right = NULL; \
34 assert((*cur)->idx != _idx); \
36 if (_idx < (*cur)->idx) { \
37 cur = (bst_node_type(name) **)&((*cur)->left); \
39 cur = (bst_node_type(name) **)&((*cur)->right); \
45 #define bst_find(name, _idx, _elemp) do { \
46 bst_node_type(name) *cur = NULL; \
48 assert((_elemp) != NULL); \
50 for (cur = (name).head; cur != NULL; ) { \
51 if (cur->idx == _idx) { \
55 if (_idx < cur->idx) { \
56 cur = (bst_node_type(name) *)(cur->left); \
58 cur = (bst_node_type(name) *)(cur->right); \
63 *(_elemp) = ((cur)->elem); \
74 cur = &((bst_node_type(name)((*cur)->right))); \