home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / Generic List Library / generic_list.c next >
Encoding:
Text File  |  1994-09-19  |  14.4 KB  |  75 lines  |  [TEXT/R*ch]

  1.    /************************************************************************
  2.    *************************************************************************
  3.    **                                                                     **
  4.    **                        Generic List Library                         **
  5.    **                                                                     **
  6.    **                          by Keith Pomakis                           **
  7.    **                     kppomaki@jeeves.uwaterlo            !(*fn)(element->pointer, args)) {
  8.         element = element->previous;
  9.     }
  10.  
  11.     if (element->pointer)
  12.         list.info->current = element;
  13.  
  14.     return element->pointer;
  15. }
  16.  
  17. /****************************************************************************/
  18.  
  19. Generic_list
  20. all_such_that(Generic_list list,
  21.               int (*fn)(const void *pointer, const void *args),
  22.               const void *args)
  23. {
  24.     Generic_list list_copy;
  25.     Generic_list_element *element;
  26.  
  27.     initialize_sorted_list(&list_copy, list.info->lt);
  28.     element = list.info->pre_element.next;
  29.     while (element != &list.info->post_element) {
  30.         if ((*fn)(element->pointer, args))
  31.             add_to_end(list_copy, element->pointer);
  32.         element = element->next;
  33.     }
  34.     
  35.     return list_copy;
  36. }
  37.  
  38. /****************************************************************************/
  39.  
  40. void
  41. remove_all_such_that(Generic_list list,
  42.                      int (*fn)(const void *pointer, const void *args),
  43.                      const void *args)
  44. {
  45.     void *obj;
  46.  
  47.     reset_to_beginning(list);
  48.     while (obj = next_in_list(list))
  49.         if ((*fn)(obj, args))
  50.             remove_current(list);
  51. }
  52.  
  53.  
  54.  
  55. /****************************************************************************/
  56. /****************************************************************************/
  57. /**                                                                        **/
  58. /**                         Internal functions                             **/
  59. /**                                                                        **/
  60. /****************************************************************************/
  61. /****************************************************************************/
  62.  
  63. static void *
  64. emalloc(unsigned int n)
  65. {
  66.     void *ptr;
  67.  
  68.     ptr = (void *) malloc(n);
  69.     if ( ptr == NULL ) {
  70.         fprintf(stderr,"%s: error allocating memory\n", module);
  71.         exit(EXIT_FAILURE);
  72.     }
  73.     return ptr;
  74. }
  75.