home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / alt / sources / 2587 / List.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-20  |  4.5 KB  |  228 lines

  1. /*
  2.  * This is a semi-generic List class
  3.  * This was written for FolkScan v2.2
  4.  * Copyright (C) 1992 Cory West
  5.  * corywest@rice.edu ; List.cc ; Fri Nov 13 17:34:46 CST 1992
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 1, or (at your option)
  10.  * any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21.  
  22. #include "List.h"
  23.  
  24. void List::dump(void)
  25. {
  26.   ListLink* current = head;
  27.   head = NULL;
  28.   ListLink* old = NULL;
  29.  
  30.   // Clean up all of our garbage
  31.   while (current != NULL)
  32.     {
  33.       old = current;
  34.       current = current->next;
  35.       delete old;
  36.     }
  37.   length = 0;
  38. }
  39.  
  40. void List::AddElement(String newMember)
  41. {
  42.   ListLink* current = head;
  43.   
  44.   if (current == NULL)
  45.     // The special case of the new list
  46.     {
  47.       head = new ListLink;
  48.       head->name = newMember;
  49.       length++;
  50.       return;
  51.     }
  52.   
  53.   // Walk to the last element in the list
  54.   while (current->next != NULL)
  55.     current = current->next;
  56.   
  57.   current->next = new ListLink;
  58.   (current->next)->prev = current;
  59.   (current->next)->name = newMember;
  60.   length++;
  61. }
  62.  
  63. String List::GetElement(int location)
  64. {
  65.   ListLink* current = head;
  66.   
  67.   if (location > length-1)
  68.     // Then it's out of range.  The minus
  69.     // one is because length is index 1 based
  70.     // and location is index 0 based.
  71.     {
  72.       cerr << "Index reference error in List::GetElement!  Send bug report!\n"; 
  73.       return String("BOGUS");
  74.     }
  75.   
  76.   for (int i = 0; i < location; i++)
  77.     current = current->next;
  78.   
  79.   return current->name;
  80. }
  81.  
  82. bool List::unify(char* element)
  83. {
  84.   // If char* exists in this List, return FALSE.
  85.   // Otherwise, remember the char* and return TRUE.
  86.  
  87.   String Selement(element);
  88.  
  89.   for(int i = 0; i < length; i++)
  90.     {
  91.       if (Selement == GetElement(i))
  92.     // We have a match
  93.     return FALSE;
  94.     }
  95.  
  96.   AddElement(Selement);
  97.   return TRUE;
  98. }
  99.  
  100. void List::MarkDead(String element)
  101. {
  102.   ListLink* current = head;
  103.  
  104.   for (int i = 0; i < length; i++)
  105.     {
  106.       if (current->name == element)
  107.     current->alive--;
  108.       current = current->next;
  109.     }
  110. }
  111.  
  112. void List::RemoveDead(WINDOW* errwin)
  113. {
  114.   ListLink* current = head;
  115.   ListLink* DeadLink = NULL;
  116.   char ErrBuf[80];
  117.   int looper = length;
  118.   
  119.   for (int i = 0; i < looper; i++)
  120.     {
  121.       if (current->alive <= 0)
  122.     {
  123.       strcpy(ErrBuf, "Giving up on RPC host ");
  124.       strcat(ErrBuf, current->name);
  125.       strcat(ErrBuf, "!\n");
  126.       waddstr(errwin, ErrBuf);
  127.       wrefresh(errwin);
  128.       
  129.       if (current->next == NULL &&
  130.           current->prev == NULL)
  131.         // Then this is the only element in the list
  132.         {
  133.           DeadLink = current;
  134.           head = NULL;
  135.           length = 0;
  136.           delete DeadLink;
  137.           return;
  138.         }
  139.       
  140.       // Then we have to remove it
  141.       if (current->prev != NULL)
  142.         (current->prev)->next = current->next;
  143.       if (current->next != NULL)
  144.         (current->next)->prev = current->prev;
  145.       DeadLink = current;
  146.       current = current->next;
  147.       delete DeadLink; 
  148.       length--;
  149.     }
  150.       else 
  151.     current = current->next;
  152.     }
  153. }
  154.  
  155. void List::printall()
  156. {
  157.   ListLink* current = head;
  158.  
  159.   for (int i = 0; i < length; i++)
  160.     {
  161.       cerr << "*** Element " << i << " ***\n"
  162.        << current->name << "\n" 
  163.        << current->alive << "\n";
  164.       current = current->next;
  165.     }
  166. }
  167.  
  168. void List::Seen(int k)
  169. {
  170.   ListLink* current = head;
  171.  
  172.   for (int i = 0; i < k; i++)
  173.     current = current->next;
  174.  
  175.   current->seen = TRUE;
  176. }
  177.  
  178. void List::UnSee(int k)
  179. {
  180.   ListLink* current = head;
  181.  
  182.   for (int i = 0; i < k; i++)
  183.     current = current->next;
  184.  
  185.   current->seen = FALSE;
  186. }
  187.  
  188. bool List::Saw(int k)
  189. {
  190.   ListLink* current = head;
  191.  
  192.   for (int i = 0; i < k; i++)
  193.     current = current->next;
  194.  
  195.   return current->seen;
  196. }
  197.  
  198. void List::CleanAll()
  199. {
  200.   ListLink* current = head;
  201.   
  202.   for (int i = 0; i < length; i++)
  203.     {
  204.       current->clean = TRUE;
  205.       current = current->next;
  206.     }
  207. }
  208.  
  209. void List::MakeDirty(int k)
  210. {
  211.   ListLink* current = head;
  212.  
  213.   for (int i = 0; i < k; i++)
  214.     current = current->next;
  215.  
  216.   current->clean = FALSE;
  217. }
  218.  
  219. bool List::isClean(int k)
  220. {
  221.   ListLink* current = head;
  222.  
  223.   for (int i = 0; i < k; i++)
  224.     current = current->next;
  225.  
  226.   return current->clean;
  227. }
  228.