# HG changeset patch # User autobackup # Date 1611760203 -32400 # Node ID 5ddde38b3261559ca9605c349d2415e3c0475a46 # Parent e9ba3f9d64c9ff8bee29c47e4781dcc75cf1fcea backup 2021-01-28 diff -r e9ba3f9d64c9 -r 5ddde38b3261 user/anatofuz/note/2021/01/27.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/user/anatofuz/note/2021/01/27.md Thu Jan 28 00:10:03 2021 +0900 @@ -0,0 +1,198 @@ +```shell +[getDataGear] match 190 : #impl "Queue.h" for "SingleLinkedQueue.h" +[getDataGear] match 142 : typedef struct Queue<>{ +[getDataGear] match 344 : __code whenEmpty(...); +[getDataGear] match 344 : __code clear(Impl* queue, __code next(...)); +[getDataGear] match 344 : __code put(Impl* queue, union Data* data, __code next(...)); +[getDataGear] match 344 : __code take(Impl* queue, __code next(union Data* data, ...)); +[getDataGear] match 344 : __code isEmpty(Impl* queue, __code next(...), __code whenEmpty(...)); +[getDataGear] match 344 : __code next(...); +[getDataGear] match 321 : } Queue; +[getCodeGear] match 399 : typedef struct Queue<>{ +[getCodeGear] match 404 : __code whenEmpty(...); +[getCodeGear] match 404 : __code clear(Impl* queue, __code next(...)); +[getCodeGear] match 404 : __code put(Impl* queue, union Data* data, __code next(...)); +[getCodeGear] match 404 : __code take(Impl* queue, __code next(union Data* data, ...)); +[getCodeGear] match 404 : __code isEmpty(Impl* queue, __code next(...), __code whenEmpty(...)); +[getCodeGear] match 404 : __code next(...); +[getDataGear] match 158 : Queue* createSingleLinkedQueue(struct Context* context) { +[getDataGear] match 212 : __code clearSingleLinkedQueue(struct SingleLinkedQueue* queue, __code next(...)) { +[getDataGear] match 212 : __code putSingleLinkedQueue(struct SingleLinkedQueue* queue, union Data* data, __code next(...)) { +[getDataGear] match 246 : Element* element = new Element(); +[getDataGear] match 212 : __code takeSingleLinkedQueue(struct SingleLinkedQueue* queue, __code next(union Data* data, ...)) { +[getDataGear] match 246 : struct Element* top = queue->top; +[getDataGear] match 246 : struct Element* nextElement = top->next; +[getDataGear] match 212 : __code isEmptySingleLinkedQueue(struct SingleLinkedQueue* queue, __code next(...), __code whenEmpty(...)) { +[getDataGear] match 150 : typedef struct task_iterator { +[getDataGear] match 321 : StateDB state; +[getDataGear] match 321 : Element* list; +[getDataGear] match 321 : Element* last; +[getDataGear] match 354 : } TaskIterator, *TaskIteratorPtr; +[getDataGear] match 158 : Element* createQueueIterator(struct SingleLinkedQueue* queue) { +[generateDataGear] match 675 : #include "../context.h" +[generateDataGear] match 803 : #include +[generateDataGear] match 647 : #impl "Queue.h" for "SingleLinkedQueue.h" +[generateDataGear] match 666 : #data "Node.h" +[generateDataGear] match 666 : #data "Element.h" +[generateDataGear] match 803 : +[generateDataGear] match 803 : Queue* createSingleLinkedQueue(struct Context* context) { +[generateDataGear] match 803 : struct Queue* queue = new Queue(); +[generateDataGear] match 803 : struct SingleLinkedQueue* singleLinkedQueue = new SingleLinkedQueue(); +[generateDataGear] match 803 : queue->queue = (union Data*)singleLinkedQueue; +[generateDataGear] match 803 : queue->take = C_takeSingleLinkedQueue; +[generateDataGear] match 803 : queue->put = C_putSingleLinkedQueue; +[generateDataGear] match 803 : queue->isEmpty = C_isEmptySingleLinkedQueue; +[generateDataGear] match 803 : queue->clear = C_clearSingleLinkedQueue; +[generateDataGear] match 803 : singleLinkedQueue->top = new Element(); +[generateDataGear] match 803 : singleLinkedQueue->last = singleLinkedQueue->top; +[generateDataGear] match 803 : return queue; +[generateDataGear] match 803 : } +[generateDataGear] match 803 : +[generateDataGear] match 803 : void printQueue1(union Data* data) { +[generateDataGear] match 803 : struct Node* node = &data->Element.data->Node; +[generateDataGear] match 803 : if (node == NULL) { +[generateDataGear] match 803 : printf("NULL"); +[generateDataGear] match 803 : } else { +[generateDataGear] match 803 : printf("key = %d ,", node->key); +[generateDataGear] match 803 : printQueue1((union Data*)data->Element.next); +[generateDataGear] match 803 : } +[generateDataGear] match 803 : } +[generateDataGear] match 803 : +[generateDataGear] match 803 : void printQueue(union Data* data) { +[generateDataGear] match 803 : printQueue1(data); +[generateDataGear] match 803 : printf("\n"); +[generateDataGear] match 803 : } +[generateDataGear] match 803 : +[generateDataGear] match 694 : __code clearSingleLinkedQueue(struct SingleLinkedQueue* queue, __code next(...)) { +[generateDataGear] match 1042 : queue->top->next = NULL; +[generateDataGear] match 1042 : queue->last = queue->top; +[generateDataGear] match 976 : goto next(...); +[generateDataGear] match 1039 : } +[generateDataGear] match 1063 : } +[generateDataGear] match 803 : +[generateDataGear] match 694 : __code putSingleLinkedQueue(struct SingleLinkedQueue* queue, union Data* data, __code next(...)) { +[generateDataGear] match 1030 : Element* element = new Element(); +[generateDataGear] match 1042 : element->data = data; +[generateDataGear] match 1042 : element->next = NULL; +[generateDataGear] match 1042 : queue->last->next = element; +[generateDataGear] match 1042 : queue->last = element; +[generateDataGear] match 976 : goto next(...); +[generateDataGear] match 1039 : } +[generateDataGear] match 1063 : } +[generateDataGear] match 803 : +[generateDataGear] match 694 : __code takeSingleLinkedQueue(struct SingleLinkedQueue* queue, __code next(union Data* data, ...)) { +[generateDataGear] match 1030 : struct Element* top = queue->top; +[generateDataGear] match 1030 : struct Element* nextElement = top->next; +[generateDataGear] match 1042 : if (queue->top == queue->last) { +[generateDataGear] match 1042 : data = NULL; +[generateDataGear] match 1042 : } else { +[generateDataGear] match 1042 : queue->top = nextElement; +[generateDataGear] match 1042 : data = nextElement->data; +[generateDataGear] match 1042 : } +[generateDataGear] match 976 : goto next(data, ...); +[generateDataGear] match 1039 : } +[generateDataGear] match 1063 : } +[generateDataGear] match 803 : +[generateDataGear] match 694 : __code isEmptySingleLinkedQueue(struct SingleLinkedQueue* queue, __code next(...), __code whenEmpty(...)) { +[generateDataGear] match 1042 : if (queue->top == queue->last) { +[generateDataGear] match 976 : goto whenEmpty(...); +[generateDataGear] match 1042 : } else { +[generateDataGear] match 976 : goto next(...); +[generateDataGear] match 1042 : } +[generateDataGear] match 1039 : } +[generateDataGear] match 1063 : } +[generateDataGear] match 803 : +[generateDataGear] match 803 : +[generateDataGear] match 803 : int lengthSingleLinkedQueue(struct SingleLinkedQueue* queue) { +[generateDataGear] match 803 : int length = 0; +[generateDataGear] match 803 : struct Element* current = queue->top; +[generateDataGear] match 803 : +[generateDataGear] match 803 : while (current->next != NULL) { +[generateDataGear] match 803 : length++; +[generateDataGear] match 803 : current = current->next; +[generateDataGear] match 803 : } +[generateDataGear] match 803 : +[generateDataGear] match 803 : return length - 1; // top is dummy +[generateDataGear] match 803 : } +[generateDataGear] match 803 : +[generateDataGear] match 803 : +[generateDataGear] match 803 : Element* getElementByIdx(struct SingleLinkedQueue* queue, int idx) { +[generateDataGear] match 803 : struct Element* current = queue->top; +[generateDataGear] match 803 : +[generateDataGear] match 803 : while (current->next != NULL){ +[generateDataGear] match 803 : if (idx-- == 0 ) { +[generateDataGear] match 803 : return current->next; +[generateDataGear] match 803 : } +[generateDataGear] match 803 : current = current->next; +[generateDataGear] match 803 : } +[generateDataGear] match 803 : return queue->top; +[generateDataGear] match 803 : } +[generateDataGear] match 803 : +[generateDataGear] match 803 : +[generateDataGear] match 641 : typedef struct task_iterator { +[generateDataGear] match 1063 : } TaskIterator, *TaskIteratorPtr; +[generateDataGear] match 803 : +[generateDataGear] match 803 : +[generateDataGear] match 803 : +[generateDataGear] match 803 : Element* createQueueIterator(struct SingleLinkedQueue* queue) { +[generateDataGear] match 803 : TaskIteratorPtr new = (TaskIteratorPtr)malloc(sizeof(TaskIterator)); +[generateDataGear] match 803 : if (!new) die_exit("can't allocate task iterlator"); +[generateDataGear] match 803 : +[generateDataGear] match 803 : new->prev = queue->top; +[generateDataGear] match 803 : new->state = s; +[generateDataGear] match 803 : new->list = queue->top; +[generateDataGear] match 803 : new->last = queue->last; +[generateDataGear] match 803 : return new; +[generateDataGear] match 803 : } +[generateDataGear] match 803 : +[generateDataGear] match 803 : Element* takeNextIterator(struct SingleLinkedQueue* queue, TaskIteratorPtr iterator) { +[generateDataGear] match 803 : struct Element* elem = iterator->list; +[generateDataGear] match 803 : if (!elem) { +[generateDataGear] match 803 : return NULL; +[generateDataGear] match 803 : } +[generateDataGear] match 803 : +[generateDataGear] match 803 : struct Element* next = elem->next; +[generateDataGear] match 803 : if (next == NULL) { +[generateDataGear] match 803 : return next; +[generateDataGear] match 803 : } +[generateDataGear] match 803 : +[generateDataGear] match 803 : if (next == iterator->last) { +[generateDataGear] match 803 : return NULL; +[generateDataGear] match 803 : } +[generateDataGear] match 803 : +[generateDataGear] match 803 : iterator->list = next; +[generateDataGear] match 803 : return next; +[generateDataGear] match 803 : } +``` + +```c +Element* createQueueIterator(struct SingleLinkedQueue* queue) { + TaskIteratorPtr new = (TaskIteratorPtr)malloc(sizeof(TaskIterator)); + if (!new) die_exit("can't allocate task iterlator"); + + new->prev = queue->top; + new->state = s; + new->list = queue->top; + new->last = queue->last; + return new; +} + +Element* takeNextIterator(struct SingleLinkedQueue* queue, TaskIteratorPtr iterator) { + struct Element* elem = iterator->list; + if (!elem) { + return NULL; + } + + struct Element* next = elem->next; + if (next == NULL) { + return next; + } + + if (next == iterator->last) { + return NULL; + } + + iterator->list = next; + return next; +} +```