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
10 This file is part of GNU CC.
12 GNU CC is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
17 GNU CC is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GNU CC; see the file COPYING. If not, write to
24 the Free Software Foundation, 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA. */
27 /* As a special exception, if you link this library with files compiled with
28 GCC to produce an executable, this does not cause the resulting executable
29 to be covered by the GNU General Public License. This exception does not
30 however invalidate any other reasons why the executable file might be
31 covered by the GNU General Public License. */
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
40 #define xrealloc(ptr, size) wrealloc(ptr, size)
42 /* Return a cons cell produced from (head . tail) */
45 list_cons(void* head
, LinkedList
* tail
)
49 cell
= (LinkedList
*)wmalloc(sizeof(LinkedList
));
55 /* Inserts "item" in list using "compare" function to make a sorted list */
58 list_insert_sorted(void* item
, LinkedList
** list
, int (*compare
)(void*, void*))
60 LinkedList
*tmp
, *cell
;
62 cell
= (LinkedList
*)wmalloc(sizeof(LinkedList
));
64 if ((*list
== NULL
) || ((*compare
)((*list
)->head
, item
) >= 0)) {
70 if ((*compare
)(tmp
->tail
->head
, item
) >= 0)
74 cell
->tail
= tmp
->tail
;
79 /* Return the length of a list, list_length(NULL) returns zero */
82 list_length(LinkedList
* list
)
93 /* Return the Nth element of LIST, where N count from zero. If N
94 larger than the list length, NULL is returned */
97 list_nth(int index
, LinkedList
* list
)
109 /* Remove the element at the head by replacing it by its successor */
113 list_remove_head(LinkedList
** list
)
118 LinkedList
* tail
= (*list
)->tail
; /* fetch next */
119 *(*list
) = *tail
; /* copy next to list head */
120 free(tail
); /* free next */
122 else /* only one element in list */
129 /* Alfredo, is there any reason for the function to be as above,
130 * instead of the more simple version below?
131 * The difference is that above function do a copying of the structure
132 * unlike the below that only changes where list points.
133 * This makes the above function twice slower, because it copies the
134 * whole structure, which contains 2 pointers. -Dan.
136 * I don't know. I just copied this file from the gcc distribution
137 * (I liked the API). One could think that the above function would
138 * keep the pointer to the list unchanged, while the below would change
139 * it, but since the list is passed by reference that doesn't make any
145 list_remove_head(LinkedList
** list
)
150 LinkedList
* next
= (*list
)->tail
; /* fetch next */
151 free(*list
); /* free head */
152 *list
= next
; /* update list pointer */
158 /* Remove the element with `car' set to ELEMENT */
161 list_remove_elem(LinkedList** list, void* elem)
165 if ((*list)->head == elem)
166 list_remove_head(list);
167 *list = (*list ? (*list)->tail : NULL);
172 list_remove_elem(LinkedList
* list
, void* elem
)
177 if (list
->head
== elem
) {
182 list
->tail
= list_remove_elem(list
->tail
, elem
);
189 /* Return element that has ELEM as car */
192 list_find(LinkedList
* list
, void* elem
)
196 if (list
->head
== elem
)
203 /* Free list (backwards recursive) */
206 list_free(LinkedList
* list
)
210 list_free(list
->tail
);
215 /* Map FUNCTION over all elements in LIST */
218 list_mapcar(LinkedList
* list
, void(*function
)(void*))
222 (*function
)(list
->head
);