view src/parallel_execution/RedBlackTree.cbc @ 1054:7274db9ef926

...
author matac42 <matac@cr.ie.u-ryukyu.ac.jp>
date Thu, 25 Jan 2024 22:58:56 +0900
parents e53cbb2fa8b0
children 81439e83c4d2
line wrap: on
line source

#include <stdio.h>

#include "context.h"
#impl "Tree.h" as "RedBlackTree.h"
#interface "Stack.h"

extern enum Relational compare(struct Node* node1, struct Node* node2);

Tree* createRedBlackTree(struct Context* context) {
    struct Tree* tree = new Tree();
    struct RedBlackTree* redBlackTree = new RedBlackTree();

    tree->tree = (union Data*)redBlackTree;
    tree->put = C_putRedBlackTree;
    tree->get = C_getRedBlackTree;
    tree->remove = C_removeRedBlackTree;
    // tree->clear = C_clearRedBlackTree;
    tree->copy = C_copyRedBlackTree;

    redBlackTree->root = NULL;
    redBlackTree->nodeStack = createSingleLinkedStack(context);
    redBlackTree->inputStack = createSingleLinkedStack(context);
    return tree;
}

void printTree1(union Data* data) {
    struct Node* node = &data->Node;
    if (node == NULL) {
        printf("NULL");
    } else {
        printf("key = %d (", node->key);
        printTree1((union Data*)(node->right));
        printf("), (");
        printTree1((union Data*)(node->left));
        printf(")");
    }
}

void printTree(union Data* data) {
    printTree1(data);
    printf("\n");
}

__code putRedBlackTree(struct RedBlackTree* tree, struct Node* node) {
    struct Node* newNode = &ALLOCATE(context, Node)->Node;
    struct Node* root = tree->root;
    //printTree((union Data*)(tree->root));
    tree->newNode = newNode;
    tree->root = newNode; // this should done at stackClear
    tree->parent = NULL;
    if (root) {
        tree->current = root;
        tree->result = compare(tree->current, node);
        tree->findNodeNext = C_insertNode;
        goto findNode(tree);
    }
    goto insertNode(tree, node);
}

__code findNode(struct RedBlackTree* tree) {
    struct Stack* nodeStack = tree->nodeStack;
    struct Node* oldNode = tree->current;
    struct Node* newNode = tree->newNode;
    tree->previous = newNode;
    *newNode = *oldNode;
    goto nodeStack->push((union Data*)newNode, findNode1);
}

__code findNode1(struct RedBlackTree* tree, struct Node* node, __code next(...)) {
    struct Node* oldNode = tree->current;
    struct Node* newNode = tree->previous;
    struct Node* newnewNode = &ALLOCATE(context, Node)->Node;
    int result = tree->result;
    if (result == EQ) {
        newNode->value = node->value;
        // go to stack clear
        goto next(...);
    } else if (result == GT) {
        tree->current = oldNode->right;
        newNode->right = newnewNode;
    } else {
        tree->current = oldNode->left;
        newNode->left = newnewNode;
    }
    tree->newNode = newnewNode;
    if (tree->current) {
        tree->result = compare(tree->current, node);
        goto findNode(tree);
    }
    goto meta(context, tree->findNodeNext);
    //   gato tree->findNodeNext(tree, node);
    
}

__code copyRedBlackTree(struct RedBlackTree* tree) {
    tree->current = tree->root;
    // 最初にrootノードをコピーしておきたい
    goto leftDown(tree);
}

//
// leftDown*()
// currentをinputStackにpushして、current->leftがあれば降りる
//

__code leftDown(struct RedBlackTree* tree) {
    struct Stack* inputStack = tree->inputStack;

    printf("leftDown\n");
    
    goto inputStack->get(leftDown1);
}

__code leftDown1(struct RedBlackTree* tree, struct Stack* stack) {
    struct Stack* inputStack = tree->inputStack;
    struct Node* newNode = &ALLOCATE(context, Node)->Node;
    struct Node* data = (Node*)(stack->data);
    newNode->key = tree->current->key;
    newNode->value = (union Data*)new Integer();
    ((Integer*)newNode->value)->value = ((Integer*)tree->current->value)->value;
    newNode->color = tree->current->color;

    if(data) {
        data->left = newNode;
    }

    printf("leftDown1\n");

    // 新規ノードをpushしている
    goto inputStack->push(newNode, leftDown2);
}

