Blame Modules/rotatingtree.c

rpm-build 2bd099
#include "rotatingtree.h"
rpm-build 2bd099
rpm-build 2bd099
#define KEY_LOWER_THAN(key1, key2)  ((char*)(key1) < (char*)(key2))
rpm-build 2bd099
rpm-build 2bd099
/* The randombits() function below is a fast-and-dirty generator that
rpm-build 2bd099
 * is probably irregular enough for our purposes.  Note that it's biased:
rpm-build 2bd099
 * I think that ones are slightly more probable than zeroes.  It's not
rpm-build 2bd099
 * important here, though.
rpm-build 2bd099
 */
rpm-build 2bd099
rpm-build 2bd099
static unsigned int random_value = 1;
rpm-build 2bd099
static unsigned int random_stream = 0;
rpm-build 2bd099
rpm-build 2bd099
static int
rpm-build 2bd099
randombits(int bits)
rpm-build 2bd099
{
rpm-build 2bd099
    int result;
rpm-build 2bd099
    if (random_stream < (1U << bits)) {
rpm-build 2bd099
        random_value *= 1082527;
rpm-build 2bd099
        random_stream = random_value;
rpm-build 2bd099
    }
rpm-build 2bd099
    result = random_stream & ((1<
rpm-build 2bd099
    random_stream >>= bits;
rpm-build 2bd099
    return result;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
/* Insert a new node into the tree.
rpm-build 2bd099
   (*root) is modified to point to the new root. */
rpm-build 2bd099
void
rpm-build 2bd099
RotatingTree_Add(rotating_node_t **root, rotating_node_t *node)
rpm-build 2bd099
{
rpm-build 2bd099
    while (*root != NULL) {
rpm-build 2bd099
        if (KEY_LOWER_THAN(node->key, (*root)->key))
rpm-build 2bd099
            root = &((*root)->left);
rpm-build 2bd099
        else
rpm-build 2bd099
            root = &((*root)->right);
rpm-build 2bd099
    }
rpm-build 2bd099
    node->left = NULL;
rpm-build 2bd099
    node->right = NULL;
rpm-build 2bd099
    *root = node;
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
/* Locate the node with the given key.  This is the most complicated
rpm-build 2bd099
   function because it occasionally rebalances the tree to move the
rpm-build 2bd099
   resulting node closer to the root. */
rpm-build 2bd099
rotating_node_t *
rpm-build 2bd099
RotatingTree_Get(rotating_node_t **root, void *key)
rpm-build 2bd099
{
rpm-build 2bd099
    if (randombits(3) != 4) {
rpm-build 2bd099
        /* Fast path, no rebalancing */
rpm-build 2bd099
        rotating_node_t *node = *root;
rpm-build 2bd099
        while (node != NULL) {
rpm-build 2bd099
            if (node->key == key)
rpm-build 2bd099
                return node;
rpm-build 2bd099
            if (KEY_LOWER_THAN(key, node->key))
rpm-build 2bd099
                node = node->left;
rpm-build 2bd099
            else
rpm-build 2bd099
                node = node->right;
rpm-build 2bd099
        }
rpm-build 2bd099
        return NULL;
rpm-build 2bd099
    }
rpm-build 2bd099
    else {
rpm-build 2bd099
        rotating_node_t **pnode = root;
rpm-build 2bd099
        rotating_node_t *node = *pnode;
rpm-build 2bd099
        rotating_node_t *next;
rpm-build 2bd099
        int rotate;
rpm-build 2bd099
        if (node == NULL)
rpm-build 2bd099
            return NULL;
rpm-build 2bd099
        while (1) {
rpm-build 2bd099
            if (node->key == key)
rpm-build 2bd099
                return node;
rpm-build 2bd099
            rotate = !randombits(1);
rpm-build 2bd099
            if (KEY_LOWER_THAN(key, node->key)) {
rpm-build 2bd099
                next = node->left;
rpm-build 2bd099
                if (next == NULL)
rpm-build 2bd099
                    return NULL;
rpm-build 2bd099
                if (rotate) {
rpm-build 2bd099
                    node->left = next->right;
rpm-build 2bd099
                    next->right = node;
rpm-build 2bd099
                    *pnode = next;
rpm-build 2bd099
                }
rpm-build 2bd099
                else
rpm-build 2bd099
                    pnode = &(node->left);
rpm-build 2bd099
            }
rpm-build 2bd099
            else {
rpm-build 2bd099
                next = node->right;
rpm-build 2bd099
                if (next == NULL)
rpm-build 2bd099
                    return NULL;
rpm-build 2bd099
                if (rotate) {
rpm-build 2bd099
                    node->right = next->left;
rpm-build 2bd099
                    next->left = node;
rpm-build 2bd099
                    *pnode = next;
rpm-build 2bd099
                }
rpm-build 2bd099
                else
rpm-build 2bd099
                    pnode = &(node->right);
rpm-build 2bd099
            }
rpm-build 2bd099
            node = next;
rpm-build 2bd099
        }
rpm-build 2bd099
    }
rpm-build 2bd099
}
rpm-build 2bd099
rpm-build 2bd099
/* Enumerate all nodes in the tree.  The callback enumfn() should return
rpm-build 2bd099
   zero to continue the enumeration, or non-zero to interrupt it.
rpm-build 2bd099
   A non-zero value is directly returned by RotatingTree_Enum(). */
rpm-build 2bd099
int
rpm-build 2bd099
RotatingTree_Enum(rotating_node_t *root, rotating_tree_enum_fn enumfn,
rpm-build 2bd099
                  void *arg)
rpm-build 2bd099
{
rpm-build 2bd099
    int result;
rpm-build 2bd099
    rotating_node_t *node;
rpm-build 2bd099
    while (root != NULL) {
rpm-build 2bd099
        result = RotatingTree_Enum(root->left, enumfn, arg);
rpm-build 2bd099
        if (result != 0) return result;
rpm-build 2bd099
        node = root->right;
rpm-build 2bd099
        result = enumfn(root, arg);
rpm-build 2bd099
        if (result != 0) return result;
rpm-build 2bd099
        root = node;
rpm-build 2bd099
    }
rpm-build 2bd099
    return 0;
rpm-build 2bd099
}