view src/insert_verification/akashaLLRBContext.c @ 38:593ab851ad76

Convert C function to cs (getMinHeight)
author Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
date Mon, 13 Jun 2016 11:47:37 +0900
parents be67b0312bea
children 56ea709e7af3
line wrap: on
line source

#include <stdlib.h>
#include <string.h>

#include "akashaLLRBContext.h"

extern __code showTree_stub(struct Context*);
extern __code iterateInsertion_stub(struct Context*);
extern __code putAndGoToNextDepth_stub (struct Context*);
extern __code showTrace_stub(struct Context*);
extern __code verifySpecification_stub (struct Context*);
extern __code duplicateIterator_stub(struct Context*);
extern __code duplicateIteratorElem_stub(struct Context*);
extern __code duplicateTree_stub(struct Context*);
extern __code goToPreviousDepth(struct Context*);

/* definitions from llrb */
extern __code meta(struct Context*, enum Code next);
extern __code put_stub(struct Context*);
extern __code replaceNode_stub(struct Context*);
extern __code insertNode_stub(struct Context*);
extern __code rotateLeft_stub(struct Context*);
extern __code rotateRight_stub(struct Context*);
extern __code colorFlip_stub(struct Context*);
extern __code fixUp_stub(struct Context*);
extern __code changeReference_stub(struct Context*);
extern __code insert1_stub(struct Context*);
extern __code insert2_stub(struct Context*);
extern __code insert3_stub(struct Context*);
extern __code insert4_stub(struct Context*);
extern __code insert4_1_stub(struct Context*);
extern __code insert4_2_stub(struct Context*);
extern __code insert5_stub(struct Context*);
extern __code stackClear_stub(struct Context*);
extern __code get_stub(struct Context*);
extern __code search_stub(struct Context*);
extern __code delete_stub(struct Context*);
extern __code delete1_stub(struct Context*);
extern __code delete2_stub(struct Context*);
extern __code delete3_stub(struct Context*);
extern __code replaceNodeForDelete1_stub(struct Context*);
extern __code replaceNodeForDelete2_stub(struct Context*);
extern __code findMax1_stub(struct Context*);
extern __code findMax2_stub(struct Context*);
extern __code deleteCase1_stub(struct Context*);
extern __code deleteCase2_stub(struct Context*);
extern __code deleteCase3_stub(struct Context*);
extern __code deleteCase4_stub(struct Context*);
extern __code deleteCase5_stub(struct Context*);
extern __code deleteCase6_stub(struct Context*);
extern __code exitCode(struct Context*);

/* for allocator */
extern void allocator(struct Context* context);


__code initIterator(struct Context* context, struct Allocate* allocate, struct Iterator* iter);
__code initFinishIterator(struct Context*, struct IterElem*);
__code initIteratorElem(struct Context* context, struct Allocate* allocate, struct IterElem* prev);

__code initLLRBContext(struct Context* context, enum Code next) {
    context->codeNum   = Exit;

    context->heapLimit = sizeof(union Data)*ALLOCATE_SIZE;
    context->code      = malloc(sizeof(__code*)*context->codeNum);
    context->data      = malloc(sizeof(union Data*)*ALLOCATE_SIZE);
    context->heapStart = malloc(context->heapLimit);
    memset(context->heapStart, 0, context->heapLimit);


    context->code[ShowTree]              = showTree_stub;
    context->code[IterateInsertion]      = iterateInsertion_stub;
    context->code[PutAndGoToNextDepth]   = putAndGoToNextDepth_stub;
    context->code[ShowTrace]             = showTrace_stub;
    context->code[VerifySpecification]   = verifySpecification_stub;
    context->code[DuplicateIterator]     = duplicateIterator_stub;
    context->code[DuplicateIteratorElem] = duplicateIteratorElem_stub;
    context->code[DuplicateTree]         = duplicateTree_stub;
    context->code[GoToPreviousDepth]     = goToPreviousDepth;

    /* definitions from llrb */
    context->code[Put]           = put_stub;
    context->code[Replace]       = replaceNode_stub;
    context->code[Insert]        = insertNode_stub;
    context->code[RotateL]       = rotateLeft_stub;
    context->code[RotateR]       = rotateRight_stub;
    context->code[InsertCase1]   = insert1_stub;
    context->code[InsertCase2]   = insert2_stub;
    context->code[InsertCase3]   = insert3_stub;
    context->code[InsertCase4]   = insert4_stub;
    context->code[InsertCase4_1] = insert4_1_stub;
    context->code[InsertCase4_2] = insert4_2_stub;
    context->code[InsertCase5]   = insert5_stub;
    context->code[StackClear]    = stackClear_stub;
    context->code[Exit]          = exitCode;

    context->heap = context->heapStart;

    context->data[Allocate] = context->heap;
    context->heap += sizeof(struct Allocate);

    context->data[Tree] = context->heap;
    context->heap += sizeof(struct Tree);

    context->data[Node] = context->heap;
    context->heap += sizeof(struct Node);

    context->data[Iter] = context->heap;
    context->heap += sizeof(struct Iterator);

    context->data[AkashaInfo] = context->heap;
    context->heap += sizeof(struct AkashaInfo);

    context->dataNum = AkashaInfo;

    struct Tree* tree = &context->data[Tree]->tree;
    tree->root = 0;
    tree->current = 0;
    tree->deleted = 0;

    context->node_stack = stack_init(sizeof(struct Node*), 1000);
    context->code_stack = stack_init(sizeof(enum Code), 1000);

    context->next = next;
    goto initIterator_stub(context);
}

__code initIterator_stub(struct Context* context) {
    goto initIterator(context, &context->data[Allocate]->allocate, &context->data[Iter]->iterator);
}

__code initIterator(struct Context* context, struct Allocate* allocate, struct Iterator* iter) {
    struct IterElem* ie   = context->heap;
    allocate->size = sizeof(struct IterElem);
    allocator(context);

    ie->val    = 1;
    ie->next   = NULL;
    iter->tree = NULL;
    iter->head = ie;

    goto initIteratorElem_stub(context, ie);
}
__code initIteratorElem_stub(struct Context* context, struct IterElem* prev) {
    goto initIteratorElem(context, &context->data[Allocate]->allocate, prev);
}

__code initIteratorElem(struct Context* context, struct Allocate* allocate, struct IterElem* prev) {
    if (prev->val == LIMIT_OF_VERIFICATION_SIZE) { goto initFinishIterator(context, prev); }

    struct IterElem* ie = context->heap;
    allocate->size = sizeof(struct IterElem);
    allocator(context);

    ie->val             = prev->val + 1;
    prev->next          = ie;
    goto initIteratorElem_stub(context, ie);
}

__code initFinishIterator(struct Context* context, struct IterElem* elem) {
    struct Iterator* iter = &context->data[Iter]->iterator;
    elem->next            = iter->head;
    iter->last            = iter->head;
    goto meta(context, context->next);
}