view src/llrb/llrb.c @ 54:0299b90256e5

syntax suggest
author kkb
date Thu, 11 Jun 2015 17:23:50 +0900
parents 399ed10d1760
children c469c5ed5b4d
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"

#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 struct Node* pre;

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

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

__code meta_code1_before(struct Context* context) {
    goto code1(context, &context->data[Allocate]->allocate);
}

__code code1(struct Context* context, struct Allocate *allocate) {
    allocate->size = sizeof(struct Count);
    allocate->next = Code2;
    goto meta(context, Allocater);
}

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

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

__code code2_stub(struct Context* context) {
    goto code2(context, &context->data[context->dataNum]->count);
}

__code code2(struct Context* context, struct Count* count) {
    count->i = 1;
    goto meta(context, Code3);
}

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

__code put_stub(struct Context* context) {
    goto put(context, &context->data[Tree]->tree, &context->data[Allocate]->allocate);
}

__code put(struct Context* context, struct Tree* tree, struct Allocate* allocate) {
    allocate->size = sizeof(struct Node);
    if (tree->root == 0) {
        allocate->next = Insert;
        goto meta(context, Allocator);
    }

    allocate->next = Create;
    tree->current = tree->root;

    goto meta(context, Compare);
}

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

    int result = context->data[Tree]->tree.result;

    *newNode = *persistentNode;

    if (result == 0) {
        stack_pop(pstack, &tree->current);
        goto meta(context, RotateL);
    } else if (result == 1) {
        tree->current = persistentNode->right;
        newNode->right = context->heap;
    } else {
        tree->current = persistentNode->left;
        newNode->left = context->heap;
    }

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

__code insertNode(struct Context* context) {
    struct Node* newNode = &context->data[context->dataNum]->node;
    struct Node* temporalNode = &context->data[Node]->node;
    struct Tree* tree = &context->data[Tree]->tree;

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

    if (tree->root == 0) {
        newNode->color = Black;
        tree->root = newNode;
        goto meta(context, context->data[Next]->next);
    }

    goto meta(context, RotateL);
}

__code compare(struct Context* context) {
    int persistentKey = context->data[Tree]->tree.current->key;
    int temporalKey = context->data[Node]->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 createNode(struct Context* context) {
    stack_push(pstack, &context->heap);

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

__code rotateLeft(struct Context* context) {
    struct Node* node = context->data[Tree]->tree.current;
    
    if (node->right != 0) {
        if (node->right->color == Red) {
            struct Node* tmp = node->right;
            node->right = tmp->left;
            tmp->left = node;
            tmp->color = tmp->left->color;
            tmp->left->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;

    if (node->left != 0) {
        if (node->left->left != 0) {
            if (node->left->color == Red && node->left->left->color == Red) {
                struct Node* tmp = node->left;
                node->left = tmp->right;
                tmp->right = node;
                tmp->color = tmp->right->color;
                tmp->right->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;
    
    if (node->right != 0 && node->left != 0) {
        if (node->right->color == Red && node->left->color == Red) {
            node->color ^= 1;
            node->left->color ^= 1;
            node->right->color ^= 1;
        }
    }
    
    goto meta(context, FixUp);
}

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

    allocate->next = ChangeRef;

    context->data[Node]->node.key = node->key;
    context->data[Tree]->tree.prev = node;
    
    if (stack_pop(pstack, &context->data[Tree]->tree.current) == 0) {
        goto meta(context, Compare);
    }
    
    context->data[Tree]->tree.root = node;
    
    goto meta(context, context->data[Next]->next);
}    

__code changeReference(struct Context* context) {
    struct Node* node = context->data[Tree]->tree.current;
    int result = context->data[Tree]->tree.result;
    
    if (result == 1) {
        node->right = context->data[Tree]->tree.prev;
    } else if (result == -1) {
        node->left = context->data[Tree]->tree.prev;
    } else {
        perror("bad status");
    }
    
    goto meta(context, RotateL);
}

__code get(struct Context* context) {
    context->data[Tree]->tree.current = context->data[Tree]->tree.root;
    context->data[Next]->next = context->data[Allocate]->allocate.next;
    context->data[Allocate]->allocate.next = Traverse;

    goto meta(context, Compare);
}

__code traverse(struct Context* context) {
    int result = context->data[Tree]->tree.result;
    struct Tree* tree = &context->data[Tree]->tree;
    
    if (result == 0) {
        goto meta(context, context->data[Next]->next);
    } else if (result == 1) {
        tree->current = tree->current->right;
    } else {
        tree->current = tree->current->left;
    }

    if(tree->current == 0) {
        goto meta(context, context->data[Next]->next);
    }

    goto meta(context, Compare);
}

__code delete(struct Context* context) {
    goto meta(context, Get);
}

__code meta_code3_before(struct Context* context) {
    goto code3(context, &context->data[Node]->node, &context->data[4]->count);
}

__code code3(struct Context* context, struct Node* node, struct Count* count) {
    if (count->i == num) {
        goto meta(context, Code4);
    }

    node->next = Code3;
    node->key = loop;
    node->value = loop;
    
    count->i++;
    goto meta(context, Put);
}
__code code4(struct Context* context) {
    puts("---before---");
    print_tree(context->data[Tree]->tree.root, 0);
    
    struct Allocate* allocate = &context->data[Allocate]->allocate;
    allocate->size = sizeof(struct Node);
    allocate->next = Code5;

    struct Node* node = &context->data[Node]->node;
    node->key = 0;
    node->value = 0;
    
    goto meta(context, Put);
}

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

    context->data[Allocate]->allocate.next = Code6;
    context->data[Node]->node.key = 2;

    goto meta(context, Get);
}

__code code6(struct Context* context) {
    puts("---get---");
    print_tree(context->data[Tree]->tree.current, 0);
    goto meta(context, Exit);
}

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