pike.git / src / post_modules / CritBit / tree_low.c

version» Context lines:

pike.git/src/post_modules/CritBit/tree_low.c:34:   # define CB_SOURCE   #endif      static inline void cb_zap_node(const struct cb_tree*, cb_node_t);   static inline cb_node_t cb_node_from_string(const struct cb_tree*,    const cb_key, const cb_value *);         static inline cb_key CB_KEY_FROM_STRING(const cb_string string) {    cb_key key; -  /* printf("key from string: %p (%d, %d)\n", -  string, key.len.chars, key.len.bits); */ +     key.str = string;    key.len.chars = CB_LENGTH(string);    key.len.bits = 0; -  +  /* fprintf(stderr, "key from string: %p (%ld, %ld)\n", +  string, key.len.chars, key.len.bits); */    return key;   }      static inline cb_size cb_prefix_count_fallback(const cb_string s1,    const cb_string s2,    const cb_size len,    cb_size start) {    ptrdiff_t i;    unsigned INT32 width = MAX(CB_WIDTH(s1), CB_WIDTH(s2));   
pike.git/src/post_modules/CritBit/tree_low.c:89:   static inline cb_node_t node_init() {    cb_node_t tree;       tree = CB_NODE_ALLOC();    memset(tree, 0, sizeof(cb_node));    CB_INIT_VALUE(tree);       return tree;   }    - CB_STATIC CB_INLINE void cb_get_range(const struct cb_tree * src, + CB_STATIC inline void cb_get_range(const struct cb_tree * src,    struct cb_tree * dst,    const cb_key a, const cb_key b) {    const cb_node_t tree = src->root;    cb_node_t node = cb_index(tree, a);    cb_node_t end = cb_index(tree, b);    /* printf("start: %p, stop: %p in line %d\n", node, end, __LINE__); */    if (!node) node = cb_find_next(tree, a);    /* printf("start: %p, stop: %p in line %d\n", node, end, __LINE__); */    if (node) {   
pike.git/src/post_modules/CritBit/tree_low.c:137:    /* printf("adding %p\n", _); */    cb_insert(dst, _->key, &_->value);    if (_ == end) break;    }    });       /* printf("new range has %d members.\n", dst->root->size); */    }   }    - static inline cb_node_t cb_node_from_string(const struct cb_tree * tree, + static inline cb_node_t cb_node_from_string(const struct cb_tree * UNUSED(tree),    const cb_key s,    const cb_value * val) {    cb_node_t node = node_init();    CB_SET_KEY(node, s);    node->size = 1;    CB_SET_VALUE(node, val);      #ifdef DEBUG_CHECKS    if (!CB_HAS_VALUE(node))    printf("culprit here. %d\n", __LINE__);   #endif       return node;   }    - static inline cb_node_t cb_clone_node(const struct cb_tree * tree, + static inline cb_node_t cb_clone_node(const struct cb_tree * UNUSED(tree),    const cb_node_t node) {    cb_node_t nnode = CB_NODE_ALLOC();       memcpy(nnode, node, sizeof(cb_node));    CB_ADD_KEY_REF(node->key);    CB_INIT_VALUE(node);    CB_SET_CHILD(nnode, 0, CB_CHILD(nnode, 0));    CB_SET_CHILD(nnode, 1, CB_CHILD(nnode, 1));    CB_SET_CHILD(node, 0, NULL);    CB_SET_CHILD(node, 1, NULL);       return nnode;   }    - CB_STATIC CB_INLINE void cb_copy_tree(struct cb_tree * dst, + CB_STATIC inline void cb_copy_tree(struct cb_tree * dst,    cb_node_t from) {       cb_node_t parent;    cb_node_t node = from;       if (!from) return;       parent = from->parent;    from->parent = NULL;   
pike.git/src/post_modules/CritBit/tree_low.c:205:    cb_free_node(tree, CB_CHILD(node, 0));    CB_SET_CHILD(node, 0, NULL);    }    if (CB_HAS_CHILD(node, 1)) {    cb_free_node(tree, CB_CHILD(node, 1));    CB_SET_CHILD(node, 1, NULL);    }    cb_zap_node(tree, node);   }    - static inline void cb_zap_node(const struct cb_tree * tree, cb_node_t node) { + static inline void cb_zap_node(const struct cb_tree * UNUSED(tree), +  cb_node_t node) {    CB_FREE_KEY(node->key);    CB_RM_VALUE(node);    CB_NODE_FREE(node);   }    - CB_STATIC CB_INLINE cb_node_t cb_find_first(cb_node_t tree) { + CB_STATIC inline cb_node_t cb_find_first(cb_node_t tree) {    while (tree && !CB_HAS_VALUE(tree)) { tree = CB_CHILD(tree, 0); };       return tree;   }    - CB_STATIC CB_INLINE cb_node_t cb_find_last(cb_node_t tree) { + CB_STATIC inline cb_node_t cb_find_last(cb_node_t tree) {    while (1) {    if (CB_HAS_CHILD(tree, 1)) tree = CB_CHILD(tree, 1);    else if (CB_HAS_CHILD(tree, 0)) tree = CB_CHILD(tree, 0);    else break;    }    return tree;   }    - CB_STATIC CB_INLINE size_t cb_get_depth(cb_node_t node) { + CB_STATIC inline size_t cb_get_depth(cb_node_t node) {    size_t a = 0, b = 0, len = 1;       if (CB_HAS_CHILD(node, 0)) {    a = cb_get_depth(CB_CHILD(node, 0));    }       if (CB_HAS_CHILD(node, 1)) {    b = cb_get_depth(CB_CHILD(node, 1));    }       return len + MAX(b, a);   }    - CB_STATIC CB_INLINE cb_node_t cb_subtree_prefix(cb_node_t node, cb_key key) { + CB_STATIC inline cb_node_t cb_subtree_prefix(cb_node_t node, cb_key key) {    cb_size start = {0,0};       do {    unsigned INT32 bit;    start = cb_prefix_count(node->key.str, key.str,    CB_MIN(node->key.len, key.len), start);       if (CB_S_EQ(start, key.len)) { /* key is substring */    return node;    }       bit = CB_GET_BIT(key.str, start);    node = CB_CHILD(node, bit);    } while (node);       return NULL;   }    - CB_STATIC CB_INLINE void cb_delete(struct cb_tree * tree, + CB_STATIC inline void cb_delete(struct cb_tree * tree,    const cb_key key, cb_value * val) {    cb_node_t node = cb_index(tree->root, key);       if (node && CB_HAS_VALUE(node)) {    unsigned INT32 bit;    cb_node_t t;    if (val) CB_GET_VALUE(node, val);       CB_RM_VALUE(node);    node->size--;
pike.git/src/post_modules/CritBit/tree_low.c:323:    tree->root = NULL;    return;    }    }       }       cb_check_node(tree->root);   }    - CB_STATIC CB_INLINE cb_node_t cb_index(const cb_node_t tree, const cb_key key) { + CB_STATIC inline cb_node_t cb_index(const cb_node_t tree, const cb_key key) {    cb_node_t node = tree;    if (tree) cb_check_node(tree);       while (node) {    if (CB_LT(node->key.len, key.len)) {    unsigned INT32 bit = CB_GET_BIT(key.str, node->key.len);       if (CB_HAS_CHILD(node, bit)) {    node = CB_CHILD(node, bit);    continue;
pike.git/src/post_modules/CritBit/tree_low.c:349:    return node;    }       break;    }       if (tree) cb_check_node(tree);    return NULL;   }    - CB_STATIC CB_INLINE cb_node_t cb_find_next(const cb_node_t tree, + CB_STATIC inline cb_node_t cb_find_next(const cb_node_t tree,    const cb_key key) {    cb_size size;    size_t bit;    cb_node_t node;    size.bits = size.chars = 0;       /* index is cheap. also in many cases its quite likely that we */    /* hit. */    node = cb_index(tree, key);   
pike.git/src/post_modules/CritBit/tree_low.c:400:       bit = CB_GET_BIT(key.str, size);       /* printf("bit is %u\n", bit); */       if (CB_S_EQ(size, node->key.len)) { /* node is substring */    if (CB_HAS_CHILD(node, bit)) {    node = CB_CHILD(node, bit);    continue;    } -  if (!bit && CB_HAS_CHILD(node, 1)) { +  +  /* this is not very elegant */ +  if (bit && CB_HAS_CHILD(node, 0)) +  node = cb_find_last(CB_CHILD(node, 0)); +  +  /* just find the next node */    WALK_FORWARD(node, {    if (CB_HAS_VALUE(_)) return _;    }); -  } +        return NULL;    }       if (!bit) break;       WALK_UP(node, bit, {    if (!bit && CB_HAS_CHILD(_, 1)) {    _ = CB_CHILD(_, 1);    break;
pike.git/src/post_modules/CritBit/tree_low.c:428:    break;    }       if (node && !CB_HAS_VALUE(node))    WALK_FORWARD(node, {    if (CB_HAS_VALUE(_)) break;    });    return node;   }    - CB_STATIC CB_INLINE cb_node_t cb_find_ne(const cb_node_t tree, const cb_key key) { + CB_STATIC inline cb_node_t cb_find_ne(const cb_node_t tree, const cb_key key) {    cb_node_t ne = cb_index(tree, key);    if (!ne) ne = cb_find_next(tree, key);    return ne;   }    - CB_STATIC CB_INLINE cb_node_t cb_get_nth(const cb_node_t tree, size_t n) { + CB_STATIC inline cb_node_t cb_get_nth(const cb_node_t tree, size_t n) {    cb_node_t node = tree;    size_t ln;       while (node) {    if (n >= node->size) return NULL;       if (n == 0) return cb_find_first(node);    else if (n == node->size - 1) return cb_find_last(node);       if (CB_HAS_VALUE(node)) n--;
pike.git/src/post_modules/CritBit/tree_low.c:461:    }    n -= ln;    }       node = CB_CHILD(node, 1);    }       return NULL;   }    - CB_STATIC CB_INLINE cb_node_t cb_find_previous(const cb_node_t tree, + CB_STATIC inline cb_node_t cb_find_previous(const cb_node_t tree,    const cb_key key) {    cb_node_t node = cb_index(tree, key);    if (!node) node = cb_find_next(tree, key);    if (!node) return cb_find_last(tree);    if (node) WALK_BACKWARD(node, {    if (CB_HAS_VALUE(_)) break;    });    return node;   }    - CB_STATIC CB_INLINE cb_node_t cb_find_le(const cb_node_t tree, const cb_key key) { + CB_STATIC inline cb_node_t cb_find_le(const cb_node_t tree, const cb_key key) {    cb_node_t ne = cb_index(tree, key);    if (!ne) ne = cb_find_previous(tree, key);    return ne;   }      static inline int cb_low_insert(struct cb_tree * tree,    const cb_key key, const cb_value *val) {    cb_node_t node = tree->root;    cb_size size;    size.bits = 0;
pike.git/src/post_modules/CritBit/tree_low.c:559:    node->key.len = size;    bit = CB_GET_BIT(key.str, size);    CB_SET_CHILD(node, bit, cb_node_from_string(tree, key, val));    CB_SET_CHILD(node, !bit, new);    CB_RM_VALUE(node); /* do not free here, clone does take ref */       return 1;    }   }    - CB_STATIC CB_INLINE void cb_insert(struct cb_tree * tree, + CB_STATIC inline void cb_insert(struct cb_tree * tree,    const cb_key key,    const cb_value *val) {    if (!tree->root) {    tree->root = cb_node_from_string(tree, key, val);    return;    }       cb_check_node(tree->root);       cb_low_insert(tree, key, val);       cb_check_node(tree->root);   }