replaced linked lists with WMBag, added progress indicator
[wmaker-crm.git] / src / list.c
blob745ae141870bbd05ee63cd680240eeb47d6f974b
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)
16 any later version.
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. */
34 #include <WUtil.h>
35 #include "list.h"
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
38 #endif
39 #include <stdlib.h>
41 #define xrealloc(ptr, size) wrealloc(ptr, size)
43 /* Return a cons cell produced from (head . tail) */
45 INLINE LinkedList*
46 list_cons(void* head, LinkedList* tail)
48 LinkedList* cell;
50 cell = (LinkedList*)wmalloc(sizeof(LinkedList));
51 cell->head = head;
52 cell->tail = tail;
53 return cell;
56 /* Inserts "item" in list using "compare" function to make a sorted list */
58 INLINE void
59 list_insert_sorted(void* item, LinkedList** list, int (*compare)(void*, void*))
61 LinkedList *tmp, *cell;
63 cell = (LinkedList*)wmalloc(sizeof(LinkedList));
64 cell->head = item;
65 if ((*list == NULL) || ((*compare)((*list)->head, item) >= 0)) {
66 cell->tail = *list;
67 *list = cell;
68 } else {
69 tmp = *list;
70 while (tmp->tail) {
71 if ((*compare)(tmp->tail->head, item) >= 0)
72 break;
73 tmp = tmp->tail;
75 cell->tail = tmp->tail;
76 tmp->tail = cell;
80 /* Return the length of a list, list_length(NULL) returns zero */
82 INLINE int
83 list_length(LinkedList* list)
85 int i = 0;
86 while(list)
88 i += 1;
89 list = list->tail;
91 return i;
94 /* Return the Nth element of LIST, where N count from zero. If N
95 larger than the list length, NULL is returned */
97 INLINE void*
98 list_nth(int index, LinkedList* list)
100 while(index-- != 0)
102 if(list->tail)
103 list = list->tail;
104 else
105 return 0;
107 return list->head;
110 /* Remove the element at the head by replacing it by its successor */
112 #if 0
113 INLINE void
114 list_remove_head(LinkedList** list)
116 if (!*list) return;
117 if ((*list)->tail)
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 */
125 free(*list);
126 (*list) = 0;
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
141 * sense. -Alfredo
144 #else
145 INLINE void
146 list_remove_head(LinkedList** list)
148 if (!*list) return;
151 LinkedList* next = (*list)->tail; /* fetch next */
152 free(*list); /* free head */
153 *list = next; /* update list pointer */
156 #endif
159 /* Remove the element with `car' set to ELEMENT */
161 INLINE void
162 list_remove_elem(LinkedList** list, void* elem)
164 while (*list)
166 if ((*list)->head == elem)
167 list_remove_head(list);
168 *list = (*list ? (*list)->tail : NULL);
172 INLINE LinkedList *
173 list_remove_elem(LinkedList* list, void* elem)
175 LinkedList *tmp;
177 if (list) {
178 if (list->head == elem) {
179 tmp = list->tail;
180 free(list);
181 return tmp;
183 list->tail = list_remove_elem(list->tail, elem);
184 return list;
186 return NULL;
190 /* Return element that has ELEM as car */
192 INLINE LinkedList*
193 list_find(LinkedList* list, void* elem)
195 while(list)
197 if (list->head == elem)
198 return list;
199 list = list->tail;
201 return NULL;
204 /* Free list (backwards recursive) */
206 INLINE void
207 list_free(LinkedList* list)
209 if(list)
211 list_free(list->tail);
212 free(list);
216 /* Map FUNCTION over all elements in LIST */
218 INLINE void
219 list_mapcar(LinkedList* list, void(*function)(void*))
221 while(list)
223 (*function)(list->head);
224 list = list->tail;