// 実際にDownする
__code leftDown2(struct RedBlackTree* tree) {
    struct Stack* nodeStack = tree->nodeStack;
    struct Node* oldNode = tree->current;
    tree->current = tree->current->left;

    printf("leftDown2\n");
    goto nodeStack->push((union Data*)oldNode, leftDown3);
}

__code leftDown3(struct RedBlackTree* tree) {
    struct Stack* nodeStack = tree->nodeStack;
    printf("leftDown3\n");
    if (tree->current) {
        goto leftDown(tree);
    } else if (tree->current == NULL) {
        // ここでinputStackをpopする必要はない
        // currentの付け替えをして
        goto nodeStack->pop(leftDown4);
    } else if (tree->current->left) {
        // 謎の処理
        printf("nazo\n");
        goto rightDown(tree, inputStack);
    }
}

__code leftDown4(struct RedBlackTree* tree, struct Stack* stack) {
    struct Node* data = (Node*)(stack->data);
    tree->current = data;
    printf("leftDown4\n");

    goto up();
}

__code rightDown(struct RedBlackTree* tree) {
    struct Stack* inputStack = tree->inputStack;

    printf("rightDown\n");
    
    goto inputStack->get(rightDown1);
}

// inputStackに2をpushしてなくない?
__code rightDown1(struct RedBlackTree* tree, struct Stack* stack) {
    struct Stack* inputStack = tree->inputStack;
    struct Node* newNode = &ALLOCATE(context, Node)->Node;
    struct Node* data = (Node*)(stack->data);
    newNode->key = tree->current->right->key;
    newNode->value = (union Data*)new Integer();
    ((Integer*)newNode->value)->value = ((Integer*)tree->current->right->value)->value;
    newNode->color = tree->current->right->color;

    if(data) {
        data->right = newNode;
    }

    printf("rightDown1\n");

    // 新規ノードをpushしている
    goto inputStack->push(newNode, rightDown2);
}

// 実際にDownする
__code rightDown2(struct RedBlackTree* tree) {
    struct Stack* nodeStack = tree->nodeStack;
    struct Node* oldNode = tree->current;
    tree->current = tree->current->right;

    printf("rightDown2\n");
    goto nodeStack->push((union Data*)oldNode, rightDown3);
}

__code rightDown3(struct RedBlackTree* tree) {
    struct Stack* inputStack = tree->inputStack;
    printf("rightDown3\n");
    if (tree->current) {
        goto leftDown(tree, inputStack);
    } else if (tree->current == NULL) {
        // currentの付け替えは?
        goto inputStack->pop2(rightDown);
    } else if (tree->current->left) {
        // 謎の処理
        printf("nazo\n");
        goto rightDown(tree, inputStack);
    }
}

//
// upしつつright見つつ
// NULLからのup
// nodeStackが空だったらtreeの入れ替えへ
//
__code up(struct RedBlackTree* tree) {
    struct Stack* nodeStack = tree->nodeStack;

    printf("up\n");
    // current付け替えしたい
    // getして付け替えてpopだな...
    goto nodeStack->get(up1);
}

__code up1(struct RedBlackTree* tree, struct Stack* stack) {
    struct Stack* inputStack = tree->inputStack;
    struct Node* data = (Node*)(stack->data);
    tree->current = data;

    if (tree->current == NULL) {
        // 木の入れ替え
        // copyの終了
        printf("copy finished!\n");
    }

    printf("up1\n");

    // rightの状態を見る前にpopしてしまっている気がする
    goto inputStack->pop(up2);
}

// rightを見てさらにupするか考える
__code up2(struct RedBlackTree* tree) {
    struct Stack* inputStack = tree->inputStack;
    printf("up2\n");
    if (tree->current->right) {
        goto rightDown(tree);
    }

    goto inputStack->pop(up);
}

