home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / word2x0a.zip / source / fifo.h < prev    next >
C/C++ Source or Header  |  1998-04-20  |  4KB  |  266 lines

  1. /* $Id: fifo.h,v 1.17 1998/04/19 15:44:31 dps Exp $ */
  2. /* Generalised fifo */
  3.  
  4. #ifndef __FIFO_H__
  5. #define __FIFO_H__
  6.  
  7. #include <iostream.h>
  8. #include <stddef.h>
  9. #ifndef NULL
  10. #define NULL (void *) 0
  11. #endif /* NULL */
  12.  
  13. template<class T>
  14. class fifo
  15. {
  16. private:
  17.     typedef struct queue
  18.     {
  19.     const T *data;
  20.     struct queue *next;
  21.     } queue;
  22.     struct queue *start;
  23.     struct queue **end;
  24.     int length;
  25.  
  26. public:
  27.     inline fifo(void)
  28.     {
  29.     start=NULL;
  30.     end=&start;
  31.     length=0;
  32.     }
  33.     inline is_empty(void)
  34.     {
  35. #ifdef CONSIST_CHECK
  36.     if (end==NULL)
  37.     {
  38.         cerr<<"Oops! end of list is NULL\n";
  39.         exit(1);
  40.     }
  41. #endif
  42.     return (length==0) ? 1 : 0;
  43.     }
  44.     inline int qlen(void)
  45.     {
  46.     return length;
  47.     }
  48.     ~fifo(void);
  49.     void enqueue(const T *d);    /* Add to end of queue */
  50.     void insert(const T *d);    /* Add to start of queue */
  51.     void ins_trans(fifo *f);    /* Insert fifo */
  52.     void rev(void);        /* Reverse */
  53.     void clear(void);        /* Wipe list to empty */
  54.     void transfer(fifo *f);
  55.     const T *dequeue(void);
  56.     ostream &__print_fifo(ostream &) const; /* Print */
  57. };
  58.  
  59. #ifndef __NO_FIFO_FUNCTIONS__
  60.  
  61. template<class T>
  62. void fifo<T>::clear(void)
  63. {
  64.     struct queue *ptr, *next;
  65.     
  66.     ptr=start;
  67.     while (ptr!=NULL)
  68.     {
  69.     next=ptr->next;
  70.     delete(ptr->data);
  71.     delete(ptr);
  72.     ptr=next;
  73.     }
  74.     start=NULL;
  75.     end=&start;
  76.     length=0;
  77. }
  78.  
  79. template<class T>
  80. fifo<T>::~fifo(void)
  81. {
  82.     struct queue *ptr, *next;
  83.     
  84.     ptr=start;
  85.     while (ptr!=NULL)
  86.     {
  87.     next=ptr->next;
  88.     delete(ptr->data);
  89.     delete(ptr);
  90.     ptr=next;
  91.     }
  92. }
  93.  
  94. template<class T>
  95. void fifo<T>::enqueue(const T *d)
  96. {
  97.     struct queue *q;
  98.     
  99. #ifdef DEBUG_FIFO
  100.     cerr<<"Queue "<<(void *) d<<"\n";
  101. #endif
  102.     q=new(struct queue);
  103.     q->next=NULL;
  104.     q->data=d;
  105.     *end=q;
  106.     end=&(q->next);
  107.     length++;
  108. }
  109.  
  110. template<class T>
  111. void fifo<T>::insert(const T *d)
  112. {
  113.     struct queue *q;
  114. #ifdef CONSIST_CHECK
  115.     if (end==NULL)
  116.     {
  117.     cerr<<"Oops! end of list is NULL\n";
  118.     exit(1);
  119.     }
  120. #endif
  121.     
  122.     q=new(struct queue);
  123.     q->next=start;
  124.     q->data=d;
  125.     start=q;
  126.     if (q->next==NULL)
  127.     end=&(q->next);
  128.     length++;
  129. }
  130.  
  131. template<class T>
  132. const T *fifo<T>::dequeue(void)
  133. {
  134.     const T *d;
  135.     struct queue *q;
  136. #ifdef CONSIST_CHECK
  137.     if (end==NULL)
  138.     {
  139.     cerr<<"Oops! end of list is NULL\n";
  140.     exit(1);
  141.     }
  142. #endif
  143.  
  144.     if (length==0)
  145.     return NULL;
  146.  
  147.     q=start;
  148.     d=q->data;
  149. #ifdef DEBUG_FIFO
  150.     cerr<<"Dequeue "<<(void *) d<<"\n";
  151. #endif
  152.     start=start->next;
  153.     if (--length==0)
  154.     end=&start;
  155.     delete(q);
  156.     return d;
  157. }
  158.  
  159. template <class T>
  160. void fifo<T>::transfer(fifo<T> *d)
  161. {
  162. #ifdef CONSIST_CHECK
  163.     if (end==NULL || d->end==NULL)
  164.     {
  165.     cerr<<"Oops! end of list is NULL\n";
  166.     exit(1);
  167.     }
  168. #endif
  169.  
  170.     if (d==NULL || d->length==0)
  171.     return;
  172.  
  173.     *end=d->start;
  174.     end=d->end;
  175.     length+=d->length;
  176.     d->start=NULL;
  177.     d->end=&(d->start);
  178.     d->length=0;
  179. }
  180.  
  181. template<class T>
  182. void fifo<T>::ins_trans(fifo<T> *d)
  183. {
  184. #ifdef CONSIST_CHECK
  185.     if (end==NULL || d->end==NULL)
  186.     {
  187.     cerr<<"Oops! end of list is NULL\n";
  188.     exit(1);
  189.     }
  190. #endif
  191.  
  192.     if (d==NULL || d->length==0)
  193.     return;
  194.  
  195.     *(d->end)=start;
  196.     start=d->start;
  197.     if (*(d->end)==NULL)
  198.     end=d->end;
  199.     length+=d->length;
  200.  
  201.     d->length=0;
  202.     d->start=NULL;
  203.     d->end=&(d->start);
  204. }
  205.  
  206. template<class T>
  207. void fifo<T>::rev(void)
  208. {
  209.     struct queue *p, *n, *hdr, **ep;
  210. #ifdef CONSIST_CHECK
  211.     if (end==NULL || d->end==NULL)
  212.     {
  213.     cerr<<"Oops! end of list is NULL\n";
  214.     exit(1);
  215.     }
  216. #endif
  217.  
  218.     if (length==0)
  219.     return;
  220.  
  221.     ep=&(start->next);
  222.     for (hdr=NULL, p=start; p!=NULL; )
  223.     {
  224.     n=p->next;
  225.     p->next=hdr;
  226.     hdr=p;
  227.     p=n;
  228.     }
  229.     start=hdr;
  230.     end=ep;
  231. }
  232.  
  233.  
  234. template<class T>
  235. ostream &fifo<T>::__print_fifo(ostream &os) const
  236. {
  237.     const struct fifo<T>::queue *q;
  238.     
  239. #ifdef CONSIST_CHECK
  240.     if (end==NULL || this->end==NULL)
  241.     {
  242.     cerr<<"Oops! end of list is NULL\n";
  243.     exit(1);
  244.     }
  245. #endif
  246.  
  247.     q=this->start;
  248.     while (q!=NULL)
  249.     {
  250.     os<<(q->data)<<',';
  251.     q=q->next;
  252.     }
  253.     return os;
  254. }
  255.  
  256. template<class T>
  257. inline ostream &operator << (ostream &os, const fifo<T> *d)
  258. {
  259.     d->__print_fifo(os);
  260.     return os;
  261. }
  262.  
  263.  
  264. #endif /* not __NO_FIFO_FUNCTIONS__ */
  265. #endif /* __FIFO_H__ */
  266.