-
Notifications
You must be signed in to change notification settings - Fork 2
/
linked_list.h
136 lines (99 loc) · 4.09 KB
/
linked_list.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
#ifndef AEM_LINKED_LIST_H
#define AEM_LINKED_LIST_H
#ifndef aem_assert
#include <aem/log.h>
#endif
// Linked lists with sentinel nodes
// Initialize a linked list
#define AEM_LL1_INIT(chain, next) \
(chain)->next = (chain) \
#define AEM_LL2_INIT(chain, name) do { \
AEM_LL1_INIT((chain), name##_prev); \
AEM_LL1_INIT((chain), name##_next); \
} while (0)
// Insert an item into a linked list
#define AEM_LL1_INSERT_AFTER(chain, node, next) do { \
__typeof__(chain) _chain = (chain); \
__typeof__(node ) _node = (node ); \
\
_node->next = _chain->next; \
_chain->next = _node; \
} while (0)
#define AEM_LL2_INSERT_BEFORE_4(chain, node, prev, next) do { \
__typeof__(chain) _chain = (chain); \
__typeof__(node ) _node = (node ); \
\
_node ->next = _chain; \
_node ->prev = _chain->prev; \
_chain->prev->next = _node ; \
_chain->prev = _node ; \
} while (0)
#define AEM_LL2_INSERT_AFTER_4(chain, node, prev, next) \
AEM_LL2_INSERT_BEFORE_4((chain), (node), next, prev)
#define AEM_LL2_INSERT_BEFORE(chain, node, name) \
AEM_LL2_INSERT_BEFORE_4((chain), (node), name##_prev, name##_next)
#define AEM_LL2_INSERT_AFTER(chain, node, name) \
AEM_LL2_INSERT_AFTER_4((chain), (node), name##_prev, name##_next)
// Remove an item from a linked list
#define AEM_LL2_REMOVE_EXPLICIT(node, prev, next) do { \
__typeof__(node) _node = (node); \
\
_node->next->prev = _node->prev; \
_node->prev->next = _node->next; \
_node->next = _node; \
_node->prev = _node; \
} while (0)
#define AEM_LL2_REMOVE(node, name) \
AEM_LL2_REMOVE_EXPLICIT(node, name##_prev, name##_next)
#define AEM_LL_EMPTY(chain, next) ((chain)->next == (chain))
#define AEM_LL2_EMPTY(chain, name) AEM_LL_EMPTY((chain), name##_next)
// Iterate over a linked list
#define AEM_LL_FOR_RANGE_TP(T, curr, start, end, next) \
for (T curr = (start); curr != (end); curr = curr->next)
#define AEM_LL_FOR_RANGE(curr, start, end, next) \
AEM_LL_FOR_RANGE_TP(__typeof__(start), curr, (start), (end), next)
#define AEM_LL_FOR_ALL_TP(T, curr, chain, next) \
AEM_LL_FOR_RANGE_TP(T, curr, (chain)->next, (chain), next)
#define AEM_LL_FOR_ALL(curr, chain, next) \
AEM_LL_FOR_ALL_TP(__typeof__((chain)->next), curr, (chain), next)
#define AEM_LL2_FOR_RANGE_TP(T, curr, start, end, name) \
AEM_LL_FOR_RANGE_TP(T, curr, start, end, name##_next)
#define AEM_LL2_FOR_RANGE(curr, start, end, name) \
AEM_LL_FOR_RANGE(curr, start, end, name##_next)
#define AEM_LL2_FOR_ALL_TP(T, curr, chain, name) \
AEM_LL_FOR_ALL_TP(T, curr, chain, name##_next)
#define AEM_LL2_FOR_ALL(curr, chain, name) \
AEM_LL_FOR_ALL(curr, chain, name##_next)
// Iterate over a linked list, deleting an element if curr is set to NULL by the loop body
#define AEM_LL_FILTER_RANGE_TP(T, curr, start, end, next) \
for (T *aem_ll_prev = (start), *curr, *aem_ll_next; \
\
(aem_ll_next = aem_ll_prev->next->next), \
(curr = aem_ll_prev->next) != (end); \
\
curr ? (aem_ll_prev = aem_ll_prev->next /* advance */) \
: (aem_ll_prev->next = aem_ll_next /* remove curr */))
#define AEM_LL_FILTER_RANGE(curr, start, end, next) \
AEM_LL_FILTER_RANGE_TP(__typeof__(*(start)), curr, (start), (end), next)
#define AEM_LL_FILTER_ALL_TP(T, curr, chain, next) \
AEM_LL_FILTER_RANGE_TP(T, curr, (chain), (chain), next)
#define AEM_LL_FILTER_ALL(curr, chain, next) \
AEM_LL_FILTER_ALL_TP(__typeof__(*(chain)->next), curr, (chain), next)
// Repeatedly look at the first element of a linked list until the list is empty.
#define AEM_LL_WHILE_FIRST_TP(T, curr, chain, next) \
for (T *curr; (curr = (chain)->next) != (chain);)
#define AEM_LL_WHILE_FIRST(curr, chain, next) \
AEM_LL_WHILE_FIRST_TP(__typeof__(*(chain)->next), curr, (chain), next)
// Empty a linked list
// Calls provided destructor on first element until no elements remain.
#define AEM_LL_DTOR(chain, next, dtor) \
AEM_LL_WHILE_FIRST(aem_ll_curr, (chain), next) { \
(dtor)(aem_ll_curr); \
}
// Verify a doubly linked list
#define AEM_LL2_VERIFY(chain, prev, next, assert) \
AEM_LL_FOR_ALL(_curr, chain, next) { \
assert((_curr)->prev->next == (_curr)); \
assert((_curr)->next->prev == (_curr)); \
}
#endif /* AEM_LINKED_LIST_H */