Mercurial > hg > Members > innparusu > Gears
diff src/llrb/llrb.c @ 72:5c4b9d116eda
use stack for code segment
author | Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 10 Nov 2015 01:59:04 +0900 |
parents | 368306e1bfed |
children | 2667c3251a00 |
line wrap: on
line diff
--- a/src/llrb/llrb.c Tue Oct 27 01:15:29 2015 +0900 +++ b/src/llrb/llrb.c Tue Nov 10 01:59:04 2015 +0900 @@ -2,18 +2,18 @@ #include "llrbContext.h" #include "origin_cs.h" -#include "stack.h" extern void allocator(struct Context* context); extern void compare(struct Context* context, struct Tree* tree, int key1, int key2); extern int num; -extern stack_ptr node_stack; __code put(struct Context* context, struct Tree* tree, struct Allocate* allocate) { allocate->size = sizeof(struct Node); allocator(context); + stack_push(context->code_stack, &context->next); + if (tree->root == 0) { goto meta(context, Insert); } else { @@ -29,28 +29,15 @@ } __code replaceNode(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode, int result) { - stack_push(node_stack, &newNode); + stack_push(context->node_stack, &newNode); *newNode = *oldNode; if (result == 0) { - stack_pop(node_stack, &tree->current); + stack_pop(context->node_stack, &tree->current); newNode->value = context->data[Node]->node.value; - if (tree->current->right != 0) - if (tree->current->right->color == Red) - goto meta(context, RotateL); - - if (tree->current->left != 0) - if (tree->current->left->left != 0) - if (tree->current->left->color == Red && tree->current->left->left->color == Red) - goto meta(context, RotateR); - - if (tree->current->right != 0 && tree->current->left != 0) - if (tree->current->right->color == Red && tree->current->left->color == Red) - goto meta(context, ColorFlip); - - goto meta(context, FixUp); + goto meta(context, Balance1); } else if (result == 1) { tree->current = oldNode->right; newNode->right = context->heap; @@ -62,7 +49,7 @@ allocator(context); if (tree->current == 0) { - stack_pop(node_stack, &tree->current); + stack_pop(context->node_stack, &tree->current); goto meta(context, Insert); } else { compare(context, tree, tree->current->key, context->data[Node]->node.key); @@ -75,6 +62,55 @@ goto replaceNode(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node, context->data[Tree]->tree.result); } +__code balance1(struct Context* context, struct Node* current) { + if (current->right != 0) + if (current->right->color == Red) { + context->next = Balance2; + + stack_push(context->code_stack, &context->next); + goto meta(context, RotateL); + } + + goto meta(context, Balance2); +} + +__code balance1_stub(struct Context* context) { + goto balance1(context, context->data[Tree]->tree.current); +} + +__code balance2(struct Context* context, struct Node* current) { + if (current->left != 0) + if (current->left->left != 0) + if (current->left->color == Red && current->left->left->color == Red) { + context->next = Balance3; + + stack_push(context->code_stack, &context->next); + goto meta(context, RotateR); + } + + goto meta(context, Balance3); +} + +__code balance2_stub(struct Context* context) { + goto balance2(context, context->data[Tree]->tree.current); +} + +__code balance3(struct Context* context, struct Node* current){ + if (current->right != 0 && current->left != 0) + if (current->right->color == Red && current->left->color == Red) { + context->next = FixUp; + + stack_push(context->code_stack, &context->next); + goto meta(context, ColorFlip); + } + + goto meta(context, FixUp); +} + +__code balance3_stub(struct Context* context) { + goto balance3(context, context->data[Tree]->tree.current); +} + __code insertNode(struct Context* context, struct Tree* tree, struct Node* node, struct Node* newNode) { node->color = Red; *newNode = *node; @@ -83,24 +119,12 @@ newNode->color = Black; tree->root = newNode; tree->current = 0; + + stack_pop(context->code_stack, &context->next); goto meta(context, context->next); } - if (tree->current->right != 0) - if (tree->current->right->color == Red) - goto meta(context, RotateL); - - - if (tree->current->left != 0) - if (tree->current->left->left != 0) - if (tree->current->left->color == Red && tree->current->left->left->color == Red) - goto meta(context, RotateR); - - if (tree->current->right != 0 && tree->current->left != 0) - if (tree->current->right->color == Red && tree->current->left->color == Red) - goto meta(context, ColorFlip); - - goto meta(context, FixUp); + goto meta(context, Balance1); } __code insertNode_stub(struct Context* context) { @@ -115,16 +139,8 @@ node->color = Red; tree->current = tmp; - if (tree->current->left != 0) - if (tree->current->left->left != 0) - if (tree->current->left->color == Red && tree->current->left->left->color == Red) - goto meta(context, RotateR); - - if (tree->current->right != 0 && tree->current->left != 0) - if (tree->current->right->color == Red && tree->current->left->color == Red) - goto meta(context, ColorFlip); - - goto meta(context, FixUp); + stack_pop(context->code_stack, &context->next); + goto meta(context, context->next); } __code rotateLeft_stub(struct Context* context) { @@ -138,12 +154,9 @@ tmp->color = node->color; node->color = Red; context->data[Tree]->tree.current = tmp; - - if (tree->current->right != 0 && tree->current->left != 0) - if (tree->current->right->color == Red && tree->current->left->color == Red) - goto meta(context, ColorFlip); - goto meta(context, FixUp); + stack_pop(context->code_stack, &context->next); + goto meta(context, context->next); } __code rotateRight_stub(struct Context* context) { @@ -154,8 +167,9 @@ node->color ^= 1; node->left->color ^= 1; node->right->color ^= 1; - - goto meta(context, FixUp); + + stack_pop(context->code_stack, &context->next); + goto meta(context, context->next); } __code colorFlip_stub(struct Context* context) { @@ -166,7 +180,7 @@ node->key = newNode->key; tree->prev = newNode; - if (stack_pop(node_stack, &tree->current) == 0) { + if (stack_pop(context->node_stack, &tree->current) == 0) { compare(context, tree, tree->current->key, node->key); goto meta(context, ChangeRef); } @@ -175,6 +189,7 @@ tree->root = newNode; tree->current = 0; + stack_pop(context->code_stack, &context->next); goto meta(context, context->next); } @@ -191,20 +206,7 @@ perror("bad status"); } - if (tree->current->right != 0) - if (tree->current->right->color == Red) - goto meta(context, RotateL); - - if (tree->current->left != 0) - if (tree->current->left->left != 0) - if (tree->current->left->color == Red && tree->current->left->left->color == Red) - goto meta(context, RotateR); - - if (tree->current->right != 0 && tree->current->left != 0) - if (tree->current->right->color == Red && tree->current->left->color == Red) - goto meta(context, ColorFlip); - - goto meta(context, FixUp); + goto meta(context, Balance1); } __code changeReference_stub(struct Context* context) { @@ -238,6 +240,8 @@ allocate->size = sizeof(struct Node); allocator(context); + stack_push(context->code_stack, &context->next); + tree->current = tree->root; compare(context, tree, tree->current->key, context->data[Node]->node.key); goto meta(context, Replace_d);