-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdqueue_simple.h
210 lines (202 loc) · 9.71 KB
/
dqueue_simple.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef DQUEUE_SIMPLE_H
#define DQUEUE_SIMPLE_H
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
#include <assert.h>
// DQueue
#define DQUEUE_MEMBER(type) type * node_prev, * node_next;
#define DQUEUE_HEAD(uniquename, type) \
struct uniquename ## dqueue_head_s \
{ \
type * begin, * end; \
} uniquename ## dqueue_head; \
#define DQUEUE_INIT_FUNC(uniquename, type) \
static inline void uniquename ## dqueue_init(struct uniquename ## dqueue_head_s * head, type * node) \
{ \
assert(head); \
head->begin = head->end = node; \
} \
static inline int uniquename ## dqueue_not_empty(struct uniquename ## dqueue_head_s * head) \
{ \
assert(head); \
return head->begin && head->end; \
} \
static inline type * uniquename ## dqueue_node_init(type * node) \
{ \
node->node_prev = node->node_next = NULL; \
return node; \
} \
static inline int uniquename ## dqueue_node_not_empty(const type * node) \
{ \
return node != NULL; \
} \
static inline type * uniquename ## dqueue_get_prev_node(const type * node) \
{ \
assert(node); \
return node->node_prev; \
} \
static inline type * uniquename ## dqueue_get_next_node(const type * node) \
{ \
return node->node_next; \
} \
static inline type * uniquename ## dqueue_get_first_node(const struct uniquename ## dqueue_head_s * head) \
{ \
return head->begin; \
} \
static inline type * uniquename ## dqueue_get_last_node(const struct uniquename ## dqueue_head_s * head) \
{ \
return head->end; \
} \
static inline int uniquename ## dqueue_node_is_first(const struct uniquename ## dqueue_head_s * head, \
const type * node) \
{ \
return head->begin == node; \
} \
static inline int uniquename ## dqueue_node_is_last(const struct uniquename ## dqueue_head_s * head, \
const type * node) \
{ \
return head->end == node; \
} \
static type * uniquename ## dqueue_insert_before(struct uniquename ## dqueue_head_s * head, \
type * node, type * nodenew) \
{ \
assert(uniquename ## dqueue_node_not_empty(nodenew)); \
if(uniquename ## dqueue_not_empty(head)) \
{ \
assert(uniquename ## dqueue_node_not_empty(node)); \
type * nodeprev = uniquename ## dqueue_get_prev_node(node); \
nodenew->node_prev = nodeprev; \
nodenew->node_next = node; \
node->node_prev = nodenew; \
if(uniquename ## dqueue_node_not_empty(nodeprev)) \
{ \
nodeprev->node_next = nodenew; \
} else { \
head->begin = nodenew; \
} \
} else { \
assert(!uniquename ## dqueue_node_not_empty(node)); \
uniquename ## dqueue_init(head, nodenew); \
} \
return nodenew; \
} \
static type * uniquename ## dqueue_insert_after(struct uniquename ## dqueue_head_s * head, \
type * node, type * nodenew) \
{ \
assert(uniquename ## dqueue_node_not_empty(nodenew)); \
if(uniquename ## dqueue_not_empty(head)) \
{ \
assert(uniquename ## dqueue_node_not_empty(node)); \
type * nodenext = uniquename ## dqueue_get_next_node(node); \
nodenew->node_prev = node; \
nodenew->node_next = nodenext; \
node->node_next = nodenew; \
if(uniquename ## dqueue_node_not_empty(nodenext)) \
{ \
nodenext->node_prev = nodenew; \
} else { \
head->end = nodenew; \
} \
} else { \
assert(!uniquename ## dqueue_node_not_empty(node)); \
uniquename ## dqueue_init(head, nodenew); \
} \
return nodenew; \
} \
static inline type * uniquename ## dqueue_push_first(struct uniquename ## dqueue_head_s * head, type * nodenew) \
{ \
assert(uniquename ## dqueue_node_not_empty(nodenew)); \
if(uniquename ## dqueue_not_empty(head)) \
{ \
type * next = head->begin; \
nodenew->node_prev = NULL; \
nodenew->node_next = next; \
next->node_prev = nodenew; \
head->begin = nodenew; \
} else { \
uniquename ## dqueue_init(head, nodenew); \
} \
return nodenew; \
} \
static inline type * uniquename ## dqueue_push_back(struct uniquename ## dqueue_head_s * head, type * nodenew) \
{ \
assert(uniquename ## dqueue_node_not_empty(nodenew)); \
if(uniquename ## dqueue_not_empty(head)) \
{ \
type * prev = head->end; \
nodenew->node_prev = prev; \
nodenew->node_next = NULL; \
prev->node_next = nodenew; \
head->end = nodenew; \
} else { \
uniquename ## dqueue_init(head, nodenew); \
} \
return nodenew; \
} \
static inline type * uniquename ## dqueue_remove(struct uniquename ## dqueue_head_s * head, type * const node) \
{ \
assert(uniquename ## dqueue_not_empty(head)); \
if(uniquename ## dqueue_node_not_empty(node)) \
{ \
type * next = uniquename ## dqueue_get_next_node(node); \
type * prev = uniquename ## dqueue_get_prev_node(node); \
if(uniquename ## dqueue_node_not_empty(prev)) \
{ \
prev->node_next = next; \
} else { \
head->begin = next; \
} \
if(uniquename ## dqueue_node_not_empty(next)) \
{ \
next->node_prev = prev; \
} else { \
head->end = prev; \
} \
} \
uniquename ## dqueue_node_init(node); \
return node; \
} \
static inline type * uniquename ## dqueue_remove_first(struct uniquename ## dqueue_head_s * head) \
{ \
if(uniquename ## dqueue_not_empty(head)) \
{ \
type * first = uniquename ## dqueue_get_first_node(head); \
type * next = uniquename ## dqueue_get_next_node(first); \
head->begin = next; \
if(uniquename ## dqueue_node_not_empty(next)) \
{ \
next->node_prev = NULL; \
} else { \
head->end = NULL; \
} \
uniquename ## dqueue_node_init(first); \
return first; \
} else { \
return NULL; \
} \
} \
static inline type * uniquename ## dqueue_remove_last(struct uniquename ## dqueue_head_s * head) \
{ \
if(uniquename ## dqueue_not_empty(head)) \
{ \
type * last = uniquename ## dqueue_get_last_node(head); \
type * prev = uniquename ## dqueue_get_next_node(last); \
head->end = prev; \
if(uniquename ## dqueue_node_not_empty(prev)) \
{ \
prev->node_next = NULL; \
} else { \
head->begin = NULL; \
} \
uniquename ## dqueue_node_init(last); \
return last; \
} else { \
return NULL; \
} \
} \
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#endif // DQUEUE_SIMPLE_H