__code up3(struct Node* node, struct RedBlackTree* tree, struct Stack* inputStack) {
    printf("up3\n");
    tree->current->right = node->right;
    goto up(tree, inputStack);
}

__code popWhenEmpty(struct Node* node, struct RedBlackTree* tree, struct Stack* inputStack, __code next(...)) {
    // treeの入れ替え
    // struct Tree* newTree = new Tree();
    struct RedBlackTree* newRedBlackTree = new RedBlackTree();
    // newTree->tree = (union Data*)newRedBlackTree;
    // ここtreeじゃなくてinputStackからpopしたNodeを入れてあげないといけない。
    newRedBlackTree->root = tree->newNode;
    newRedBlackTree->current = tree->newNode;
    tree = newRedBlackTree;
    printf("popWhenEmpty\n");
    goto next(...);
}

__code popWhenNoEmpty(struct Node* node, struct RedBlackTree* tree, struct Stack* inputStack) {
    struct Stack* nodeStack = tree->nodeStack;
    printf("popWhenNoEmpty\n");
    goto nodeStack->pop(up2);
}

__code insertNode(struct RedBlackTree* tree, struct Node* node) {
    struct Stack* nodeStack = tree->nodeStack;
    struct Node* newNode = tree->newNode;
    *newNode = *node;
    newNode->color = Red;
    tree->current = newNode;
    goto nodeStack->get2(insertCase1);
}

__code insertCase1(struct RedBlackTree* tree, struct Node *parent, struct Node *grandparent) {
    if (parent != NULL) {
        tree->parent = parent;
        tree->grandparent = grandparent;
        goto insertCase2(tree);
    }
    tree->root->color = Black;
    goto stackClear();
}

__code insertCase1_stub(struct Context* context) {
    goto insertCase1(context, 
        &Gearef(context, Tree)->tree->Tree.tree->RedBlackTree,
        &context->data[D_Stack]->Stack.data->Node,
        &context->data[D_Stack]->Stack.data1->Node);
}

__code insertCase2(struct RedBlackTree* tree) {
    if (tree->parent->color == Black) {
        goto stackClear();
    }
    goto insertCase3(tree);
}

__code insertCase3(struct RedBlackTree* tree) {
    struct Stack* nodeStack = tree->nodeStack;
    struct Node* uncle;

    if (tree->grandparent->left == tree->parent) {
        uncle = tree->grandparent->right;
    } else {
        uncle = tree->grandparent->left;
    }

    if (uncle && (uncle->color == Red)) {
        // do insertcase1 on grandparent, stack must be pop by two
        tree->parent->color = Black;
        uncle->color = Black;
        tree->grandparent->color = Red;
        tree->current = tree->grandparent;
        goto nodeStack->pop2(insertCase1);
    }
    goto insertCase4();
}

__code insertCase4(struct RedBlackTree* tree, struct RotateTree* rotateTree) {
    struct Stack* nodeStack = tree->nodeStack;

    if ((tree->current == tree->parent->right) && (tree->parent == tree->grandparent->left)) {
        tree->current = tree->current->left;
        tree->parent = tree->grandparent;

        rotateTree->traverse = tree;
        rotateTree->next = C_insertCase5;

        goto nodeStack->pop(rotateLeft);
    } else if ((tree->current == tree->parent->left) && (tree->parent == tree->grandparent->right)) {
        tree->parent = tree->grandparent;
        tree->current = tree->current->right;

        rotateTree->traverse = tree;
        rotateTree->next = C_insertCase5;

        goto nodeStack->pop(rotateRight);
    }

    goto insertCase5();
}

__code insertCase5(struct RedBlackTree* tree) {
    struct Stack* nodeStack = tree->nodeStack;
    goto nodeStack->pop2(insertCase51);
}

__code insertCase51(struct RedBlackTree* tree, struct RotateTree* rotateTree, struct Node* parent, struct Node* grandparent) {
    struct Node* current = tree->current;
    tree->parent = parent;
    tree->grandparent = grandparent;

    parent->color = Black;
    grandparent->color = Red;

    tree->current = grandparent;

    rotateTree->traverse = tree;
    rotateTree->next = C_stackClear;

    if ((current == parent->left) && (parent == grandparent->left)){
        goto rotateRight();
    } else {
        goto rotateLeft();
    }
}

