view src/llrb/llrb.c @ 25:390cf0523ea7

add file
author Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
date Fri, 01 May 2015 05:35:10 +0900
parents 7494c0b87ec4
children 06fcbe45e85c
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

#include "llrbContext.h"

#include "allocate.h"
#include "origin_cs.h"

#include "stack.h"

#ifdef CLANG
#define _CbC_retrun __return
#define _CbC_environment __environment
#endif

#define NUM 100

extern __code initLLRBContext(struct Context* context);

/* 
__code code1(Allocate allocate) {
    allocate.size = sizeof(long);
    allocate.next = Code2;
    goto Allocate(allocate);
}
*/
static double st_time;
static double ed_time;
static int num;
static clock_t c1,c2;

static stack_ptr pstack;
static union Data* pre;

static double getTime() {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec + (double)tv.tv_usec*1e-6;
}

void print_tree(union Data* data, int n) {
    if (data != 0) {
        print_tree(data->node.left, n+1);
        for (int i=0;i<n;i++)
            printf("  ");
        printf("key=%d depth=%d\t%p\n", data->node.key, n, data);
        print_tree(data->node.right, n+1);
    }
}

__code code1(struct Context* context) {
    context->data[Allocate]->allocate.size = sizeof(long);
    context->data[Allocate]->allocate.next = Code2;
    goto meta(context, Allocator);
}

__code meta(struct Context* context, enum Code next) {
    goto (context->code[next])(context);
}

__code put(struct Context* context) {
    struct Tree* tree = &context->data[Tree]->tree;
    
    if (tree->root == 0) {
        struct Node* node = &context->data[Allocate]->allocate.node;
        node->color = Black;
        node->left = 0;
        node->right = 0;

        context->data[Allocate]->allocate.next = InitNode;
        goto meta(context, Allocator);
    }

    context->data[Allocate]->allocate.next = Insert;
    tree->current = tree->root;

    goto meta(context, Compare);
}

__code clone(struct Context* context) {
    struct Node* node = &context->data[context->dataNum]->node;
    struct Node* persistentNode = &context->data[Tree]->tree.current->node;
    struct Tree* tree = &context->data[Tree]->tree;

    *node = *persistentNode;

    switch (context->data[Tree]->tree.result) {
    case 0:
        stack_pop(pstack, &tree->current);
        goto meta(context, RotateL);
    case 1:
        tree->current = persistentNode->right;
        node->right = context->heap;
        break;
    default:
        tree->current = persistentNode->left;
        node->left = context->heap;
        break;
    }

    if (context->data[Tree]->tree.current == 0) {
        stack_pop(pstack, &context->data[Tree]->tree.current);
        context->data[Allocate]->allocate.next = InitNode;
        goto meta(context, Allocator);
    }
    
    context->data[Allocate]->allocate.next = Insert;
    goto meta(context, Compare);
}

__code initNode(struct Context* context) {
    struct Node* node = &context->data[context->dataNum]->node;
    struct Node* temporalNode = &context->data[Allocate]->allocate.node;
    struct Tree* tree = &context->data[Tree]->tree;

    temporalNode->color = Red;
    *node = *temporalNode;

    if (tree->root == 0) {
        tree->root = context->data[context->dataNum];
        goto meta(context, context->data[Allocate]->allocate.after_put);
    }

    context->data[Allocate]->allocate.next = Insert;
    goto meta(context, RotateL);
}

__code compare(struct Context* context) {
    int persistentKey = context->data[Tree]->tree.current->node.key;
    int temporalKey = context->data[Allocate]->allocate.node.key;
    
    struct Tree* tree = &context->data[Tree]->tree;

    if (persistentKey == temporalKey) {
        tree->result = 0;
    } else if (persistentKey < temporalKey) {
        tree->result = 1;
    } else {
        tree->result = -1;
    }

    goto meta(context, context->data[Allocate]->allocate.next);
}
        
__code insert(struct Context* context) {
    stack_push(pstack, &context->heap);

    context->data[Allocate]->allocate.next = Clone;
    goto meta(context, Allocator);
}

