]> git.xonotic.org Git - xonotic/darkplaces.git/blob - com_list.c
com_list: Rename Delete to Delete_Init and implement the actual Delete
[xonotic/darkplaces.git] / com_list.c
1 /*
2 Copyright (C) 2020 David "Cloudwalk" Knapp
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20
21 // com_list.c - generic doubly linked list interface, inspired by Linux list.h
22
23 #include "qtypes.h"
24 #include "com_list.h"
25
26 /*
27  * Insert a node between two known nodes.
28  * 
29  * Only use when prev and next are known.
30  */
31 static void __List_Add(llist_t *node, llist_t *prev, llist_t *next)
32 {
33         next->prev = node;
34         node->next = next;
35         node->prev = prev;
36         prev->next = node;
37 }
38
39 /*
40  * Insert a node immediately after head.
41  */
42 void List_Add(llist_t *node, llist_t *head)
43 {
44         __List_Add(node, head, head->next);
45 }
46
47 /*
48  * Insert a node immediately before head.
49  */
50 void List_Add_Tail(llist_t *node, llist_t *head)
51 {
52         __List_Add(node, head->prev, head);
53 }
54
55 /*
56  * Bridge prev and next together, when removing the parent of them.
57  */
58 static void __List_Delete(llist_t *prev, llist_t *next)
59 {
60         next->prev = prev;
61         prev->next = next;
62 }
63
64 /*
65  * Redundant?
66  */
67 static void __List_Delete_Node(llist_t *node)
68 {
69         __List_Delete(node->prev, node->next);
70 }
71
72 /*
73  * Removes a node from its list. Sets its pointers to NULL.
74  */
75 void List_Delete(llist_t *node)
76 {
77         __List_Delete_Node(node);
78         node->next = node->prev = NULL;
79 }
80
81 /*
82  * Removes a node from its list. Reinitialize it.
83  */
84 void List_Delete_Init(llist_t *node)
85 {
86         __List_Delete_Node(node);
87         node->next = node->prev = node;
88 }
89
90 /*
91  * Replace old with new. Old is overwritten if empty.
92  */
93 void List_Replace(llist_t *old, llist_t *_new)
94 {
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;
100 }
101
102 /*
103  * Swap node1 with node2 in place.
104  */
105 void List_Swap(llist_t *node1, llist_t *node2)
106 {
107         llist_t *pos = node2->prev;
108         List_Delete_Init(node2);
109         List_Replace(node1, node2);
110         if(pos == node1)
111                 pos = node2;
112         List_Add(node1, pos);
113 }
114
115 /*
116  * Delete list from its... list, then insert after head.
117  */
118 void List_Move(llist_t *list, llist_t *head)
119 {
120         __List_Delete_Node(list);
121         List_Add(list, head);
122 }
123
124 /*
125  * Delete list from its... list, then insert before head.
126  */
127 void List_Move_Tail(llist_t *list, llist_t *head)
128 {
129         __List_Delete_Node(list);
130         List_Add_Tail(list, head);
131 }
132
133 /*
134  * Move the first node of a range of nodes immediately after head.
135  * All three parameters must belong to the same list.
136  */
137
138 void List_Bulk_Move_Tail(llist_t *head, llist_t *first, llist_t *last)
139 {
140         first->prev->next = last->next;
141         last->next->prev = first->prev;
142
143         head->prev->next = first;
144         first->prev = head->prev;
145
146         last->next = head;
147         head->prev = last;
148 }
149
150 /*
151  * Shift the head to the right (like rotating a wheel counterclockwise).
152  * The node immediately to the right becomes the new head.
153  */
154 void List_Rotate_Left(llist_t *head)
155 {
156         llist_t *first;
157
158         if (!List_IsEmpty(head))
159         {
160                 first = head->next;
161                 List_Move_Tail(first, head);
162         }
163 }
164
165 /*
166  * Make list the new head.
167  */
168 void List_Rotate_To_Front(llist_t *list, llist_t *head)
169 {
170         List_Move_Tail(head, list);
171 }
172
173 /*
174  * Concatenate two lists. The head of list will be discarded.
175  */
176 static void __List_Splice(const llist_t *list, llist_t *prev, llist_t *next)
177 {
178         llist_t *first = list->next;
179         llist_t *last = list->prev;
180
181         first->prev = prev;
182         prev->next = first;
183
184         last->next = next;
185         next->prev = last;
186 }
187
188 /*
189  * Concatenate two lists. The first node of list will be inserted after head.
190  */
191 void List_Splice(const llist_t *list, llist_t *head)
192 {
193         if(!List_IsEmpty(list))
194                 __List_Splice(list, head, head->next);
195 }
196
197 /*
198  * Concatenate two lists. The tail of list will be inserted before head.
199  */
200 void List_Splice_Tail(const llist_t *list, llist_t *head)
201 {
202         if (!List_IsEmpty(list))
203                 __List_Splice(list, head->prev, head);
204 }
205
206 qboolean List_IsFirst(llist_t *list, llist_t *start)
207 {
208         return list->prev == start;
209 }
210
211 qboolean List_IsLast(llist_t *list, llist_t *start)
212 {
213         return list->next == start;
214 }
215
216 qboolean List_IsEmpty(const llist_t *list)
217 {
218         return list->next == list;
219 }