liba 0.1.15
An algorithm library based on C/C++
 
Loading...
Searching...
No Matches
slist.h
Go to the documentation of this file.
1
5
6#ifndef LIBA_SLIST_H
7#define LIBA_SLIST_H
8
9#include "a.h"
10
16
17/* clang-format off */
18#define A_SLIST_NODE {A_NULL}
19#define A_SLIST_INIT(list) {{A_NULL}, &(list).head}
20/* clang-format on */
21
25typedef struct a_slist_node
26{
27 struct a_slist_node *next;
29
36#define a_slist_(_, x) a_cast_s(a_slist_node _, a_cast_s(void _, x))
37
41typedef struct a_slist
42{
43 a_slist_node head;
44 a_slist_node *tail;
46
53#define a_slist_entry(ptr, type, member) a_container_of(ptr, type, member)
54#define a_slist_entry_next(ptr, type, member) a_slist_entry((ptr)->next, type, member)
55
61#define a_slist_foreach(it, ctx) \
62 for (a_slist_node *it = (ctx)->head.next; it; it = it->next)
63#define A_SLIST_FOREACH(it, ctx) \
64 for (it = (ctx)->head.next; it; it = it->next)
65
72#define a_slist_forsafe(it, at, ctx) \
73 for (a_slist_node *at = &(ctx)->head, *it = at->next; \
74 it; at = it ? it : at, it = at->next)
75#define A_SLIST_FORSAFE(it, at, ctx) \
76 for (at = &(ctx)->head, it = at->next; it; \
77 at = it ? it : at, it = at->next)
78
83A_INTERN void a_slist_ctor(a_slist *ctx)
84{
85 ctx->head.next = A_NULL;
86 ctx->tail = &ctx->head;
87}
88
93A_INTERN void a_slist_init(a_slist *ctx)
94{
95 ctx->head.next = A_NULL;
96 ctx->tail = &ctx->head;
97}
98
103A_INTERN void a_slist_dtor(a_slist *ctx)
104{
105 ctx->head.next = A_NULL;
106 ctx->tail = &ctx->head;
107}
108
114A_INTERN void a_slist_link(a_slist_node *head, a_slist_node *tail) { head->next = tail; }
115
122A_INTERN void a_slist_add(a_slist *ctx, a_slist_node *prev, a_slist_node *node)
123{
124 if (!prev->next) { ctx->tail = node; }
125 a_slist_link(node, prev->next);
126 a_slist_link(prev, node);
127}
128
134A_INTERN void a_slist_add_head(a_slist *ctx, a_slist_node *node)
135{
136 a_slist_add(ctx, &ctx->head, node);
137}
138
144A_INTERN void a_slist_add_tail(a_slist *ctx, a_slist_node *node)
145{
146 a_slist_link(ctx->tail, node);
147 node->next = A_NULL;
148 ctx->tail = node;
149}
150
156A_INTERN void a_slist_del(a_slist *ctx, a_slist_node *prev)
157{
158 a_slist_node *const node = prev->next;
159 if (node)
160 {
161 a_slist_link(prev, node->next);
162 if (!node->next) { ctx->tail = prev; }
163 }
164}
165
170A_INTERN void a_slist_del_head(a_slist *ctx)
171{
172 a_slist_node *const node = ctx->head.next;
173 if (node)
174 {
175 a_slist_link(&ctx->head, node->next);
176 if (!node->next) { ctx->tail = &ctx->head; }
177 }
178}
179
186A_INTERN void a_slist_mov(a_slist *ctx, a_slist *to, a_slist_node *at)
187{
188 a_slist_node *const node = ctx->head.next;
189 if (node)
190 {
191 if (!at->next) { to->tail = ctx->tail; }
192 a_slist_link(ctx->tail, at->next);
193 a_slist_link(at, node);
194 }
195}
196
201A_INTERN void a_slist_rot(a_slist *ctx)
202{
203 a_slist_node *const node = ctx->head.next;
204 if (node)
205 {
206 a_slist_link(&ctx->head, node->next);
207 a_slist_link(ctx->tail, node);
208 node->next = A_NULL;
209 ctx->tail = node;
210 }
211}
212
214
215#endif /* a/slist.h */
algorithm library
void a_slist_ctor(a_slist *ctx)
constructor for singly linked list head
Definition slist.h:83
void a_slist_rot(a_slist *ctx)
rotate a node in the list
Definition slist.h:201
void a_slist_init(a_slist *ctx)
initialize for singly linked list head
Definition slist.h:93
void a_slist_mov(a_slist *ctx, a_slist *to, a_slist_node *at)
moving a list to another list
Definition slist.h:186
void a_slist_add_head(a_slist *ctx, a_slist_node *node)
insert a node to a list head
Definition slist.h:134
void a_slist_link(a_slist_node *head, a_slist_node *tail)
link head node and tail node
Definition slist.h:114
void a_slist_del_head(a_slist *ctx)
delete a node from a list head
Definition slist.h:170
void a_slist_del(a_slist *ctx, a_slist_node *prev)
delete a node from a list
Definition slist.h:156
void a_slist_dtor(a_slist *ctx)
destructor for singly linked list head
Definition slist.h:103
void a_slist_add_tail(a_slist *ctx, a_slist_node *node)
insert a node to a list tail
Definition slist.h:144
void a_slist_add(a_slist *ctx, a_slist_node *prev, a_slist_node *node)
insert a node to a list
Definition slist.h:122
instance structure for singly linked list node
Definition slist.h:26
instance structure for singly linked list head
Definition slist.h:42