sw_heap.c 1.9 KB

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