b4d068543c27491c937fbf9cd4a5701e19dca7b0
[dockapps.git] / wmbiff / wmgeneral / list.c
blobb4d068543c27491c937fbf9cd4a5701e19dca7b0
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)
15 any later version.
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. */
33 #include "list.h"
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37 #include <stdlib.h>
39 /* Return a cons cell produced from (head . tail) */
41 INLINE LinkedList *list_cons(void *head, LinkedList * tail)
43 LinkedList *cell;
45 cell = (LinkedList *) malloc(sizeof(LinkedList));
46 cell->head = head;
47 cell->tail = tail;
48 return cell;
51 /* Return the length of a list, list_length(NULL) returns zero */
53 INLINE int list_length(LinkedList * list)
55 int i = 0;
56 while (list) {
57 i += 1;
58 list = list->tail;
60 return i;
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)
68 while (idx-- != 0) {
69 if (list->tail)
70 list = list->tail;
71 else
72 return 0;
74 return list->head;
77 /* Remove the element at the head by replacing it by its successor */
79 INLINE void list_remove_head(LinkedList ** list)
81 if (!*list)
82 return;
83 if ((*list)->tail) {
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 */
89 free(*list);
90 (*list) = 0;
95 /* Remove the element with `car' set to ELEMENT */
97 INLINE void
98 list_remove_elem(LinkedList** list, void* elem)
100 while (*list)
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)
110 LinkedList *tmp;
112 if (list) {
113 if (list->head == elem) {
114 tmp = list->tail;
115 free(list);
116 return tmp;
118 list->tail = list_remove_elem(list->tail, elem);
119 return list;
121 return NULL;
125 /* Return element that has ELEM as car */
127 INLINE LinkedList *list_find(LinkedList * list, void *elem)
129 while (list) {
130 if (list->head == elem)
131 return list;
132 list = list->tail;
134 return NULL;
137 /* Free list (backwards recursive) */
139 INLINE void list_free(LinkedList * list)
141 if (list) {
142 list_free(list->tail);
143 free(list);
147 /* Map FUNCTION over all elements in LIST */
149 INLINE void list_mapcar(LinkedList * list, void (*function) (void *))
151 while (list) {
152 (*function) (list->head);
153 list = list->tail;