pairheap.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef MICROPY_INCLUDED_PY_PAIRHEAP_H
  27. #define MICROPY_INCLUDED_PY_PAIRHEAP_H
  28. // This is an implementation of a pairing heap. It is stable and has deletion
  29. // support. Only the less-than operation needs to be defined on elements.
  30. //
  31. // See original paper for details:
  32. // Michael L. Fredman, Robert Sedjewick, Daniel D. Sleator, and Robert E. Tarjan.
  33. // The Pairing Heap: A New Form of Self-Adjusting Heap.
  34. // Algorithmica 1:111-129, 1986.
  35. // https://www.cs.cmu.edu/~sleator/papers/pairing-heaps.pdf
  36. #include <assert.h>
  37. #include "py/obj.h"
  38. // This struct forms the nodes of the heap and is intended to be extended, by
  39. // placing it first in another struct, to include additional information for the
  40. // element stored in the heap. It includes "base" so it can be a MicroPython
  41. // object allocated on the heap and the GC can automatically trace all nodes by
  42. // following the tree structure.
  43. typedef struct _mp_pairheap_t {
  44. mp_obj_base_t base;
  45. struct _mp_pairheap_t *child;
  46. struct _mp_pairheap_t *child_last;
  47. struct _mp_pairheap_t *next;
  48. } mp_pairheap_t;
  49. // This is the function for the less-than operation on nodes/elements.
  50. typedef int (*mp_pairheap_lt_t)(mp_pairheap_t *, mp_pairheap_t *);
  51. // Core functions.
  52. mp_pairheap_t *mp_pairheap_meld(mp_pairheap_lt_t lt, mp_pairheap_t *heap1, mp_pairheap_t *heap2);
  53. mp_pairheap_t *mp_pairheap_pairing(mp_pairheap_lt_t lt, mp_pairheap_t *child);
  54. mp_pairheap_t *mp_pairheap_delete(mp_pairheap_lt_t lt, mp_pairheap_t *heap, mp_pairheap_t *node);
  55. // Create a new heap.
  56. static inline mp_pairheap_t *mp_pairheap_new(mp_pairheap_lt_t lt) {
  57. (void)lt;
  58. return NULL;
  59. }
  60. // Initialise a single pairing-heap node so it is ready to push on to a heap.
  61. static inline void mp_pairheap_init_node(mp_pairheap_lt_t lt, mp_pairheap_t *node) {
  62. (void)lt;
  63. node->child = NULL;
  64. node->next = NULL;
  65. }
  66. // Test if the heap is empty.
  67. static inline bool mp_pairheap_is_empty(mp_pairheap_lt_t lt, mp_pairheap_t *heap) {
  68. (void)lt;
  69. return heap == NULL;
  70. }
  71. // Peek at the top of the heap. Will return NULL if empty.
  72. static inline mp_pairheap_t *mp_pairheap_peek(mp_pairheap_lt_t lt, mp_pairheap_t *heap) {
  73. (void)lt;
  74. return heap;
  75. }
  76. // Push new node onto existing heap. Returns the new heap.
  77. static inline mp_pairheap_t *mp_pairheap_push(mp_pairheap_lt_t lt, mp_pairheap_t *heap, mp_pairheap_t *node) {
  78. assert(node->child == NULL && node->next == NULL);
  79. return mp_pairheap_meld(lt, node, heap); // node is first to be stable
  80. }
  81. // Pop the top off the heap, which must not be empty. Returns the new heap.
  82. static inline mp_pairheap_t *mp_pairheap_pop(mp_pairheap_lt_t lt, mp_pairheap_t *heap) {
  83. assert(heap->next == NULL);
  84. mp_pairheap_t *child = heap->child;
  85. heap->child = NULL;
  86. return mp_pairheap_pairing(lt, child);
  87. }
  88. #endif // MICROPY_INCLUDED_PY_PAIRHEAP_H