Example #1
0
/*
 * Iterative bottom-up (postorder) traversal utility,
 * which allows the callback to destroy the node.
 */
static void safe_traverse(dict_t *dict, void (*func)(dnode_t *, void *))
{
    dnode_t *nil = dict_nil(dict), *current = dict_root(dict);
    enum { from_parent, from_left, from_right } came_from = from_parent;

    while (current != nil) {
        dnode_t *next = nil; /* Initialized to shut up gcc warning. */

        switch (came_from) {
        case from_parent:
            if (current->left != nil) {
                next = current->left;
            } else 
        case from_left:
            if (current->right != nil) {
                came_from = from_parent;
                next = current->right;
            } else 
        case from_right:
            {
                came_from = (current == current->parent->left) 
                            ? from_left : from_right;
                next = current->parent;
                func(current, dict->context);
            }
            break;
        }

        current = next;
    }
}
Example #2
0
dnode_t *dict_upper_bound(dict_t *dict, const void *key)
{
    dnode_t *root = dict_root(dict);
    dnode_t *nil = dict_nil(dict);
    dnode_t *tentative = 0;

    while (root != nil) {
        int result = dict->compare(key, root->key);

        if (result < 0) {
            root = root->left;
        } else if (result > 0) {
            tentative = root;
            root = root->right;
        } else {
            if (!dict->dupes) {
                return root;
            } else {
                tentative = root;
                root = root->right;
            }
        }
    }

    return tentative;
}
Example #3
0
dnode_t *dict_lookup(dict_t *dict, const void *key)
{
    dnode_t *root = dict_root(dict);
    dnode_t *nil = dict_nil(dict);
    dnode_t *saved;
    int result;

    /* simple binary search adapted for trees that contain duplicate keys */

    while (root != nil) {
        result = dict->compare(key, root->key);
        if (result < 0)
            root = root->left;
        else if (result > 0)
            root = root->right;
        else {
            if (!dict->dupes) { /* no duplicates, return match          */
                return root;
            } else {            /* could be dupes, find leftmost one    */
                do {
                    saved = root;
                    root = root->left;
                    while (root != nil && dict->compare(key, root->key))
                        root = root->right;
                } while (root != nil);
                return saved;
            }
        }
    }

    return NULL;
}
Example #4
0
dnode_t *dict_lower_bound(dict_t *dict, const void *key)
{
    dnode_t *root = dict_root(dict);
    dnode_t *nil = dict_nil(dict);
    dnode_t *tentative = 0;

    while (root != nil) {
        int result = dict->compare(key, root->key, dict->context);

        if (result > 0) {
            root = root->right;
        } else if (result < 0) {
            tentative = root;
            root = root->left;
        } else {
#ifdef NO_FC_SOLVE
            if (!dict->dupes) {
                return root;
            } else {
                tentative = root;
                root = root->left;
            }
#else
            return root;
#endif
        }
    }

    return tentative;
}
Example #5
0
dnode_t *dict_first(dict_t *dict)
{
    dnode_t *nil = dict_nil(dict), *root = dict_root(dict), *left;

    if (root != nil)
        while ((left = root->left) != nil)
            root = left;

    return (root == nil) ? NULL : root;
}
Example #6
0
dnode_t *dict_last(dict_t *dict)
{
    dnode_t *nil = dict_nil(dict), *root = dict_root(dict), *right;

    if (root != nil)
        while ((right = root->right) != nil)
            root = right;

    return (root == nil) ? NULL : root;
}
Example #7
0
/*
 * Look for the node corresponding to the lowest key that
 * is greater than the given key.
 */