__code insertCase51_stub(struct Context* context) {
    struct Node* parent = &context->data[D_Stack]->Stack.data->Node;
    struct Node* grandparent = &context->data[D_Stack]->Stack.data1->Node;
    goto insertCase51(context,
                      &Gearef(context, Tree)->tree->Tree.tree->RedBlackTree,
                      Gearef(context, RotateTree),
                      parent,
                      grandparent);
}

__code rotateLeft(struct RedBlackTree* tree) {
    struct Stack* nodeStack = tree->nodeStack;
    goto nodeStack->get(rotateLeft1);
}

__code rotateLeft_stub(struct Context* context) {
    struct RedBlackTree* traverse = context->data[D_RotateTree]->RotateTree.traverse;
    goto rotateLeft(context, traverse);
}
    
__code rotateLeft1(struct Node* node, struct RedBlackTree* tree, struct Node* parent, struct RotateTree* rotateTree) {
    struct Node* tmp = node->right;

    if (parent) {
        if (node == parent->left)
            parent->left = tmp;
        else
            parent->right = tmp;
    } else {
        tree->root = tmp;
    }

    node->right = tmp->left;
    tmp->left = node;
    tree->current = tmp;
    
    goto meta(context, rotateTree->next);
}

__code rotateLeft1_stub(struct Context* context) {
    struct RedBlackTree* traverse = context->data[D_RotateTree]->RotateTree.traverse;
    struct Node* parent = &context->data[D_Stack]->Stack.data->Node;
    goto rotateLeft1(context,
                    traverse->current,
                    traverse,
                    parent,
                    Gearef(context, RotateTree));
}

__code rotateRight(struct RedBlackTree* tree) {
    struct Stack* nodeStack = tree->nodeStack;
    goto nodeStack->get(rotateRight1);
}

__code rotateRight_stub(struct Context* context) {
    struct RedBlackTree* traverse = context->data[D_RotateTree]->RotateTree.traverse;
    goto rotateLeft(context, traverse);
}

__code rotateRight1(struct Node* node, struct RedBlackTree* traverse,struct Node *parent,struct RotateTree *rotateTree) {
    struct Node* tmp = node->left;
    
    if (parent) {
        if (node == parent->left)
            parent->left = tmp;
        else
            parent->right = tmp;
    } else {
        traverse->root = tmp;
    }

    node->left = tmp->right;
    tmp->right = node;
    traverse->current = tmp;
    
    goto meta(context, rotateTree->next);
}

__code rotateRight1_stub(struct Context* context) {
    struct RedBlackTree* traverse = context->data[D_RotateTree]->RotateTree.traverse;
    struct Node* parent = &context->data[D_Stack]->Stack.data->Node;
    goto rotateRight1(context,
                     traverse->current,
                     traverse,
                     parent,
                     Gearef(context, RotateTree));
}

__code stackClear(struct RedBlackTree* tree, struct Stack* nodeStack, __code next(...)) {
    tree->current = 0;
    nodeStack->stack = (union Data*)tree->nodeStack;
    nodeStack->next = next;
    goto meta(context, tree->nodeStack->clear);
}

__code getRedBlackTree(struct RedBlackTree* tree, struct Node* node, __code next(...)) {
    if (tree->root) {
        tree->current = tree->root;

        goto search(node);
    }

    goto next(...);
}

__code search(struct RedBlackTree* tree, struct Node* node, __code next(...)) {
    // compare(context, traverse, traverse->current->key, node->key);
    tree->result = compare(tree->current, node);
    if (tree->result == EQ) {
        *node = *tree->current;
        
        goto meta(context, next);
    } else if (tree->result == GT) {
        tree->current = tree->current->right;
    } else {
        tree->current = tree->current->left;
    }
        
    if (tree->current) {
        goto meta(context, C_search);
    }

    goto next(...);
}


