Mercurial > hg > Members > Moririn
view src/llrb/llrb.c @ 59:e8bf3ee224e7
merge
author | Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 16 Jun 2015 16:00:38 +0900 |
parents | c469c5ed5b4d |
children | 4e5b425922ce |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include "llrbContext.h" #include "allocate.h" #include "origin_cs.h" #include "stack.h" #define NUM 100 extern __code initLLRBContext(struct Context* context); /* __code code1(Allocate allocate) { allocate.size = sizeof(long); allocate.next = Code2; goto Allocate(allocate); } */ static double st_time; static double ed_time; static int num; static clock_t c1,c2; static stack_ptr pstack; static struct Node* pre; static double getTime() { struct timeval tv; gettimeofday(&tv, NULL); return tv.tv_sec + (double)tv.tv_usec*1e-6; } void print_tree(struct Node* node, int n) { if (node != 0) { print_tree(node->left, n+1); for (int i=0;i<n;i++) printf(" "); printf("key=%d depth=%d\t%p\n", node->key, n, node); print_tree(node->right, n+1); } } __code code1(struct Context* context, struct Allocate *allocate) { allocate->size = sizeof(struct Count); context->next[context->current++] = Code2; goto meta(context, Allocator); } __code code1_stub(struct Context* context) { goto code1(context, &context->data[Allocate]->allocate); } /* __code code2(Allocate allocate, Count count) { count.count = 0; goto code3(count); } */ __code code2(struct Context* context, struct Count* count) { count->i = 1; goto meta(context, Code3); } __code code2_stub(struct Context* context) { goto code2(context, &context->data[context->dataNum]->count); } __code code3(struct Context* context, struct Node* node, struct Count* count) { if (count->i == num) { goto meta(context, Code4); } context->next[context->current++] = Code3; node->key = count->i; node->value = count->i; count->i++; goto meta(context, Put); } __code code3_stub(struct Context* context) { goto code3(context, &context->data[Node]->node, &context->data[3]->count); } __code meta(struct Context* context, enum Code next) { goto (context->code[next])(context); } __code put(struct Context* context, struct Tree* tree, struct Allocate* allocate) { allocate->size = sizeof(struct Node); if (tree->root == 0) { context->next[context->current++] = Insert; } else { context->next[context->current++] = Compare; context->next[context->current++] = Replace; tree->current = tree->root; } goto meta(context, Allocator); } __code put_stub(struct Context* context) { goto put(context, &context->data[Tree]->tree, &context->data[Allocate]->allocate); } __code replaceNode(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode, int result) { stack_push(pstack, &newNode); *newNode = *oldNode; if (result == 0) { stack_pop(pstack, &tree->current); goto meta(context, RotateL); } else if (result == 1) { tree->current = oldNode->right; newNode->right = context->heap; } else { tree->current = oldNode->left; newNode->left = context->heap; } if (tree->current == 0) { stack_pop(pstack, &tree->current); context->next[context->current++] = Insert; } else { context->next[context->current++] = Compare; context->next[context->current++] = Replace; } goto meta(context, Allocator); } __code replaceNode_stub(struct Context* context) { goto replaceNode(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node, context->data[Tree]->tree.result); } __code insertNode(struct Context* context, struct Tree* tree, struct Node* node, struct Node* newNode) { node->color = Red; *newNode = *node; if (tree->root == 0) { newNode->color = Black; tree->root = newNode; goto meta(context, context->next[--context->current]); } goto meta(context, RotateL); } __code insertNode_stub(struct Context* context) { goto insertNode(context, &context->data[Tree]->tree, &context->data[Node]->node, &context->data[context->dataNum]->node); } __code compare(struct Context* context, struct Tree* tree, int key1, int key2) { if (key1 == key2) { tree->result = 0; } else if (key1 < key2) { tree->result = 1; } else { tree->result = -1; } goto meta(context, context->next[--context->current]); } __code compare_stub(struct Context* context) { goto compare(context, &context->data[Tree]->tree, context->data[Tree]->tree.current->key, context->data[Node]->node.key); } __code rotateLeft(struct Context* context, struct Node* node) { if (node->right != 0) { if (node->right->color == Red) { struct Node* tmp = node->right; node->right = tmp->left; tmp->left = node; tmp->color = tmp->left->color; tmp->left->color = Red; context->data[Tree]->tree.current = tmp; } } goto meta(context, RotateR); } __code rotateLeft_stub(struct Context* context) { goto rotateLeft(context, context->data[Tree]->tree.current); } __code rotateRight(struct Context* context, struct Node* node) { if (node->left != 0) { if (node->left->left != 0) { if (node->left->color == Red && node->left->left->color == Red) { struct Node* tmp = node->left; node->left = tmp->right; tmp->right = node; tmp->color = tmp->right->color; tmp->right->color = Red; context->data[Tree]->tree.current = tmp; } } } goto meta(context, ColorFlip); } __code rotateRight_stub(struct Context* context) { goto rotateRight(context, context->data[Tree]->tree.current); } __code colorFlip(struct Context* context, struct Node* node) { if (node->right != 0 && node->left != 0) { if (node->right->color == Red && node->left->color == Red) { node->color ^= 1; node->left->color ^= 1; node->right->color ^= 1; } } goto meta(context, FixUp); } __code colorFlip_stub(struct Context* context) { goto colorFlip(context, context->data[Tree]->tree.current); } __code fixUp(struct Context* context, struct Tree* tree, struct Node* node, struct Node* newNode) { node->key = newNode->key; tree->prev = newNode; if (stack_pop(pstack, &tree->current) == 0) { context->next[context->current++] = ChangeRef; goto meta(context, Compare); } tree->root = newNode; goto meta(context, context->next[--context->current]); } __code fixUp_stub(struct Context* context) { goto fixUp(context, &context->data[Tree]->tree, &context->data[Node]->node, context->data[Tree]->tree.current); } __code changeReference(struct Context* context, struct Tree* tree, struct Node* node, int result) { if (result == 1) { node->right = tree->prev; } else if (result == -1) { node->left = tree->prev; } else { perror("bad status"); } goto meta(context, RotateL); } __code changeReference_stub(struct Context* context) { goto changeReference(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, context->data[Tree]->tree.result); } /* __code get(struct Context* context) { */ /* context->data[Tree]->tree.current = context->data[Tree]->tree.root; */ /* context->data[Next]->next = context->data[Allocate]->allocate.next; */ /* context->data[Allocate]->allocate.next = Traverse; */ /* goto meta(context, Compare); */ /* } */ /* __code traverse(struct Context* context) { */ /* int result = context->data[Tree]->tree.result; */ /* struct Tree* tree = &context->data[Tree]->tree; */ /* if (result == 0) { */ /* goto meta(context, context->data[Next]->next); */ /* } else if (result == 1) { */ /* tree->current = tree->current->right; */ /* } else { */ /* tree->current = tree->current->left; */ /* } */ /* if(tree->current == 0) { */ /* goto meta(context, context->data[Next]->next); */ /* } */ /* goto meta(context, Compare); */ /* } */ /* __code delete(struct Context* context) { */ /* goto meta(context, Get); */ /* } */ __code code4(struct Context* context) { puts("---before---"); print_tree(context->data[Tree]->tree.root, 0); struct Allocate* allocate = &context->data[Allocate]->allocate; allocate->size = sizeof(struct Node); allocate->next = Code5; struct Node* node = &context->data[Node]->node; node->key = 0; node->value = 0; goto meta(context, Put); } __code code5(struct Context* context) { puts("---after---"); print_tree(context->data[Tree]->tree.root, 0); puts("--Number of Data--"); printf("%d\n", context->dataNum); stack_free(pstack); goto meta(context, Exit); } /* __code code6(struct Context* context) { */ /* puts("---get---"); */ /* print_tree(context->data[Tree]->tree.current, 0); */ /* goto meta(context, Exit); */ /* } */ int main(int argc, char** argv) { num = (int)atoi(argv[1]); pstack = stack_init(sizeof(union Data*), num); struct Context* context = (struct Context*)malloc(sizeof(struct Context)); initLLRBContext(context); goto start_code(context, Code1); }