]> git.xonotic.org Git - xonotic/darkplaces.git/blob - com_list.c
cb0014802516b9a712567abe910328528d6ea5ff
[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.
74  */
75 void List_Delete(llist_t *node)
76 {
77         __List_Delete_Node(node);
78         node->next = node->prev = node;
79 }
80
81 /*
82  * Replace old with new. Old is overwritten if empty.
83  */
84 void List_Replace(llist_t *old, llist_t *_new)
85 {
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;
91 }
92
93 /*
94  * Swap node1 with node2 in place.
95  */
96 void List_Swap(llist_t *node1, llist_t *node2)
97 {
98         llist_t *pos = node2->prev;
99         List_Delete(node2);
100         List_Replace(node1, node2);
101         if(pos == node1)
102                 pos = node2;
103         List_Add(node1, pos);
104 }
105
106 /*
107  * Delete list from its... list, then insert after head.
108  */
109 void List_Move(llist_t *list, llist_t *head)
110 {
111         __List_Delete_Node(list);
112         List_Add(list, head);
113 }
114
115 /*
116  * Delete list from its... list, then insert before head.
117  */
118 void List_Move_Tail(llist_t *list, llist_t *head)
119 {
120         __List_Delete_Node(list);
121         List_Add_Tail(list, head);
122 }
123
124 /*
125  * Move the first node of a range of nodes immediately after head.
126  * All three parameters must belong to the same list.
127  */
128
129 void List_Bulk_Move_Tail(llist_t *head, llist_t *first, llist_t *last)
130 {
131         first->prev->next = last->next;
132         last->next->prev = first->prev;
133
134         head->prev->next = first;
135         first->prev = head->prev;
136
137         last->next = head;
138         head->prev = last;
139 }
140
141 /*
142  * Shift the head to the right (like rotating a wheel counterclockwise).
143  * The node immediately to the right becomes the new head.
144  */
145 void List_Rotate_Left(llist_t *head)
146 {
147         llist_t *first;
148
149         if (!List_IsEmpty(head))
150         {
151                 first = head->next;
152                 List_Move_Tail(first, head);
153         }
154 }
155
156 /*
157  * Make list the new head.
158  */
159 void List_Rotate_To_Front(llist_t *list, llist_t *head)
160 {
161         List_Move_Tail(head, list);
162 }
163
164 /*
165  * Concatenate two lists. The head of list will be discarded.
166  */
167 static void __List_Splice(const llist_t *list, llist_t *prev, llist_t *next)
168 {
169         llist_t *first = list->next;
170         llist_t *last = list->prev;
171
172         first->prev = prev;
173         prev->next = first;
174
175         last->next = next;
176         next->prev = last;
177 }
178
179 /*
180  * Concatenate two lists. The first node of list will be inserted after head.
181  */
182 void List_Splice(const llist_t *list, llist_t *head)
183 {
184         if(!List_IsEmpty(list))
185                 __List_Splice(list, head, head->next);
186 }
187
188 /*
189  * Concatenate two lists. The tail of list will be inserted before head.
190  */
191 void List_Splice_Tail(const llist_t *list, llist_t *head)
192 {
193         if (!List_IsEmpty(list))
194                 __List_Splice(list, head->prev, head);
195 }
196
197 qboolean List_IsFirst(llist_t *list, llist_t *start)
198 {
199         return list->prev == start;
200 }
201
202 qboolean List_IsLast(llist_t *list, llist_t *start)
203 {
204         return list->next == start;
205 }
206
207 qboolean List_IsEmpty(const llist_t *list)
208 {
209         return list->next == list;
210 }