Debian Codegraph @

src/common/list.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1998,1999,2000 Ross Combs ([email protected])
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. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include "common/setup_before.h"
19 #define LIST_INTERNAL_ACCESS
20 #include "common/list.h"
21
22 #include <cassert>
23
24 #include "common/eventlog.h"
25 #include "common/xalloc.h"
26 #include "common/setup_after.h"
27
28
29 namespace pvpgn
30 {
31
32 static t_elem listhead;
33
34
35 extern t_list * list_create(void)
36 {
37 t_list * newl;
38
39 newl = (t_list*)xmalloc(sizeof(t_list));
40 newl->head = NULL;
41 newl->tail = NULL;
42 newl->len = 0;
43
44 return newl;
45 }
46
47
48 extern int list_destroy(t_list * list)
49 {
50 if (!list)
51 {
52 eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
53 return -1;
54 }
55
56 if (list->head)
57 eventlog(eventlog_level_error, __FUNCTION__, "got non-empty list");
58
59 xfree(list);
60
61 return 0;
62 }
63
64 extern unsigned int list_get_length(t_list const * list)
65 {
66 if (!list)
67 {
68 eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
69 return 0;
70 }
71
72 return list->len;
73 }
74
75
76 extern int list_prepend_data(t_list * list, void * data)
77 {
78 t_elem * elem;
79
80 assert(list != NULL);
81
82 elem = (t_elem*)xmalloc(sizeof(t_elem));
83 elem->data = data;
84
85 if (list->head)
86 list->head->prev = elem;
87 elem->next = list->head;
88 elem->prev = NULL;
89 list->head = elem;
90 if (!list->tail)
91 list->tail = elem;
92 list->len++;
93
94 return 0;
95 }
96
97
98 extern int list_append_data(t_list * list, void * data)
99 {
100 t_elem * elem;
101
102 assert(list != NULL);
103
104 elem = (t_elem*)xmalloc(sizeof(t_elem));
105 elem->data = data;
106
107 elem->next = NULL;
108 if (!list->head)
109 {
110 list->head = elem;
111 elem->prev = NULL;
112 }
113 if (list->tail)
114 {
115 elem->prev = list->tail;
116 list->tail->next = elem;
117 }
118 list->tail = elem;
119 list->len++;
120
121 return 0;
122 }
123
124
125 extern t_elem * list_get_elem_by_data(t_list const * list, void const * data)
126 {
127 t_elem * curr;
128
129 if (!list)
130 {
131 eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
132 return NULL;
133 }
134
135 LIST_TRAVERSE(list, curr)
136 if (curr->data == data)
137 return curr;
138
139 return NULL;
140 }
141
142
143 extern t_elem const * list_get_elem_by_data_const(t_list const * list, void const * data)
144 {
145 t_elem const * curr;
146
147 if (!list)
148 {
149 eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
150 return NULL;
151 }
152
153 LIST_TRAVERSE_CONST(list, curr)
154 if (curr->data == data)
155 return curr;
156
157 return NULL;
158 }
159
160
161 extern int list_remove_elem(t_list * list, t_elem ** elem)
162 {
163 t_elem * target;
164
165 if (!list)
166 {
167 eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
168 return -1;
169 }
170
171 if (!elem)
172 {
173 eventlog(eventlog_level_error, __FUNCTION__, "got NULL *elem");
174 return -1;
175 }
176
177 target = *elem;
178
179 if (target->prev)
180 {
181 target->prev->next = target->next;
182 }
183 if (target->next)
184 {
185 target->next->prev = target->prev;
186 }
187
188 if (target == list->tail)
189 {
190 list->tail = target->prev;
191 }
192 if (target == list->head)
193 {
194 list->head = target->next;
195 *elem = &listhead;
196 }
197 else
198 *elem = target->prev;
199
200 target->next = NULL;
201 target->prev = NULL;
202 xfree(target);
203
204 list->len--;
205
206 return 0;
207 }
208
209
210 extern int list_remove_data(t_list * list, void const * data, t_elem ** elem)
211 {
212 if (!list)
213 {
214 eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
215 return -1;
216 }
217
218 if (!(*elem = list_get_elem_by_data(list, data)))
219 return -1;
220
221 return list_remove_elem(list, elem);
222 }
223
224
225 extern int elem_set_data(t_elem * elem, void * data)
226 {
227 if (!elem)
228 {
229 eventlog(eventlog_level_error, __FUNCTION__, "got NULL elem");
230 return -1;
231 }
232
233 elem->data = data;
234
235 return 0;
236 }
237
238
239 extern void * elem_get_data(t_elem const * elem)
240 {
241 if (!elem)
242 {
243 eventlog(eventlog_level_error, __FUNCTION__, "got NULL elem");
244 return NULL;
245 }
246
247 return elem->data;
248 }
249
250
251 extern void * list_get_data_by_pos(t_list const * list, unsigned int pos)
252 {
253 t_elem const * curr;
254 unsigned int len;
255
256 len = 0;
257 LIST_TRAVERSE_CONST(list, curr)
258 if (len++ == pos)
259 return curr->data;
260
261 eventlog(eventlog_level_error, __FUNCTION__, "requested position {} but len={}", pos, len);
262 return NULL;
263 }
264
265
266 #ifdef LIST_DEBUG
267 extern t_elem * list_get_first_real(t_list const * list, char const * fn, unsigned int ln)
268 #else
269 extern t_elem * list_get_first(t_list const * list)
270 #endif
271 {
272 if (!list)
273 {
274 #ifdef LIST_DEBUG
275 eventlog(eventlog_level_error, __FUNCTION__, "got NULL list from {}:{}", fn, ln);
276 #else
277 eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
278 #endif
279 return NULL;
280 }
281
282
283 return list->head;
284 }
285
286
287 #ifdef LIST_DEBUG
288 extern t_elem const * list_get_first_const_real(t_list const * list, char const * fn, unsigned int ln)
289 #else
290 extern t_elem const * list_get_first_const(t_list const * list)
291 #endif
292 {
293 if (!list)
294 {
295 #ifdef LIST_DEBUG
296 eventlog(eventlog_level_error, __FUNCTION__, "got NULL list from {}:{}", fn, ln);
297 #else
298 eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
299 #endif
300 return NULL;
301 }
302
303 return list->head;
304 }
305
306
307 extern t_elem * elem_get_next_real(t_list const * list, t_elem const * elem, char const * fn, unsigned int ln)
308 {
309 if (!elem)
310 {
311 eventlog(eventlog_level_error, __FUNCTION__, "got NULL elem from {}:{}", fn, ln);
312 return NULL;
313 }
314
315 if (elem == &listhead)
316 return list->head;
317 else
318 return elem->next;
319 }
320
321
322 extern t_elem const * elem_get_next_const(t_list const * list, t_elem const * elem)
323 {
324 if (!elem)
325 {
326 eventlog(eventlog_level_error, __FUNCTION__, "got NULL elem");
327 return NULL;
328 }
329
330 if (elem == &listhead)
331 return list->head;
332 else
333 return elem->next;
334 }
335
336 }