home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / gcc / objc / list.h < prev    next >
C/C++ Source or Header  |  1995-06-15  |  4KB  |  151 lines

  1. /* Generic single linked list to keep various information 
  2.    Copyright (C) 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. Author: Kresten Krab Thorup
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU CC; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 59 Temple Place - Suite 330,
  21. Boston, 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
  31. void * __objc_xrealloc (void *optr, size_t size);
  32. void * __objc_xmalloc (size_t size);
  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_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.  
  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 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.  
  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.       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.  
  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 void
  142. list_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
  151.