src/tree.c

changeset 426
9aa38cd4c992
parent 425
a75b808d653b
child 431
dcf01bb852f4
     1.1 --- a/src/tree.c	Sun Sep 26 14:21:20 2021 +0200
     1.2 +++ b/src/tree.c	Sun Sep 26 14:41:49 2021 +0200
     1.3 @@ -28,9 +28,33 @@
     1.4  
     1.5  #include "cx/tree.h"
     1.6  
     1.7 +#define CX_TR_PTR(cur, off) ((void**)(((char*)cur)+off))
     1.8 +
     1.9 +void* cx_tree_last(void *node, ptrdiff_t loc_next) {
    1.10 +    void *last;
    1.11 +    do {
    1.12 +        last = node;
    1.13 +    } while ((node = *CX_TR_PTR(node, loc_next)) != NULL);
    1.14 +    return last;
    1.15 +}
    1.16  
    1.17  int cx_tree_add_node(void *node, ptrdiff_t loc_parent, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
    1.18 -    return 1;
    1.19 +    void *last = cx_tree_last(node, loc_next);
    1.20 +    if(!last)
    1.21 +        return 1;
    1.22 +    
    1.23 +    // next pointer must be present
    1.24 +    *CX_TR_PTR(last, loc_next) = new_node;
    1.25 +    
    1.26 +    // optional fields
    1.27 +    if(loc_parent >= 0) {
    1.28 +        *CX_TR_PTR(new_node, loc_parent) = *CX_TR_PTR(node, loc_parent);
    1.29 +    }
    1.30 +    if(loc_prev >= 0) {
    1.31 +        *CX_TR_PTR(new_node, loc_prev) = last;
    1.32 +    }
    1.33 +    
    1.34 +    return 0;
    1.35  }
    1.36  
    1.37  int cx_tree_add_child_node(

mercurial