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. Sets its pointers to NULL.
75 void List_Delete(llist_t *node)
77 __List_Delete_Node(node);
78 node->next = node->prev = NULL;
82 * Removes a node from its list. Reinitialize it.
84 void List_Delete_Init(llist_t *node)
86 __List_Delete_Node(node);
87 node->next = node->prev = node;
91 * Replace old with new. Old is overwritten if empty.
93 void List_Replace(llist_t *old, llist_t *_new)
95 _new->next = old->next;
96 _new->next->prev = _new;
97 _new->prev = old->prev;
98 _new->prev->next = _new;
99 old->next = old->prev = old;
103 * Swap node1 with node2 in place.
105 void List_Swap(llist_t *node1, llist_t *node2)
107 llist_t *pos = node2->prev;
108 List_Delete_Init(node2);
109 List_Replace(node1, node2);
112 List_Add(node1, pos);
116 * Delete list from its... list, then insert after head.
118 void List_Move(llist_t *list, llist_t *head)
120 __List_Delete_Node(list);
121 List_Add(list, head);
125 * Delete list from its... list, then insert before head.
127 void List_Move_Tail(llist_t *list, llist_t *head)
129 __List_Delete_Node(list);
130 List_Add_Tail(list, head);
134 * Move the first node of a range of nodes immediately after head.
135 * All three parameters must belong to the same list.
138 void List_Bulk_Move_Tail(llist_t *head, llist_t *first, llist_t *last)
140 first->prev->next = last->next;
141 last->next->prev = first->prev;
143 head->prev->next = first;
144 first->prev = head->prev;
151 * Shift the head to the right (like rotating a wheel counterclockwise).
152 * The node immediately to the right becomes the new head.
154 void List_Rotate_Left(llist_t *head)
158 if (!List_IsEmpty(head))
161 List_Move_Tail(first, head);
166 * Make list the new head.
168 void List_Rotate_To_Front(llist_t *list, llist_t *head)
170 List_Move_Tail(head, list);
174 * Concatenate two lists. The head of list will be discarded.
176 static void __List_Splice(const llist_t *list, llist_t *prev, llist_t *next)
178 llist_t *first = list->next;
179 llist_t *last = list->prev;
189 * Concatenate two lists. The first node of list will be inserted after head.
191 void List_Splice(const llist_t *list, llist_t *head)
193 if(!List_IsEmpty(list))
194 __List_Splice(list, head, head->next);
198 * Concatenate two lists. The tail of list will be inserted before head.
200 void List_Splice_Tail(const llist_t *list, llist_t *head)
202 if (!List_IsEmpty(list))
203 __List_Splice(list, head->prev, head);
206 qboolean List_IsFirst(llist_t *list, llist_t *start)
208 return list->prev == start;
211 qboolean List_IsLast(llist_t *list, llist_t *start)
213 return list->next == start;
216 qboolean List_IsEmpty(const llist_t *list)
218 return list->next == list;