dnode_t *dict_strict_lower_bound(dict_t *dict, const void *key)
{
    dnode_t *root = dict_root(dict);
    dnode_t *nil = dict_nil(dict);
    dnode_t *tentative = 0;

    while (root != nil) {
        int result = dict->compare(key, root->key);

        if (result >= 0) {
            root = root->right;
        } else {
            tentative = root;
            root = root->left;
        }
    }

    return tentative;
}
Example #8
0
dnode_t *dict_prev(dict_t *dict, dnode_t *curr)
{
    dnode_t *nil = dict_nil(dict), *parent, *right;

    if (curr->left != nil) {
        curr = curr->left;
        while ((right = curr->right) != nil)
            curr = right;
        return curr;
    }

    parent = curr->parent;

    while (parent != nil && curr == parent->left) {
        curr = parent;
        parent = curr->parent;
    }

    return (parent == nil) ? NULL : parent;
}
Example #9
0
dnode_t * fc_solve_kaz_tree_next(dict_t *dict, dnode_t *curr)
{
    dnode_t *nil = dict_nil(dict), *parent, *left;

    if (curr->right != nil) {
        curr = curr->right;
        while ((left = curr->left) != nil)
            curr = left;
        return curr;
    }

    parent = curr->parent;

    while (parent != nil && curr == parent->right) {
        curr = parent;
        parent = curr->parent;
    }

    return (parent == nil) ? NULL : parent;
}
Example #10
0
dnode_t *dict_lookup(dict_t *dict, const void *key)
{
    dnode_t *root = dict_root(dict);
    dnode_t *nil = dict_nil(dict);
    int result;

    /* simple binary search adapted for trees that contain duplicate keys */

    while (root != nil) {
        result = dict->compare(key, root->key);
        if (result < 0)
            root = root->left;
        else if (result > 0)
            root = root->right;
        else {
            return root;
        }
    }

    return NULL;
}
Example #11
0
int dict_verify(dict_t *dict)
{
    dnode_t *nil = dict_nil(dict), *root = dict_root(dict);

    /* check that the sentinel node and root node are black */
    if (root->color != dnode_black)
        return 0;
    if (nil->color != dnode_black)
        return 0;
    if (nil->right != nil)
        return 0;
    /* nil->left is the root node; check that its parent pointer is nil */
    if (nil->left->parent != nil)
        return 0;
    /* perform a weak test that the tree is a binary search tree */
    if (!verify_bintree(dict))
        return 0;
    /* verify that the tree is a red-black tree */
    if (!verify_redblack(nil, root))
        return 0;
    if (verify_node_count(nil, root) != dict_count(dict))
        return 0;
    return 1;
}
Example #12
0
const void * fc_solve_kaz_tree_insert(dict_t *dict, dnode_t *node, const void *key)
{
    dnode_t *where = dict_root(dict), *nil = dict_nil(dict);
    dnode_t *parent = nil, *uncle, *grandpa;
    int result = -1;

    node->key = key;

    assert (!dict_isfull(dict));
    assert (!dict_contains(dict, node));
    assert (!dnode_is_in_a_dict(node));

    /* basic binary tree insert */

    while (where != nil) {
        parent = where;
        result = dict->compare(key, where->key, dict->context);

        /* We are remming it out because instead of duplicating the key
         * we return the existing key. -- Shlomi Fish, fc-solve.
         * */
#if 0
        /* trap attempts at duplicate key insertion unless it's explicitly allowed */
        assert (dict->dupes || result != 0);
#endif
        if (result == 0)
        {
            return where->dict_key;
        }
        else if (result < 0)
        {
            where = where->left;
        }
        else
        {
            where = where->right;
        }
    }

    assert (where == nil);

    if (result < 0)
        parent->left = node;
    else
        parent->right = node;

    node->parent = parent;
    node->left = nil;
    node->right = nil;

#ifdef NO_FC_SOLVE
    dict->nodecount++;
#endif

    /* red black adjustments */

    node->color = dnode_red;

    while (parent->color == dnode_red) {
        grandpa = parent->parent;
        if (parent == grandpa->left) {
            uncle = grandpa->right;
            if (uncle->color == dnode_red) {    /* red parent, red uncle */
                parent->color = dnode_black;
                uncle->color = dnode_black;
                grandpa->color = dnode_red;
                node = grandpa;
                parent = grandpa->parent;
            } else {                            /* red parent, black uncle */
                if (node == parent->right) {
                    rotate_left(parent);
                    parent = node;
                    assert (grandpa == parent->parent);
                    /* rotation between parent and child preserves grandpa */
                }
                parent->color = dnode_black;
                grandpa->color = dnode_red;
                rotate_right(grandpa);
                break;
            }
        } else {        /* symmetric cases: parent == parent->parent->right */
            uncle = grandpa->left;
            if (uncle->color == dnode_red) {
                parent->color = dnode_black;
                uncle->color = dnode_black;
                grandpa->color = dnode_red;
                node = grandpa;
                parent = grandpa->parent;
            } else {
                if (node == parent->left) {
                    rotate_right(parent);
                    parent = node;
                    assert (grandpa == parent->parent);
                }
                parent->color = dnode_black;
                grandpa->color = dnode_red;
                rotate_left(grandpa);
                break;
            }
        }
    }

    dict_root(dict)->color = dnode_black;

    assert (dict_verify(dict));

    return NULL;
}
Example #13
0
    }

    dict_root(dict)->color = dnode_black;

    assert (dict_verify(dict));
}

/*
 * Delete the given node from the dictionary. If the given node does not belong
 * to the given dictionary, undefined behavior results.  A pointer to the
 * deleted node is returned.
 */

