diff options
author | 3gg <3gg@shellblade.net> | 2024-06-15 11:43:10 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2024-06-15 11:43:10 -0700 |
commit | bec2d50c843ec4fd98bbbb212848ce4f24b96ebb (patch) | |
tree | 59c33bc964e723350886035fe249e41d0f8b397e /list/include/list.h | |
parent | 04e3ded4c28c0b559620609daaae7b939d776b61 (diff) |
More convenient list iteration.
Diffstat (limited to 'list/include/list.h')
-rw-r--r-- | list/include/list.h | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/list/include/list.h b/list/include/list.h index facf820..24291e1 100644 --- a/list/include/list.h +++ b/list/include/list.h | |||
@@ -5,15 +5,15 @@ | |||
5 | #include <stddef.h> | 5 | #include <stddef.h> |
6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
7 | 7 | ||
8 | #define DEF_LIST(type) \ | 8 | #define DEF_LIST(name, type) \ |
9 | typedef struct type##_node { \ | 9 | typedef struct name##_node { \ |
10 | type val; \ | 10 | type val; \ |
11 | struct type##_node* prev; \ | 11 | struct name##_node* prev; \ |
12 | struct type##_node* next; \ | 12 | struct name##_node* next; \ |
13 | } type##_node; \ | 13 | } name##_node; \ |
14 | typedef struct type##_list { \ | 14 | typedef struct name##_list { \ |
15 | type##_node* head; \ | 15 | name##_node* head; \ |
16 | } type##_list; | 16 | } name##_list; |
17 | 17 | ||
18 | static inline void* alloc(size_t size) { | 18 | static inline void* alloc(size_t size) { |
19 | void* ptr = calloc(1, size); | 19 | void* ptr = calloc(1, size); |
@@ -35,7 +35,7 @@ static inline void* alloc(size_t size) { | |||
35 | list.head = 0; | 35 | list.head = 0; |
36 | 36 | ||
37 | /// Prepend a value to the list. | 37 | /// Prepend a value to the list. |
38 | #define list_push(list, value) \ | 38 | #define list_add(list, value) \ |
39 | { \ | 39 | { \ |
40 | __typeof__(list.head) node = alloc(sizeof(*list.head)); \ | 40 | __typeof__(list.head) node = alloc(sizeof(*list.head)); \ |
41 | node->val = value; \ | 41 | node->val = value; \ |
@@ -86,13 +86,13 @@ static inline void* alloc(size_t size) { | |||
86 | /// | 86 | /// |
87 | /// Use 'value' to refer to the address of the current node's value during | 87 | /// Use 'value' to refer to the address of the current node's value during |
88 | /// iteration. | 88 | /// iteration. |
89 | #define list_foreach(list, body) \ | 89 | #define list_foreach(list, value, body) \ |
90 | { \ | 90 | { \ |
91 | __typeof__(list.head) node = list.head; \ | 91 | __typeof__(list.head)* pNode = &list.head; \ |
92 | while (node) { \ | 92 | while (*pNode) { \ |
93 | const __typeof__(node->val)* value = &node->val; \ | 93 | __typeof__((*pNode)->val) value = (*pNode)->val; \ |
94 | body; \ | 94 | body; \ |
95 | node = node->next; \ | 95 | pNode = &(*pNode)->next; \ |
96 | } \ | 96 | } \ |
97 | } | 97 | } |
98 | 98 | ||
@@ -100,12 +100,12 @@ static inline void* alloc(size_t size) { | |||
100 | /// | 100 | /// |
101 | /// Use 'value' to refer to the address of the current node's value during | 101 | /// Use 'value' to refer to the address of the current node's value during |
102 | /// iteration. | 102 | /// iteration. |
103 | #define list_foreach_mut(list, body) \ | 103 | #define list_foreach_mut(list, value, body) \ |
104 | { \ | 104 | { \ |
105 | __typeof__(list.head) node = list.head; \ | 105 | __typeof__(list.head) node = list.head; \ |
106 | while (node) { \ | 106 | while (node) { \ |
107 | __typeof__(node->val)* value = &node->val; \ | 107 | __typeof__(node->val) value = node->val; \ |
108 | body; \ | 108 | body; \ |
109 | node = node->next; \ | 109 | node = node->next; \ |
110 | } \ | 110 | } \ |
111 | } | 111 | } |