]> git.xonotic.org Git - xonotic/darkplaces.git/blob - com_list.c
Make trace entity culling optional for spectators with sv_cullentities_trace_spectators
[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, adapted from Linux list.h
22
23 #include "qtypes.h"
24 #include "com_list.h"
25
26 /*
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.
29  */
30 void List_Create(llist_t *list)
31 {
32         list->next = list->prev = NULL;
33 }
34
35 /*
36  * Insert a node between two known nodes.
37  * 
38  * Only use when prev and next are known.
39  */
40 static void __List_Add(llist_t *node, llist_t *prev, llist_t *next)
41 {
42         next->prev = node;
43         node->next = next;
44         node->prev = prev;
45         prev->next = node;
46 }
47
48 /*
49  * Insert a node immediately after head.
50  */
51 void List_Add(llist_t *node, llist_t *head)
52 {
53         __List_Add(node, head, head->next);
54 }
55
56 /*
57  * Insert a node immediately before head.
58  */
59 void List_Add_Tail(llist_t *node, llist_t *head)
60 {
61         __List_Add(node, head->prev, head);
62 }
63
64 /*
65  * Bridge prev and next together, when removing the parent of them.
66  */
67 static inline void __List_Delete(llist_t *prev, llist_t *next)
68 {
69         next->prev = prev;
70         prev->next = next;
71 }
72
73 /*
74  * Redundant?
75  */
76 static inline void __List_Delete_Node(llist_t *node)
77 {
78         __List_Delete(node->prev, node->next);
79 }
80
81 /*
82  * Removes a node from its list. Sets its pointers to NULL.
83  */
84 void List_Delete(llist_t *node)
85 {
86         __List_Delete_Node(node);
87         node->next = node->prev = NULL;
88 }
89
90 /*
91  * Removes a node from its list. Reinitialize it.
92  */
93 void List_Delete_Init(llist_t *node)
94 {
95         __List_Delete_Node(node);
96         node->next = node->prev = node;
97 }
98
99 /*
100  * Replace old with new. Old is overwritten if empty.
101  */
102 void List_Replace(llist_t *old, llist_t *_new)
103 {
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;
109 }
110
111 /*
112  * Replace old with new. Initialize old.
113  * Old is overwritten if empty.
114  */
115 void List_Replace_Init(llist_t *old, llist_t *_new)
116 {
117         List_Replace(old, _new);
118         List_Create(old);
119 }
120
121 /*
122  * Swap node1 with node2 in place.
123  */
124 void List_Swap(llist_t *node1, llist_t *node2)
125 {
126         llist_t *pos = node2->prev;
127         List_Delete_Init(node2);
128         List_Replace(node1, node2);
129         if(pos == node1)
130                 pos = node2;
131         List_Add(node1, pos);
132 }
133
134 /*
135  * Delete list from its... list, then insert after head.
136  */
137 void List_Move(llist_t *list, llist_t *head)
138 {
139         __List_Delete_Node(list);
140         List_Add(list, head);
141 }
142
143 /*
144  * Delete list from its... list, then insert before head.
145  */
146 void List_Move_Tail(llist_t *list, llist_t *head)
147 {
148         __List_Delete_Node(list);
149         List_Add_Tail(list, head);
150 }
151
152 /*
153  * Move the first node of a range of nodes immediately after head.
154  * All three parameters must belong to the same list.
155  */
156
157 void List_Bulk_Move_Tail(llist_t *head, llist_t *first, llist_t *last)
158 {
159         first->prev->next = last->next;
160         last->next->prev = first->prev;
161
162         head->prev->next = first;
163         first->prev = head->prev;
164
165         last->next = head;
166         head->prev = last;
167 }
168
169 /*
170  * Shift the head to the right (like rotating a wheel counterclockwise).
171  * The node immediately to the right becomes the new head.
172  */
173 void List_Rotate_Left(llist_t *head)
174 {
175         llist_t *first;
176
177         if (!List_Is_Empty(head))
178         {
179                 first = head->next;
180                 List_Move_Tail(first, head);
181         }
182 }
183
184 /*
185  * Make list the new head.
186  */
187 void List_Rotate_To_Front(llist_t *list, llist_t *head)
188 {
189         List_Move_Tail(head, list);
190 }
191
192 /*
193  * Concatenate two lists. The head of list will be discarded.
194  */
195 static inline void __List_Splice(const llist_t *list, llist_t *prev, llist_t *next)
196 {
197         llist_t *first = list->next;
198         llist_t *last = list->prev;
199
200         first->prev = prev;
201         prev->next = first;
202
203         last->next = next;
204         next->prev = last;
205 }
206
207 /*
208  * Concatenate two lists. The first node of list will be inserted after head.
209  */
210 void List_Splice(const llist_t *list, llist_t *head)
211 {
212         if(!List_Is_Empty(list))
213                 __List_Splice(list, head, head->next);
214 }
215
216 /*
217  * Concatenate two lists. The tail of list will be inserted before head.
218  */
219 void List_Splice_Tail(const llist_t *list, llist_t *head)
220 {
221         if (!List_Is_Empty(list))
222                 __List_Splice(list, head->prev, head);
223 }
224
225 qbool List_Is_First(llist_t *list, llist_t *start)
226 {
227         return list->prev == start;
228 }
229
230 qbool List_Is_Last(llist_t *list, llist_t *start)
231 {
232         return list->next == start;
233 }
234
235 qbool List_Is_Empty(const llist_t *list)
236 {
237         return list->next == list;
238 }