Debian Codegraph @

crt/aws-c-common/include/aws/common/linked_list.h

raw symbols SCIP
1 #ifndef AWS_COMMON_LINKED_LIST_H
2 #define AWS_COMMON_LINKED_LIST_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/common/common.h>
10
11 #include <stddef.h>
12
13 AWS_PUSH_SANE_WARNING_LEVEL
14
15 struct aws_linked_list_node {
16 struct aws_linked_list_node *next;
17 struct aws_linked_list_node *prev;
18 };
19
20 struct aws_linked_list {
21 struct aws_linked_list_node head;
22 struct aws_linked_list_node tail;
23 };
24
25 AWS_EXTERN_C_BEGIN
26
27 /**
28 * Set node's next and prev pointers to NULL.
29 */
30 AWS_STATIC_IMPL void aws_linked_list_node_reset(struct aws_linked_list_node *node);
31
32 /**
33 * These functions need to be defined first as they are used in pre
34 * and post conditions.
35 */
36
37 /**
38 * Tests if the list is empty.
39 */
40 AWS_STATIC_IMPL bool aws_linked_list_empty(const struct aws_linked_list *list);
41
42 /**
43 * Checks that a linked list is valid.
44 */
45 AWS_STATIC_IMPL bool aws_linked_list_is_valid(const struct aws_linked_list *list);
46 /**
47 * Checks that the prev of the next pointer of a node points to the
48 * node. As this checks whether the [next] connection of a node is
49 * bidirectional, it returns false if used for the list tail.
50 */
51 AWS_STATIC_IMPL bool aws_linked_list_node_next_is_valid(const struct aws_linked_list_node *node);
52
53 /**
54 * Checks that the next of the prev pointer of a node points to the
55 * node. Similarly to the above, this returns false if used for the
56 * head of a list.
57 */
58 AWS_STATIC_IMPL bool aws_linked_list_node_prev_is_valid(const struct aws_linked_list_node *node);
59 /**
60 * Checks that a linked list satisfies double linked list connectivity
61 * constraints. This check is O(n) as it traverses the whole linked
62 * list to ensure that tail is reachable from head (and vice versa)
63 * and that every connection is bidirectional.
64 *
65 * Note: This check *cannot* go into an infinite loop, because we
66 * ensure that the connection to the next node is
67 * bidirectional. Therefore, if a node's [a] a.next is a previous node
68 * [b] in the list, b.prev != &a and so this check would fail, thus
69 * terminating the loop.
70 */
71 AWS_STATIC_IMPL bool aws_linked_list_is_valid_deep(const struct aws_linked_list *list);
72
73 /**
74 * Initializes the list. List will be empty after this call.
75 */
76 AWS_STATIC_IMPL void aws_linked_list_init(struct aws_linked_list *list);
77
78 /**
79 * Returns an iteration pointer for the first element in the list.
80 */
81 AWS_STATIC_IMPL struct aws_linked_list_node *aws_linked_list_begin(const struct aws_linked_list *list);
82
83 /**
84 * Returns an iteration pointer for one past the last element in the list.
85 */
86 AWS_STATIC_IMPL const struct aws_linked_list_node *aws_linked_list_end(const struct aws_linked_list *list);
87
88 /**
89 * Returns a pointer for the last element in the list.
90 * Used to begin iterating the list in reverse. Ex:
91 * for (i = aws_linked_list_rbegin(list); i != aws_linked_list_rend(list); i = aws_linked_list_prev(i)) {...}
92 */
93 AWS_STATIC_IMPL struct aws_linked_list_node *aws_linked_list_rbegin(const struct aws_linked_list *list);
94
95 /**
96 * Returns the pointer to one before the first element in the list.
97 * Used to end iterating the list in reverse.
98 */
99 AWS_STATIC_IMPL const struct aws_linked_list_node *aws_linked_list_rend(const struct aws_linked_list *list);
100
101 /**
102 * Returns the next element in the list.
103 */
104 AWS_STATIC_IMPL struct aws_linked_list_node *aws_linked_list_next(const struct aws_linked_list_node *node);
105
106 /**
107 * Returns the previous element in the list.
108 */
109 AWS_STATIC_IMPL struct aws_linked_list_node *aws_linked_list_prev(const struct aws_linked_list_node *node);
110
111 /**
112 * Inserts to_add immediately after after.
113 */
114 AWS_STATIC_IMPL void aws_linked_list_insert_after(
115 struct aws_linked_list_node *after,
116 struct aws_linked_list_node *to_add);
117 /**
118 * Swaps the order two nodes in the linked list.
119 */
120 AWS_STATIC_IMPL void aws_linked_list_swap_nodes(struct aws_linked_list_node *a, struct aws_linked_list_node *b);
121
122 /**
123 * Inserts to_add immediately before before.
124 */
125 AWS_STATIC_IMPL void aws_linked_list_insert_before(
126 struct aws_linked_list_node *before,
127 struct aws_linked_list_node *to_add);
128
129 /**
130 * Removes the specified node from the list (prev/next point to each other) and
131 * returns the next node in the list.
132 */
133 AWS_STATIC_IMPL void aws_linked_list_remove(struct aws_linked_list_node *node);
134
135 /**
136 * Append new_node.
137 */
138 AWS_STATIC_IMPL void aws_linked_list_push_back(struct aws_linked_list *list, struct aws_linked_list_node *node);
139
140 /**
141 * Returns the element in the back of the list.
142 */
143 AWS_STATIC_IMPL struct aws_linked_list_node *aws_linked_list_back(const struct aws_linked_list *list);
144
145 /**
146 * Returns the element in the back of the list and removes it
147 */
148 AWS_STATIC_IMPL struct aws_linked_list_node *aws_linked_list_pop_back(struct aws_linked_list *list);
149
150 /**
151 * Prepend new_node.
152 */
153 AWS_STATIC_IMPL void aws_linked_list_push_front(struct aws_linked_list *list, struct aws_linked_list_node *node);
154 /**
155 * Returns the element in the front of the list.
156 */
157 AWS_STATIC_IMPL struct aws_linked_list_node *aws_linked_list_front(const struct aws_linked_list *list);
158 /**
159 * Returns the element in the front of the list and removes it
160 */
161 AWS_STATIC_IMPL struct aws_linked_list_node *aws_linked_list_pop_front(struct aws_linked_list *list);
162
163 AWS_STATIC_IMPL void aws_linked_list_swap_contents(
164 struct aws_linked_list *AWS_RESTRICT a,
165 struct aws_linked_list *AWS_RESTRICT b);
166
167 /**
168 * Remove all nodes from one list, and add them to the back of another.
169 *
170 * Example: if dst={1,2} and src={3,4}, they become dst={1,2,3,4} and src={}
171 */
172 AWS_STATIC_IMPL void aws_linked_list_move_all_back(
173 struct aws_linked_list *AWS_RESTRICT dst,
174 struct aws_linked_list *AWS_RESTRICT src);
175
176 /**
177 * Remove all nodes from one list, and add them to the front of another.
178 *
179 * Example: if dst={2,1} and src={4,3}, they become dst={4,3,2,1} and src={}
180 */
181 AWS_STATIC_IMPL void aws_linked_list_move_all_front(
182 struct aws_linked_list *AWS_RESTRICT dst,
183 struct aws_linked_list *AWS_RESTRICT src);
184
185 /**
186 * Returns true if the node is currently in a list, false otherwise.
187 */
188 AWS_STATIC_IMPL bool aws_linked_list_node_is_in_list(struct aws_linked_list_node *node);
189 AWS_EXTERN_C_END
190
191 #ifndef AWS_NO_STATIC_IMPL
192 # include <aws/common/linked_list.inl>
193 #endif /* AWS_NO_STATIC_IMPL */
194 AWS_POP_SANE_WARNING_LEVEL
195
196 #endif /* AWS_COMMON_LINKED_LIST_H */