liba 0.1.15
An algorithm library based on C/C++
Loading...
Searching...
No Matches
list.h
Go to the documentation of this file.
1
6#ifndef LIBA_LIST_H
7#define LIBA_LIST_H
8
9#include "a.h"
10
17// clang-format off
18#define A_LIST_INIT(node) {&(node), &(node)}
19// clang-format on
20
24typedef struct a_list
25{
26 struct a_list *next, *prev;
28
35#define a_list_(_, x) a_cast_s(a_list _, a_cast_s(void _, x))
36
43#define a_list_entry(ptr, type, member) a_container_of(ptr, type, member)
44#define a_list_entry_next(ptr, type, member) a_list_entry((ptr)->next, type, member)
45#define a_list_entry_prev(ptr, type, member) a_list_entry((ptr)->prev, type, member)
46
55#define a_list_foreach_(it, ctx, next) \
56 for (a_list *it = (ctx)->next; it != (ctx); it = it->next)
57#define a_list_foreach_next(it, ctx) a_list_foreach_(it, ctx, next)
58#define a_list_foreach_prev(it, ctx) a_list_foreach_(it, ctx, prev)
59
69#define a_list_forsafe_(it, at, ctx, next) \
70 for (a_list *it = (ctx)->next, *at = it->next; it != (ctx); it = at, at = it->next)
71#define a_list_forsafe_next(it, at, ctx) a_list_forsafe_(it, at, ctx, next)
72#define a_list_forsafe_prev(it, at, ctx) a_list_forsafe_(it, at, ctx, prev)
73
78A_INTERN void a_list_ctor(a_list *ctx) { ctx->prev = ctx->next = ctx; }
79
84A_INTERN void a_list_init(a_list *ctx) { ctx->prev = ctx->next = ctx; }
85
90A_INTERN void a_list_dtor(a_list *ctx) { ctx->prev = ctx->next = ctx; }
91
107A_INTERN void a_list_link(a_list *head, a_list *tail)
108{
109 head->next = tail;
110 tail->prev = head;
111}
112
128A_INTERN void a_list_loop(a_list *head, a_list *tail)
129{
130 head->prev = tail;
131 tail->next = head;
132}
133
158A_INTERN void a_list_add_(a_list *head1, a_list *tail1, a_list *head2, a_list *tail2)
159{
160 a_list_link(tail1, head2);
161 a_list_link(tail2, head1);
162}
163
183A_INTERN void a_list_add_node(a_list *head, a_list *tail, a_list *node)
184{
185 a_list_add_(head, tail, node, node);
186}
187
206A_INTERN void a_list_add_next(a_list *ctx, a_list *node)
207{
208 a_list_add_(ctx->next, ctx, node, node);
209}
210
229A_INTERN void a_list_add_prev(a_list *ctx, a_list *node)
230{
231 a_list_add_(ctx, ctx->prev, node, node);
232}
233
251A_INTERN void a_list_del_(a_list const *head, a_list const *tail)
252{
253 a_list_link(head->prev, tail->next);
254}
255
273A_INTERN void a_list_del_node(a_list const *node) { a_list_del_(node, node); }
274
292A_INTERN void a_list_del_next(a_list const *node) { a_list_del_(node->next, node->next); }
293
311A_INTERN void a_list_del_prev(a_list const *node) { a_list_del_(node->prev, node->prev); }
312
333A_INTERN void a_list_set_(a_list const *head1, a_list const *tail1, a_list *head2, a_list *tail2)
334{
335 a_list_add_(tail1->next, head1->prev, head2, tail2);
336}
337
343A_INTERN void a_list_set_node(a_list const *ctx, a_list *rhs)
344{
345 a_list_add_(ctx->next, ctx->prev, rhs, rhs);
346}
347
366A_INTERN void a_list_mov_next(a_list *ctx, a_list const *rhs)
367{
368 a_list_add_(ctx->next, ctx, rhs->next, rhs->prev);
369}
370
389A_INTERN void a_list_mov_prev(a_list *ctx, a_list const *rhs)
390{
391 a_list_add_(ctx, ctx->prev, rhs->next, rhs->prev);
392}
393
414A_INTERN void a_list_rot_next(a_list *ctx)
415{
416 a_list *const node = ctx->prev;
417 a_list_del_(node, node);
418 a_list_add_(ctx->next, ctx, node, node);
419}
420
441A_INTERN void a_list_rot_prev(a_list *ctx)
442{
443 a_list *const node = ctx->next;
444 a_list_del_(node, node);
445 a_list_add_(ctx, ctx->prev, node, node);
446}
447
470A_INTERN void a_list_swap_(a_list *head1, a_list *tail1, a_list *head2, a_list *tail2)
471{
472 a_list *const head = tail2->next, *const tail = head2->prev;
473 a_list_add_(tail1->next, head1->prev, head2, tail2);
474 a_list_add_(head, tail, head1, tail1);
475}
476
482A_INTERN void a_list_swap_node(a_list *lhs, a_list *rhs)
483{
484 a_list_swap_(lhs, lhs, rhs, rhs);
485}
486
489#endif /* a/list.h */
algorithm library
void a_list_dtor(a_list *ctx)
destructor for circular doubly linked list
Definition list.h:90
void a_list_swap_(a_list *head1, a_list *tail1, a_list *head2, a_list *tail2)
swap a section of one list and a section of another list
Definition list.h:470
void a_list_ctor(a_list *ctx)
constructor for circular doubly linked list
Definition list.h:78
void a_list_add_node(a_list *head, a_list *tail, a_list *node)
insert a node to a list
Definition list.h:183
void a_list_set_(a_list const *head1, a_list const *tail1, a_list *head2, a_list *tail2)
modify a section of a list
Definition list.h:333
void a_list_set_node(a_list const *ctx, a_list *rhs)
modify a node of a list
Definition list.h:343
void a_list_init(a_list *ctx)
initialize for circular doubly linked list
Definition list.h:84
void a_list_rot_next(a_list *ctx)
rotate a node in the list backward
Definition list.h:414
void a_list_link(a_list *head, a_list *tail)
link head node and tail node
Definition list.h:107
struct a_list a_list
instance structure for circular doubly linked list
void a_list_del_node(a_list const *node)
delete a node from a list
Definition list.h:273
void a_list_swap_node(a_list *lhs, a_list *rhs)
swap the node lhs and the node rhs
Definition list.h:482
void a_list_mov_next(a_list *ctx, a_list const *rhs)
moving a list from another list forward
Definition list.h:366
void a_list_rot_prev(a_list *ctx)
rotate a node in the list forward
Definition list.h:441
void a_list_del_prev(a_list const *node)
remove a node from a list backward
Definition list.h:311
void a_list_add_prev(a_list *ctx, a_list *node)
append a node to a list backward
Definition list.h:229
void a_list_loop(a_list *head, a_list *tail)
loop head node to tail node
Definition list.h:128
void a_list_del_(a_list const *head, a_list const *tail)
delete a section of a list
Definition list.h:251
void a_list_add_(a_list *head1, a_list *tail1, a_list *head2, a_list *tail2)
connect list1 and list2
Definition list.h:158
void a_list_add_next(a_list *ctx, a_list *node)
append a node to a list forward
Definition list.h:206
void a_list_del_next(a_list const *node)
remove a node from a list forward
Definition list.h:292
void a_list_mov_prev(a_list *ctx, a_list const *rhs)
moving a list from another list backward
Definition list.h:389
instance structure for circular doubly linked list
Definition list.h:25