dnode_t *dict_delete(dict_t *dict, dnode_t *delete)
{
    dnode_t *nil = dict_nil(dict), *child, *delparent = delete->parent;

    /* basic deletion */

    assert (dict_contains(dict, delete));

    /*
     * If the node being deleted has two children, then we replace it with its
     * successor (i.e. the leftmost node in the right subtree.) By doing this,
     * we avoid the traditional algorithm under which the successor's key and
     * value *only* move to the deleted node and the successor is spliced out
     * from the tree. We cannot use this approach because the user may hold
     * pointers to the successor, or nodes may be inextricably tied to some
     * other structures by way of embedding, etc. So we must splice out the
     * node we are given, not some other node, and must not move contents from
     * one node to another behind the user's back.
Example #14
0
dnode_t *dict_delete(dict_t *dict, dnode_t *target)
{
    dnode_t *nil = dict_nil(dict), *child, *delparent = target->parent;

    /* basic deletion */

    assert (!dict_isempty(dict));
    assert (dict_contains(dict, target));

    /*
     * If the node being deleted has two children, then we replace it with its
     * successor (i.e. the leftmost node in the right subtree.) By doing this,
     * we avoid the traditional algorithm under which the successor's key and
     * value *only* move to the deleted node and the successor is spliced out
     * from the tree. We cannot use this approach because the user may hold
     * pointers to the successor, or nodes may be inextricably tied to some
     * other structures by way of embedding, etc. So we must splice out the
     * node we are given, not some other node, and must not move contents from
     * one node to another behind the user's back.
     */

    if (target->left != nil && target->right != nil) {
        dnode_t *next = dict_next(dict, target);
        dnode_t *nextparent = next->parent;
        dnode_color_t nextcolor = next->color;

        assert (next != nil);
        assert (next->parent != nil);
        assert (next->left == nil);

        /*
         * First, splice out the successor from the tree completely, by
         * moving up its right child into its place.
         */

        child = next->right;
        child->parent = nextparent;

        if (nextparent->left == next) {
            nextparent->left = child;
        } else {
            assert (nextparent->right == next);
            nextparent->right = child;
        }

        /*
         * Now that the successor has been extricated from the tree, install it
         * in place of the node that we want deleted.
         */

        next->parent = delparent;
        next->left = target->left;
        next->right = target->right;
        next->left->parent = next;
        next->right->parent = next;
        next->color = target->color;
        target->color = nextcolor;

        if (delparent->left == target) {
            delparent->left = next;
        } else {
            assert (delparent->right == target);
            delparent->right = next;
        }

    } else {
        assert (target != nil);
        assert (target->left == nil || target->right == nil);

        child = (target->left != nil) ? target->left : target->right;

        child->parent = delparent = target->parent;

        if (target == delparent->left) {
            delparent->left = child;
        } else {
            assert (target == delparent->right);
            delparent->right = child;
        }
    }

    target->parent = NULL;
    target->right = NULL;
    target->left = NULL;

    dict->nodecount--;

    assert (verify_bintree(dict));

    /* red-black adjustments */

    if (target->color == dnode_black) {
        dnode_t *parent, *sister;

        dict_root(dict)->color = dnode_red;

        while (child->color == dnode_black) {
            parent = child->parent;
            if (child == parent->left) {
                sister = parent->right;
                assert (sister != nil);
                if (sister->color == dnode_red) {
                    sister->color = dnode_black;
                    parent->color = dnode_red;
                    rotate_left(parent);
                    sister = parent->right;
                    assert (sister != nil);
                }
                if (sister->left->color == dnode_black
                        && sister->right->color == dnode_black) {
                    sister->color = dnode_red;
                    child = parent;
                } else {
                    if (sister->right->color == dnode_black) {
                        assert (sister->left->color == dnode_red);
                        sister->left->color = dnode_black;
                        sister->color = dnode_red;
                        rotate_right(sister);
                        sister = parent->right;
                        assert (sister != nil);
                    }
                    sister->color = parent->color;
                    sister->right->color = dnode_black;
                    parent->color = dnode_black;
                    rotate_left(parent);
                    break;
                }
            } else {    /* symmetric case: child == child->parent->right */
                assert (child == parent->right);
                sister = parent->left;
                assert (sister != nil);
                if (sister->color == dnode_red) {
                    sister->color = dnode_black;
                    parent->color = dnode_red;
                    rotate_right(parent);
                    sister = parent->left;
                    assert (sister != nil);
                }
                if (sister->right->color == dnode_black
                        && sister->left->color == dnode_black) {
                    sister->color = dnode_red;
                    child = parent;
                } else {
                    if (sister->left->color == dnode_black) {
                        assert (sister->right->color == dnode_red);
                        sister->right->color = dnode_black;
                        sister->color = dnode_red;
                        rotate_left(sister);
                        sister = parent->left;
                        assert (sister != nil);
                    }
                    sister->color = parent->color;
                    sister->left->color = dnode_black;
                    parent->color = dnode_black;
                    rotate_right(parent);
                    break;
                }
            }
        }

        child->color = dnode_black;
        dict_root(dict)->color = dnode_black;
    }

    assert (dict_verify(dict));

    return target;
}
Example #15
0
void dict_insert(dict_t *dict, dnode_t *node, const void *key)
{
    dnode_t *where = dict_root(dict), *nil = dict_nil(dict);
    dnode_t *parent = nil, *uncle, *grandpa;
    int result = -1;

    node->key = key;

    assert (!dict_isfull(dict));
    assert (!dict_contains(dict, node));
    assert (!dnode_is_in_a_dict(node));

    /* basic binary tree insert */

    while (where != nil) {
        parent = where;
        result = dict->compare(key, where->key);
        /* trap attempts at duplicate key insertion unless it's explicitly allowed */
        assert (dict->dupes || result != 0);
        if (result < 0)
            where = where->left;
        else
            where = where->right;
    }

    assert (where == nil);

    if (result < 0)
        parent->left = node;
    else
        parent->right = node;

    node->parent = parent;
    node->left = nil;
    node->right = nil;

    dict->nodecount++;

    /* red black adjustments */

    node->color = dnode_red;

    while (parent->color == dnode_red) {
        grandpa = parent->parent;
        if (parent == grandpa->left) {
            uncle = grandpa->right;
            if (uncle->color == dnode_red) {    /* red parent, red uncle */
                parent->color = dnode_black;
                uncle->color = dnode_black;
                grandpa->color = dnode_red;
                node = grandpa;
                parent = grandpa->parent;
            } else {                            /* red parent, black uncle */
                if (node == parent->right) {
                    rotate_left(parent);
                    parent = node;
                    assert (grandpa == parent->parent);
                    /* rotation between parent and child preserves grandpa */
                }
                parent->color = dnode_black;
                grandpa->color = dnode_red;
                rotate_right(grandpa);
                break;
            }
        } else {        /* symmetric cases: parent == parent->parent->right */
            uncle = grandpa->left;
            if (uncle->color == dnode_red) {
                parent->color = dnode_black;
                uncle->color = dnode_black;
                grandpa->color = dnode_red;
                node = grandpa;
                parent = grandpa->parent;
            } else {
                if (node == parent->left) {
                    rotate_right(parent);
                    parent = node;
                    assert (grandpa == parent->parent);
                }
                parent->color = dnode_black;
                grandpa->color = dnode_red;
                rotate_left(grandpa);
                break;
            }
        }
    }

    dict_root(dict)->color = dnode_black;

    assert (dict_verify(dict));
}
Example #16
0
int dict_contains(dict_t *dict, dnode_t *node)
{
    return verify_dict_has_node(dict_nil(dict), dict_root(dict), node);
}
Example #17
0
void dict_load_end(dict_load_t *load)
{
    dict_t *dict = load->dictptr;
    dnode_t *tree[DICT_DEPTH_MAX] = { 0 };
    dnode_t *curr, *dictnil = dict_nil(dict), *loadnil = &load->nilnode, *next;
    dnode_t *complete = 0;
    dictcount_t fullcount = DICTCOUNT_T_MAX, nodecount = dict->nodecount;
    dictcount_t botrowcount;
    unsigned baselevel = 0, level = 0, i;

    assert (dnode_red == 0 && dnode_black == 1);

    while (fullcount >= nodecount && fullcount)
        fullcount >>= 1;

    botrowcount = nodecount - fullcount;

    for (curr = loadnil->left; curr != loadnil; curr = next) {
        next = curr->left;

        if (complete == NULL && botrowcount-- == 0) {
            assert (baselevel == 0);
            assert (level == 0);
            baselevel = level = 1;
            complete = tree[0];

            if (complete != 0) {
                tree[0] = 0;
                complete->right = dictnil;
                while (tree[level] != 0) {
                    tree[level]->right = complete;
                    complete->parent = tree[level];
                    complete = tree[level];
                    tree[level++] = 0;
                }
            }
        }

        if (complete == NULL) {
            curr->left = dictnil;
            curr->right = dictnil;
            curr->color = (dnode_color_t) (level % 2);
            complete = curr;

            assert (level == baselevel);
            while (tree[level] != 0) {
                tree[level]->right = complete;
                complete->parent = tree[level];
                complete = tree[level];
                tree[level++] = 0;
            }
        } else {
            curr->left = complete;
            curr->color = (dnode_color_t) ((level + 1) % 2);
            complete->parent = curr;
            tree[level] = curr;
            complete = 0;
            level = baselevel;
        }
    }

    if (complete == NULL)
        complete = dictnil;

    for (i = 0; i < DICT_DEPTH_MAX; i++) {
        if (tree[i] != 0) {
            tree[i]->right = complete;
            complete->parent = tree[i];
            complete = tree[i];
        }
    }

    dictnil->color = dnode_black;
    dictnil->right = dictnil;
    complete->parent = dictnil;
    complete->color = dnode_black;
    dict_root(dict) = complete;

    assert (dict_verify(dict));
}