Back to index

python3.2  3.2.2
Defines | Functions | Variables
rotatingtree.c File Reference
#include "rotatingtree.h"

Go to the source code of this file.

Defines

#define KEY_LOWER_THAN(key1, key2)   ((char*)(key1) < (char*)(key2))

Functions

static int randombits (int bits)
void RotatingTree_Add (rotating_node_t **root, rotating_node_t *node)
rotating_node_t * RotatingTree_Get (rotating_node_t **root, void *key)
int RotatingTree_Enum (rotating_node_t *root, rotating_tree_enum_fn enumfn, void *arg)

Variables

static unsigned int random_value = 1
static unsigned int random_stream = 0

Define Documentation

#define KEY_LOWER_THAN (   key1,
  key2 
)    ((char*)(key1) < (char*)(key2))

Definition at line 3 of file rotatingtree.c.


Function Documentation

static int randombits ( int  bits) [static]

Definition at line 15 of file rotatingtree.c.

{
    int result;
    if (random_stream < (1U << bits)) {
        random_value *= 1082527;
        random_stream = random_value;
    }
    result = random_stream & ((1<<bits)-1);
    random_stream >>= bits;
    return result;
}

Here is the caller graph for this function:

void RotatingTree_Add ( rotating_node_t **  root,
rotating_node_t *  node 
)

Definition at line 31 of file rotatingtree.c.

{
    while (*root != NULL) {
        if (KEY_LOWER_THAN(node->key, (*root)->key))
            root = &((*root)->left);
        else
            root = &((*root)->right);
    }
    node->left = NULL;
    node->right = NULL;
    *root = node;
}

Here is the caller graph for this function:

int RotatingTree_Enum ( rotating_node_t *  root,
rotating_tree_enum_fn  enumfn,
void arg 
)

Definition at line 107 of file rotatingtree.c.

{
    int result;
    rotating_node_t *node;
    while (root != NULL) {
        result = RotatingTree_Enum(root->left, enumfn, arg);
        if (result != 0) return result;
        node = root->right;
        result = enumfn(root, arg);
        if (result != 0) return result;
        root = node;
    }
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

rotating_node_t* RotatingTree_Get ( rotating_node_t **  root,
void key 
)

Definition at line 48 of file rotatingtree.c.

{
    if (randombits(3) != 4) {
        /* Fast path, no rebalancing */
        rotating_node_t *node = *root;
        while (node != NULL) {
            if (node->key == key)
                return node;
            if (KEY_LOWER_THAN(key, node->key))
                node = node->left;
            else
                node = node->right;
        }
        return NULL;
    }
    else {
        rotating_node_t **pnode = root;
        rotating_node_t *node = *pnode;
        rotating_node_t *next;
        int rotate;
        if (node == NULL)
            return NULL;
        while (1) {
            if (node->key == key)
                return node;
            rotate = !randombits(1);
            if (KEY_LOWER_THAN(key, node->key)) {
                next = node->left;
                if (next == NULL)
                    return NULL;
                if (rotate) {
                    node->left = next->right;
                    next->right = node;
                    *pnode = next;
                }
                else
                    pnode = &(node->left);
            }
            else {
                next = node->right;
                if (next == NULL)
                    return NULL;
                if (rotate) {
                    node->right = next->left;
                    next->left = node;
                    *pnode = next;
                }
                else
                    pnode = &(node->right);
            }
            node = next;
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

unsigned int random_stream = 0 [static]

Definition at line 12 of file rotatingtree.c.

unsigned int random_value = 1 [static]

Definition at line 11 of file rotatingtree.c.