aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcathook <cat.hook31894@gmail.com>2013-12-02 01:43:17 +0800
committercathook <cat.hook31894@gmail.com>2013-12-02 01:43:17 +0800
commitb2efa5eafae2ad48a55b9bf53b5e124d70ff9ff5 (patch)
tree0d0c091a818aad50e9129d5956b8f8d6b91d38c7
parentee73796830fe9843975c63e24f611b20e786caf7 (diff)
downloadctl-b2efa5eafae2ad48a55b9bf53b5e124d70ff9ff5.tar.gz
ctl-b2efa5eafae2ad48a55b9bf53b5e124d70ff9ff5.tar.zst
ctl-b2efa5eafae2ad48a55b9bf53b5e124d70ff9ff5.zip
update src
-rw-r--r--include/queue.h30
-rw-r--r--src/queue.c103
-rw-r--r--test.c33
3 files changed, 133 insertions, 33 deletions
diff --git a/include/queue.h b/include/queue.h
new file mode 100644
index 0000000..6799407
--- /dev/null
+++ b/include/queue.h
@@ -0,0 +1,30 @@
+#ifndef __QUEUE_H__
+#define __QUEUE_H__
+
+#include "utility.h"
+
+pvoid ctl_queue_initX(ppvoid q, uint size);
+pvoid ctl_queue_freeX(ppvoid q);
+
+uint ctl_queue_getEntrySizeX(ppcvoid q);
+pcvoid ctl_queue_getFrontX (ppcvoid q);
+int ctl_queue_isEmptyX (ppcvoid q);
+
+pcvoid ctl_queue_addX(ppvoid q, pcvoid data);
+int ctl_queue_delX(ppvoid q);
+
+pvoid ctl_queue_copyX(ppcvoid q, ppvoid q2);
+
+#define ctl_queue_init(X,Y) ctl_queue_initX(ppVoid(X),Y)
+#define ctl_queue_free(X) ctl_queue_freeX(ppVoid(X))
+
+#define ctl_queue_getEntrySize(X) ctl_queue_getEntrySizeX(ppcVoid(X))
+#define ctl_queue_getFront(X) ctl_queue_getFrontX (ppcVoid(X))
+#define ctl_queue_isEmpty(X) ctl_queue_isEmptyX (ppcVoid(X))
+
+#define ctl_queue_add(X,Y) ctl_queue_addX(ppVoid(X),pcVoid(Y))
+#define ctl_queue_del(X) ctl_queue_delX(ppVoid(X))
+
+#define ctl_queue_copy(X,Y) ctl_queue_copyX(ppcVoid(X),ppVoid(Y))
+
+#endif /* __QUEUE_H__ */
diff --git a/src/queue.c b/src/queue.c
new file mode 100644
index 0000000..f8dc493
--- /dev/null
+++ b/src/queue.c
@@ -0,0 +1,103 @@
+#include "queue.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <stddef.h>
+
+struct QueueHeadStruct;
+struct QueueNodeStruct;
+
+struct QueueNodeStruct{
+ struct QueueHeadStruct* head;
+ struct QueueNodeStruct* next;
+ char buf[];
+};
+struct QueueHeadStruct{
+ uint size;
+ struct QueueNodeStruct* first;
+ struct QueueNodeStruct* last ;
+ struct QueueNodeStruct data;
+};
+
+typedef struct QueueNodeStruct QueueNode;
+typedef struct QueueHeadStruct QueueHead;
+
+#define getHead(X) (((QueueNode*)(pChar(X)-offsetof(QueueNode, buf)))->head)
+#define getSize(X) (sizeof(QueueNode) + (getHead(X)->size))
+
+pvoid ctl_queue_initX(ppvoid q, uint size){
+ QueueHead* head = (QueueHead*)ctl_malloc(sizeof(QueueHead));
+ head->size = size;
+ head->first = NULL;
+ head->last = NULL;
+ head->data.head = head;
+ head->data.next = NULL;
+ if(q != NULL){
+ *q = pVoid(head->data.buf);
+ }
+ return pVoid(head->data.buf);
+}
+pvoid ctl_queue_freeX(ppvoid q){
+ QueueHead* head = getHead(*q);
+ QueueNode* node;
+ QueueNode* temp;
+ for(node = head->first; node != NULL; node = temp){
+ temp = node->next;
+ free(node);
+ }
+ free(head);
+ *q = NULL;
+ return NULL;
+}
+
+uint ctl_queue_getEntrySizeX(ppcvoid q){
+ return getHead(*q)->size;
+}
+
+pcvoid ctl_queue_getFrontX(ppcvoid q){
+ return *q;
+}
+int ctl_queue_isEmptyX(ppcvoid q){
+ return (getHead(*q)->first == NULL ? 1 : 0);
+}
+
+pcvoid ctl_queue_addX(ppvoid q, pcvoid data){
+ QueueHead* head = getHead(*q);
+ if(head->first == NULL){
+ head->first = (QueueNode*)ctl_malloc(getSize(*q));
+ head->last = head->first;
+ *q = pVoid(head->first->buf);
+ }else{
+ head->last->next = (QueueNode*)ctl_malloc(getSize(*q));
+ head->last = head->last->next;
+ }
+ head->last->head = head;
+ head->last->next = NULL;
+ memcpy(head->last->buf, data, head->size);
+ return pVoid(head->last->buf);
+}
+int ctl_queue_delX(ppvoid q){
+ QueueHead* head = getHead(*q);
+ QueueNode* temp = head->first;
+ head->first = head->first->next;
+ free(temp);
+ if(head->first == NULL){
+ head->last = NULL;
+ *q = pVoid(head->data.buf);
+ return 1;
+ }else{
+ *q = pVoid(head->first->buf);
+ return 0;
+ }
+}
+
+pvoid ctl_queue_copyX(ppcvoid q, ppvoid q2){
+ ctl_queue_initX(q2, getHead(q)->size);
+ if(!ctl_queue_isEmpty(q)){
+ QueueNode* node = getHead(q)->first;
+ for( ; node != NULL; node = node->next){
+ ctl_queue_addX(q2, node->buf);
+ }
+ }
+ return *q2;
+}
diff --git a/test.c b/test.c
deleted file mode 100644
index e2d73e6..0000000
--- a/test.c
+++ /dev/null
@@ -1,33 +0,0 @@
-#include <stdio.h>
-#include "include/queue.h"
-
-void print(char* q){
- printf("size = %d, ", ctl_queue_getEntrySize(&q));
- printf("isEmpty? %s\n", (ctl_queue_isEmpty(&q) ? "Yes" : "No"));
- if(ctl_queue_isEmpty(&q)){
- ;
- }else{
- printf("*q = %d\n", *(const char*)q);
- printf("q.front = %d\n", *(const char*)ctl_queue_getFront(&q));
- }
-}
-
-int main(){
- char *q, c;
- ctl_queue_init(&q, sizeof(char));
- print(q);
- c = 10; ctl_queue_add(&q, &c); print(q);
- ctl_queue_del(&q); print(q);
- c = 11; ctl_queue_add(&q, &c); print(q);
- c = 12; ctl_queue_add(&q, &c); print(q);
- c = 13; ctl_queue_add(&q, &c); print(q);
- c = 14; ctl_queue_add(&q, &c); print(q);
- ctl_queue_del(&q); print(q);
- ctl_queue_del(&q); print(q);
- ctl_queue_del(&q); print(q);
- ctl_queue_del(&q); print(q);
-
- ctl_queue_free(&q);
-
- return 0;
-}