home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cmdline.lha / cmdline / src / lib / fifolist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-03  |  3.1 KB  |  127 lines

  1. //------------------------------------------------------------------------
  2. // ^FILE: fifolist.c - implementation of FIFO linked lists
  3. //
  4. // ^DESCRIPTION:
  5. //    This file implements the classes declared in fifolist.h
  6. //
  7. // ^HISTORY:
  8. //    03/21/92    Brad Appleton    <brad@ssd.csd.harris.com>    Created
  9. //-^^---------------------------------------------------------------------
  10.  
  11. #include <stdlib.h>
  12. #include "cmdline.h"
  13. #include "fifolist.h"
  14.  
  15. //------------------------------------------------------------- GenericFifoList
  16.  
  17.    // Destructor
  18. GenericFifoList::~GenericFifoList(void) {
  19.    GenericFifoListNode * nd = head;
  20.    head = NULL;
  21.    while (nd) {
  22.       GenericFifoListNode * to_delete = nd;
  23.       nd = nd->next;
  24.       if (del_items)  delete  to_delete->contents;
  25.       delete  to_delete;
  26.    }
  27. }
  28.  
  29.    // Add an item to the end
  30. void
  31. GenericFifoList::add(void * item) {
  32.    if (item) {
  33.       GenericFifoListNode * nd = new GenericFifoListNode(NULL, item);
  34.       if (head == NULL) {
  35.          head = tail = nd;
  36.       } else {
  37.          tail->next = nd;
  38.          tail = nd;
  39.       }
  40.       ++num_items;
  41.       mod = 1;
  42.    }
  43. }
  44.  
  45.    // Remove an item off the front
  46. void *
  47. GenericFifoList::remove(void) {
  48.    if (head == NULL)  return  NULL;
  49.    GenericFifoListNode * nd = head;
  50.    void * result = head->contents;
  51.    head = head->next;
  52.    delete  nd;
  53.    --num_items;
  54.    mod = 1;
  55.    return  result;
  56. }
  57.  
  58. //--------------------------------------------------------- GenericFifoListIter
  59.  
  60. GenericFifoListIter::~GenericFifoListIter(void) {}
  61.  
  62. void *
  63. GenericFifoListIter::operator()(void) {
  64.    void * result = NULL;
  65.    if (current) {
  66.       result  = current->contents;
  67.       current = current->next;
  68.    }
  69.    return  result;
  70. }
  71.  
  72. //-------------------------------------------------------- GenericFifoListArray
  73.  
  74. GenericFifoListArray::~GenericFifoListArray(void) {}
  75.  
  76. void *
  77. GenericFifoListArray::operator[](unsigned  ndx) {
  78.    unsigned  max_index = count();
  79.    if (! max_index--)  return  NULL;     // check for underflow
  80.    if (ndx > max_index)  return  NULL;  // check for overflow
  81.  
  82.    // if we want the first element -- just return the head
  83.    if (ndx == 0)  return  list.head->contents;
  84.  
  85.    // if we want the last element -- just return the tail
  86.    if (ndx == max_index)  return  list.tail->contents;
  87.  
  88.    // If we are going backward or stuff has been modified, then rewind
  89.    if ((ndx < index) || list.modified()) {
  90.       index = 0;
  91.       current = list.head;
  92.    }
  93.  
  94.    // Skip from current to the desired element
  95.    while (index < ndx) {
  96.       current = current->next;
  97.       ++index;
  98.    }
  99.  
  100.    return  current->contents;
  101. }
  102.  
  103. //-------------------------------------------------------------------- FifoList
  104.  
  105. #ifdef TEMPLATES
  106.  
  107.    // Destructor
  108. template <class Type>
  109. FifoList<Type>::~FifoList(void) {
  110.    GenericFifoListNode * nd = head;
  111.    head = NULL;
  112.    while (nd) {
  113.       GenericFifoListNode * to_delete = nd;
  114.       nd = nd->next;
  115.       if (del_items)  delete (Type *)to_delete->contents;
  116.       delete  to_delete;
  117.    }
  118. }
  119.  
  120. template <class Type>
  121. FifoListIter<Type>::~FifoListIter(void) {}
  122.  
  123. template <class Type>
  124. FifoListArray<Type>::~FifoListArray(void) {}
  125.  
  126. #endif
  127.