__code removeRedBlackTree(struct RedBlackTree* tree, struct Node* node, __code next(...)) {
    struct Node* newNode = &ALLOCATE(context, Node)->Node;
    struct Node* root = tree->root;
    printTree((union Data*)(tree->root));
    tree->newNode = newNode;
    tree->root = newNode; // this should done at stackClear
    tree->parent = NULL;
    if (root) {
        tree->current = root;
        tree->result = compare(tree->current, node);
        tree->findNodeNext = C_replaceNodeForDelete2;
        goto findNode(tree);
    }
    goto next(...);
}



__code delete2(struct Node* current) {
    if (current->color == Black) {
        struct Node* child = current->right == NULL ? current->left : current->right;
        current->color = child == NULL ? Black : child->color;

        goto deleteCase1(current);
    }

    goto delete3(tree, current);
}



__code delete3(struct RedBlackTree* tree, struct Node* current, __code next(...)) {
    struct Node* tmp = current->right == NULL ? current->left : current->right;
    struct Stack* nodeStack = tree->nodeStack;

    if (tree->parent) {
        if (current == tree->parent->left)
            tree->parent->left = tmp;
        else
            tree->parent->right = tmp;
    } else {
        tree->root = tmp;
    }


    if (tree->parent == NULL && tmp) {
        tmp->color = Black;
    }

    current == tree->parent->left ? (tree->parent->left = NULL) : (tree->parent->right = NULL);

    Gearef(context, Stack)->stack = (union Data*) nodeStack;
    Gearef(context, Stack)->next = next;
    goto meta(context, nodeStack->pop);

//    gato nodeStack->pop(next);
}



__code replaceNodeForDelete2(struct RedBlackTree* tree, struct Node* newNode) {
    if (tree->current->left && tree->current->right) {
        tree->parent = newNode;
        tree->current = newNode->left;
        newNode->left = context->heap;


        tree->parent = newNode;
        
        goto findMax1(tree,oldNode, newNode);
    }

    goto delete2(current);
}


__code findMax1(struct RedBlackTree* tree, struct Node* oldNode, struct Node* newNode) {
    *newNode = *oldNode;

    if (newNode->right) {
        goto findMax2(tree, oldNode, newNode);
    }
    
    tree->current = newNode;

    goto delete2(current);
}


    

__code findMax2(struct RedBlackTree* tree, struct Node* oldNode, struct Node* newNode) {
    *newNode = *oldNode;

    if (newNode->right->right) {
        tree->current = newNode->right;
        newNode->right = context->heap;

        tree->parent = newNode;
        
        goto findMax2(tree, oldNode, newNode);
    }

    tree->current = newNode;
    
    goto delete2(tree,current);
}
    

__code deleteCase1(struct RedBlackTree* tree, struct Node* current) {
    if (tree->parent) {
        goto deleteCase2(tree,current);
    }

    goto delete3(tree, current);
}



__code deleteCase2(struct RedBlackTree* tree, struct Node* current, struct RotateTree* rotateTree) {
    struct Node* sibling = current == tree->parent->left ? tree->parent->right : tree->parent->left;
    struct Stack* nodeStack = tree->nodeStack;
    
    if ((sibling == NULL ? Black : sibling->color) == Red) {
        tree->parent->color = Red;
        sibling->color = Black;

        current == tree->parent->left ? (tree->parent->left = context->heap) : (tree->parent->right = context->heap);

        struct Node* node = sibling;
        
        tree->current = tree->parent;

        rotateTree->traverse = tree;
        rotateTree->next = C_deleteCase3;

        if (current == tree->parent->left) {
            goto nodeStack->push((union Data*)node,rotateLeft);
        } else {
            goto nodeStack->push((union Data*)node,rotateRight);
        }

        goto deleteCase3(tree,current);
    }
}



__code deleteCase3(struct RedBlackTree* tree, struct Node* current) {
    struct Node* sibling = current == tree->parent->left ? tree->parent->right : tree->parent->left;
    
    if (tree->parent->color == Black &&
        (sibling == NULL ? Black : sibling->color) == Black &&
        (sibling->left == NULL ? Black : sibling->left->color) == Black &&
        (sibling->right == NULL ? Black : sibling->right->color) == Black) {
        sibling->color = Red;

        tree->current = tree->parent;
        goto deleteCase1(current);
    }

    goto deleteCase4(current);
}



