Mercurial > hg > CbC > old > akasha
changeset 27:a416dd4093cf
Remove deep copy functions
author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 26 Apr 2016 11:21:47 +0900 |
parents | cf7cbe70404f |
children | 04283ef8f3ca |
files | src/insert_verification/akashaCS.c src/insert_verification/main.c |
diffstat | 2 files changed, 1 insertions(+), 143 deletions(-) [+] |
line wrap: on
line diff
--- a/src/insert_verification/akashaCS.c Sat Apr 16 09:44:48 2016 +0900 +++ b/src/insert_verification/akashaCS.c Tue Apr 26 11:21:47 2016 +0900 @@ -9,12 +9,6 @@ /* utils */ -void* akashaMalloc(struct Context* akashaContext, unsigned int size) { - akashaContext->data[++akashaContext->dataNum] = akashaContext->heap; - akashaContext->heap += size; - return akashaContext->data[akashaContext->dataNum]; -} - bool eqNode(struct Node* n1, struct Node* n2) { if ((n1 == NULL) && (n2 == NULL)) return true; if (n1->key != n2->key) return false; @@ -58,133 +52,6 @@ /* C function */ -struct Node* nodeDeepCopy(struct Context* newContext, struct Node* oldNode) { - if (oldNode == NULL) return NULL; - - struct Allocate* allocate = &newContext->data[Allocate]->allocate; - allocate->size = sizeof(Node); - allocator(newContext); - - struct Node* newNode = (struct Node*)newContext->data[newContext->dataNum]; - 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; -} - -struct Tree* treeDeepCopy(struct Context* newContext, struct Tree* oldTree) { - if (oldTree == NULL) return NULL; - - struct Allocate* allocate = &newContext->data[Allocate]->allocate; - allocate->size = sizeof(Node); - allocator(newContext); - - struct Tree* newTree = (struct Tree*)newContext->data[newContext->dataNum]; - 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); - - return newTree; -} - -void iterElemDeepCopy(struct Context* newContext, struct Iterator* newIter, struct Iterator *oldIter) { - newIter->head = (struct IterElem*)akashaMalloc(newContext, sizeof(struct IterElem)); - - newIter->head->val = oldIter->head->val; - struct IterElem *oldElem = oldIter->head; - struct IterElem *newElem = newIter->head; - while (1) { - if (oldElem->next->val == oldIter->head->val) break; - newElem->next = akashaMalloc(newContext, sizeof(struct IterElem)); - - newElem->next->val = oldElem->next->val; - newElem = newElem->next; - oldElem = oldElem->next; - } - newElem->next = newIter->head; - - while (newElem->val != oldIter->last->val) { - newElem = newElem->next; - } - newIter->last = newElem; -} - -void iterDeepCopy(struct Context* newContext, struct Iterator* newIter, struct Iterator* oldIter) { - newIter->tree = treeDeepCopy(newContext, oldIter->tree); - - newIter->iteratedValue = oldIter->iteratedValue; - iterElemDeepCopy(newContext, newIter, oldIter); - - if (oldIter->previousDepth != NULL) { - newIter->previousDepth = akashaMalloc(newContext, sizeof(struct Iterator)); - iterDeepCopy(newContext, newIter->previousDepth, oldIter->previousDepth); - } else { - newIter->previousDepth = NULL; - } -} - -void memoryRefresh(struct Context* oldContext) { - if (oldContext->node_stack->num != 0) { abort(); } - if (oldContext->code_stack->num != 0) { abort(); } - - struct Context* newContext = (struct Context*)malloc(sizeof(struct Context)); - - newContext->dataLimit = oldContext->dataLimit; - newContext->heapLimit = oldContext->heapLimit; - newContext->code = oldContext->code; - newContext->data = malloc(newContext->dataLimit * sizeof(union Data*)); - newContext->heapStart = malloc(newContext->heapLimit); - newContext->codeNum = Exit; - newContext->heap = newContext->heapStart; - memset(newContext->heapStart, newContext->heapLimit, 0); - - printf("reallocation! : %lu\n", newContext->heapLimit); - - newContext->data[Allocate] = newContext->heap; - newContext->heap += sizeof(struct Allocate); - newContext->data[Allocate]->allocate = oldContext->data[Allocate]->allocate; - - newContext->data[Tree] = newContext->heap; - newContext->heap += sizeof(struct Tree); - - newContext->data[Node] = newContext->heap; - newContext->heap += sizeof(struct Node); - - newContext->data[Iter] = newContext->heap; - newContext->heap += sizeof(struct Iterator); - - newContext->dataNum = Iter; - - newContext->data[Node]->node = *nodeDeepCopy(newContext, &oldContext->data[Node]->node); - newContext->data[Tree]->tree = *treeDeepCopy(newContext, &oldContext->data[Tree]->tree); - if (!eqTree(&newContext->data[Tree]->tree, &oldContext->data[Tree]->tree)) abort(); - iterDeepCopy(newContext, &newContext->data[Iter]->iterator, &oldContext->data[Iter]->iterator); - if (!eqIter(&newContext->data[Iter]->iterator, &oldContext->data[Iter]->iterator)) abort(); - - newContext->node_stack = oldContext->node_stack; - newContext->code_stack = oldContext->code_stack; - newContext->next = oldContext->next; - free(oldContext->data); - free(oldContext->heapStart); - *oldContext = *newContext; -} - void validateNodePointer(struct Node* node) { if (node == NULL) return; validateNodePointer(node->left); @@ -207,10 +74,6 @@ /* Code Segments */ __code meta(struct Context* context, enum Code next) { - if (next == Put) { - findInvalidPointer(context); - if (context->dataNum > (context->dataLimit * 0.9)) memoryRefresh(context); - } findInvalidPointer(context); context->prev = next; goto (context->code[next])(context);
--- a/src/insert_verification/main.c Sat Apr 16 09:44:48 2016 +0900 +++ b/src/insert_verification/main.c Tue Apr 26 11:21:47 2016 +0900 @@ -4,8 +4,6 @@ #include "akashaLLRBContext.h" #include "akashaCS.h" -extern struct Tree* treeDeepCopy(struct Context* newContext, struct Tree* oldTree); // FIXME -extern bool eqTree(struct Tree*, struct Tree*); extern void allocator(struct Context* context); void showTrace(struct Iterator* iter); //FIXME @@ -184,10 +182,7 @@ __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. - allocate->size = sizeof(Tree); - allocator(context); - iter->previousDepth->tree = treeDeepCopy(context, tree); - if (!eqTree(iter->previousDepth->tree, tree)) abort(); + iter->previousDepth->tree = tree; goto meta(context, PutAndGoToNextDepth); }