OSDN Git Service

e542bfc0498fe0a25ac9fbb5c5feb8bfbf01d2a9
[pf3gnuchains/gcc-fork.git] / libobjc / objc / objc-list.h
1 /* Generic single linked list to keep various information 
2    Copyright (C) 1993, 1994, 1996, 2009 Free Software Foundation, Inc.
3    Contributed by Kresten Krab Thorup.
4
5 This file is part of GCC.
6
7 GCC 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 3, or (at your option)
10 any later version.
11
12 GCC 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 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 <http://www.gnu.org/licenses/>.  */
25
26
27 #ifndef __GNU_OBJC_LIST_H
28 #define __GNU_OBJC_LIST_H
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif /* __cplusplus */
33
34 struct objc_list {
35   void *head;
36   struct objc_list *tail;
37 };
38
39 /* Return a cons cell produced from (head . tail) */
40
41 static inline struct objc_list* 
42 list_cons(void* head, struct objc_list* tail)
43 {
44   struct objc_list* cell;
45
46   cell = (struct objc_list*)objc_malloc(sizeof(struct objc_list));
47   cell->head = head;
48   cell->tail = tail;
49   return cell;
50 }
51
52 /* Return the length of a list, list_length(NULL) returns zero */
53
54 static inline int
55 list_length(struct objc_list* list)
56 {
57   int i = 0;
58   while(list)
59     {
60       i += 1;
61       list = list->tail;
62     }
63   return i;
64 }
65
66 /* Return the Nth element of LIST, where N count from zero.  If N 
67    larger than the list length, NULL is returned  */
68
69 static inline void*
70 list_nth(int indx, struct objc_list* list)
71 {
72   while(indx-- != 0)
73     {
74       if(list->tail)
75         list = list->tail;
76       else
77         return 0;
78     }
79   return list->head;
80 }
81
82 /* Remove the element at the head by replacing it by its successor */
83
84 static inline void
85 list_remove_head(struct objc_list** list)
86 {
87   if ((*list)->tail)
88     {
89       struct objc_list* tail = (*list)->tail; /* fetch next */
90       *(*list) = *tail;         /* copy next to list head */
91       objc_free(tail);                  /* free next */
92     }
93   else                          /* only one element in list */
94     {
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     {
120       (*function)(list->head);
121       list = list->tail;
122     }
123 }
124
125 /* Return element that has ELEM as car */
126
127 static inline struct objc_list**
128 list_find(struct objc_list** list, void* elem)
129 {
130   while(*list)
131     {
132     if ((*list)->head == elem)
133       return list;
134     list = &((*list)->tail);
135     }
136   return NULL;
137 }
138
139 /* Free list (backwards recursive) */
140
141 static inline void
142 list_free(struct objc_list* list)
143 {
144   if(list)
145     {
146       list_free(list->tail);
147       objc_free(list);
148     }
149 }
150
151 #ifdef __cplusplus
152 }
153 #endif /* __cplusplus */
154
155 #endif /* not __GNU_OBJC_LIST_H */