Mercurial > hg > GearsTemplate
changeset 266:ffcd80cc3a83
create SingleLinkedQueue.cbc
author | mir3636 |
---|---|
date | Sat, 28 Jan 2017 23:12:44 +0900 |
parents | 4ccf776db994 |
children | d041069bc7fe |
files | src/parallel_execution/Queue.cbc src/parallel_execution/SingleLinkedQueue.cbc |
diffstat | 2 files changed, 80 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parallel_execution/Queue.cbc Sat Jan 28 23:12:44 2017 +0900 @@ -0,0 +1,11 @@ +typedef struct Queue<Impl>{ + union Data* queue; + union Data* data; + __code whenEmpty(...); + __code clear(Impl* queue, __code next(...)); + __code put(Impl* queue, union Data* data, __code next(...)); + __code take(Impl* queue, __code next(union Data*, ...)); + __code isEmpty(Impl* queue, __code next(...), __code whenEmpty(...)); + __code next(...); +} Stack; +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parallel_execution/SingleLinkedQueue.cbc Sat Jan 28 23:12:44 2017 +0900 @@ -0,0 +1,69 @@ +#include "../context.h" +#include "../origin_cs.h" +#include <stdio.h> + +Queue* createSingleLinkedQueue(struct Context* context) { + struct Queue* queue = new Stack(); + struct SingleLinkedQueue* singleLinkedQueue = new SingleLinkedQueue(); + queue->queue = (union Data*)singleLinkedQueue; + singleLinkedQueue->top = NULL; + singleLinkedQueue->last = NULL; + queue->take = C_takeSingleLinkedQueue; + queue->put = C_putSingleLinkedQueue; + queue->isEmpty = C_isEmptySingleLinkedQueue; + queue->clear = C_clearSingleLinkedQueue; + return 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 SingleLinkedQueue* queue, __code next(...)) { + queue->top = NULL; + goto next(...); +} + +__code putSingleLinkedQueue(struct SingleLinkedQueue* queue, union Data* data, __code next(...)) { + Element* element = new Element(); + 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 next(...); +} + +__code takeSingleLinkedQueue(struct SingleLinkedQueue* queue, _code next(union Data* data, ...)) { + if (queue->top) { + data = queue->top->data; + queue->top = queue->top->next; + } else { + data = NULL; + } + goto next(data, ...); +} + +__code isEmptySingleLinkedQueue(struct SingleLinkedQueue* queue, __code next(...), __code whenEmpty(...)) { + if (queue->top) + goto next(...); + else + goto whenEmpty(...); +} +