view src/insert_verification/main.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 <stdbool.h>
#include <stdio.h>

#include "akashaLLRBContext.h"
#include "akashaCS.h"

extern void allocator(struct Context* context);

void* akashaMalloc(struct Context* context, size_t size) {
    context->data[++context->dataNum] = context->heap;
    context->heap += size;
    return context->data[context->dataNum];
}

struct Node* nodeDeepCopy(struct Context* newContext, struct Node* oldNode) {
    if (oldNode == NULL) return NULL;

    struct Node* newNode = akashaMalloc(newContext, sizeof(struct Node));
    newNode->next  = oldNode->next;
    newNode->key   = oldNode->key;
    newNode->value = oldNode->value;
    newNode->color = oldNode->color;

    if (oldNode->left != NULL)  {
        newNode->left = nodeDeepCopy(newContext, oldNode->left);
    } else {
        newNode->left = NULL;
    }

    if (oldNode->right != NULL)  {
        newNode->right = nodeDeepCopy(newContext, oldNode->right);
    } else {
        newNode->right = NULL;
    }

    return newNode;
}

void treeDeepCopy(struct Context* newContext, struct Tree* newTree, struct Tree* oldTree) {
    if (oldTree == NULL) return;
    newTree->next    = oldTree->next;
    newTree->result  = oldTree->result;
    newTree->root    = nodeDeepCopy(newContext, oldTree->root);
    newTree->current = nodeDeepCopy(newContext, oldTree->current);
    newTree->deleted = nodeDeepCopy(newContext, oldTree->deleted);
}

/* Code Segments */

__code showTree_stub(struct Context* context) {
    goto showTree(context, &context->data[Tree]->tree);
}

__code showTree(struct Context* context, struct Tree* tree) {
    printTree(tree->root, 0);
    puts("");

    goto meta(context, Exit);
}

__code iterateInsertion_stub(struct Context* context) {
    goto iterateInsertion(context, &context->data[Iter]->iterator, &context->data[Node]->node);
}

__code iterateInsertion(struct Context* context, struct Iterator* iter, struct Node* node) {
    // puts all elements in iterator into tree.
    node->key   = iter->head->val;
    node->value = iter->head->val;
    iter->head  = iter->head->next;

    if (iter->head == iter->last) {
        context->next = ShowTree;
    } else {
        context->next = IterateInsertion;
    }
    goto meta(context, Put);
}

__code putAndGoToNextDepth_stub(struct Context* context) {
    goto putAndGoToNextDepth(context, &context->data[Iter]->iterator, &context->data[Tree]->tree, &context->data[Node]->node);
}

__code putAndGoToNextDepth(struct Context* context, struct Iterator* iter, struct Tree* tree, struct Node* node) {
    node->key           = iter->head->val;
    node->value         = iter->head->val;
    node->left          = NULL;
    node->right         = NULL;
    iter->head          = iter->head->next;
    iter->iteratedValue = node->value;

    if (iter->head == iter->head->next) {
        context->next = GoToPreviousDepth;
    } else {
        context->next = VerifySpecification;
    }
    goto meta(context, Put);
}

__code showTrace_stub(struct Context* context) {
    goto showTrace(context, &context->data[Iter]->iterator);
}

__code verifySpecification_stub(struct Context* context) {
    goto verifySpecification(context, &context->data[Allocate]->allocate, &context->data[Tree]->tree, &context->data[AkashaInfo]->akashaInfo);
}

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

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

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

    dup->val  = iter->head->val;
    dup->next = NULL;

    newIter->previousDepth = iter;
    newIter->head          = dup;
    newIter->last          = NULL;
    context->data[Iter]    = (union Data*) newIter;

    goto duplicateIteratorElem_stub(context);
}

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

__code duplicateIteratorElem(struct Context* context, struct Allocate* allocate, struct Iterator* iter) {
    // All elements in iterator must be unique.
    struct IterElem *oldElem = iter->previousDepth->head->next;
    struct IterElem *newElem = iter->head;

    while (oldElem->val != iter->previousDepth->iteratedValue) {
        allocate->size = sizeof(struct IterElem);
        allocator(context);

        newElem->next      = (struct IterElem*)context->data[context->dataNum];
        newElem->next->val = oldElem->val;

        newElem = newElem->next;
        oldElem = oldElem->next;
    }
    newElem->next = iter->head;
    iter->last    = iter->head;
    goto meta(context, DuplicateTree);
}

__code duplicateTree_stub(struct Context* context) {
    goto duplicateTree(context, &context->data[Allocate]->allocate, &context->data[Tree]->tree, &context->data[Iter]->iterator);
}

__code duplicateTree(struct Context* context, struct Allocate* allocate, struct Tree* tree, struct Iterator* iter) {
    // Tree must be non destructive.
    // If you use destructive tree, you must copy tree.
    iter->previousDepth->tree = akashaMalloc(context, sizeof(struct Tree));
    treeDeepCopy(context, iter->previousDepth->tree, tree);
    goto meta(context, PutAndGoToNextDepth);
}

__code goToPreviousDepth(struct Context* context) {
    struct Iterator* finishedIter = &context->data[Iter]->iterator;

    while (finishedIter->last == finishedIter->head) {
        if (finishedIter->previousDepth == NULL) {
            goto meta(context, ShowTree); // all enumerations finished.
        }

        finishedIter = finishedIter->previousDepth;
    }
    context->data[Iter] = (union Data*)finishedIter;
    context->data[Tree] = (union Data*)finishedIter->tree;

    goto meta(context, PutAndGoToNextDepth);
}

int main(int argc, char const* argv[]) {
    goto startCode(PutAndGoToNextDepth);
}