home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / lan / soss.arj / INCLUDE / Q.H < prev    next >
C/C++ Source or Header  |  1991-04-11  |  2KB  |  84 lines

  1. /* Copyright 1986 by Carnegie Mellon */
  2. /*  Copyright 1983 by the Massachusetts Institute of Technology  */
  3.  
  4. #ifndef Q_H                /* DDP */
  5. #define Q_H    1            /* DDP */
  6.  
  7. /* Definitions for general-purpose queue manipulation package.
  8.    Modified from Larry Allen's queue package from CSR Unix for TCP and
  9.    tasks. */
  10.  
  11.  
  12. typedef    struct    q_elt    {        /* queue element: cast to right type */
  13.     struct    q_elt    *qe_next;    /* it's just a pointer to next elt */
  14. } *q_elt;
  15.  
  16. typedef    struct    queue    {        /* queue header */
  17.     q_elt    q_head;            /* first element in queue */
  18.     q_elt    q_tail;            /* last element in queue */
  19.     int    q_len;            /* number of elements in queue */
  20.     int    q_max;            /* maximum length */
  21.     int    q_min;            /* minimum length */
  22. } queue;
  23.  
  24. extern    q_elt    q_deq ();
  25. extern queue *q_create();
  26.  
  27. /* The following macros implement most of the common queue operations */
  28.  
  29. /* Add an element to the head of the queue */
  30.  
  31. #define    q_addh(q, elt)     { \
  32.     if ((q)->q_head == 0) (q)->q_tail = (elt); \
  33.     (elt)->qe_next = (q)->q_head; \
  34.     (q)->q_head = (elt); \
  35.     (q)->q_len++; \
  36.     if(++((q)->q_len) > (q)->q_max) (q)->q_max = (q)->q_len; \
  37. }
  38.  
  39. /* Add an element to the tail of a queue */
  40.  
  41. #define    q_addt(q, elt)    { \
  42.     (elt)->qe_next = 0; \
  43.     if ((q)->q_head == 0) { \
  44.         (q)->q_head = (elt); \
  45.     } else { \
  46.         (q)->q_tail->qe_next = (elt); \
  47.     } \
  48.     (q)->q_tail = (elt); \
  49.     if(++((q)->q_len) > (q)->q_max) (q)->q_max = (q)->q_len; \
  50. }
  51.  
  52. /* Add an element after a specified element in the queue.  If prev == */
  53. /* &q->q_head, can be used to add an element to the head of the queue */
  54.  
  55. #define    q_adda(q, prev, new)    { \
  56.     if ((q)->q_tail == (prev) || (q)->q_tail == 0) { \
  57.         (q)->q_tail = (new); \
  58.     } \
  59.     (new)->qe_next = (prev)->qe_next; \
  60.     (prev)->qe_next = (new); \
  61.     if(++((q)->q_len) > (q)->q_max) (q)->q_max = (q)->q_len; \
  62. }
  63.  
  64. /* Delete an element from a queue, given a pointer to the preceeding element */
  65. /* Will delete the first element if prev == &q->q_head */
  66.  
  67. #define    q_dela(q, elt, prev)    { \
  68.     if ((q)->q_tail == (elt)) { \
  69.         if ((q)->q_head == (elt)) \
  70.             (q)->q_tail = 0; \
  71.         else \
  72.             (q)->q_tail = (prev); \
  73.     } \
  74.     (prev)->qe_next = (elt)->qe_next; \
  75.     (elt)->qe_next = 0; \
  76.     if(--((q)->q_len) < (q)->q_min) (q)->q_min = (q)->q_len; \
  77. }
  78.  
  79.  
  80. #define    aq_addt(q, elt)    {int_off(); q_addt((q), (elt)); int_on(); }
  81.  
  82. #define    aq_addh(q, elt)    {int_off(); q_addh((q), (elt)); int_on(); }
  83. #endif                    /* DDP */
  84.