changeset 133:568730b1239e

call stack interface in rb_tree
author mir3636
date Mon, 07 Nov 2016 21:12:19 +0900
parents 7c309e1aea73
children 2eccf4564efe
files src/parallel_execution/context.h src/parallel_execution/rb_tree.c src/parallel_execution/stack.c
diffstat 3 files changed, 68 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/parallel_execution/context.h	Thu Oct 27 18:54:11 2016 +0900
+++ b/src/parallel_execution/context.h	Mon Nov 07 21:12:19 2016 +0900
@@ -10,21 +10,29 @@
 #define NEW(type) (type*)(calloc(1, sizeof(type)))
 #define NEWN(n, type) (type*)(calloc(n, sizeof(type)))
 
-#define ALLOC_DATA(context, dseg) ({ context->data[dseg] = context->heap; context->heap += sizeof(struct dseg); (struct dseg *)context->data[dseg]; })
+#define ALLOC_DATA(context, dseg) ({\
+    struct Meta* meta = (struct Meta*)context->heap;\
+    meta->type = dseg;\
+    context->heap += sizeof(struct Meta);\
+    context->data[dseg] = context->heap; context->heap += sizeof(struct dseg); (struct dseg *)context->data[dseg]; })
 
-#define ALLOC_DATA_TYPE(context, dseg, type) ({ context->data[dseg] = context->heap; context->heap += sizeof(struct type); (struct type *)context->data[dseg]; })
+#define ALLOC_DATA_TYPE(context, dseg, t) ({\
+    struct Meta* meta = (struct Meta*)context->heap;\
+    meta->type = t;\
+    context->heap += sizeof(struct Meta);\
+    context->data[dseg] = context->heap; context->heap += sizeof(struct t); (struct t *)context->data[dseg]; })
 
 #define ALLOCATE(context, t) ({ \
-  union Data* data = context->heap; \
-  context->heap += sizeof(struct t) + ((void *)(&data->element) - (void *)data) ; \
-  data->type = t; \
-  data; })
+    struct Meta* meta = (struct Meta*)context->heap;\
+    context->heap += sizeof(struct Meta);\
+    union Data* data = context->heap; \
+    context->heap += sizeof(struct t); \
+    meta->type = t; \
+    data; })
 
-#define GET_DATA(spesificData) ({ \
-  union Data dummy; \
-  void* ptr = (void *)spesificData; \
-  ptr -= (void *)(&dummy.element) - (void *)(&dummy); \
-  (union Data*)ptr; })
+#define GET_TYPE(dseg) ({ \
+    struct Meta* meta = (struct Meta*)(((void*)dseg) - sizeof(struct Meta));\
+    meta->type; })
 
 enum Code {
     Code1,
@@ -141,7 +149,9 @@
 };
 
 union Data {
-    enum DataType type;
+    struct Meta {
+        enum DataType type;
+    } meta;
     struct Time {
         enum Code next;
         double time;
@@ -180,9 +190,13 @@
     struct Stack {
         union Data* stack;
         union Data* data;
+        union Data* data1;
         enum Code push;
         enum Code pop;
+        enum Code pop2;
         enum Code isEmpty;
+        enum Code whenEmpty;
+        enum Code get2;
         enum Code next;
     } stack;
     // Stack implementations
--- a/src/parallel_execution/rb_tree.c	Thu Oct 27 18:54:11 2016 +0900
+++ b/src/parallel_execution/rb_tree.c	Mon Nov 07 21:12:19 2016 +0900
@@ -12,9 +12,9 @@
         printf("NULL");
     } else {
         printf("key = %d (", node->key);
-        printTree1(GET_DATA(node->right));
+        printTree1((union Data*)(node->right));
         printf("), (");
-        printTree1(GET_DATA(node->left));
+        printTree1((union Data*)(node->left));
         printf(")");
     }
 }
