source: trunk/third/gcc/objc/list.h @ 8834

Revision 8834, 3.4 KB checked in by ghudson, 28 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r8833, which included commits to RCS files with non-trunk default branches.
Line 
1/* Generic single linked list to keep various information
2   Copyright (C) 1993, 1994 Free Software Foundation, Inc.
3
4Author: Kresten Krab Thorup
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING.  If not, write to
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA.  */
22
23/* As a special exception, if you link this library with files compiled with
24   GCC to produce an executable, this does not cause the resulting executable
25   to be covered by the GNU General Public License. This exception does not
26   however invalidate any other reasons why the executable file might be
27   covered by the GNU General Public License.  */
28
29#ifndef __GNU_OBJC_LIST_H
30#define __GNU_OBJC_LIST_H
31void * __objc_xrealloc (void *optr, size_t size);
32void * __objc_xmalloc (size_t size);
33
34struct objc_list {
35  void *head;
36  struct objc_list *tail;
37};
38
39/* Return a cons cell produced from (head . tail) */
40
41static inline struct objc_list*
42list_cons(void* head, struct objc_list* tail)
43{
44  struct objc_list* cell;
45
46  cell = (struct objc_list*)__objc_xmalloc(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
54static inline int
55list_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
69static inline void*
70list_nth(int index, struct objc_list* list)
71{
72  while(index-- != 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
84static inline void
85list_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      free(tail);                       /* free next */
92    }
93  else                          /* only one element in list */
94    {
95      free (*list);
96      (*list) = 0;
97    }
98}
99
100
101/* Remove the element with `car' set to ELEMENT */
102
103static inline void
104list_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
115static inline void
116list_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
127static inline struct objc_list**
128list_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
141static void
142list_free(struct objc_list* list)
143{
144  if(list)
145    {
146      list_free(list->tail);
147      free(list);
148    }
149}
150#endif __GNU_OBJC_LIST_H
Note: See TracBrowser for help on using the repository browser.