aboutsummaryrefslogtreecommitdiffstats
path: root/src/stack.c
blob: 0cd95b3127ea4d6d79c0e47d340046bc7051d41c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "stack.h"
#include "utility.h"

#include <stdlib.h>
#include <string.h>
#include <stddef.h>

struct StackHeadStruct;
struct StackNodeStruct;

struct StackNodeStruct{
    struct StackHeadStruct* head;
    struct StackNodeStruct* prev;
    char buf[];
};
struct StackHeadStruct{
    uint size;
    struct StackNodeStruct* top;
    struct StackNodeStruct  data;
};

typedef struct StackNodeStruct StackNode;
typedef struct StackHeadStruct StackHead;

#define getHead(X) (((StackNode*)(pChar(X)-offsetof(StackNode, buf)))->head)
#define getSize(X) (sizeof(StackNode) + (getHead(X)->size))

pvoid ctl_stack_initX(ppvoid q, uint size){
    StackHead* head = (StackHead*)ctl_malloc(sizeof(StackHead));
    head->size = size;
    head->top = NULL;
    head->data.head = head;
    head->data.prev = NULL;
    if(q != NULL){
        *q = pVoid(head->data.buf);
    }
    return pVoid(head->data.buf);
}
pvoid ctl_stack_freeX(ppvoid q){
    StackHead* head = getHead(*q);
    StackNode* node;
    StackNode* temp;
    for(node = head->top; node != NULL; node = temp){
        temp = node->prev;
        free(node);
    }
    free(head);
    *q = NULL;
    return NULL;
}

uint ctl_stack_getEntrySizeX(ppcvoid q){
    return getHead(*q)->size;
}
    
pcvoid ctl_stack_getX(ppcvoid q){
    return *q;
}
int ctl_stack_isEmptyX(ppcvoid q){
    return (getHead(*q)->top == NULL ? 1 : 0);
}

pcvoid ctl_stack_addX(ppvoid q, pcvoid data){
    StackHead* head = getHead(*q);
    StackNode* temp = (StackNode*)ctl_malloc(getSize(*q));
    temp->head = head;
    temp->prev = head->top;
    memcpy(temp->buf, data, head->size);
    head->top = temp;
    *q = pVoid(head->top->buf);
    return *q;
}
int ctl_stack_delX(ppvoid q){
    StackHead* head = getHead(*q);
    StackNode* temp = head->top->prev;
    free(head->top);
    head->top = temp;
    if(temp == NULL){
        *q = pVoid(head->data.buf);
        return 1;
    }else{
        *q = pVoid(head->top->buf);
        return 0;
    }
}

pvoid ctl_stack_copyX(ppcvoid q, ppvoid q2){
    ctl_stack_initX(q2, getHead(q)->size);
    if(!ctl_stack_isEmpty(q)){
        StackNode* node = getHead(q)->top;
        StackNode* nod2;
        StackHead* hea2 = getHead(*q2);
        ctl_stack_addX(q2, node->buf);
        nod2 = hea2->top;
        for(node = node->prev; node != NULL; node = node->prev){
            nod2->prev = (StackNode*)ctl_malloc(getSize(q2));
            nod2->prev->head = getHead(q2);
            nod2->prev->prev = NULL;
            memcpy(nod2->prev->buf, node->buf, getHead(q2)->size);
        }
    }
    return *q2;
}