Mercurial > hg > Members > Moririn
changeset 460:5fd0502a6c35
Split RedBlackTree.cbc RedBlackTreeReWrite.cbc
author | ryokka |
---|---|
date | Tue, 19 Dec 2017 17:49:57 +0900 |
parents | 57c715bd6283 |
children | b3a2246e3218 |
files | src/parallel_execution/RedBlackTree.cbc src/parallel_execution/RedBlackTreeReWright.cbc |
diffstat | 2 files changed, 874 insertions(+), 208 deletions(-) [+] |
line wrap: on
line diff
--- a/src/parallel_execution/RedBlackTree.cbc Sat Dec 16 06:04:32 2017 +0900 +++ b/src/parallel_execution/RedBlackTree.cbc Tue Dec 19 17:49:57 2017 +0900 @@ -1,253 +1,652 @@ #include <stdio.h> #include "../context.h" -#include "../compare.c" extern enum Relational compare(struct Node* node1, struct Node* node2); - Tree* createRedBlackTree(struct Context* context) { struct Tree* tree = new Tree(); - struct RedBlackTree* rbtree = new RedBlackTree(); - - tree->tree = (union Data*)rbtree; - rbtree->root = NULL; - rbtree->nodeStack = (union Data*)createSingleLinkedStack(context); - tree->put = C_putRedBlackTree; - // tree->get = C_getRedBlackTree; + struct RedBlackTree* redBlackTree = new RedBlackTree(); + tree->tree = (union Data*)redBlackTree; + redBlackTree->root = NULL; + redBlackTree->nodeStack = (union Data*)createSingleLinkedStack(context); + tree->put = C_putRedBlackTree; + tree->get = C_getRedBlackTree; // tree->remove = C_removeRedBlackTree; // tree->clear = C_clearRedBlackTree; - return tree; + 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"); } -void printNode(struct Node* node) { - if (node == NULL) { - printf("leaf"); - } else { - printf("((%d,%d (",node->color, node->key); - printNode(node->right); - printf(") ("); - printNode(node->left); - printf(")"); - } +__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); + 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(newNode, replaceNode1); } -void printTree(struct RedBlackTree* tree) { - printf("\n"); - tree->current = tree->root; - printNode(tree->current); - printf(")\n"); +__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 insertNode(tree, node); + +} + +__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 putRedBlackTree(struct RedBlackTree* tree, struct Node* node, __code next(...)) { - printf("C_putRedBlackTree\n"); - //struct Node* newNode = &ALLOCATE(context, Node)->Node; - printf("value->%d,key->%d \n",node->value,node->key); - tree->previous = tree->newNode; - tree->newNode = node; - tree->newNode->color = Red; - // tree->root = newNode; // this should done at stackClear - // tree->parent = NULL; - // if (root) { - tree->current = tree->root; - // tree->result = compare(tree->current, node); - goto insertRBTree(node, tree); - // } - // printf("error : __code putRedBlackTree \n"); - // goto meta(context, C_exit_code); +__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 stackClear(struct RedBlackTree* tree, struct Stack* nodeStack, __code next(...)) { - // tree->current = 0; - // nodeStack->stack = tree->nodeStack; - // nodeStack->next = next; - // goto meta(context, tree->nodeStack->clear); - //} +__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; -// __code getRedBlackTree(struct RedBlackTree* tree, __code next(...)) { - // if (tree->root) { - // tree->current = tree->root; - // goto insertRBTree(); - // // goto deleteRBTree(); - // } - // - // goto next(...); - // } + 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 insertRBTree(struct Node* node, struct RedBlackTree* tree, struct Stack* stack, __code next(...)) { - // first case tree->current = root; - printf("C_insertRBTree\n"); - //stack = tree->nodeStack; - printf("value->%d,key->%d\n",node->value,node->key); - printf("newNode value->%d,newNode key->%d\n",tree->newNode->value,tree->newNode->key); +__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 (tree->root == NULL) { - printf("insertRBTree_root eq NULL\n"); - tree->root = tree->newNode; - tree->root->color = Black; - printf("tree->root->key = %d, tree->root->color = %d \n",tree->root->key,tree->root->color); - printTree(tree); - goto next(tree,...); - } else { - goto searchInsertLocation(node, tree, stack); - } + 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; -__code searchInsertLocation(struct Node* node, struct RedBlackTree* tree) { - // first case tree->current = root; PreCase remove root=NULL case.don't exist firstCase tree->current=NULL - printf("C_searchInsertLocation\n"); - printf("nownode->key %d , previous->key %d \n",tree->newNode->key,tree->previous->key); + 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); +} - tree->result = compare(tree->current, node); - printf("tree->current->key = %d, node->key %d\n",tree->current->key,node->key); - printf("compare (%d,%d)\n",tree->current,node); - struct Stack* stack = tree->nodeStack; - if (tree->current == NULL) { - printf("goto insertLocationBackInsert stack->pop"); - goto stack->pop(insertLocationBackInsert); - } - if (tree->result == GT) { - printf("GT searchInsertLocation"); - tree->current = tree->current->right; - goto stack->push(tree->newNode,insertLocationBackInsert); - } else if (tree->result == LT) { - printf("LT searchInsertLocation"); - tree->current = tree->current->left; - goto stack->push(tree->newNode, searchInsertLocation); - } else if (tree->result == EQ) { - printf("already member this node : __code searchInsertLocation()\n"); - goto meta(context, C_exit_code); - } else { - printf("$insert value tree : __code searchInsertLocation() \n"); - goto meta(context, C_exit_code); - } +__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, __code next(...)) { + if (tree->root) { + tree->current = tree->root; + + goto search(); + } + + 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 insertLocationBackInsert(struct RedBlackTree* tree, struct Node* node, struct Stack* stack) { - printf("C_insertLocationBackInsert\n"); - struct Node* hoge = stack->data; - printf("stackpopdata%d\n",stack->data); - tree->current = tree->previous; - // tree->current = nodeStack->data; - // this CS is ones only backTrace, and insert node - tree->result = compare(tree->previous,tree->newNode); - printf("back,compare\n"); - if (tree->result == GT) { - printf("ok\n"); - tree->current->right = tree->newNode; - printTree(tree); - goto insertBalance(); - } else if (tree->result == LT) { - printf("LT\n"); - tree->current->left = tree->newNode; - goto insertBalance(); - } else { - printf("error : __code insertLocationBackTrace() \n"); - goto meta(context, C_exit_code); - } -} +/* /\* __code delete(struct Context* context, struct Tree* tree) { *\/ */ +/* /\* if (tree->root) { *\/ */ +/* /\* stack_push(context->code_stack, &context->next); *\/ */ +/* /\* context->next = Delete1; *\/ */ +/* /\* goto meta(context, Get); *\/ */ +/* /\* } *\/ */ + +/* /\* goto meta(context, context->next); *\/ */ +/* /\* } *\/ */ + +/* /\* __code delete_stub(struct Context* context) { *\/ */ +/* /\* goto delete(context, &context->data[Tree]->tree); *\/ */ +/* /\* } *\/ */ + +/* /\* __code delete1(struct Context* context, struct Tree* tree, struct Allocate* allocate) { *\/ */ +/* /\* allocate->size = sizeof(struct Node); *\/ */ +/* /\* allocator(context); *\/ */ + +/* /\* struct Node* root = tree->root; *\/ */ + +/* /\* tree->root = &context->data[context->dataNum]->node; *\/ */ +/* /\* tree->current = root; *\/ */ + +/* /\* compare(context, tree, tree->current->key, context->data[Node]->node.key); *\/ */ + +/* /\* goto meta(context, Replace_d1); *\/ */ +/* /\* } *\/ */ + +/* /\* __code delete1_stub(struct Context* context) { *\/ */ +/* /\* goto delete1(context, &context->data[Tree]->tree, &context->data[Allocate]->allocate); *\/ */ +/* /\* } *\/ */ + +/* /\* __code delete2(struct Context* context, 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 meta(context, DeleteCase1); *\/ */ +/* /\* } *\/ */ -__code insertBalance(struct RedBlackTree* tree, struct Node* nodeStack, struct Node* node, __code next(...)) { - printf("C_insertBalance\n"); - struct Node* traceNode = tree->nodeStack->data; - tree->current = traceNode; - struct Stack* stack = tree->nodeStack; +/* /\* goto meta(context, Delete3); *\/ */ +/* /\* } *\/ */ + +/* /\* __code delete2_stub(struct Context* context) { *\/ */ +/* /\* goto delete2(context, context->data[Tree]->tree.current); *\/ */ +/* /\* } *\/ */ + +/* /\* __code delete3(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ +/* /\* struct Node* tmp = current->right == NULL ? current->left : current->right; *\/ */ + +/* /\* if (current->parent) { *\/ */ +/* /\* if (current == current->parent->left) *\/ */ +/* /\* current->parent->left = tmp; *\/ */ +/* /\* else *\/ */ +/* /\* current->parent->right = tmp; *\/ */ +/* /\* } else { *\/ */ +/* /\* tree->root = tmp; *\/ */ +/* /\* } *\/ */ + +/* /\* if (tmp) *\/ */ +/* /\* tmp->parent = current->parent; *\/ */ + +/* /\* if (current->parent == NULL && tmp) *\/ */ +/* /\* tmp->color = Black; *\/ */ + +/* /\* current == current->parent->left ? (current->parent->left = NULL) : (current->parent->right = NULL); *\/ */ + +/* /\* stack_pop(context->code_stack, &context->next); *\/ */ +/* /\* goto meta(context, context->next); *\/ */ +/* /\* } *\/ */ + +/* /\* __code delete3_stub(struct Context* context) { *\/ */ +/* /\* goto delete3(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ +/* /\* } *\/ */ + +/* /\* __code replaceNodeForDelete1(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode, int result) { *\/ */ +/* /\* *newNode = *oldNode; *\/ */ + +/* /\* if (result == EQ) *\/ */ +/* /\* goto meta(context, Replace_d2); *\/ */ +/* /\* else if (result == GT) *\/ */ +/* /\* tree->current = newNode->right; *\/ */ +/* /\* else *\/ */ +/* /\* tree->current = newNode->left; *\/ */ - // exit insertion code - if (tree->current == tree->root) { - tree->current->color = Black; - printTree(tree); - //printTree - goto next(tree,...); - } +/* /\* tree->current->parent = newNode; *\/ */ + +/* /\* if (tree->current->left == NULL && tree->current->right == NULL) *\/ */ +/* /\* goto meta(context, Delete2); *\/ */ + +/* /\* if (result == GT) *\/ */ +/* /\* newNode->right = context->heap; *\/ */ +/* /\* else if (result == LT) *\/ */ +/* /\* newNode->left = context->heap; *\/ */ + +/* /\* allocator(context); *\/ */ + +/* /\* compare(context, tree, tree->current->key, context->data[Node]->node.key); *\/ */ + +/* /\* goto meta(context, Replace_d1); *\/ */ +/* /\* } *\/ */ +/* /\* __code replaceNodeForDelete1_stub(struct Context* context) { *\/ */ +/* /\* goto replaceNodeForDelete1(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node, context->data[Tree]->tree.result); *\/ */ +/* /\* } *\/ */ - //current color eq Red - if (tree->current->color == Red) - goto stack->pop(insertBalance); +/* /\* __code replaceNodeForDelete2(struct Context* context, struct Tree* tree, struct Node* newNode) { *\/ */ +/* /\* if (tree->current->left && tree->current->right) { *\/ */ +/* /\* newNode->left->parent = newNode; *\/ */ +/* /\* tree->current = newNode->left; *\/ */ +/* /\* newNode->left = context->heap; *\/ */ +/* /\* tree->deleted = newNode; *\/ */ + +/* /\* allocator(context); *\/ */ +/* /\* tree->current->parent = newNode; *\/ */ + +/* /\* goto meta(context, FindMax1); *\/ */ +/* /\* } *\/ */ + +/* /\* goto meta(context, Delete2); *\/ */ +/* /\* } *\/ */ + +/* /\* __code replaceNodeForDelete2_stub(struct Context* context) { *\/ */ +/* /\* goto replaceNodeForDelete2(context, &context->data[Tree]->tree, &context->data[context->dataNum]->node); *\/ */ +/* /\* } *\/ */ - // current color eq Black - if (tree->current->left->left || tree->current->left->right) { - goto insertBalanceLeft(tree,nodeStack); - } else if (tree->current->right->left || tree->current->right->right) { - goto insertBalanceRight(tree,nodeStack); - } else { - goto stack->pop(insertBalance); - } -} +/* /\* __code findMax1(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode) { *\/ */ +/* /\* *newNode = *oldNode; *\/ */ + +/* /\* if (newNode->right) *\/ */ +/* /\* goto meta(context, FindMax2); *\/ */ + +/* /\* tree->deleted->key = newNode->key; *\/ */ +/* /\* tree->deleted->value = newNode->value; *\/ */ + +/* /\* tree->current = newNode; *\/ */ + +/* /\* goto meta(context, Delete2); *\/ */ +/* /\* } *\/ */ + +/* /\* __code findMax1_stub(struct Context* context) { *\/ */ +/* /\* goto findMax1(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node); *\/ */ +/* /\* } *\/ */ + + +/* /\* __code findMax2(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode) { *\/ */ +/* /\* *newNode = *oldNode; *\/ */ -__code insertBalanceLeft(struct RedBlackTree* tree, struct Node* nodeStack, struct Node* node) { - printf("C_insertBalanceLeft\n"); - struct Stack* stack = tree->nodeStack; +/* /\* if (newNode->right->right) { *\/ */ +/* /\* tree->current = newNode->right; *\/ */ +/* /\* newNode->right = context->heap; *\/ */ + +/* /\* allocator(context); *\/ */ +/* /\* tree->current->parent = newNode; *\/ */ + +/* /\* goto meta(context, FindMax2); *\/ */ +/* /\* } *\/ */ - if (tree->current->color == Black && tree->current->left->color == Red && tree->current->left->left->color == Red) { - struct Node* tmpCurrent = tree->current; - struct Node* tmpLeft = tree->current->left; - struct Node* tmpLeftLeft = tree->current->left->left; +/* /\* tree->deleted->key = newNode->right->key; *\/ */ +/* /\* tree->deleted->value = newNode->right->value; *\/ */ + +/* /\* tree->current = newNode; *\/ */ + +/* /\* goto meta(context, Delete2); *\/ */ +/* /\* } *\/ */ + +/* /\* __code findMax2_stub(struct Context* context) { *\/ */ +/* /\* goto findMax2(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node); *\/ */ +/* /\* } *\/ */ - tree->current = tmpLeft; - tree->current->right = tmpCurrent; - tree->current->left = tmpLeftLeft; - tree->current->right->left = tmpLeft->right; - tree->current->color = Red; - tree->current->left->color = Black; - tree->current->right->color = Black; - goto stack->pop(insertBalance); +/* /\* __code deleteCase1(struct Context* context, struct Node* current) { *\/ */ +/* /\* if (current->parent) *\/ */ +/* /\* goto meta(context, DeleteCase2); *\/ */ + +/* /\* goto meta(context, Delete3); *\/ */ +/* /\* } *\/ */ + +/* /\* __code deleteCase1_stub(struct Context* context) { *\/ */ +/* /\* goto deleteCase1(context, context->data[Tree]->tree.current); *\/ */ +/* /\* } *\/ */ + +/* /\* __code deleteCase2(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ +/* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ + +/* /\* if ((sibling == NULL ? Black : sibling->color) == Red) { *\/ */ +/* /\* current->parent->color = Red; *\/ */ +/* /\* sibling->color = Black; *\/ */ - } else if(tree->current->color == Black && tree->current->left->color == Red && tree->current->left->right->color == Red) { - struct Node* tmpCurrent = tree->current; - struct Node* tmpLeft = tree->current->left; - struct Node* tmpLeftRight = tree->current->left->right; +/* /\* current == current->parent->left ? (current->parent->left = context->heap) : (current->parent->right = context->heap); *\/ */ +/* /\* allocator(context); *\/ */ +/* /\* context->data[context->dataNum]->node = *sibling; *\/ */ + +/* /\* tree->current = current->parent; *\/ */ + +/* /\* context->next = DeleteCase3; *\/ */ +/* /\* stack_push(context->code_stack, &context->next); *\/ */ + +/* /\* if (current == current->parent->left) *\/ */ +/* /\* goto meta(context, RotateL); *\/ */ +/* /\* else *\/ */ +/* /\* goto meta(context, RotateR); *\/ */ +/* /\* } *\/ */ + +/* /\* goto meta(context, DeleteCase3); *\/ */ +/* /\* } *\/ */ + +/* /\* __code deleteCase2_stub(struct Context* context) { *\/ */ +/* /\* goto deleteCase2(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ +/* /\* } *\/ */ - tree->current = tmpLeft; - tree->current->right = tmpCurrent; - tree->current->left = tmpLeftRight; - tree->current->right->left = tmpLeft->left; - tree->current->color = Red; - tree->current->left->color = Black; - tree->current->right->color = Black; - goto stack->pop(insertBalance); +/* /\* __code deleteCase3(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ +/* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ + +/* /\* if (current->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 = current->parent; *\/ */ +/* /\* goto meta(context, DeleteCase1); *\/ */ +/* /\* } *\/ */ + +/* /\* goto meta(context, DeleteCase4); *\/ */ +/* /\* } *\/ */ + +/* /\* __code deleteCase3_stub(struct Context* context) { *\/ */ +/* /\* goto deleteCase3(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ +/* /\* } *\/ */ - } -} +/* /\* __code deleteCase4(struct Context* context, struct Node* current) { *\/ */ +/* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ + +/* /\* if (current->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; *\/ */ +/* /\* current->parent->color = Black; *\/ */ -__code insertBalanceRight(struct RedBlackTree* tree, struct Node* nodeStack, struct Node* node) { - printf("C_insertBalanceLeft\n"); - struct Stack* stack = tree->nodeStack; +/* /\* goto meta(context, Delete3); *\/ */ +/* /\* } *\/ */ + +/* /\* goto meta(context, DeleteCase5); *\/ */ +/* /\* } *\/ */ + +/* /\* __code deleteCase4_stub(struct Context* context) { *\/ */ +/* /\* goto deleteCase4(context, context->data[Tree]->tree.current); *\/ */ +/* /\* } *\/ */ - if (tree->current->color == Black && tree->current->right->color == Red && tree->current->right->right->color == Red) { - struct Node* tmpCurrent = tree->current; - struct Node* tmpRight = tree->current->right; - struct Node* tmpRightRight = tree->current->right->right; +/* /\* __code deleteCase5(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ +/* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ +/* /\* sibling->parent = current->parent; *\/ */ + +/* /\* if (current == current->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); *\/ */ +/* /\* allocator(context); *\/ */ +/* /\* struct Node* tmp = &context->data[context->dataNum]->node; *\/ */ +/* /\* *tmp = *sibling; *\/ */ +/* /\* tmp->parent = current; *\/ */ + +/* /\* tmp->left = context->heap; *\/ */ +/* /\* allocator(context); *\/ */ +/* /\* context->data[context->dataNum]->node = *sibling->left; *\/ */ +/* /\* context->data[context->dataNum]->node.parent = tmp; *\/ */ - tree->current = tmpRight; - tree->current->left = tmpCurrent; - tree->current->right = tmpRightRight; - tree->current->left->right = tmpRight->left; - tree->current->color = Red; - tree->current->left->color = Black; - tree->current->right->color = Black; - goto stack->pop(insertBalance); +/* /\* tree->current = tmp; *\/ */ + +/* /\* context->next = DeleteCase6; *\/ */ +/* /\* stack_push(context->code_stack, &context->next); *\/ */ - } else if (tree->current->color == Black && tree->current->right->color == Red && tree->current->right->left->color == Red) { +/* /\* goto meta(context, RotateR); *\/ */ +/* /\* } else if (current == current->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 == sibling->parent->left ? (sibling->parent->left = context->heap) : (sibling->parent->right = context->heap); *\/ */ +/* /\* allocator(context); *\/ */ +/* /\* struct Node* tmp = &context->data[context->dataNum]->node; *\/ */ +/* /\* *tmp = *sibling; *\/ */ +/* /\* tmp->parent = current; *\/ */ + +/* /\* tmp->right = context->heap; *\/ */ +/* /\* allocator(context); *\/ */ +/* /\* context->data[context->dataNum]->node = *sibling->right; *\/ */ +/* /\* context->data[context->dataNum]->node.parent = tmp; *\/ */ - struct Node* tmpCurrent = tree->current; - struct Node* tmpRight = tree->current->right; - struct Node* tmpRightLeft = tree->current->right->left; +/* /\* tree->current = tmp; *\/ */ + +/* /\* context->next = DeleteCase6; *\/ */ +/* /\* stack_push(context->code_stack, &context->next); *\/ */ +/* /\* goto meta(context, RotateL); *\/ */ +/* /\* } *\/ */ + +/* /\* goto meta(context, DeleteCase6); *\/ */ +/* /\* } *\/ */ + +/* /\* __code deleteCase5_stub(struct Context* context) { *\/ */ +/* /\* goto deleteCase5(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ +/* /\* } *\/ */ + +/* /\* __code deleteCase6(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ +/* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ + +/* /\* sibling == sibling->parent->left ? (sibling->parent->left = context->heap) : (sibling->parent->right = context->heap); *\/ */ +/* /\* allocator(context); *\/ */ +/* /\* struct Node* tmp = &context->data[context->dataNum]->node; *\/ */ +/* /\* *tmp = *sibling; *\/ */ +/* /\* tmp->parent = current; *\/ */ - tree->current = tmpRight; - tree->current->right = tmpCurrent; - tree->current->left = tmpRightLeft; - tree->current->left->right = tmpRight->right; - tree->current->color = Red; - tree->current->left->color = Black; - tree->current->right->color = Black; - goto stack->pop(insertBalance); +/* /\* tmp->color = current->parent->color; *\/ */ +/* /\* current->parent->color = Black; *\/ */ + +/* /\* context->next = Delete3; *\/ */ +/* /\* stack_push(context->code_stack, &context->next); *\/ */ + +/* /\* if (current == current->parent->left) { *\/ */ +/* /\* tmp->right->color = Black; *\/ */ +/* /\* tree->current = current->parent; *\/ */ - } else { - printf("unkwon error : __code insertBalanceRight() \n"); - goto meta(context, C_exit_code); - } -} -// insertCode end +/* /\* goto meta(context, RotateL); *\/ */ +/* /\* } else { *\/ */ +/* /\* tmp->left->color = Black; *\/ */ +/* /\* tree->current = current->parent; *\/ */ + +/* /\* goto meta(context, RotateR); *\/ */ +/* /\* } *\/ */ +/* /\* } *\/ */ + +/* /\* __code deleteCase6_stub(struct Context* context) { *\/ */ +/* /\* goto deleteCase6(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ +/* /\* } *\/ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parallel_execution/RedBlackTreeReWright.cbc Tue Dec 19 17:49:57 2017 +0900 @@ -0,0 +1,267 @@ +#include <stdio.h> + +#include "../context.h" +#include "../compare.c" + +extern enum Relational compare(struct Node* node1, struct Node* node2); + + +Tree* createRedBlackTree(struct Context* context) { + struct Tree* tree = new Tree(); + struct RedBlackTree* rbtree = new RedBlackTree(); + + tree->tree = (union Data*)rbtree; + rbtree->root = NULL; + rbtree->nodeStack = (union Data*)createSingleLinkedStack(context); + tree->put = C_putRedBlackTree; + // tree->get = C_getRedBlackTree; + // tree->remove = C_removeRedBlackTree; + // tree->clear = C_clearRedBlackTree; + return tree; +} + +void printNode(struct Node* node) { + if (node == NULL) { + printf("leaf"); + } else { + printf("((%d,%d (",node->color, node->key); + printNode(node->right); + printf(") ("); + printNode(node->left); + printf(")"); + } +} + +void printTree(struct RedBlackTree* tree) { + printf("\n"); + tree->current = tree->root; + printNode(tree->current); + printf(")\n"); +} + +__code putRedBlackTree(struct RedBlackTree* tree, struct Node* node, __code next(...)) { + printf("C_putRedBlackTree\n"); + printf("value->%d,key->%d \n",node->value,node->key); + tree->previous = tree->newNode; + tree->newNode = node; + tree->newNode->color = Red; + tree->current = tree->root; + goto insertRBTree(node, tree); +} + +__code stackClear(struct RedBlackTree* tree, struct Stack* nodeStack, __code next(...)) { + tree->current = 0; + nodeStack->stack = tree->nodeStack; + nodeStack->next = next; + goto meta(context, tree->nodeStack->clear); + } + +__code getRedBlackTree(struct RedBlackTree* tree, __code next(...)) { + if (tree->root) { + tree->current = tree->root; + goto insertRBTree(); + // goto deleteRBTree(); + } + goto next(...); +} + +__code insertRBTree(struct Node* node, struct RedBlackTree* tree, struct Stack* stack, __code next(...)) { + // first case tree->current = root; + printf("C_insertRBTree\n"); + printf("value->%d,key->%d\n",node->value,node->key); + printf("newNode value->%d,newNode key->%d\n",tree->newNode->value,tree->newNode->key); + + if (tree->root == NULL) { + printf("insertRBTree_root eq NULL\n"); + tree->root = tree->newNode; + tree->root->color = Black; + printf("tree->root->key = %d, tree->root->color = %d \n",tree->root->key,tree->root->color); + printTree(tree); + goto next(tree,...); + } else { + goto searchInsertLocation(node, tree, stack); + } +} + +__code insertRBTree_stub(struct Context* context) { + Node* node = Gearef(context, Tree)->node; + RedBlackTree* tree = (RedBlackTree*)GearImpl(context, Tree, tree); + Stack* stack = createSingleLinkedStack(context); + enum Code next = Gearef(context, Tree)->next; + goto insertRBTree(context, node, tree, stack, next); +} + +__code searchInsertLocation(struct Node* node, struct RedBlackTree* tree) { + // first case tree->current = root; PreCase remove root=NULL case.don't exist firstCase tree->current=NULL + printf("C_searchInsertLocation\n"); + printf("nownode->key %d , previous->key %d \n",tree->newNode->key,tree->previous->key); + + tree->result = compare(tree->current, node); + printf("tree->current->key = %d, node->key %d\n",tree->current->key,node->key); + printf("compare (%d,%d)\n",tree->current,node); + + Stack* stack = tree->nodeStack; + + if (tree->current == NULL) { + printf("goto insertLocationBackInsert stack->pop\n"); + goto stack->pop(insertLocationBackInsert); + } + if (tree->result == GT) { + printf("GT searchInsertLocation\n"); + tree->current = tree->current->right; + goto stack->push(tree->newNode,insertLocationBackInsert); + } else if (tree->result == LT) { + printf("LT searchInsertLocation\n"); + tree->current = tree->current->left; + goto stack->push(tree->newNode, searchInsertLocation); + } else if (tree->result == EQ) { + printf("already member this node : __code searchInsertLocation()\n"); + goto meta(context, C_exit_code); + } else { + printf("$insert value tree : __code searchInsertLocation() \n"); + goto meta(context, C_exit_code); + } +} + +__code searchInsertLocation_stub(struct Context* context) { + Node* node = Gearef(context, Tree)->node; + RedBlackTree* tree = (RedBlackTree*)GearImpl(context, Tree, tree); + Stack* stack = (struct Stack*)Gearef(context, Stack)->stack; + goto searchInsertLocation(context, node, tree); +} + +__code insertLocationBackInsert(struct RedBlackTree* tree, struct Node* node, struct Stack* stack) { + printf("C_insertLocationBackInsert\n"); + struct Node* hoge = stack->data; + printf("stackpopdata%d\n",stack->data); + tree->current = tree->previous; + // tree->current = nodeStack->data; + // this CS is ones only backTrace, and insert node + tree->result = compare(tree->previous,tree->newNode); + printf("back,compare\n"); + if (tree->result == GT) { + printf("GT\n"); + tree->current->right = tree->newNode; + printTree(tree); + goto insertBalance(tree, stack, node, next); + } else if (tree->result == LT) { + printf("LT\n"); + tree->current->left = tree->newNode; + goto insertBalance(tree, stack, node, next); + } else { + printf("error : __code insertLocationBackTrace() \n"); + goto meta(context, C_exit_code); + } +} + +__code insertLocationBackInsert_stub(struct Context* context) { + RedBlackTree* tree = (RedBlackTree*)GearImpl(context, Tree, tree); + SingleLinkedStack* singleLinkedStack = (SingleLinkedStack*)GearImpl(context, Stack, stack); + Node* node = Gearef(context, Tree)->node; + Stack* stack = (struct Stack*)Gearef(context, Stack)->stack; + goto insertLocationBackInsert(context, tree, node, stack); +} + +__code insertBalance(struct RedBlackTree* tree, struct Node* nodeStack, struct Node* node, __code next(...)) { + printf("C_insertBalance\n"); + struct Node* traceNode = tree->nodeStack->data; + tree->current = traceNode; + struct Stack* stack = tree->nodeStack; + + // exit insertion code + if (tree->current == tree->root) { + tree->current->color = Black; + printTree(tree); + //printTree + goto next(tree,...); + } + + + //current color eq Red + if (tree->current->color == Red) + goto stack->pop(insertBalance); + + // current color eq Black + if (tree->current->left->left || tree->current->left->right) { + goto insertBalanceLeft(tree,nodeStack); + } else if (tree->current->right->left || tree->current->right->right) { + goto insertBalanceRight(tree,nodeStack); + } else { + goto stack->pop(insertBalance); + } +} + +__code insertBalanceLeft(struct RedBlackTree* tree, struct Node* nodeStack, struct Node* node) { + printf("C_insertBalanceLeft\n"); + struct Stack* stack = tree->nodeStack; + + if (tree->current->color == Black && tree->current->left->color == Red && tree->current->left->left->color == Red) { + struct Node* tmpCurrent = tree->current; + struct Node* tmpLeft = tree->current->left; + struct Node* tmpLeftLeft = tree->current->left->left; + + tree->current = tmpLeft; + tree->current->right = tmpCurrent; + tree->current->left = tmpLeftLeft; + tree->current->right->left = tmpLeft->right; + tree->current->color = Red; + tree->current->left->color = Black; + tree->current->right->color = Black; + goto stack->pop(insertBalance); + + } else if(tree->current->color == Black && tree->current->left->color == Red && tree->current->left->right->color == Red) { + struct Node* tmpCurrent = tree->current; + struct Node* tmpLeft = tree->current->left; + struct Node* tmpLeftRight = tree->current->left->right; + + tree->current = tmpLeft; + tree->current->right = tmpCurrent; + tree->current->left = tmpLeftRight; + tree->current->right->left = tmpLeft->left; + tree->current->color = Red; + tree->current->left->color = Black; + tree->current->right->color = Black; + goto stack->pop(insertBalance); + + } +} + +__code insertBalanceRight(struct RedBlackTree* tree, struct Node* nodeStack, struct Node* node) { + printf("C_insertBalanceLeft\n"); + struct Stack* stack = tree->nodeStack; + + if (tree->current->color == Black && tree->current->right->color == Red && tree->current->right->right->color == Red) { + struct Node* tmpCurrent = tree->current; + struct Node* tmpRight = tree->current->right; + struct Node* tmpRightRight = tree->current->right->right; + + tree->current = tmpRight; + tree->current->left = tmpCurrent; + tree->current->right = tmpRightRight; + tree->current->left->right = tmpRight->left; + tree->current->color = Red; + tree->current->left->color = Black; + tree->current->right->color = Black; + goto stack->pop(insertBalance); + + } else if (tree->current->color == Black && tree->current->right->color == Red && tree->current->right->left->color == Red) { + + struct Node* tmpCurrent = tree->current; + struct Node* tmpRight = tree->current->right; + struct Node* tmpRightLeft = tree->current->right->left; + + tree->current = tmpRight; + tree->current->right = tmpCurrent; + tree->current->left = tmpRightLeft; + tree->current->left->right = tmpRight->right; + tree->current->color = Red; + tree->current->left->color = Black; + tree->current->right->color = Black; + goto stack->pop(insertBalance); + + } else { + printf("unkwon error : __code insertBalanceRight() \n"); + goto meta(context, C_exit_code); + } +} +// insertCode end