pairheap.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2020 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "py/pairheap.h"
  27. // The mp_pairheap_t.next pointer can take one of the following values:
  28. // - NULL: the node is the top of the heap
  29. // - LSB set: the node is the last of the children and points to its parent node
  30. // - other: the node is a child and not the last child
  31. // The macros below help manage this pointer.
  32. #define NEXT_MAKE_RIGHTMOST_PARENT(parent) ((void *)((uintptr_t)(parent) | 1))
  33. #define NEXT_IS_RIGHTMOST_PARENT(next) ((uintptr_t)(next) & 1)
  34. #define NEXT_GET_RIGHTMOST_PARENT(next) ((void *)((uintptr_t)(next) & ~1))
  35. // O(1), stable
  36. mp_pairheap_t *mp_pairheap_meld(mp_pairheap_lt_t lt, mp_pairheap_t *heap1, mp_pairheap_t *heap2) {
  37. if (heap1 == NULL) {
  38. return heap2;
  39. }
  40. if (heap2 == NULL) {
  41. return heap1;
  42. }
  43. if (lt(heap1, heap2)) {
  44. if (heap1->child == NULL) {
  45. heap1->child = heap2;
  46. } else {
  47. heap1->child_last->next = heap2;
  48. }
  49. heap1->child_last = heap2;
  50. heap2->next = NEXT_MAKE_RIGHTMOST_PARENT(heap1);
  51. return heap1;
  52. } else {
  53. heap1->next = heap2->child;
  54. heap2->child = heap1;
  55. if (heap1->next == NULL) {
  56. heap2->child_last = heap1;
  57. heap1->next = NEXT_MAKE_RIGHTMOST_PARENT(heap2);
  58. }
  59. return heap2;
  60. }
  61. }
  62. // amortised O(log N), stable
  63. mp_pairheap_t *mp_pairheap_pairing(mp_pairheap_lt_t lt, mp_pairheap_t *child) {
  64. if (child == NULL) {
  65. return NULL;
  66. }
  67. mp_pairheap_t *heap = NULL;
  68. while (!NEXT_IS_RIGHTMOST_PARENT(child)) {
  69. mp_pairheap_t *n1 = child;
  70. child = child->next;
  71. n1->next = NULL;
  72. if (!NEXT_IS_RIGHTMOST_PARENT(child)) {
  73. mp_pairheap_t *n2 = child;
  74. child = child->next;
  75. n2->next = NULL;
  76. n1 = mp_pairheap_meld(lt, n1, n2);
  77. }
  78. heap = mp_pairheap_meld(lt, heap, n1);
  79. }
  80. heap->next = NULL;
  81. return heap;
  82. }
  83. // amortised O(log N), stable
  84. mp_pairheap_t *mp_pairheap_delete(mp_pairheap_lt_t lt, mp_pairheap_t *heap, mp_pairheap_t *node) {
  85. // Simple case of the top being the node to delete
  86. if (node == heap) {
  87. mp_pairheap_t *child = heap->child;
  88. node->child = NULL;
  89. return mp_pairheap_pairing(lt, child);
  90. }
  91. // Case where node is not in the heap
  92. if (node->next == NULL) {
  93. return heap;
  94. }
  95. // Find parent of node
  96. mp_pairheap_t *parent = node;
  97. while (!NEXT_IS_RIGHTMOST_PARENT(parent->next)) {
  98. parent = parent->next;
  99. }
  100. parent = NEXT_GET_RIGHTMOST_PARENT(parent->next);
  101. // Replace node with pairing of its children
  102. mp_pairheap_t *next;
  103. if (node == parent->child && node->child == NULL) {
  104. if (NEXT_IS_RIGHTMOST_PARENT(node->next)) {
  105. parent->child = NULL;
  106. } else {
  107. parent->child = node->next;
  108. }
  109. node->next = NULL;
  110. return heap;
  111. } else if (node == parent->child) {
  112. mp_pairheap_t *child = node->child;
  113. next = node->next;
  114. node->child = NULL;
  115. node->next = NULL;
  116. node = mp_pairheap_pairing(lt, child);
  117. parent->child = node;
  118. } else {
  119. mp_pairheap_t *n = parent->child;
  120. while (node != n->next) {
  121. n = n->next;
  122. }
  123. mp_pairheap_t *child = node->child;
  124. next = node->next;
  125. node->child = NULL;
  126. node->next = NULL;
  127. node = mp_pairheap_pairing(lt, child);
  128. if (node == NULL) {
  129. node = n;
  130. } else {
  131. n->next = node;
  132. }
  133. }
  134. node->next = next;
  135. if (NEXT_IS_RIGHTMOST_PARENT(next)) {
  136. parent->child_last = node;
  137. }
  138. return heap;
  139. }