changeset 167:34562e63981f

create queue.c
author mir3636
date Mon, 21 Nov 2016 18:03:43 +0900
parents 06cfc1289361
children fa7419e2c67c
files src/parallel_execution/context.c src/parallel_execution/context.h src/parallel_execution/queue.c src/parallel_execution/queue.h
diffstat 4 files changed, 121 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/parallel_execution/context.c	Fri Nov 18 19:34:44 2016 +0900
+++ b/src/parallel_execution/context.c	Mon Nov 21 18:03:43 2016 +0900
@@ -2,6 +2,7 @@
 
 #include "context.h"
 #include "stack.h"
+#include "queue.h"
 
 extern __code code1_stub(struct Context*);
 extern __code code2_stub(struct Context*);
@@ -29,7 +30,6 @@
 extern __code insertCase4_stub(struct Context*);
 extern __code insertCase5_stub(struct Context*);
 extern __code insertCase51_stub(struct Context*);
-extern __code stackClear_stub(struct Context*);
 extern __code get_stub(struct Context*);
 extern __code search_stub(struct Context*);
 extern __code delete_stub(struct Context*);
@@ -102,6 +102,11 @@
     context->code[C_get2SingleLinkedStack] = get2SingleLinkedStack_stub;
     context->code[C_isEmptySingleLinkedStack] = isEmptySingleLinkedStack_stub;
 
+    context->code[C_clearSingleLinkedQueue] = clearSingleLinkedQueue_stub;
+    context->code[C_putSingleLinkedQueue] = putSingleLinkedQueue_stub;
+    context->code[C_takeSingleLinkedQueue] = takeSingleLinkedQueue_stub;
+    context->code[C_isEmptySingleLinkedQueue] = isEmptySingleLinkedQueue_stub;
+
     /* context->code[Delete]        = delete_stub; */
     /* context->code[Delete1]       = delete1_stub; */
     /* context->code[Delete2]       = delete2_stub; */
--- a/src/parallel_execution/context.h	Fri Nov 18 19:34:44 2016 +0900
+++ b/src/parallel_execution/context.h	Mon Nov 21 18:03:43 2016 +0900
@@ -174,12 +174,21 @@
         int idsCount;
     } Task;
     struct Queue {
-        struct Element* first;
-        struct Element* last;
-        int count;
         union Data* queue;
+        union Data* data;
+        enum Code whenEmpty;
+
+        enum Code clear;
+        enum Code put;
+        enum Code take;
+        enum Code isEmpty;
         enum Code next;
     } Queue;
+    struct SingleLinkedQueue {
+        struct Element* top;
+        struct Element* last;
+        enum Code next;
+    } SingleLinkedQueue;
     // Stack Interface
     struct Stack {
         union Data* stack;
@@ -265,6 +274,7 @@
 typedef struct CudaTask CudaTask;
 typedef struct Task Task;
 typedef struct Queue Queue;
+typedef struct SingleLinkedQueue SingleLinkedQueue;
 typedef struct Stack Stack;
 typedef struct SingleLinkedStack SingleLinkedStack;
 typedef struct ArrayStack ArrayStack;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/queue.c	Mon Nov 21 18:03:43 2016 +0900
@@ -0,0 +1,96 @@
+#include "context.h"
+#include "queue.h"
+#include "origin_cs.h"
+#include <stdio.h>
+
+union Data* createSingleLinkedQueue(struct Context* context) {
+    struct Queue* queue = &ALLOCATE(context, Queue)->queue;
+    struct SingleLinkedQueue* singleLinkedQueue = &ALLOCATE(context, SingleLinkedQueue)->singleLinkedQueue;
+    queue->queue = (union Data*)singleLinkedQueue;
+    singleLinkedQueue->top = NULL;
+    queue->get  = C_getSingleLinkedQueue;
+    queue->take  = C_takeSingleLinkedQueue;
+    queue->put  = C_putSingleLinkedQueue;
+    queue->isEmpty = C_isEmptySingleLinkedQueue;
+    queue->clear = C_clearSingleLinkedQueue;
+    return (union Data*)(queue);
+}
+
+void printQueue1(union Data* data) {
+    struct Node* node = &data->element.data->node;
+    if (node == NULL) {
+        printf("NULL");
+    } else {
+        printf("key = %d ,", node->key);
+        printQueue1((union Data*)data->element.next);
+    }
+}
+
+void printQueue(union Data* data) {
+    printQueue1(data);
+    printf("\n");
+}
+
+__code clearSingleLinkedQueue(struct Context* context, struct SingleLinkedQueue* queue,enum Code next) {
+    queue->top = NULL;
+    goto meta(context, next);
+}
+
+__code clearSingleLinkedQueue_stub(struct Context* context) {
+    goto clearSingleLinkedQueue(context, (struct SingleLinkedQueue *)context->data[D_Queue]->queue.queue->queue.queue, context->data[D_Queue]->queue.next);
+}
+
+__code putSingleLinkedQueue(struct Context* context, struct SingleLinkedQueue* queue, struct Element* element, union Data* data, enum Code next) {
+    element->next = NULL;
+    element->data = data;
+    if (queue->last) {
+        Element* last = queue->last;
+        last->next = element;
+        queue->last = element;
+    } else {
+        queue->top = element;
+        queue->last = element;
+    } 
+    goto meta(context, next);
+}
+
+__code putSingleLinkedQueue_stub(struct Context* context) {
+    struct Element* element = &ALLOCATE(context, Element)->element;
+    goto putSingleLinkedQueue(context,
+                               (struct SingleLinkedQueue *)context->data[D_Queue]->queue.queue->queue.queue,
+                               element,
+                               context->data[D_Queue]->queue.data,
+                               context->data[D_Queue]->queue.next);
+}
+
+__code takeSingleLinkedQueue(struct Context* context, struct SingleLinkedQueue* queue, union Data** data, enum Code next) {
+    if (queue->top) {
+        *data = queue->top->data;
+        queue->top = queue->top->next;
+    } else {
+        *data = NULL;
+    }
+    goto meta(context, next);
+}
+
+__code takeSingleLinkedQueue_stub(struct Context* context) {
+    goto takeSingleLinkedQueue(context,
+                               (struct SingleLinkedQueue *)context->data[D_Queue]->queue.queue->queue.queue,
+                               &context->data[D_Queue]->queue.data,
+                               context->data[D_Queue]->queue.next);
+}
+
+__code isEmptySingleLinkedQueue(struct Context* context, struct SingleLinkedQueue* queue, enum Code next,enum Code whenEmpty) {
+    if (queue->top)
+        goto meta(context, next);
+    else
+        goto meta(context, whenEmpty);
+}
+
+__code isEmptySingleLinkedQueue_stub(struct Context* context) {
+    goto isEmptySingleLinkedQueue(context,
+                               (struct SingleLinkedQueue *)context->data[D_Queue]->queue.queue->queue.queue,
+                               context->data[D_Queue]->queue.next,
+                               context->data[D_Queue]->queue.whenEmpty);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/queue.h	Mon Nov 21 18:03:43 2016 +0900
@@ -0,0 +1,6 @@
+extern union Data* createSingleLinkedQueue(struct Context* context);
+
+extern __code putSingleLinkedQueue_stub(struct Context* context) ;
+extern __code takeSingleLinkedQueue_stub(struct Context* context) ;
+extern __code isEmptySingleLinkedQueue_stub(struct Context* context) ;
+extern __code clearSingleLinkedQueue_stub(struct Context* context) ;