objc-list.h
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:3k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

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