/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_list_c[] = "@(#)list.C	1.2 10/19/92 13:35:25 LLNL ";


#include "list.h"
#include <stdio.h>

    // insert a link in front of this one

    void link::insert(link *newlink)
      {
      if(prev)
        prev->next = newlink;

      newlink->next = this;
      newlink->prev = prev;
      prev = newlink;
      }

    // append a link after this one

    void link::append(link *newlink)
      {
      if(next)
        next->prev = newlink;

      newlink->next = next;
      newlink->prev = this;
      next = newlink;
      }

    // remove this link from the list

    void link::remove()
      {
      if(prev)
        prev->next = next;

      if(next)
        next->prev = prev;

      next = prev = 0;
      }

    int list::isempty()
      {
      return last == 0;
      }

    // clear the list

    void list::clear()
      {
      link *l = last;
      if(!l) return;

      do
        {
        link *ll = l;
        l = l->next;
        ll->next = ll->prev = 0;
        } while(l != last);

      last = 0;
      count = 0;
      }

    // set current pointer to ends of the list

    void list::reset()
        {
        curr = 0;
        }

    // return the length of the list

    int list::length()
        {
#ifndef PRODUCTION
   int i;
   link *l;
   static int zero=0;

   reset();
   for (i=0; next(l); i++)
      ;
   if (i != count)
      printf("LENGTH OF LIST CORRUPTED\n"),1/zero;
#endif
        return(count);
        }

    // get the next entry from the list, moving curr forward

    int list::next(link *&a)
      {
      if(!last)
      {
         a = 0;
         return 0;
      }

      // move curr forward, possibly to the beginning or end of the list

      if(curr)
        if(curr == last)
          {
          curr = 0;
          a = 0;
          return 0;
          }
        else
          curr = curr->next;
      else
        curr = last->next;

      a = curr;
      return 1;
      }

    // get the previous entry from the list, moving curr backwards

    int list::prev(link *&a)
      {
      if(!last)
      {
         a = 0;
         return 0;
      }

      // move curr backwards, possibly to the beginning or end of the list

      if(curr)
        if(curr == last->next)
          {
          curr = 0;
          a = 0;
          return 0;
          }
        else
          curr = curr->prev;
      else
        curr = last;

      a = curr;
      return 1;
      }


    // get the next entry from a list, removing it afterwards.
    // curr is left alone

    int list::getnext(link *&a)
    /* REPAIRED A BUG HERE: 8/4/90 -- JAG */
      {
      if(!last)
      {
         a = 0;
         return 0;
      }

      // choose the link to remove

      if(curr)
        if(curr == last)
          {
          curr = 0;
          a = 0;
          return 0;
          }
        else
        {
          a = curr->next;
          if (a == last)
             last = curr;
        }
      else
      {
        a = last->next;
        if(a == last)
          last = 0;
      }

      // remove a from the list

      a->remove();

      count--;
      return 1;
      }

    // get the previous entry from a list, removing it afterwards

    int list::getprev(link *&a)
    /* REPAIRED THE getnext BUG HERE: 8/29/90 -- JAG */
      {
      if(!last)
      {
         a = 0;
         return 0;
      }

      // choose the link to remove

      if(curr)
        if(curr == last->prev)
          {
          curr = 0;
          a = 0;
          return 0;
          }
        else
          a = curr->prev;
      else
      {
         a = last;
         last = last->prev;
         if (a==last)
            last = 0;
      }

      // remove a from the list

      a->remove();

      count--;
      return 1;
      }


    // insert an entry at the beginning of the list

    void list::insert(link *a)
      {
      if(last)
        last->next->insert(a);
      else
        {
        last = a;
        last->next = last->prev = last;
        }
      count++;
      }

    // append an entry at the end of the list.  this is the same as
    // list::insert() except that list::last is adjusted afterwards.

    void list::append(link *a)
      {
      this->insert(a);
      last = last->next;
      }

    // insert an entry before the current location of the list.  if
    // curr is not set, insert at the beginning of the list.

    void list::inserthere(link *a)
      {
      if(curr)
        curr->insert(a),count++;
      else
        this->insert(a);
      }

    // append an entry after the current location of the list.  if curr
    // is not set, append at the end of the list.

    void list::appendhere(link *a)
      {
      if(curr)
        curr->append(a),count++;
      else
        this->append(a);
      }