@@ -25,13 +25,13 @@
 }
 
 __code put(struct Context* context, struct Stack* nodeStack,  struct Tree* tree, struct Node* node, struct Traverse* traverse, struct Node* root, struct Node* newNode) {
-    printTree(GET_DATA(tree->root));
+    printTree((union Data*)(tree->root));
     traverse->newNode = newNode;
     tree->root = newNode; // this should done at stackClear
     if (root) {
         traverse->current = root;
         traverse->result = compare(traverse->current, node);
-        nodeStack->data = GET_DATA(newNode);
+        nodeStack->data = (union Data*)(newNode);
         nodeStack->next = Replace1;
         goto meta(context, nodeStack->push);
     }
@@ -55,7 +55,7 @@
 
 __code replaceNode(struct Context* context, struct Traverse* traverse, struct Node* oldNode, struct Node* newNode, struct Stack* nodeStack) {
     *newNode = *oldNode;
-    nodeStack->data = GET_DATA(newNode);
+    nodeStack->data = (union Data*)(newNode);
     nodeStack->next = Replace1;
     goto meta(context, nodeStack->push);
 }
--- a/src/parallel_execution/stack.c	Thu Oct 27 18:54:11 2016 +0900
+++ b/src/parallel_execution/stack.c	Mon Nov 07 21:12:19 2016 +0900
@@ -9,7 +9,9 @@
     singleLinkedStack->top = NULL;
     stack->push = PushSingleLinkedStack;
     stack->pop  = PopSingleLinkedStack;
-    stack->isEmpty = _;
+    stack->pop2  = Pop2SingleLinkedStack;
+    stack->get2  = Get2SingleLinkedStack;
+    stack->isEmpty = IsEmptySingleLinkedStack;
     return GET_DATA(stack);
 }
 
@@ -42,15 +44,46 @@
                                context->data[Stack]->stack.next);
 }
 
-__code isEmptySingleLinkedStack(struct Context* context, struct SingleLinkedStack* stack, union Data** data, enum Code next) {
+__code pop2SingleLinkedStack(struct Context* context, struct SingleLinkedStack* stack, union Data** data, union Data** data1, enum Code next) {
+    *data = stack->top;
+    *data1 = stack->top->next;
+    stack->top = data1->next;
+    goto meta(context, next);
+}
+
+__code pop2SingleLinkedStack_stub(struct Context* context) {
+    goto popSingleLinkedStack(context,
+                               (struct SignleLinkedStack *)context->data[Stack]->stack.stack,
+                               &context->data[Stack]->stack.data,
+                               &context->data[Stack]->stack.data1,
+                               context->data[Stack]->stack.next);
+}
+
+__code get2SingleLinkedStack(struct Context* context, struct SingleLinkedStack* stack, union Data** data, union Data** data1, enum Code next) {
     *data = stack->top;
+    *data1 = stack->top->next;
     goto meta(context, next);
 }
 
+__code get2SingleLinkedStack_stub(struct Context* context) {
+    goto popSingleLinkedStack(context,
+                               (struct SignleLinkedStack *)context->data[Stack]->stack.stack,
+                               &context->data[Stack]->stack.data,
+                               &context->data[Stack]->stack.data1,
+                               context->data[Stack]->stack.next);
+}
+
+__code isEmptySingleLinkedStack(struct Context* context, struct SingleLinkedStack* stack, enum Code next,enum Code whenEmpty) {
+    if (stack->top)
+        goto meta(context, next);
+    else
+        goto meta(context, whenEmpty);
+}
+
 __code isEmptySingleLinkedStack_stub(struct Context* context) {
     goto isEmptySingleLinkedStack(context,
                                (struct SignleLinkedStack *)context->data[Stack]->stack.stack,
-                               &context->data[Stack]->stack.data,
-                               context->data[Stack]->stack.next);
+                               context->data[Stack]->stack.next,
+                               context->data[Stack]->stack.whenEmpty);
 }