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
5
6#ifndef LIBA_LIST_H
7#define LIBA_LIST_H
8
9#include "a.h"
10
16
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_(it, ctx, next) \
58 for (it = (ctx)->next; it != (ctx); it = it->next)
59#define a_list_foreach_next(it, ctx) a_list_foreach_(it, ctx, next)
60#define a_list_foreach_prev(it, ctx) a_list_foreach_(it, ctx, prev)
61#define A_LIST_FOREACH_NEXT(it, ctx) A_LIST_FOREACH_(it, ctx, next)
62#define A_LIST_FOREACH_PREV(it, ctx) A_LIST_FOREACH_(it, ctx, prev)
63
73#define a_list_forsafe_(it, at, ctx, next) \
74 for (a_list *it = (ctx)->next, *at = it->next; it != (ctx); it = at, at = it->next)
75#define A_LIST_FORSAFE_(it, at, ctx, next) \
76 for (it = (ctx)->next, at = it->next; it != (ctx); it = at, at = it->next)
77#define a_list_forsafe_next(it, at, ctx) a_list_forsafe_(it, at, ctx, next)
78#define a_list_forsafe_prev(it, at, ctx) a_list_forsafe_(it, at, ctx, prev)
79#define A_LIST_FORSAFE_NEXT(it, at, ctx) A_LIST_FORSAFE_(it, at, ctx, next)
80#define A_LIST_FORSAFE_PREV(it, at, ctx) A_LIST_FORSAFE_(it, at, ctx, prev)
81
86A_INTERN void a_list_ctor(a_list *ctx) { ctx->prev = ctx->next = ctx; }
87
92A_INTERN void a_list_init(a_list *ctx) { ctx->prev = ctx->next = ctx; }
93
98A_INTERN void a_list_dtor(a_list *ctx) { ctx->prev = ctx->next = ctx; }
99
115A_INTERN void a_list_link(a_list *head, a_list *tail)
116{
117 head->next = tail;
118 tail->prev = head;
119}
120
136A_INTERN void a_list_loop(a_list *head, a_list *tail)
137{
138 head->prev = tail;
139 tail->next = head;
140}
141
166A_INTERN void a_list_add_(a_list *head1, a_list *tail1, a_list *head2, a_list *tail2)
167{
168 a_list_link(tail1, head2);
169 a_list_link(tail2, head1);
170}
171
191A_INTERN void a_list_add_node(a_list *head, a_list *tail, a_list *node)
192{
193 a_list_add_(head, tail, node, node);
194}
195
214A_INTERN void a_list_add_next(a_list *ctx, a_list *node)
215{
216 a_list_add_(ctx->next, ctx, node, node);
217}
218
237A_INTERN void a_list_add_prev(a_list *ctx, a_list *node)
238{
239 a_list_add_(ctx, ctx->prev, node, node);
240}
241
259A_INTERN void a_list_del_(a_list const *head, a_list const *tail)
260{
261 a_list_link(head->prev, tail->next);
262}
263
281A_INTERN void a_list_del_node(a_list const *node) { a_list_del_(node, node); }
282
300A_INTERN void a_list_del_next(a_list const *node) { a_list_del_(node->next, node->next); }
301
319A_INTERN void a_list_del_prev(a_list const *node) { a_list_del_(node->prev, node->prev); }
320
341A_INTERN void a_list_set_(a_list const *head1, a_list const *tail1, a_list *head2, a_list *tail2)
342{
343 a_list_add_(tail1->next, head1->prev, head2, tail2);
344}
345
351A_INTERN void a_list_set_node(a_list const *ctx, a_list *rhs)
352{
353 a_list_add_(ctx->next, ctx->prev, rhs, rhs);
354}
355
374A_INTERN void a_list_mov_next(a_list *ctx, a_list const *rhs)
375{
376 a_list_add_(ctx->next, ctx, rhs->next, rhs->prev);
377}
378
397A_INTERN void a_list_mov_prev(a_list *ctx, a_list const *rhs)
398{
399 a_list_add_(ctx, ctx->prev, rhs->next, rhs->prev);
400}
401
422A_INTERN void a_list_rot_next(a_list *ctx)
423{
424 a_list *const node = ctx->prev;
425 a_list_del_(node, node);
426 a_list_add_(ctx->next, ctx, node, node);
427}
428
449A_INTERN void a_list_rot_prev(a_list *ctx)
450{
451 a_list *const node = ctx->next;
452 a_list_del_(node, node);
453 a_list_add_(ctx, ctx->prev, node, node);
454}
455
478A_INTERN void a_list_swap_(a_list *head1, a_list *tail1, a_list *head2, a_list *tail2)
479{
480 a_list *const head = tail2->next, *const tail = head2->prev;
481 a_list_add_(tail1->next, head1->prev, head2, tail2);
482 a_list_add_(head, tail, head1, tail1);
483}
484
490A_INTERN void a_list_swap_node(a_list *lhs, a_list *rhs)
491{
492 a_list_swap_(lhs, lhs, rhs, rhs);
493}
494
496
497#endif /* a/list.h */
algorithm library
void a_list_dtor(a_list *ctx)
destructor for circular doubly linked list
Definition list.h:98
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:478
void a_list_ctor(a_list *ctx)
constructor for circular doubly linked list
Definition list.h:86
void a_list_add_node(a_list *head, a_list *tail, a_list *node)
insert a node to a list
Definition list.h:191
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:341
void a_list_set_node(a_list const *ctx, a_list *rhs)
modify a node of a list
Definition list.h:351
void a_list_init(a_list *ctx)
initialize for circular doubly linked list
Definition list.h:92
void a_list_rot_next(a_list *ctx)
rotate a node in the list backward
Definition list.h:422
void a_list_link(a_list *head, a_list *tail)
link head node and tail node
Definition list.h:115
void a_list_del_node(a_list const *node)
delete a node from a list
Definition list.h:281
void a_list_swap_node(a_list *lhs, a_list *rhs)
swap the node lhs and the node rhs
Definition list.h:490
void a_list_mov_next(a_list *ctx, a_list const *rhs)
moving a list from another list forward
Definition list.h:374
void a_list_rot_prev(a_list *ctx)
rotate a node in the list forward
Definition list.h:449
void a_list_del_prev(a_list const *node)
remove a node from a list backward
Definition list.h:319
void a_list_add_prev(a_list *ctx, a_list *node)
append a node to a list backward
Definition list.h:237
void a_list_loop(a_list *head, a_list *tail)
loop head node to tail node
Definition list.h:136
void a_list_del_(a_list const *head, a_list const *tail)
delete a section of a list
Definition list.h:259
void a_list_add_(a_list *head1, a_list *tail1, a_list *head2, a_list *tail2)
connect list1 and list2
Definition list.h:166
void a_list_add_next(a_list *ctx, a_list *node)
append a node to a list forward
Definition list.h:214
void a_list_del_next(a_list const *node)
remove a node from a list forward
Definition list.h:300
void a_list_mov_prev(a_list *ctx, a_list const *rhs)
moving a list from another list backward
Definition list.h:397
instance structure for circular doubly linked list
Definition list.h:25