home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / FLEXLIST.ZIP / FLEXLIST.C < prev    next >
Text File  |  1989-07-20  |  12KB  |  493 lines

  1.     /*
  2.  
  3.         flexlist.c
  4.  
  5.         Copyright 1989
  6.         John W. Small
  7.         All rights reserved
  8.  
  9.         PSW / Power SoftWare
  10.         P.O. Box 10072
  11.         McLean, Virginia 22102 8072
  12.         (703) 759-3838
  13.  
  14.         1/14/89
  15.  
  16.     */
  17.  
  18.  
  19.     #include <stdio.h>      /*  NULL      */
  20.     #include <stdlib.h>     /*  malloc(), free()  */
  21.     #include <string.h>     /*  memcpy()  */
  22.     #include <flexlist.h>   /*  size_t in stddef.h  */
  23.  
  24.     #define FlistHalloc(hdsize)  malloc ( \
  25.       sizeof(struct FlistHeader) - sizeof(FlistData) \
  26.       + (hdsize))
  27.     #define FlistNalloc(ndsize)  malloc ( \
  28.       sizeof(struct FlistNode) - sizeof(FlistData) \
  29.       + (ndsize))
  30.     #define Flistfree(mptr) free(mptr)
  31.  
  32.  
  33.     /* housekeeping primitives */
  34.  
  35.     Flist mkFlist   (size_t  ndsize, size_t  hdsize)
  36.     {
  37.       Flist lptr;
  38.  
  39.       if (ndsize)
  40.          if ((lptr = FlistHalloc(hdsize)) != NULL)  {
  41.             lptr->front = lptr->current = lptr->rear = NULL;
  42.             lptr->ncurrent = lptr->nodes = 0;
  43.             lptr->ndlen = ndsize;
  44.             lptr->hdlen = hdsize;
  45.             return lptr;
  46.          }
  47.       return NULL;
  48.     }
  49.  
  50.  
  51.  
  52.     int     clrFlist    (Flist lptr)
  53.     {
  54.       FlistN  nptr;
  55.  
  56.       if (lptr)  {
  57.          while ((nptr = lptr->front) != NULL)  {
  58.            lptr->front = nptr->next;
  59.            Flistfree(nptr);
  60.          }
  61.          lptr->current = lptr->rear = NULL;
  62.          lptr->ncurrent = lptr->nodes = 0;
  63.          return 1;
  64.       }
  65.       return 0;
  66.     }
  67.  
  68.     int      rmFlist    (Flist *lptrAddr)
  69.     {
  70.        if (clrFlist(*lptrAddr))  {
  71.           Flistfree(*lptrAddr);
  72.           *lptrAddr = NULL;
  73.           return 1;
  74.        }
  75.        return 0;
  76.     }
  77.  
  78.     unsigned nempty    (Flist lptr)
  79.     {
  80.       return lptr? lptr->nodes : 0;
  81.     }
  82.  
  83.     void    *Flistdptr  (Flist lptr)
  84.     {
  85.       if (lptr)
  86.          if (lptr->hdlen)
  87.             return &lptr->data;
  88.       return NULL;
  89.     }
  90.  
  91.  
  92.  
  93.     /* stack and queue primitives */
  94.  
  95.     void    *pushdptr  (Flist lptr, void *bufAddr)
  96.     {
  97.       FlistN nptr;
  98.  
  99.       if (lptr)
  100.          if ((nptr = FlistNalloc(lptr->ndlen)) != NULL)  {
  101.             if (bufAddr)
  102.                memcpy(&nptr->data,bufAddr,lptr->ndlen);
  103.             nptr->prev = NULL;
  104.             if ((nptr->next = lptr->front) != NULL)
  105.                nptr->next->prev = nptr;
  106.             else
  107.                lptr->rear =  nptr;
  108.             lptr->front = nptr;
  109.             lptr->nodes++;
  110.             if (lptr->ncurrent)
  111.                lptr->ncurrent++;
  112.             return &nptr->data;
  113.          }
  114.       return NULL;
  115.     }
  116.  
  117.     int      pushn     (Flist lptr, FlistN nptr)
  118.     {
  119.       if (lptr && nptr)  {
  120.          nptr->prev = NULL;
  121.          if ((nptr->next = lptr->front) != NULL)
  122.             nptr->next->prev = nptr;
  123.          else
  124.             lptr->rear =  nptr;
  125.          lptr->front = nptr;
  126.          lptr->nodes++;
  127.          if (lptr->ncurrent)
  128.             lptr->ncurrent++;
  129.          return 1;
  130.       }
  131.       return 0;
  132.     }
  133.  
  134.  
  135.  
  136.     int      popd      (Flist lptr, void *bufAddr)
  137.     {
  138.        FlistN nptr;
  139.  
  140.        if (lptr)
  141.           if ((nptr = lptr->front) != NULL)  {
  142.              if (bufAddr)
  143.                 memcpy(bufAddr,&nptr->data,lptr->ndlen);
  144.              if (lptr->front == lptr->rear)
  145.                 lptr->rear = NULL;
  146.              else
  147.                 nptr->next->prev = NULL;
  148.              lptr->front = nptr->next;
  149.              lptr->nodes--;
  150.              Flistfree(nptr);
  151.              if (lptr->ncurrent)
  152.                 if (!--lptr->ncurrent)
  153.                    lptr->current = NULL;
  154.              return 1;
  155.           }
  156.        return 0;
  157.      }
  158.  
  159.     FlistN popn      (Flist lptr)
  160.     {
  161.        FlistN nptr;
  162.  
  163.        if (lptr)
  164.           if ((nptr = lptr->front) != NULL)  {
  165.              if (lptr->front == lptr->rear)
  166.                 lptr->rear = NULL;
  167.              else
  168.                 nptr->next->prev = NULL;
  169.              lptr->front = nptr->next;
  170.              lptr->nodes--;
  171.              if (lptr->ncurrent)
  172.                 if (!--lptr->ncurrent)
  173.                    lptr->current = NULL;
  174.              return nptr;
  175.           }
  176.        return NULL;
  177.      }
  178.  
  179.  
  180.  
  181.     void    *topdptr   (Flist lptr, void *bufAddr)
  182.     {
  183.       if (lptr)
  184.          if (lptr->front)  {
  185.             if (bufAddr)
  186.                memcpy (
  187.                  bufAddr,&lptr->front->data,lptr->ndlen);
  188.             return &lptr->front->data;
  189.          }
  190.       return NULL;
  191.     }
  192.  
  193.     void    *iquedptr  (Flist lptr, void *bufAddr)
  194.     {
  195.       FlistN nptr;
  196.  
  197.       if (lptr)
  198.          if ((nptr = FlistNalloc(lptr->ndlen)) != NULL)  {
  199.             if (bufAddr)
  200.                memcpy(&nptr->data,bufAddr,lptr->ndlen);
  201.             nptr->next = NULL;
  202.             if (lptr->rear)
  203.                lptr->rear->next = nptr;
  204.             else
  205.                lptr->front = nptr;
  206.             nptr->prev = lptr->rear;
  207.             lptr->rear = nptr;
  208.             lptr->nodes++;
  209.             return &nptr->data;
  210.          }
  211.       return NULL;
  212.     }
  213.  
  214.     int      iquen     (Flist lptr, FlistN nptr)
  215.     {
  216.       if (lptr && nptr)  {
  217.          nptr->next = NULL;
  218.          if (lptr->rear)
  219.             lptr->rear->next = nptr;
  220.          else
  221.             lptr->front = nptr;
  222.          nptr->prev = lptr->rear;
  223.          lptr->rear = nptr;
  224.          lptr->nodes++;
  225.          return 1;
  226.       }
  227.       return 0;
  228.     }
  229.  
  230.  
  231.  
  232.     /* current node primitives */
  233.  
  234.     unsigned ncur      (Flist lptr)
  235.     {
  236.       return lptr? lptr->ncurrent : 0;
  237.     }
  238.  
  239.     void    *curdptr   (Flist lptr)
  240.     {
  241.       if (lptr)
  242.          if (lptr->current)
  243.             return &lptr->current->data;
  244.       return NULL;
  245.     }
  246.  
  247.  
  248.  
  249.     void    *mkcdptr   (Flist lptr, unsigned loc)
  250.     {
  251.       register FlistN nptr;
  252.       register unsigned i;
  253.  
  254.       if (lptr)  {
  255.          if ((loc < 1) || (loc > lptr->nodes))  {
  256.             lptr->current = NULL;
  257.             lptr->ncurrent = 0;
  258.             return NULL;
  259.          }
  260.          else if (loc == lptr->ncurrent)
  261.             return &lptr->current->data;
  262.          else  {
  263.             if (lptr->ncurrent)
  264.                if (loc > lptr->ncurrent)
  265.                   if (((lptr->nodes >> 1) +
  266.                       (lptr->ncurrent >> 1)) < loc)
  267.                      for (nptr=lptr->rear,
  268.                           i=lptr->nodes-loc;i;i--)
  269.                        nptr = nptr->prev;
  270.                   else
  271.                      for (nptr=lptr->current,
  272.                           i=loc-lptr->ncurrent;i;i--)
  273.                        nptr = nptr->next;
  274.                else
  275.                   if ((lptr->ncurrent >> 1) < loc)
  276.                      for (nptr=lptr->current,
  277.                           i=lptr->ncurrent-loc;i;i--)
  278.                        nptr = nptr->prev;
  279.                   else
  280.                      for (nptr=lptr->front,i=loc-1;i;i--)
  281.                        nptr = nptr->next;
  282.             else if ((lptr->nodes >> 1) < loc)
  283.                for (nptr=lptr->rear,i=lptr->nodes-loc;i;i--)
  284.                  nptr = nptr->prev;
  285.             else
  286.                for (nptr=lptr->front,i=loc-1;i;i--)
  287.                  nptr = nptr->next;
  288.             lptr->ncurrent = loc;
  289.             lptr->current = nptr;
  290.             return &lptr->current->data;
  291.          }
  292.       }
  293.       return NULL;
  294.     }
  295.  
  296.  
  297.  
  298.     /* list primitives */
  299.  
  300.     void    *insdptr   (Flist lptr, void *bufAddr)
  301.     {
  302.       FlistN nptr;
  303.  
  304.       if (lptr)
  305.          if ((nptr = FlistNalloc(lptr->ndlen)) != NULL)  {
  306.             if (bufAddr)
  307.                memcpy(&nptr->data,bufAddr,lptr->ndlen);
  308.             if ((nptr->prev = lptr->current) != NULL)  {
  309.                if ((nptr->next = lptr->current->next) != NULL)
  310.                   nptr->next->prev = nptr;
  311.                else
  312.                   lptr->rear = nptr;
  313.                lptr->current->next = nptr;
  314.             }
  315.             else {
  316.                if ((nptr->next = lptr->front) != NULL)
  317.                   nptr->next->prev = nptr;
  318.                else
  319.                   lptr->rear = nptr;
  320.                lptr->front = nptr;
  321.             }
  322.             lptr->current = nptr;
  323.             lptr->ncurrent++;
  324.             lptr->nodes++;
  325.             return &nptr->data;
  326.          }
  327.       return NULL;
  328.     }
  329.  
  330.  
  331.  
  332.     int      insn      (Flist lptr, FlistN nptr)
  333.     {
  334.       if (lptr && nptr)  {
  335.          if ((nptr->prev = lptr->current) != NULL)  {
  336.             if ((nptr->next = lptr->current->next) != NULL)
  337.                nptr->next->prev = nptr;
  338.             else
  339.                lptr->rear = nptr;
  340.             lptr->current->next = nptr;
  341.          }
  342.          else {
  343.             if ((nptr->next = lptr->front) != NULL)
  344.                nptr->next->prev = nptr;
  345.             else
  346.                lptr->rear = nptr;
  347.             lptr->front = nptr;
  348.          }
  349.          lptr->current = nptr;
  350.          lptr->ncurrent++;
  351.          lptr->nodes++;
  352.          return 1;
  353.       }
  354.       return 0;
  355.     }
  356.  
  357.  
  358.  
  359.     int      deld      (Flist lptr, void *bufAddr)
  360.     {
  361.       FlistN nptr;
  362.  
  363.       if (lptr)
  364.          if ((nptr = lptr->current) != NULL)  {
  365.             lptr->current = nptr->prev;
  366.             lptr->ncurrent--;
  367.             if (nptr->next)
  368.                nptr->next->prev = nptr->prev;
  369.             else
  370.                lptr->rear = nptr->prev;
  371.             if (nptr->prev)
  372.                nptr->prev->next = nptr->next;
  373.             else
  374.                lptr->front = nptr->next;
  375.             lptr->nodes--;
  376.             if (bufAddr)
  377.                memcpy(bufAddr,&nptr->data,lptr->ndlen);
  378.             Flistfree(nptr);
  379.             return 1;
  380.          }
  381.       return 0;
  382.     }
  383.  
  384.     FlistN deln      (Flist lptr)
  385.     {
  386.       FlistN nptr;
  387.  
  388.       if (lptr)
  389.          if ((nptr = lptr->current) != NULL)  {
  390.             lptr->current = nptr->prev;
  391.             lptr->ncurrent--;
  392.             if (nptr->next)
  393.                nptr->next->prev = nptr->prev;
  394.             else
  395.                lptr->rear = nptr->prev;
  396.             if (nptr->prev)
  397.                nptr->prev->next = nptr->next;
  398.             else
  399.                lptr->front = nptr->next;
  400.             lptr->nodes--;
  401.             return nptr;
  402.          }
  403.       return NULL;
  404.     }
  405.  
  406.  
  407.  
  408.     void    *nextdptr  (Flist lptr, void *bufAddr)
  409.     {
  410.       if (lptr)  {
  411.          if (lptr->current)
  412.             lptr->current = lptr->current->next;
  413.          else
  414.             lptr->current = lptr->front;
  415.          if (lptr->current)  {
  416.             lptr->ncurrent++;
  417.             if (bufAddr)
  418.                memcpy (
  419.                  bufAddr,&lptr->current->data,lptr->ndlen);
  420.             return &lptr->current->data;
  421.          }
  422.          else
  423.             lptr->ncurrent = 0;
  424.       }
  425.       return NULL;
  426.     }
  427.  
  428.     void    *prevdptr  (Flist lptr, void *bufAddr)
  429.     {
  430.       if (lptr)  {
  431.          if (lptr->current)  {
  432.             lptr->current = lptr->current->prev;
  433.             lptr->ncurrent--;
  434.          }
  435.          else {
  436.             lptr->current = lptr->rear;
  437.             lptr->ncurrent = lptr->nodes;
  438.          }
  439.          if (lptr->current)  {
  440.             if (bufAddr)
  441.                memcpy (
  442.                  bufAddr,&lptr->current->data,lptr->ndlen);
  443.             return &lptr->current->data;
  444.          }
  445.       }
  446.       return NULL;
  447.     }
  448.  
  449.  
  450.  
  451.     int      getd      (Flist lptr, void *bufAddr)
  452.     {
  453.       if (lptr)
  454.          if (lptr->current && bufAddr)  {
  455.             memcpy (
  456.               bufAddr,&lptr->current->data,lptr->ndlen);
  457.             return 1;
  458.          }
  459.       return 0;
  460.     }
  461.  
  462.     int      putd      (Flist lptr, void *bufAddr)
  463.     {
  464.       if (lptr)
  465.          if (lptr->current && bufAddr)  {
  466.             memcpy (
  467.             &lptr->current->data,bufAddr,lptr->ndlen);
  468.             return 1;
  469.          }
  470.       return 0;
  471.     }
  472.  
  473.  
  474.     /* array primitives */
  475.  
  476.     int   stod  (Flist lptr, void *bufAddr, unsigned loc)
  477.     {
  478.       if (mkcdptr(lptr,loc) && bufAddr)  {
  479.          memcpy(&lptr->current->data,bufAddr,lptr->ndlen);
  480.          return 1;
  481.       }
  482.       return 0;
  483.     }
  484.  
  485.     int   rcld  (Flist lptr, void *bufAddr, unsigned loc)
  486.     {
  487.       if (mkcdptr(lptr,loc) && bufAddr)  {
  488.          memcpy(bufAddr,&lptr->current->data,lptr->ndlen);
  489.          return 1;
  490.       }
  491.       return 0;
  492.     }
  493.