 |
PokerUnicorn
|
Loading...
Searching...
No Matches
Go to the documentation of this file.
21#define STRINGIFY(x) #x
22#define TOSTRING(x) STRINGIFY(x)
24#define VARCAT(left, right) left##right
26#define LIST_FOREACH(list, node) { \
27 int VARCAT(node, _i) = -1; \
28 __typeof__(list->next) _##node = NULL; \
29 __typeof__(list->next) _next_##node = list->next; \
30 while (_next_##node) { \
32 __typeof__(list->next) node = _next_##node; \
33 _next_##node = (_next_##node)->next;
38#define LISTIFY(type) \
44#define ITEMIFY(type) \
48#define LIST_INIT(list) \
51 list->terminal = NULL; \
54#define LIST_ITEM_INIT(list) \
58#define LIST_APPEND(list, node) \
63 node->prev = list->terminal; \
64 if (list->terminal) { \
65 list->terminal->next = node; \
67 list->terminal = node; \
71#define LIST_PREPEND(list, node) \
75 list->terminal = node; \
77 node->next = list->next; \
78 list->next->prev = node; \
83#define LIST_REMOVE(list, node) \
84 if (list->terminal == node) { \
85 list->terminal = node->next \
89 if (list->next == node) { \
90 list->next = node->next; \
94 node->next->prev = node->prev; \
98 node->prev->next = node->next; \
102#define LIST_SORT(list, a, b, cmp_expr) { \
103 __typeof__(list->next) array[list->length]; \
104 LIST_FOREACH(list, node) \
105 array[node_i] = node; \
107 for (int i = 0; i < list->length; i++) { \
108 for (int j = i + 1; j < list->length; j++) { \
110 __typeof__(list->next) tmp = array[i]; \
111 array[i] = array[j]; \
116 if (list->length) { \
117 list->next = array[0]; \
118 list->terminal = array[list->length - 1]; \
122#define DECL_LIST(list_name, item_name, members, list_new_body, item_new_body, list_free_body, item_free_body) \
123 typedef struct list_name { \
124 LISTIFY(item_name##_t*) \
127 typedef struct item_name { \
128 ITEMIFY(item_name##_t*) \
132 list_name##_t* list_name##_new() { \
133 list_name##_t* list = malloc(sizeof(list_name##_t)); \
135 REF_COUNTED_INIT(list, list_name##_free); \
139 item_name##_t* item_name##_new() { \
140 item_name##_t* item = malloc(sizeof(item_name##_t)); \
141 LIST_ITEM_INIT(item); \
142 REF_COUNTED_INIT(item, item_name##_free); \
146 void list_name##_free(list_name##_t* list) { \
147 LIST_FOREACH(list, item) \
148 item_name##_free(item); \
150 LIST_FOREACH(list, item) \
156 void item_name##_free(item_name##_t* item) { \
160 void list_name##_append(list_name##_t* list, item_name##_t* item) { \
162 LIST_APPEND(list, item); \
164 void list_name##_remove(list_name##_t* list, item_name##_t* item) { \
165 LIST_REMOVE(list, item); \
169#define R(expr) PKRSRV_REF_BY(expr)
171#define REF_COUNTEDIFY() PKRSRV_REF_COUNTEDIFY();
172#define REF_COUNTED_INIT(obj, free_f) PKRSRV_REF_COUNTED_INIT(obj, free_f);
173#define USE(expr) PKRSRV_REF_COUNTED_USE(expr)
174#define LEAVE(expr) PKRSRV_REF_COUNTED_LEAVE(expr)
176#define $(expr) expr; USE(expr);