You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.3 KiB

  1. /*******************************************/
  2. /* EDITEUR de PCB: routines d'AUTOROUTAGE: */
  3. /*******************************************/
  4. #include "fctsys.h"
  5. #include "gr_basic.h"
  6. #include "common.h"
  7. #include "pcbnew.h"
  8. #include "autorout.h"
  9. #include "protos.h"
  10. #include "cell.h"
  11. struct PcbQueue /* search queue structure */
  12. {
  13. struct PcbQueue *Next;
  14. int Row; /* current row */
  15. int Col; /* current column */
  16. int Side; /* 0=top, 1=bottom */
  17. int Dist; /* path distance to this cell so far */
  18. int ApxDist; /* approximate distance to target from here */
  19. };
  20. static long qlen = 0; /* current queue length */
  21. static struct PcbQueue *Head = NULL;
  22. static struct PcbQueue *Tail = NULL;
  23. static struct PcbQueue *Save = NULL; /* hold empty queue structs */
  24. /* Routines definies ici : */
  25. void InitQueue( void );
  26. void GetQueue( int *, int *, int *, int *, int * );
  27. int SetQueue( int, int, int, int, int, int, int );
  28. void ReSetQueue( int, int, int, int, int, int, int );
  29. /************************/
  30. void FreeQueue(void)
  31. /************************/
  32. /* Free the memory used for storing all the queue */
  33. {
  34. struct PcbQueue *p;
  35. InitQueue();
  36. while( (p = Save) != NULL )
  37. {
  38. Save = p->Next; MyFree(p);
  39. }
  40. }
  41. /************************/
  42. /* void InitQueue(void) */
  43. /************************/
  44. /* initialize the search queue */
  45. void InitQueue(void)
  46. {
  47. struct PcbQueue *p;
  48. while( (p = Head) != NULL )
  49. {
  50. Head = p->Next;
  51. p->Next = Save; Save = p;
  52. }
  53. Tail = NULL;
  54. OpenNodes = ClosNodes = MoveNodes = MaxNodes = qlen = 0;
  55. }
  56. /*********************************************************/
  57. /* void GetQueue(int *r, int *c, int *s, int *d, int *a) */
  58. /*********************************************************/
  59. /* get search queue item from list */
  60. void GetQueue(int *r, int *c, int *s, int *d, int *a)
  61. {
  62. struct PcbQueue *p;
  63. if( (p = Head) != NULL ) /* return first item in list */
  64. {
  65. *r = p->Row; *c = p->Col;
  66. *s = p->Side;
  67. *d = p->Dist; *a = p->ApxDist;
  68. if ((Head = p->Next) == NULL) Tail = NULL;
  69. /* put node on free list */
  70. p->Next = Save; Save = p;
  71. ClosNodes++; qlen--;
  72. }
  73. else /* empty list */
  74. {
  75. *r = *c = *s = *d = *a = ILLEGAL;
  76. }
  77. }
  78. /****************************************************************/
  79. int SetQueue (int r,int c,int side,int d,int a,int r2,int c2 )
  80. /****************************************************************/
  81. /* add a search node to the list
  82. Return:
  83. 1 si OK
  84. 0 si defaut allocation Memoire
  85. */
  86. {
  87. struct PcbQueue *p, *q, *t;
  88. int i, j;
  89. if( (p = Save) != NULL ) /* try free list first */
  90. {
  91. Save = p->Next;
  92. }
  93. else if ((p = (struct PcbQueue *) MyMalloc(sizeof(PcbQueue))) == NULL)
  94. return(0);
  95. p->Row = r;
  96. p->Col = c;
  97. p->Side = side;
  98. i = (p->Dist = d) + (p->ApxDist = a);
  99. p->Next = NULL;
  100. if( (q = Head) != NULL)
  101. { /* insert in proper position in list */
  102. if (q->Dist + q->ApxDist > i)
  103. { /* insert at head */
  104. p->Next = q; Head = p;
  105. }
  106. else { /* search for proper position */
  107. for (t = q, q = q->Next; q && i > (j = q->Dist + q->ApxDist);
  108. t = q, q = q->Next)
  109. ;
  110. if (q && i == j && q->Row == r2 && q->Col == c2)
  111. {
  112. /* insert after q, which is a goal node */
  113. if ( (p->Next = q->Next) == NULL) Tail = p;
  114. q->Next = p;
  115. }
  116. else
  117. { /* insert in front of q */
  118. if ((p->Next = q) == NULL) Tail = p;
  119. t->Next = p;
  120. }
  121. }
  122. }
  123. else /* empty search list */
  124. Head = Tail = p;
  125. OpenNodes++;
  126. if (++qlen > MaxNodes) MaxNodes = qlen;
  127. return(1);
  128. }
  129. /******************************************************************/
  130. void ReSetQueue (int r,int c,int s,int d,int a,int r2,int c2 )
  131. /******************************************************************/
  132. /* reposition node in list */
  133. {
  134. struct PcbQueue *p, *q;
  135. /* first, see if it is already in the list */
  136. for (q = NULL, p = Head; p; q = p, p = p->Next) {
  137. if (p->Row == r && p->Col == c && p->Side == s) {
  138. /* old one to remove */
  139. if (q)
  140. {
  141. if ( (q->Next = p->Next) == NULL) Tail = q;
  142. }
  143. else if ((Head = p->Next) == NULL) Tail = NULL;
  144. p->Next = Save; Save = p;
  145. OpenNodes--; MoveNodes++;
  146. qlen--;
  147. break;
  148. }
  149. }
  150. if (!p) /* not found, it has already been closed once */
  151. ClosNodes--; /* we will close it again, but just count once */
  152. /* if it was there, it's gone now; insert it at the proper position */
  153. SetQueue( r, c, s, d, a, r2, c2 );
  154. }