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, inspired by Linux list.h
27 * Insert a node between two known nodes.
29 * Only use when prev and next are known.
31 static void __List_Add(llist_t *node, llist_t *prev, llist_t *next)
40 * Insert a node immediately after head.
42 void List_Add(llist_t *node, llist_t *head)
44 __List_Add(node, head, head->next);
48 * Insert a node immediately before head.
50 void List_Add_Tail(llist_t *node, llist_t *head)
52 __List_Add(node, head->prev, head);
56 * Bridge prev and next together, when removing the parent of them.
58 static void __List_Delete(llist_t *prev, llist_t *next)
67 static void __List_Delete_Node(llist_t *node)
69 __List_Delete(node->prev, node->next);
73 * Removes a node from its list.
75 void List_Delete(llist_t *node)
77 __List_Delete_Node(node);
78 node->next = node->prev = node;
82 * Replace old with new. Old is overwritten if empty.
84 void List_Replace(llist_t *old, llist_t *_new)
86 _new->next = old->next;
87 _new->next->prev = _new;
88 _new->prev = old->prev;
89 _new->prev->next = _new;
90 old->next = old->prev = old;
94 * Swap node1 with node2 in place.
96 void List_Swap(llist_t *node1, llist_t *node2)
98 llist_t *pos = node2->prev;
100 List_Replace(node1, node2);
103 List_Add(node1, pos);
107 * Delete list from its... list, then insert after head.
109 void List_Move(llist_t *list, llist_t *head)
111 __List_Delete_Node(list);
112 List_Add(list, head);
116 * Delete list from its... list, then insert before head.
118 void List_Move_Tail(llist_t *list, llist_t *head)
120 __List_Delete_Node(list);
121 List_Add_Tail(list, head);
125 * Move the first node of a range of nodes immediately after head.
126 * All three parameters must belong to the same list.
129 void List_Bulk_Move_Tail(llist_t *head, llist_t *first, llist_t *last)
131 first->prev->next = last->next;
132 last->next->prev = first->prev;
134 head->prev->next = first;
135 first->prev = head->prev;
142 * Shift the head to the right (like rotating a wheel counterclockwise).
143 * The node immediately to the right becomes the new head.
145 void List_Rotate_Left(llist_t *head)
149 if (!List_IsEmpty(head))
152 List_Move_Tail(first, head);
157 * Make list the new head.
159 void List_Rotate_To_Front(llist_t *list, llist_t *head)
161 List_Move_Tail(head, list);
165 * Concatenate two lists. The head of list will be discarded.
167 static void __List_Splice(const llist_t *list, llist_t *prev, llist_t *next)
169 llist_t *first = list->next;
170 llist_t *last = list->prev;
180 * Concatenate two lists. The first node of list will be inserted after head.
182 void List_Splice(const llist_t *list, llist_t *head)
184 if(!List_IsEmpty(list))
185 __List_Splice(list, head, head->next);
189 * Concatenate two lists. The tail of list will be inserted before head.
191 void List_Splice_Tail(const llist_t *list, llist_t *head)
193 if (!List_IsEmpty(list))
194 __List_Splice(list, head->prev, head);
197 qboolean List_IsFirst(llist_t *list, llist_t *start)
199 return list->prev == start;
202 qboolean List_IsLast(llist_t *list, llist_t *start)
204 return list->next == start;
207 qboolean List_IsEmpty(const llist_t *list)
209 return list->next == list;