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
6#ifndef LIBA_SLIST_H
7#define LIBA_SLIST_H
8
9#include "a.h"
10
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
70#define a_slist_forsafe(it, at, ctx) \
71 for (a_slist_node *at = &(ctx)->head, *it = at->next; \
72 it; at = it ? it : at, it = at->next)
73
78A_INTERN void a_slist_ctor(a_slist *ctx)
79{
80 ctx->head.next = A_NULL;
81 ctx->tail = &ctx->head;
82}
83
88A_INTERN void a_slist_init(a_slist *ctx)
89{
90 ctx->head.next = A_NULL;
91 ctx->tail = &ctx->head;
92}
93
98A_INTERN void a_slist_dtor(a_slist *ctx)
99{
100 ctx->head.next = A_NULL;
101 ctx->tail = &ctx->head;
102}
103
109A_INTERN void a_slist_link(a_slist_node *head, a_slist_node *tail) { head->next = tail; }
110
117A_INTERN void a_slist_add(a_slist *ctx, a_slist_node *prev, a_slist_node *node)
118{
119 if (!prev->next) { ctx->tail = node; }
120 a_slist_link(node, prev->next);
121 a_slist_link(prev, node);
122}
123
129A_INTERN void a_slist_add_head(a_slist *ctx, a_slist_node *node)
130{
131 a_slist_add(ctx, &ctx->head, node);
132}
133
139A_INTERN void a_slist_add_tail(a_slist *ctx, a_slist_node *node)
140{
141 a_slist_link(ctx->tail, node);
142 node->next = A_NULL;
143 ctx->tail = node;
144}
145
151A_INTERN void a_slist_del(a_slist *ctx, a_slist_node *prev)
152{
153 a_slist_node *const node = prev->next;
154 if (node)
155 {
156 a_slist_link(prev, node->next);
157 if (!node->next) { ctx->tail = prev; }
158 }
159}
160
165A_INTERN void a_slist_del_head(a_slist *ctx)
166{
167 a_slist_node *const node = ctx->head.next;
168 if (node)
169 {
170 a_slist_link(&ctx->head, node->next);
171 if (!node->next) { ctx->tail = &ctx->head; }
172 }
173}
174
181A_INTERN void a_slist_mov(a_slist *ctx, a_slist *to, a_slist_node *at)
182{
183 a_slist_node *const node = ctx->head.next;
184 if (node)
185 {
186 if (!at->next) { to->tail = ctx->tail; }
187 a_slist_link(ctx->tail, at->next);
188 a_slist_link(at, node);
189 }
190}
191
196A_INTERN void a_slist_rot(a_slist *ctx)
197{
198 a_slist_node *const node = ctx->head.next;
199 if (node)
200 {
201 a_slist_link(&ctx->head, node->next);
202 a_slist_link(ctx->tail, node);
203 node->next = A_NULL;
204 ctx->tail = node;
205 }
206}
207
210#endif /* a/slist.h */
algorithm library
void a_slist_ctor(a_slist *ctx)
constructor for singly linked list head
Definition slist.h:78
void a_slist_rot(a_slist *ctx)
rotate a node in the list
Definition slist.h:196
void a_slist_init(a_slist *ctx)
initialize for singly linked list head
Definition slist.h:88
void a_slist_mov(a_slist *ctx, a_slist *to, a_slist_node *at)
moving a list to another list
Definition slist.h:181
struct a_slist a_slist
instance structure for singly linked list head
void a_slist_add_head(a_slist *ctx, a_slist_node *node)
insert a node to a list head
Definition slist.h:129
void a_slist_link(a_slist_node *head, a_slist_node *tail)
link head node and tail node
Definition slist.h:109
void a_slist_del_head(a_slist *ctx)
delete a node from a list head
Definition slist.h:165
void a_slist_del(a_slist *ctx, a_slist_node *prev)
delete a node from a list
Definition slist.h:151
void a_slist_dtor(a_slist *ctx)
destructor for singly linked list head
Definition slist.h:98
void a_slist_add_tail(a_slist *ctx, a_slist_node *node)
insert a node to a list tail
Definition slist.h:139
struct a_slist_node a_slist_node
instance structure for singly linked list node
void a_slist_add(a_slist *ctx, a_slist_node *prev, a_slist_node *node)
insert a node to a list
Definition slist.h:117
instance structure for singly linked list node
Definition slist.h:26
instance structure for singly linked list head
Definition slist.h:42