]> err.no Git - sope/blob - gnustep-objc/objc-list.h
Added WEPrototype and bumped framework versions. WEPrototype hasn't been tested very...
[sope] / gnustep-objc / objc-list.h
1 /* Generic single linked list to keep various information 
2    Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc.
3    Contributed by Kresten Krab Thorup.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* As a special exception, if you link this library with files compiled with
23    GCC to produce an executable, this does not cause the resulting executable
24    to be covered by the GNU General Public License. This exception does not
25    however invalidate any other reasons why the executable file might be
26    covered by the GNU General Public License.  */
27
28 #ifndef __GNU_OBJC_LIST_H
29 #define __GNU_OBJC_LIST_H
30
31 #include <objc/objc-mem.h>
32
33 struct objc_list {
34   void *head;
35   struct objc_list *tail;
36 };
37
38 /* Return a cons cell produced from (head . tail) */
39
40 static __inline__ struct objc_list* 
41 list_cons(void* head, struct objc_list* tail)
42 {
43   struct objc_list* cell;
44
45 #if OBJC_WITH_GC
46   cell = GC_MALLOC_UNCOLLECTABLE(sizeof(struct objc_list));
47 #else
48   cell = malloc(sizeof(struct objc_list));
49 #endif
50   cell->head = head;
51   cell->tail = tail;
52   return cell;
53 }
54
55 /* Return the length of a list, list_length(NULL) returns zero */
56
57 static __inline__ int
58 list_length(struct objc_list* list)
59 {
60   int i = 0;
61   while(list)
62     {
63       i += 1;
64       list = list->tail;
65     }
66   return i;
67 }
68
69 /* Return the Nth element of LIST, where N count from zero.  If N 
70    larger than the list length, NULL is returned  */
71
72 static __inline__ void*
73 list_nth(int index, struct objc_list* list)
74 {
75   while(index-- != 0) {
76     if(list->tail)
77       list = list->tail;
78     else
79       return 0;
80   }
81   return list->head;
82 }
83
84 /* Remove the element at the head by replacing it by its successor */
85
86 static __inline__ void
87 list_remove_head(struct objc_list** list)
88 {
89   if ((*list)->tail) {
90     struct objc_list* tail = (*list)->tail; /* fetch next */
91     *(*list) = *tail;                       /* copy next to list head */
92     objc_free(tail);                        /* free next */
93   }
94   else {                                /* only one element in list */
95     objc_free(*list);
96     (*list) = 0;
97   }
98 }
99
100
101 /* Remove the element with `car' set to ELEMENT */
102
103 static __inline__ void
104 list_remove_elem(struct objc_list** list, void* elem)
105 {
106   while (*list) {
107     if ((*list)->head == elem)
108       list_remove_head(list);
109     list = &((*list)->tail);
110   }
111 }
112
113 /* Map FUNCTION over all elements in LIST */
114
115 static __inline__ void
116 list_mapcar(struct objc_list* list, void(*function)(void*))
117 {
118   while (list) {
119     (*function)(list->head);
120     list = list->tail;
121   }
122 }
123
124 /* Return element that has ELEM as car */
125
126 static __inline__ struct objc_list**
127 list_find(struct objc_list** list, void* elem)
128 {
129   while (*list) {
130     if ((*list)->head == elem)
131       return list;
132     list = &((*list)->tail);
133   }
134   return NULL;
135 }
136
137 /* Free list (backwards recursive) */
138
139 static void list_free(struct objc_list *list)
140 {
141   if (list) {
142     list_free(list->tail);
143     objc_free(list);
144   }
145 }
146 #endif /* __GNU_OBJC_LIST_H */