home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1 / HamRadio.cdr / tech / pcbsrcs2 / work.c < prev    next >
C/C++ Source or Header  |  1991-02-07  |  3KB  |  138 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include "cell.h"
  5.  
  6. struct work { /* a unit of work is a hole-pair to connect */
  7.     int FromRow;        /* source row        */
  8.     int FromCol;        /* source column    */
  9.     char far *FromName;    /* source name        */
  10.     int ToRow;        /* target row        */
  11.     int ToCol;        /* target column    */
  12.     char far *ToName;    /* target name        */
  13.     int ApxDist;        /* approximate distance    */
  14.     int Priority;        /* 0=no, 1=yes        */
  15.     struct work far *Next;
  16.     };
  17.  
  18. /* pointers to the first and last item of work to do */
  19. static struct work far *Head = NULL;
  20. static struct work far *Tail = NULL;
  21. static struct work far *Current = NULL;
  22.  
  23. extern int SortConnects; /* 0 = don't sort, 1 = sort */
  24.  
  25. extern int GetApxDist( int, int, int, int );
  26. extern void Nomem( void );
  27.  
  28. void InitWork( void );
  29. void ReInitWork( void );
  30. void SetWork( int, int, char far *, int, int, char far *, int );
  31. void GetWork( int *, int *, char far * far *, int *, int *, char far * far * );
  32. void SortWork( void );
  33.  
  34. void InitWork () { /* initialize the work list */
  35.     struct work far *p;
  36.  
  37.     while (p = Head) {
  38.         Head = p->Next;
  39.         _ffree( p );
  40.         }
  41.     Tail = Current = NULL;
  42.     }
  43.  
  44. void ReInitWork () { /* initialize the work list */
  45.     Current = Head;
  46.     }
  47.  
  48. void SetWork ( r1, c1, n1, r2, c2, n2, pri )
  49.     /* add a unit of work to the work list */
  50.     int r1, c1, r2, c2, pri;
  51.     char far *n1;
  52.     char far *n2;
  53.     {
  54.     struct work far *p;
  55.  
  56.     if (p = (struct work far *)_fmalloc( sizeof(struct work) )) {
  57.         p->FromRow = r1;
  58.         p->FromCol = c1;
  59.         p->FromName = n1;
  60.         p->ToRow = r2;
  61.         p->ToCol = c2;
  62.         p->ToName = n2;
  63.         p->ApxDist = SortConnects ? GetApxDist( r1, c1, r2, c2 ) : 0;
  64.         p->Priority = pri;
  65.         p->Next = NULL;
  66.         if (Head) /* attach at end */
  67.             Tail->Next = p;
  68.         else /* first in list */
  69.             Head = Current = p;
  70.         Tail = p;
  71.         }
  72.     else /* can't get any more memory */
  73.         Nomem();
  74.     }
  75.  
  76. void GetWork ( r1, c1, n1, r2, c2, n2 )
  77.     /* fetch a unit of work from the work list */
  78.     int *r1, *c1, *r2, *c2;
  79.     char far * far *n1;
  80.     char far * far *n2;
  81.     {
  82.     if (Current) {
  83.         *r1 = Current->FromRow;
  84.         *c1 = Current->FromCol;
  85.         *n1 = Current->FromName;
  86.         *r2 = Current->ToRow;
  87.         *c2 = Current->ToCol;
  88.         *n2 = Current->ToName;
  89.         Current = Current->Next;
  90.         }
  91.     else { /* none left */
  92.         *r1 = *c1 = *r2 = *c2 = ILLEGAL;
  93.         *n1 = *n2 = NULL;
  94.         }
  95.     }
  96.  
  97. void SortWork () { /* order the work items; shortest first */
  98.     struct work far *p;
  99.     struct work far *q0; /* put PRIORITY CONNECTs in q0 */
  100.     struct work far *q1; /* sort other CONNECTs in q1 */
  101.     struct work far *r;
  102.  
  103.     q0 = q1 = NULL;
  104.     while (p = Head) { /* prioritize each work item */
  105.         Head = Head->Next;
  106.         if (p->Priority) { /* put at end of priority list */
  107.             p->Next = NULL;
  108.             if (!(r = q0)) /* empty list? */
  109.                 q0 = p;
  110.             else { /* attach at end */
  111.                 while (r->Next) /* search for end */
  112.                     r = r->Next;
  113.                 r->Next = p; /* attach */
  114.                 }
  115.             }
  116.         else if (!(r = q1) || p->ApxDist < q1->ApxDist) {
  117.             p->Next = q1;
  118.             q1 = p;
  119.             }
  120.         else { /* find proper position in list */
  121.             while (r->Next && p->ApxDist >= r->Next->ApxDist)
  122.                 r = r->Next;
  123.             p->Next = r->Next;
  124.             r->Next = p;
  125.             }
  126.         }
  127.     if (p = q0) { /* any priority CONNECTs? */
  128.         while (q0->Next)
  129.             q0 = q0->Next;
  130.         q0->Next = q1;
  131.         }
  132.     else
  133.         p = q1;
  134.     /* reposition Head and Tail */
  135.     for (Head = Current = Tail = p; Tail && Tail->Next; Tail = Tail->Next)
  136.         ;
  137.     }
  138.