1 /* Generic single linked list to keep various information
2 Copyright (C) 1993, 1994 Free Software Foundation, Inc.
5 Author: Kresten Krab Thorup
7 Many modifications by Alfredo K. Kojima
8 Some fixes and additions by Dan Pascu
11 This file is part of GNU CC.
13 GNU CC is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
18 GNU CC is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with GNU CC; see the file COPYING. If not, write to
25 the Free Software Foundation, 59 Temple Place - Suite 330,
26 Boston, MA 02111-1307, USA. */
28 /* As a special exception, if you link this library with files compiled with
29 GCC to produce an executable, this does not cause the resulting executable
30 to be covered by the GNU General Public License. This exception does not
31 however invalidate any other reasons why the executable file might be
32 covered by the GNU General Public License. */
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
41 #define xrealloc(ptr, size) wrealloc(ptr, size)
43 /* Return a cons cell produced from (head . tail) */
46 list_cons(void* head
, LinkedList
* tail
)
50 cell
= (LinkedList
*)wmalloc(sizeof(LinkedList
));
56 /* Inserts "item" in list using "compare" function to make a sorted list */
59 list_insert_sorted(void* item
, LinkedList
** list
, int (*compare
)(void*, void*))
61 LinkedList
*tmp
, *cell
;
63 cell
= (LinkedList
*)wmalloc(sizeof(LinkedList
));
65 if ((*list
== NULL
) || ((*compare
)((*list
)->head
, item
) >= 0)) {
71 if ((*compare
)(tmp
->tail
->head
, item
) >= 0)
75 cell
->tail
= tmp
->tail
;
80 /* Return the length of a list, list_length(NULL) returns zero */
83 list_length(LinkedList
* list
)
94 /* Return the Nth element of LIST, where N count from zero. If N
95 larger than the list length, NULL is returned */
98 list_nth(int index
, LinkedList
* list
)
110 /* Remove the element at the head by replacing it by its successor */
114 list_remove_head(LinkedList
** list
)
119 LinkedList
* tail
= (*list
)->tail
; /* fetch next */
120 *(*list
) = *tail
; /* copy next to list head */
121 free(tail
); /* free next */
123 else /* only one element in list */
130 /* Alfredo, is there any reason for the function to be as above,
131 * instead of the more simple version below?
132 * The difference is that above function do a copying of the structure
133 * unlike the below that only changes where list points.
134 * This makes the above function twice slower, because it copies the
135 * whole structure, which contains 2 pointers. -Dan.
137 * I don't know. I just copied this file from the gcc distribution
138 * (I liked the API). One could think that the above function would
139 * keep the pointer to the list unchanged, while the below would change
140 * it, but since the list is passed by reference that doesn't make any
146 list_remove_head(LinkedList
** list
)
151 LinkedList
* next
= (*list
)->tail
; /* fetch next */
152 free(*list
); /* free head */
153 *list
= next
; /* update list pointer */
159 /* Remove the element with `car' set to ELEMENT */
162 list_remove_elem(LinkedList** list, void* elem)
166 if ((*list)->head == elem)
167 list_remove_head(list);
168 *list = (*list ? (*list)->tail : NULL);
173 list_remove_elem(LinkedList
* list
, void* elem
)
178 if (list
->head
== elem
) {
183 list
->tail
= list_remove_elem(list
->tail
, elem
);
190 /* Return element that has ELEM as car */
193 list_find(LinkedList
* list
, void* elem
)
197 if (list
->head
== elem
)
204 /* Free list (backwards recursive) */
207 list_free(LinkedList
* list
)
211 list_free(list
->tail
);
216 /* Map FUNCTION over all elements in LIST */
219 list_mapcar(LinkedList
* list
, void(*function
)(void*))
223 (*function
)(list
->head
);