priority_queue.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2011 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/priority_queue.h>
  19. #include <grub/mm.h>
  20. #include <grub/dl.h>
  21. GRUB_MOD_LICENSE ("GPLv3+");
  22. struct grub_priority_queue
  23. {
  24. grub_size_t elsize;
  25. grub_size_t allocated;
  26. grub_size_t used;
  27. grub_comparator_t cmp;
  28. void *els;
  29. };
  30. static inline void *
  31. element (struct grub_priority_queue *pq, grub_size_t k)
  32. {
  33. return ((grub_uint8_t *) pq->els) + k * pq->elsize;
  34. }
  35. static inline void
  36. swap (struct grub_priority_queue *pq, grub_size_t m, grub_size_t n)
  37. {
  38. grub_uint8_t *p1, *p2;
  39. grub_size_t l;
  40. p1 = (grub_uint8_t *) element (pq, m);
  41. p2 = (grub_uint8_t *) element (pq, n);
  42. for (l = pq->elsize; l; l--, p1++, p2++)
  43. {
  44. grub_uint8_t t;
  45. t = *p1;
  46. *p1 = *p2;
  47. *p2 = t;
  48. }
  49. }
  50. static inline grub_size_t
  51. parent (grub_size_t v)
  52. {
  53. return (v - 1) / 2;
  54. }
  55. static inline grub_size_t
  56. left_child (grub_size_t v)
  57. {
  58. return 2 * v + 1;
  59. }
  60. static inline grub_size_t
  61. right_child (grub_size_t v)
  62. {
  63. return 2 * v + 2;
  64. }
  65. void *
  66. grub_priority_queue_top (grub_priority_queue_t pq)
  67. {
  68. if (!pq->used)
  69. return 0;
  70. return element (pq, 0);
  71. }
  72. void
  73. grub_priority_queue_destroy (grub_priority_queue_t pq)
  74. {
  75. grub_free (pq->els);
  76. grub_free (pq);
  77. }
  78. grub_priority_queue_t
  79. grub_priority_queue_new (grub_size_t elsize,
  80. grub_comparator_t cmp)
  81. {
  82. struct grub_priority_queue *ret;
  83. void *els;
  84. els = grub_calloc (8, elsize);
  85. if (!els)
  86. return 0;
  87. ret = (struct grub_priority_queue *) grub_malloc (sizeof (*ret));
  88. if (!ret)
  89. {
  90. grub_free (els);
  91. return 0;
  92. }
  93. ret->elsize = elsize;
  94. ret->allocated = 8;
  95. ret->used = 0;
  96. ret->cmp = cmp;
  97. ret->els = els;
  98. return ret;
  99. }
  100. /* Heap property: pq->cmp (element (pq, p), element (pq, parent (p))) <= 0. */
  101. grub_err_t
  102. grub_priority_queue_push (grub_priority_queue_t pq, const void *el)
  103. {
  104. grub_size_t p;
  105. if (pq->used == pq->allocated)
  106. {
  107. void *els;
  108. els = grub_realloc (pq->els, pq->elsize * 2 * pq->allocated);
  109. if (!els)
  110. return grub_errno;
  111. pq->allocated *= 2;
  112. pq->els = els;
  113. }
  114. pq->used++;
  115. grub_memcpy (element (pq, pq->used - 1), el, pq->elsize);
  116. for (p = pq->used - 1; p; p = parent (p))
  117. {
  118. if (pq->cmp (element (pq, p), element (pq, parent (p))) <= 0)
  119. break;
  120. swap (pq, p, parent (p));
  121. }
  122. return GRUB_ERR_NONE;
  123. }
  124. void
  125. grub_priority_queue_pop (grub_priority_queue_t pq)
  126. {
  127. grub_size_t p;
  128. swap (pq, 0, pq->used - 1);
  129. pq->used--;
  130. for (p = 0; left_child (p) < pq->used; )
  131. {
  132. grub_size_t c;
  133. if (pq->cmp (element (pq, left_child (p)), element (pq, p)) <= 0
  134. && (right_child (p) >= pq->used
  135. || pq->cmp (element (pq, right_child (p)), element (pq, p)) <= 0))
  136. break;
  137. if (right_child (p) >= pq->used
  138. || pq->cmp (element (pq, left_child (p)),
  139. element (pq, right_child (p))) > 0)
  140. c = left_child (p);
  141. else
  142. c = right_child (p);
  143. swap (pq, p, c);
  144. p = c;
  145. }
  146. }