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
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING.  If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, 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
31struct objc_list {
32  void *head;
33  struct objc_list *tail;
34};
35
36/* Return a cons cell produced from (head . tail) */
37
38static inline struct objc_list*
39list_cons(void* head, struct objc_list* tail)
40{
41  struct objc_list* cell;
42
43  cell = (struct objc_list*)objc_malloc(sizeof(struct objc_list));
44  cell->head = head;
45  cell->tail = tail;
46  return cell;
47}
48
49/* Return the length of a list, list_length(NULL) returns zero */
50
51static inline int
52list_length(struct objc_list* list)
53{
54  int i = 0;
55  while(list)
56    {
57      i += 1;
58      list = list->tail;
59    }
60  return i;
61}
62
63/* Return the Nth element of LIST, where N count from zero.  If N
64   larger than the list length, NULL is returned  */
65
66static inline void*
67list_nth(int index, struct objc_list* list)
68{
69  while(index-- != 0)
70    {
71      if(list->tail)
72	list = list->tail;
73      else
74	return 0;
75    }
76  return list->head;
77}
78
79/* Remove the element at the head by replacing it by its successor */
80
81static inline void
82list_remove_head(struct objc_list** list)
83{
84  if ((*list)->tail)
85    {
86      struct objc_list* tail = (*list)->tail; /* fetch next */
87      *(*list) = *tail;		/* copy next to list head */
88      objc_free(tail);			/* free next */
89    }
90  else				/* only one element in list */
91    {
92      objc_free(*list);
93      (*list) = 0;
94    }
95}
96
97
98/* Remove the element with `car' set to ELEMENT */
99
100static inline void
101list_remove_elem(struct objc_list** list, void* elem)
102{
103  while (*list) {
104    if ((*list)->head == elem)
105      list_remove_head(list);
106    list = &((*list)->tail);
107  }
108}
109
110/* Map FUNCTION over all elements in LIST */
111
112static inline void
113list_mapcar(struct objc_list* list, void(*function)(void*))
114{
115  while(list)
116    {
117      (*function)(list->head);
118      list = list->tail;
119    }
120}
121
122/* Return element that has ELEM as car */
123
124static inline struct objc_list**
125list_find(struct objc_list** list, void* elem)
126{
127  while(*list)
128    {
129    if ((*list)->head == elem)
130      return list;
131    list = &((*list)->tail);
132    }
133  return NULL;
134}
135
136/* Free list (backwards recursive) */
137
138static void
139list_free(struct objc_list* list)
140{
141  if(list)
142    {
143      list_free(list->tail);
144      objc_free(list);
145    }
146}
147#endif /* not __GNU_OBJC_LIST_H */
148