__code rotateLeft(struct Context* context) {
    struct Node* node = &context->data[Tree]->tree.current->node;
    struct Allocate* allocate = &context->data[Allocate]->allocate;
    
    allocate->next = ChangeRef;
    allocate->node.key = node->key;
    allocate->node.key = node->key;

    if (node->right != 0) {
        if (node->right->node.color == Red) {
            union Data* tmp = context->data[Tree]->tree.current->node.right;
            context->data[Tree]->tree.current->node.right = tmp->node.left;
            tmp->node.left = context->data[Tree]->tree.current;
            tmp->node.color = tmp->node.left->node.color;
            tmp->node.left->node.color = Red;
            context->data[Tree]->tree.current = tmp;
        }
    }
    
    goto meta(context, RotateR);
}
    
__code rotateRight(struct Context* context) {
    struct Node* node = &context->data[Tree]->tree.current->node;

    if (node->left != 0) {
        if (node->left->node.left != 0) {
            if (node->left->node.color == Red && node->left->node.left->node.color == Red) {
                union Data* tmp = context->data[Tree]->tree.current->node.left;
                context->data[Tree]->tree.current->node.left = tmp->node.right;
                tmp->node.right = context->data[Tree]->tree.current;
                tmp->node.color = tmp->node.right->node.color;
                tmp->node.right->node.color = Red;
                context->data[Tree]->tree.current = tmp;
            }
        }
    }

    goto meta(context, ColorFlip);
}
__code colorFlip(struct Context* context) {
    struct Node* node = &context->data[Tree]->tree.current->node;

    if (node->right != 0 && node->left != 0) {
        if (node->right->node.color == Red && node->left->node.color == Red) {
            node->color ^= 1;
            node->left->node.color ^= 1;
            node->right->node.color ^= 1;
        }
    }
    
    goto meta(context, Compare);
}

__code changeReference(struct Context* context) {
    union Data* data = context->data[Tree]->tree.current;
    
    if (stack_pop(pstack, &context->data[Tree]->tree.current) == 0) {
        struct Node* node = &context->data[Tree]->tree.current->node;
        
        switch (context->data[Tree]->tree.result) {
        case 1:
            node->left = data;
            break;
        default:
            node->right = data;
            break;
        }

        goto meta(context, RotateL);
    }

    context->data[Tree]->tree.root = context->data[Tree]->tree.current;
    
    goto meta(context, context->data[Allocate]->allocate.after_put);
}


/*
__code code2(Allocate allocate, Count count) {
    count.count = 0;
    goto code3(count);
}
*/

__code code2(struct Context* context) {
    context->data[2]->count = 0;
    goto meta(context, Code3);
}

__code code3(struct Context* context) {
    struct Allocate* allocate = &context->data[Allocate]->allocate;
    long loop = context->data[2]->count;
    if (loop == num) {
        goto meta(context, Code4);
    }
    allocate->size = sizeof(struct Node);
    allocate->after_put = Code3;
    allocate->node.key = loop;
    allocate->node.value = loop;
    
    context->data[2]->count++;
    goto meta(context, Put);
}

__code code4(struct Context* context) {
    pre = context->data[Tree]->tree.root;
    context->data[Allocate]->allocate.size = sizeof(struct Node);
    context->data[Allocate]->allocate.after_put = Code5;
    context->data[Allocate]->allocate.node.key = num;
    context->data[Allocate]->allocate.node.value = num;
    
    goto meta(context, Put);
}

__code code5(struct Context* context) {
    puts("---prev---");
    print_tree(pre, 0);
    puts("---follow---");
    print_tree(context->data[Tree]->tree.root, 0);
    puts("--Number of Data--");
    printf("%d\n", context->dataNum);
    stack_free(pstack);
    goto meta(context, Exit);
}

int main(int argc, char** argv) {
    num = (int)atoi(argv[1]);
    pstack = stack_init(sizeof(union Data*), num/2);
    struct Context* context = (struct Context*)malloc(sizeof(struct Context));
    initLLRBContext(context);
    goto start_code(context, Code1);
}