__code deleteCase4(struct RedBlackTree* tree,struct Node* current) {
    struct Node* sibling = current == tree->parent->left ? tree->parent->right : tree->parent->left;
    
    if (tree->parent->color == Red &&
        (sibling == NULL ? Black : sibling->color) == Black &&
        (sibling->left == NULL ? Black : sibling->left->color) == Black &&
        (sibling->right == NULL ? Black : sibling->right->color) == Black) {
        sibling->color = Red;
        tree->parent->color = Black;

        goto delete3(tree,current);
    }

    goto deleteCase5(tree,current);
}



__code deleteCase5(struct RedBlackTree* tree, struct Node* current, struct RotateTree* rotateTree) {
    struct Node* sibling = current == tree->parent->left ? tree->parent->right : tree->parent->left;
    struct Stack* nodeStack = tree->nodeStack;
    // sibling->parent = tree->parent;
    
    if (current == tree->parent->left &&
        (sibling == NULL ? Black : sibling->color) == Black &&
        (sibling->left == NULL ? Black : sibling->left->color) == Red &&
        (sibling->right == NULL ? Black : sibling->right->color) == Black) {
        sibling->color = Red;
        sibling->left->color = Black;
        
        // sibling == sibling->parent->left ? (sibling->parent->left = context->heap) : (sibling->parent->right = context->heap);
        sibling == tree->parent->left ? (tree->parent->left = context->heap) : (tree->parent->right = context->heap);

        struct Node* node = new Node();
        node = sibling->left;

        struct Node* tmp = node;
        *tmp = *sibling;
        tree->parent = current;
        
        tmp->left = context->heap;
/*         struct Node* node = new Node(); */
/*         node = *sibling->left; */
        tree->parent = tmp;

        tree->current = tmp;
        

        rotateTree->traverse = tree;
        rotateTree->next = C_deleteCase6;

        goto nodeStack->push((union Data*)node,rotateRight);
    } else if (current == tree->parent->right &&
               (sibling == NULL ? Black : sibling->color) == Black &&
               (sibling->left == NULL ? Black : sibling->left->color) == Black &&
               (sibling->right == NULL ? Black : sibling->right->color) == Red) {
        sibling->color = Red;
        sibling->right->color = Black;

        sibling == tree->parent->left ? (tree->parent->left = context->heap) : (tree->parent->right = context->heap);

        struct Node* node = new Node();
        node = sibling->right;

        struct Node* tmp = node;
        *tmp = *sibling;
        // tmp->parent = current;

        tmp->right = context->heap;
/*         struct Node* node = new Node(); */
/*         node = *sibling->right; */
        //node->parent = tmp;

        tree->current = tmp;
        

        rotateTree->traverse = tree;
        rotateTree->next = C_deleteCase6;

        goto nodeStack->push((union Data*)node,rotateLeft);
    }

    goto deleteCase6(tree,current);
}


__code deleteCase6(struct RedBlackTree* tree, struct Node* current, struct RotateTree* rotateTree) {
    struct Node* sibling = current == tree->parent->left ? tree->parent->right : tree->parent->left;
    struct Stack* nodeStack = tree->nodeStack;
    sibling == tree->parent->left ? (tree->parent->left = context->heap) : (tree->parent->right = context->heap);

    struct Node* tmp = sibling;
    // *tmp = *sibling;
    tree->parent = current;

    tmp->color = tree->parent->color;
    tree->parent->color = Black;
    
    
    if (current == tree->parent->left) {
        tmp->right->color = Black;
        tree->current = tree->parent;

        rotateTree->traverse = tree;
        rotateTree->next = C_delete3;

        goto nodeStack->push((union Data*)tmp,rotateLeft);
    } else {
        tmp->left->color = Black;
        tree->current = tree->parent;

        rotateTree->traverse = tree;
        rotateTree->next = C_delete3;

        goto nodeStack->push((union Data*)tmp,rotateLeft);
    }
}