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, 51 Franklin Street, Fifth Floor,
25 Boston, MA 02110-1301 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. */
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
39 /* Return a cons cell produced from (head . tail) */
41 INLINE LinkedList
*list_cons(void *head
, LinkedList
* tail
)
45 cell
= (LinkedList
*) malloc(sizeof(LinkedList
));
51 /* Return the length of a list, list_length(NULL) returns zero */
53 INLINE
int list_length(LinkedList
* list
)
63 /* Return the Nth element of LIST, where N count from zero. If N
64 larger than the list length, NULL is returned */
66 INLINE
void *list_nth(int idx
, LinkedList
* list
)
77 /* Remove the element at the head by replacing it by its successor */
79 INLINE
void list_remove_head(LinkedList
** list
)
84 LinkedList
*tail
= (*list
)->tail
; /* fetch next */
85 *(*list
) = *tail
; /* copy next to list head */
86 free(tail
); /* free next */
87 } else { /* only one element in list */
95 /* Remove the element with `car' set to ELEMENT */
98 list_remove_elem(LinkedList** list, void* elem)
102 if ((*list)->head == elem)
103 list_remove_head(list);
104 *list = (*list ? (*list)->tail : NULL);
108 INLINE LinkedList
*list_remove_elem(LinkedList
* list
, void *elem
)
113 if (list
->head
== elem
) {
118 list
->tail
= list_remove_elem(list
->tail
, elem
);
125 /* Return element that has ELEM as car */
127 INLINE LinkedList
*list_find(LinkedList
* list
, void *elem
)
130 if (list
->head
== elem
)
137 /* Free list (backwards recursive) */
139 INLINE
void list_free(LinkedList
* list
)
142 list_free(list
->tail
);
147 /* Map FUNCTION over all elements in LIST */
149 INLINE
void list_mapcar(LinkedList
* list
, void (*function
) (void *))
152 (*function
) (list
->head
);