PokerUnicorn
Loading...
Searching...
No Matches
sugar.h
Go to the documentation of this file.
1/*
2 * PokerUnicorn Server
3 * This project uses test network, NO real coin or NO real money involved.
4 * Copyright (C) 2023, Oğuzhan Eroğlu <meowingcate@gmail.com> (https://meowingcat.io)
5 * Licensed under GPLv3 License
6 * See LICENSE for more info
7 */
8
9#pragma once
10
21#define STRINGIFY(x) #x
22#define TOSTRING(x) STRINGIFY(x)
23
24#define VARCAT(left, right) left##right
25
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) { \
31 VARCAT(node, _i)++; \
32 __typeof__(list->next) node = _next_##node; \
33 _next_##node = (_next_##node)->next;
34
35#define END_FOREACH \
36 }}
37
38#define LISTIFY(type) \
39 type next; \
40 type prev; \
41 type terminal; \
42 int length;
43
44#define ITEMIFY(type) \
45 type next; \
46 type prev;
47
48#define LIST_INIT(list) \
49 list->prev = NULL; \
50 list->next = NULL; \
51 list->terminal = NULL; \
52 list->length = 0;
53
54#define LIST_ITEM_INIT(list) \
55 list->prev = NULL; \
56 list->next = NULL;
57
58#define LIST_APPEND(list, node) \
59 if (!list->next) { \
60 list->next = node; \
61 } \
62 \
63 node->prev = list->terminal; \
64 if (list->terminal) { \
65 list->terminal->next = node; \
66 } \
67 list->terminal = node; \
68 \
69 list->length++;
70
71#define LIST_PREPEND(list, node) \
72 node->prev = NULL; \
73 if (!list->next) { \
74 list->next = node; \
75 list->terminal = node; \
76 } else { \
77 node->next = list->next; \
78 list->next->prev = node; \
79 list->next = node; \
80 } \
81 list->length++;
82
83#define LIST_REMOVE(list, node) \
84 if (list->terminal == node) { \
85 list->terminal = node->next \
86 ? node->next \
87 : node->prev; \
88 } \
89 if (list->next == node) { \
90 list->next = node->next; \
91 } \
92 \
93 if (node->next) { \
94 node->next->prev = node->prev; \
95 } \
96 \
97 if (node->prev) { \
98 node->prev->next = node->next; \
99 } \
100 list->length--;
101
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; \
106 END_FOREACH \
107 for (int i = 0; i < list->length; i++) { \
108 for (int j = i + 1; j < list->length; j++) { \
109 if (cmp_expr) { \
110 __typeof__(list->next) tmp = array[i]; \
111 array[i] = array[j]; \
112 airray[j] = tmp; \
113 } \
114 } \
115 } \
116 if (list->length) { \
117 list->next = array[0]; \
118 list->terminal = array[list->length - 1]; \
119 } \
120}
121
122#define R(expr) PKRSRV_REF_BY(expr)
123