home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / new / misc / sci / splines / dlist.c < prev    next >
C/C++ Source or Header  |  1994-09-16  |  4KB  |  158 lines

  1. /* The routines in this file are copyright (c) 1987 by Helene (Lee) Taran.
  2.  * Permission is granted for use and free distribution as long as the
  3.  * original author's name is included with the code.
  4.  */
  5.  
  6. /*
  7.  * Reformatted, and modified to compile without warnings and errors
  8.  * under SAS/C -6.5x by gduncan@philips.oz.au (GMD). This included
  9.  * proto generation, renaming of some literals to avoid name collisions,
  10.  * and Amiga Version string (also displayed in Title bar).
  11.  * No original version number, this version arbitrarily named 1.1. 
  12.  * - otherwise no functional changes.
  13.  * GMD - Sep 94
  14.  */
  15.  
  16. #include "all.h"
  17.  
  18. void
  19. Init_List (list)
  20.      DLISTPTR list;
  21. {
  22.   list->next = list;
  23.   list->prev = list;
  24.   list->contents = calloc (1, sizeof (int));
  25.   *((int *) (list->contents)) = 0;
  26. }
  27.  
  28.  
  29. /* Inserts <element> before <position> in <list> */
  30.  
  31. void
  32. Insert_Before (position, element, list)
  33.      DLISTPTR position, element, list;
  34. {
  35.   *((int *) (list->contents)) = LENGTH (list) + 1;
  36.   element->next = position;
  37.   element->prev = position->prev;
  38.   position->prev->next = element;
  39.   position->prev = element;
  40. }
  41.  
  42. /* Inserts <element> after <position> in <list> */
  43.  
  44. void
  45. Insert_After (position, element, list)
  46.      DLISTPTR position, element, list;
  47. {
  48.   *((int *) (list->contents)) = LENGTH (list) + 1;
  49.   element->prev = position;
  50.   element->next = position->next;
  51.   position->next->prev = element;
  52.   position->next = element;
  53. }
  54.  
  55. /* Removes <element> from <list>. Assumes the <element> is currently
  56.  * a member of <list>. Does not deallocate <element>
  57.  */
  58.  
  59. DLISTPTR
  60. Remove_Element (element, list)
  61.      DLISTPTR element, list;
  62. {
  63.   if (ISEMPTY (list))
  64.     return (list);
  65.   *((int *) (list->contents)) = LENGTH (list) - 1;
  66.   element->prev->next = element->next;
  67.   element->next->prev = element->prev;
  68.   element->next = NULL;
  69.   element->prev = NULL;
  70.   return (element);
  71. }
  72.  
  73.  
  74. /* Member: Returns TRUE if element is a member of the list 
  75.  */
  76.  
  77. BOOL
  78. Member (element, list)
  79.      DLISTPTR element, list;
  80. {
  81.   DLISTPTR tmp = list;
  82.   while ((tmp = tmp->next) != list)
  83.     if (tmp == element)
  84.       return (TRUE);
  85.   return (FALSE);
  86. }
  87.  
  88. /* Get_Member : returns the member of the list that contains <element>
  89.  * returns NULL if not a member of the list.
  90.  */
  91. DLISTPTR
  92. GET_MEMBER (element, list)
  93.      void *element;
  94.      DLISTPTR list;
  95. {
  96.   DLISTPTR tmp = list;
  97.   while ((tmp = tmp->next) != list)
  98.     if (tmp->contents == element)
  99.       return (tmp);
  100.   return (FALSE);
  101. }
  102.  
  103. /* Find_Element: returns the member of the list that contains some element
  104.  * that satisfies the given test with respect to <element>.  For example:
  105.  * you can define a function equal that returns true if two elements have
  106.  * the exact same fields. 
  107.  * Returns FALSE if no member of the list satifies the test. 
  108.  */
  109.  
  110. DLISTPTR
  111. Find_Element (element, list, test)
  112.      void *element;
  113.      DLISTPTR list;
  114.      int (*test) (void *, void *);
  115. {
  116.   DLISTPTR tmp = list;
  117.   while ((tmp = tmp->next) != list)
  118.     if ((*test) (tmp->contents, element))
  119.       return (tmp);
  120.   return (FALSE);
  121. }
  122.  
  123. /* Apply : applies the given function to the contents of each list
  124.  * element.
  125.  */
  126.  
  127. void
  128. Apply (fn, list)
  129.      int (*fn) (void *);
  130.      DLISTPTR list;
  131. {
  132.   DLISTPTR tmp = list;
  133.   while ((tmp = tmp->next) != list)
  134.     (*fn) (tmp->contents);
  135.  
  136. }
  137.  
  138.  
  139. /* Removes and disposes of all elements in a list --- does not deallocate
  140.  * the head of the list. if <foo> is non-zero then deallocate the contents
  141.  * of each element.
  142.  */
  143.  
  144. void
  145. ClearList (list, foo)
  146.      DLISTPTR list;
  147.      int foo;
  148. {
  149.   DLISTPTR doomed;
  150.   while ((doomed = Remove_Element (FIRST (list), list)) != list)
  151.     {
  152.       if (foo)
  153.     free (doomed->contents);
  154.       free (doomed);
  155.     }
  156. }
  157. /*--*/
  158.