home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / gnu / lib / gcc-lib / amigados / 2.5.8 / include / objc / list.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  3.4 KB  |  152 lines

  1. #ifdef __cplusplus
  2. extern "C" {
  3. #endif
  4. /* Generic single linked list to keep various information 
  5.    Copyright (C) 1993 Free Software Foundation, Inc.
  6.  
  7. Author: Kresten Krab Thorup
  8.  
  9. This file is part of GNU CC.
  10.  
  11. GNU CC is free software; you can redistribute it and/or modify it under the
  12.    terms of the GNU General Public License as published by the Free Software
  13.    Foundation; either version 2, or (at your option) any later version.
  14.  
  15. GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
  16.    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17.    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  18.    details.
  19.  
  20. You should have received a copy of the GNU General Public License along with
  21.    GNU CC; see the file COPYING.  If not, write to the Free Software
  22.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  23.  
  24. /* As a special exception, if you link this library with files compiled with
  25.    GCC to produce an executable, this does not cause the resulting executable
  26.    to be covered by the GNU General Public License. This exception does not
  27.    however invalidate any other reasons why the executable file might be
  28.    covered by the GNU General Public License.  */
  29.  
  30. void * __objc_xrealloc (void *optr, size_t size);
  31. void * __objc_xmalloc (size_t size);
  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.   cell = (struct objc_list*)__objc_xmalloc(sizeof(struct objc_list));
  46.   cell->head = head;
  47.   cell->tail = tail;
  48.   return cell;
  49. }
  50.  
  51. /* Return the length of a list, list_length(NULL) returns zero */
  52.  
  53. static inline int
  54. list_length(struct objc_list* list)
  55. {
  56.   int i = 0;
  57.   while(list)
  58.     {
  59.       i += 1;
  60.       list = list->tail;
  61.     }
  62.   return i;
  63. }
  64.  
  65. /* Return the Nth element of LIST, where N count from zero.  If N 
  66.    larger than the list length, NULL is returned  */
  67.  
  68. static inline void*
  69. list_nth(int index, struct objc_list* list)
  70. {
  71.   while(index-- != 0)
  72.     {
  73.       if(list->tail)
  74.     list = list->tail;
  75.       else
  76.     return 0;
  77.     }
  78.   return list->head;
  79. }
  80.  
  81. /* Remove the element at the head by replacing it by its successor */
  82.  
  83. static inline void
  84. list_remove_head(struct objc_list** list)
  85. {
  86.   if ((*list)->tail)
  87.     {
  88.       struct objc_list* tail = (*list)->tail; /* fetch next */
  89.       *(*list) = *tail;        /* copy next to list head */
  90.       free(tail);            /* free next */
  91.     }
  92.   else                /* only one element in list */
  93.     {
  94.       free (*list);
  95.       (*list) = 0;
  96.     }
  97. }
  98.  
  99.  
  100. /* Remove the element with `car' set to ELEMENT */
  101.  
  102. static inline void
  103. list_remove_elem(struct objc_list** list, void* elem)
  104. {
  105.   while (*list) {
  106.     if ((*list)->head == elem)
  107.       list_remove_head(list);
  108.     list = &((*list)->tail);
  109.   }
  110. }
  111.  
  112. /* Map FUNCTION over all elements in LIST */
  113.  
  114. static inline void
  115. list_mapcar(struct objc_list* list, void(*function)(void*))
  116. {
  117.   while(list)
  118.     {
  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.     {
  131.     if ((*list)->head == elem)
  132.       return list;
  133.     list = &((*list)->tail);
  134.     }
  135.   return NULL;
  136. }
  137.  
  138. /* Free list (backwards recursive) */
  139.  
  140. static void
  141. list_free(struct objc_list* list)
  142. {
  143.   if(list)
  144.     {
  145.       list_free(list->tail);
  146.       free(list);
  147.     }
  148. }
  149. #ifdef __cplusplus
  150. }
  151. #endif
  152.