sw_heap.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <stdlib.h>
  2. #include "sw_defs.h"
  3. int PQinsert(struct Halfedge *he, struct Site *v, double offset)
  4. {
  5. struct Halfedge *last, *next;
  6. he->vertex = v;
  7. ref(v);
  8. he->ystar = v->coord.y + offset;
  9. last = &PQhash[PQbucket(he)];
  10. while ((next = last->PQnext) != (struct Halfedge *)NULL &&
  11. (he->ystar > next->ystar ||
  12. (he->ystar == next->ystar && v->coord.x > next->vertex->coord.x)))
  13. {
  14. last = next;
  15. };
  16. he->PQnext = last->PQnext;
  17. last->PQnext = he;
  18. PQcount += 1;
  19. return 0;
  20. }
  21. int PQdelete(struct Halfedge *he)
  22. {
  23. struct Halfedge *last;
  24. if (he->vertex != (struct Site *)NULL) {
  25. last = &PQhash[PQbucket(he)];
  26. while (last->PQnext != he)
  27. last = last->PQnext;
  28. last->PQnext = he->PQnext;
  29. PQcount -= 1;
  30. deref(he->vertex);
  31. he->vertex = (struct Site *)NULL;
  32. };
  33. return 0;
  34. }
  35. int PQbucket(struct Halfedge *he)
  36. {
  37. int bucket;
  38. bucket = (he->ystar - ymin) / deltay * PQhashsize;
  39. if (bucket < 0)
  40. bucket = 0;
  41. if (bucket >= PQhashsize)
  42. bucket = PQhashsize - 1;
  43. if (bucket < PQmin)
  44. PQmin = bucket;
  45. return (bucket);
  46. }
  47. int PQempty(void)
  48. {
  49. return (PQcount == 0);
  50. }
  51. struct Point PQ_min(void)
  52. {
  53. struct Point answer;
  54. while (PQhash[PQmin].PQnext == (struct Halfedge *)NULL) {
  55. PQmin += 1;
  56. };
  57. answer.x = PQhash[PQmin].PQnext->vertex->coord.x;
  58. answer.y = PQhash[PQmin].PQnext->ystar;
  59. return (answer);
  60. }
  61. struct Halfedge *PQextractmin(void)
  62. {
  63. struct Halfedge *curr;
  64. curr = PQhash[PQmin].PQnext;
  65. PQhash[PQmin].PQnext = curr->PQnext;
  66. PQcount -= 1;
  67. return (curr);
  68. }
  69. int PQinitialize(void)
  70. {
  71. int i;
  72. PQcount = 0;
  73. PQmin = 0;
  74. PQhashsize = 4 * sqrt_nsites;
  75. PQhash = (struct Halfedge *)myalloc(PQhashsize * sizeof *PQhash);
  76. for (i = 0; i < PQhashsize; i += 1)
  77. PQhash[i].PQnext = (struct Halfedge *)NULL;
  78. return 0;
  79. }