Debian Codegraph @

src/utils/list.h

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3
4 #pragma once
5 #include <stdbool.h>
6
7 struct list_node {
8 struct list_node *next, *prev;
9 };
10
11 #define list_entry(ptr, type, node) container_of(ptr, type, node)
12 #define list_next_entry(ptr, node) list_entry((ptr)->node.next, __typeof__(*(ptr)), node)
13 #define list_prev_entry(ptr, node) list_entry((ptr)->node.prev, __typeof__(*(ptr)), node)
14
15 /// Insert a new node between two adjacent nodes in the list
16 static inline void __list_insert_between(struct list_node *prev, struct list_node *next,
17 struct list_node *new_) {
18 new_->prev = prev;
19 new_->next = next;
20 next->prev = new_;
21 prev->next = new_;
22 }
23
24 /// Insert a new node after `curr`
25 static inline void list_insert_after(struct list_node *curr, struct list_node *new_) {
26 __list_insert_between(curr, curr->next, new_);
27 }
28
29 /// Insert a new node before `curr`
30 static inline void list_insert_before(struct list_node *curr, struct list_node *new_) {
31 __list_insert_between(curr->prev, curr, new_);
32 }
33
34 /// Link two nodes in the list, so `next` becomes the successor node of `prev`
35 static inline void __list_link(struct list_node *prev, struct list_node *next) {
36 next->prev = prev;
37 prev->next = next;
38 }
39
40 /// Remove a node from the list
41 static inline void list_remove(struct list_node *to_remove) {
42 __list_link(to_remove->prev, to_remove->next);
43 to_remove->prev = (void *)-1;
44 to_remove->next = (void *)-2;
45 }
46
47 /// Move `to_move` so that it's before `new_next`
48 static inline void list_move_before(struct list_node *to_move, struct list_node *new_next) {
49 list_remove(to_move);
50 list_insert_before(new_next, to_move);
51 }
52
53 /// Move `to_move` so that it's after `new_prev`
54 static inline void list_move_after(struct list_node *to_move, struct list_node *new_prev) {
55 list_remove(to_move);
56 list_insert_after(new_prev, to_move);
57 }
58
59 /// Initialize a list node that's intended to be the head node
60 static inline void list_init_head(struct list_node *head) {
61 head->next = head->prev = head;
62 }
63
64 /// Replace list node `old` with `n`
65 static inline void list_replace(struct list_node *old, struct list_node *n) {
66 __list_insert_between(old->prev, old->next, n);
67 old->prev = (void *)-1;
68 old->next = (void *)-2;
69 }
70
71 /// Return true if head is the only node in the list. Under usual circumstances this means
72 /// the list is empty
73 static inline bool list_is_empty(const struct list_node *head) {
74 return head->prev == head;
75 }
76
77 /// Splice a list of nodes from `from` to into the beginning of list `to`.
78 static inline void list_splice(struct list_node *from, struct list_node *to) {
79 if (list_is_empty(from)) {
80 return;
81 }
82 __list_link(from->prev, to->next);
83 __list_link(to, from->next);
84 list_init_head(from);
85 }
86
87 /// Return true if `to_check` is the first node in list headed by `head`
88 static inline bool
89 list_node_is_first(const struct list_node *head, const struct list_node *to_check) {
90 return head->next == to_check;
91 }
92
93 /// Return true if `to_check` is the last node in list headed by `head`
94 static inline bool
95 list_node_is_last(const struct list_node *head, const struct list_node *to_check) {
96 return head->prev == to_check;
97 }
98
99 #define list_foreach(type, i, head, member) \
100 for (type *i = list_entry((head)->next, type, member); &i->member != (head); \
101 i = list_next_entry(i, member))
102
103 /// Like list_for_each, but it's safe to remove the current list node from the list
104 #define list_foreach_safe(type, i, head, member) \
105 for (type *i = list_entry((head)->next, type, member), \
106 *__tmp = list_next_entry(i, member); \
107 &i->member != (head); i = __tmp, __tmp = list_next_entry(i, member))