-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdl-macro2.h
64 lines (52 loc) · 1.82 KB
/
dl-macro2.h
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
// no "#ifndef" guard as this header may be included multiple times
// see dl-intru2.* for details on the methodology
#define DL_CALLOC(type, len) (type*)calloc((len), sizeof(type))
#define DL_TOKCAT(pre, suf) pre ##_## suf
#define DL_NODE_S(suf) DL_TOKCAT(dl_node_s, suf)
#define DL_NODE_T(suf) DL_TOKCAT(dl_node_t, suf)
#define DL_LIST_T(suf) DL_TOKCAT(dl_list_t, suf)
#define DL_INIT(suf) DL_TOKCAT(dl_init, suf)
#define DL_PUSH(suf) DL_TOKCAT(dl_push, suf)
#define DL_POP(suf) DL_TOKCAT(dl_pop, suf)
#define DL_EMPTY(suf) DL_TOKCAT(dl_empty, suf)
#define DL_DESTROY(suf) DL_TOKCAT(dl_destroy, suf)
typedef struct DL_NODE_S(DL_NAME) {
DL_TYPE data;
struct DL_NODE_S(DL_NAME) *p[2];
} DL_NODE_T(DL_NAME);
typedef struct {
DL_NODE_T(DL_NAME) *head[2];
} DL_LIST_T(DL_NAME);
DL_LIST_T(DL_NAME) *DL_INIT(DL_NAME)(void)
{
return DL_CALLOC(DL_LIST_T(DL_NAME), 1);
}
void DL_PUSH(DL_NAME)(DL_LIST_T(DL_NAME) *list, DL_TYPE data, int dir)
{
DL_NODE_T(DL_NAME) *p = DL_CALLOC(DL_NODE_T(DL_NAME), 1);
dir = !!dir;
p->data = data;
if (list->head[0] == 0 && list->head[1] == 0) list->head[0] = list->head[1] = p;
else list->head[dir]->p[dir] = p, p->p[!dir] = list->head[dir], list->head[dir] = p;
}
int DL_POP(DL_NAME)(DL_LIST_T(DL_NAME) *list, DL_TYPE *data, int dir)
{
DL_NODE_T(DL_NAME) *p;
dir = !!dir;
if (list->head[0] == 0 && list->head[1] == 0) return 0;
else if (list->head[0] == list->head[1]) p = list->head[0], list->head[0] = list->head[1] = 0;
else p = list->head[dir], list->head[dir] = p->p[!dir], list->head[dir]->p[dir] = 0;
*data = p->data;
free(p);
return 1;
}
int DL_EMPTY(DL_NAME)(const DL_LIST_T(DL_NAME) *list)
{
return (list == 0 || list->head[0] == 0);
}
void DL_DESTROY(DL_NAME)(DL_LIST_T(DL_NAME) *list)
{
DL_NODE_T(DL_NAME) *q, *p = list->head[0];
while (p) { q = p->p[1]; free(p); p = q; }
free(list);
}