2 Copyright (C) 2020 David "Cloudwalk" Knapp
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 // com_list.c - generic doubly linked list interface, adapted from Linux list.h
27 * Creates a new linked list. Initializes the head to point to itself.
28 * If it's a list header, the result is an empty list.
30 void List_Create(llist_t *list)
32 list->next = list->prev = NULL;
36 * Insert a node between two known nodes.
38 * Only use when prev and next are known.
40 static void __List_Add(llist_t *node, llist_t *prev, llist_t *next)
49 * Insert a node immediately after head.
51 void List_Add(llist_t *node, llist_t *head)
53 __List_Add(node, head, head->next);
57 * Insert a node immediately before head.
59 void List_Add_Tail(llist_t *node, llist_t *head)
61 __List_Add(node, head->prev, head);
65 * Bridge prev and next together, when removing the parent of them.
67 static inline void __List_Delete(llist_t *prev, llist_t *next)
76 static inline void __List_Delete_Node(llist_t *node)
78 __List_Delete(node->prev, node->next);
82 * Removes a node from its list. Sets its pointers to NULL.
84 void List_Delete(llist_t *node)
86 __List_Delete_Node(node);
87 node->next = node->prev = NULL;
91 * Removes a node from its list. Reinitialize it.
93 void List_Delete_Init(llist_t *node)
95 __List_Delete_Node(node);
96 node->next = node->prev = node;
100 * Replace old with new. Old is overwritten if empty.
102 void List_Replace(llist_t *old, llist_t *_new)
104 _new->next = old->next;
105 _new->next->prev = _new;
106 _new->prev = old->prev;
107 _new->prev->next = _new;
108 old->next = old->prev = old;
112 * Replace old with new. Initialize old.
113 * Old is overwritten if empty.
115 void List_Replace_Init(llist_t *old, llist_t *_new)
117 List_Replace(old, _new);
122 * Swap node1 with node2 in place.
124 void List_Swap(llist_t *node1, llist_t *node2)
126 llist_t *pos = node2->prev;
127 List_Delete_Init(node2);
128 List_Replace(node1, node2);
131 List_Add(node1, pos);
135 * Delete list from its... list, then insert after head.
137 void List_Move(llist_t *list, llist_t *head)
139 __List_Delete_Node(list);
140 List_Add(list, head);
144 * Delete list from its... list, then insert before head.
146 void List_Move_Tail(llist_t *list, llist_t *head)
148 __List_Delete_Node(list);
149 List_Add_Tail(list, head);
153 * Move the first node of a range of nodes immediately after head.
154 * All three parameters must belong to the same list.
157 void List_Bulk_Move_Tail(llist_t *head, llist_t *first, llist_t *last)
159 first->prev->next = last->next;
160 last->next->prev = first->prev;
162 head->prev->next = first;
163 first->prev = head->prev;
170 * Shift the head to the right (like rotating a wheel counterclockwise).
171 * The node immediately to the right becomes the new head.
173 void List_Rotate_Left(llist_t *head)
177 if (!List_Is_Empty(head))
180 List_Move_Tail(first, head);
185 * Make list the new head.
187 void List_Rotate_To_Front(llist_t *list, llist_t *head)
189 List_Move_Tail(head, list);
193 * Concatenate two lists. The head of list will be discarded.
195 static inline void __List_Splice(const llist_t *list, llist_t *prev, llist_t *next)
197 llist_t *first = list->next;
198 llist_t *last = list->prev;
208 * Concatenate two lists. The first node of list will be inserted after head.
210 void List_Splice(const llist_t *list, llist_t *head)
212 if(!List_Is_Empty(list))
213 __List_Splice(list, head, head->next);
217 * Concatenate two lists. The tail of list will be inserted before head.
219 void List_Splice_Tail(const llist_t *list, llist_t *head)
221 if (!List_Is_Empty(list))
222 __List_Splice(list, head->prev, head);
225 qbool List_Is_First(llist_t *list, llist_t *start)
227 return list->prev == start;
230 qbool List_Is_Last(llist_t *list, llist_t *start)
232 return list->next == start;
235 qbool List_Is_Empty(const llist_t *list)
237 return list->next == list;