annotate src/parallel_execution/SingleLinkedQueue.cbc @ 266:ffcd80cc3a83

create SingleLinkedQueue.cbc
author mir3636
date Sat, 28 Jan 2017 23:12:44 +0900
parents
children 68cf983475e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
266
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
1 #include "../context.h"
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
2 #include "../origin_cs.h"
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
3 #include <stdio.h>
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
4
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
5 Queue* createSingleLinkedQueue(struct Context* context) {
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
6 struct Queue* queue = new Stack();
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
7 struct SingleLinkedQueue* singleLinkedQueue = new SingleLinkedQueue();
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
8 queue->queue = (union Data*)singleLinkedQueue;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
9 singleLinkedQueue->top = NULL;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
10 singleLinkedQueue->last = NULL;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
11 queue->take = C_takeSingleLinkedQueue;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
12 queue->put = C_putSingleLinkedQueue;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
13 queue->isEmpty = C_isEmptySingleLinkedQueue;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
14 queue->clear = C_clearSingleLinkedQueue;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
15 return queue;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
16 }
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
17
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
18 void printQueue1(union Data* data) {
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
19 struct Node* node = &data->Element.data->Node;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
20 if (node == NULL) {
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
21 printf("NULL");
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
22 } else {
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
23 printf("key = %d ,", node->key);
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
24 printQueue1((union Data*)data->Element.next);
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
25 }
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
26 }
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
27
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
28 void printQueue(union Data* data) {
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
29 printQueue1(data);
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
30 printf("\n");
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
31 }
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
32
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
33 __code clearSingleLinkedQueue(struct SingleLinkedQueue* queue, __code next(...)) {
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
34 queue->top = NULL;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
35 goto next(...);
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
36 }
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
37
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
38 __code putSingleLinkedQueue(struct SingleLinkedQueue* queue, union Data* data, __code next(...)) {
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
39 Element* element = new Element();
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
40 element->next = NULL;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
41 element->data = data;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
42 if (queue->last) {
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
43 Element* last = queue->last;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
44 last->next = element;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
45 queue->last = element;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
46 } else {
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
47 queue->top = element;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
48 queue->last = element;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
49 }
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
50 goto next(...);
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
51 }
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
52
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
53 __code takeSingleLinkedQueue(struct SingleLinkedQueue* queue, _code next(union Data* data, ...)) {
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
54 if (queue->top) {
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
55 data = queue->top->data;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
56 queue->top = queue->top->next;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
57 } else {
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
58 data = NULL;
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
59 }
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
60 goto next(data, ...);
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
61 }
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
62
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
63 __code isEmptySingleLinkedQueue(struct SingleLinkedQueue* queue, __code next(...), __code whenEmpty(...)) {
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
64 if (queue->top)
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
65 goto next(...);
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
66 else
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
67 goto whenEmpty(...);
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
68 }
ffcd80cc3a83 create SingleLinkedQueue.cbc
mir3636
parents:
diff changeset
69